Git Product home page Git Product logo

curl-generator's Introduction

CurlGenerator

CurlGenerator is a small (~1kb), dependecy free, library to generate curl snippets. Currently it has only 1 API, but I'm open to suggestion on how to improve and to integrate with different http request libraries.

Getting started

install in your project

npm install curl-generator

Or you can download the file you need from the dist folder

Using it

import {CurlGenerator} from "curl-generator";

const curlSnippet = CurlGenerator({url: "https://jsonplaceholder.typicode.com/posts/1"}); 
// curlSnippet => curl "https://jsonplaceholder.typicode.com/posts/101"

If you are going to import it with in node via require

const CurlGenerator = require("curl-generator").CurlGenerator;

API

Currently the library export just CurlGenerator, and it's a function with just 1 object parameter with the following description:

/**
 * @param {string} url - the request url
 * @param {string} [param.method] - a value between ("GET" | "POST" | "PUT" | "PATCH" | "DELETE") it's case insensitive
 * @param {Object<string, string>} [param.headers] - an object containing the headers of the request
 * @param {Object} [body] - the body of the request
 */

Example of a more "andvanced" use

import {CurlGenerator} from "curl-generator";

const params = {
    url: "https://jsonplaceholder.typicode.com/posts",
    method: "POST",
    headers: {
        "Content-type": "application/json; charset=UTF-8"
    },
    body: {
        "id": "123-456-789",
        "key1": "value 1",
        "key2": `a "complex" value`
    }
}
const curlSnippet = CurlGenerator(params); 
// curlSnippet => curl "https://jsonplaceholder.typicode.com/posts" -X POST -H "Content-type: application/json; charset=UTF-8" -d "{\"id\":\"123-456-789\",\"key1\":\"value 1\",\"key2\":\"a \\\"complex\\\" value\"}"

You can also pass Curl options as additional options:

import {CurlGenerator} from "curl-generator";

const params = {
    url: "https://jsonplaceholder.typicode.com/posts/1"
}
const options = {
    output: "test.txt",
    silent: true
}
const curlSnippet = CurlGenerator(params, options); 
// curl "https://jsonplaceholder.typicode.com/posts/1" --output test.txt --silent

Currently the following options are supported (you can submit a PR if you need others):

Name Type
compressed boolean
compressedSsh boolean
fail boolean
failEarly boolean
head boolean
include boolean
insecure boolean
ipv4 boolean
ipv6 boolean
listOnly boolean
location boolean
locationTrusted boolean
noKeepalive boolean
output string
showError boolean
silent boolean
ssl boolean
sslv2 boolean
sslv3 boolean
verbose boolean

Contributing

The library is written in typescript (it's my first typescript project so I'm very open to tips and suggestions) and it comes with 3 scripts Dev build with watch

npm run dev

Build bundle

npm run build

Test (I plan to move testing with jest if the library grows)

npm run test

The build generate three files:

  • dist/curl-generator.cjs.js A CommonJS bundle, suitable for use in Node.js, that requires the external dependency. This corresponds to the "main" field in package.json
  • dist/curl-generator.esm.js an ES module bundle, suitable for use in other people's libraries and applications, that imports the external dependency. This corresponds to the "module" field in package.json
  • dist/curl-generator.umd.js a UMD build, suitable for use in any environment (including the browser, as a <script> tag), that includes the external dependency. This corresponds to the "browser" field in package.json

Author and License

Alberto De Agostini MIT.

curl-generator's People

Contributors

albertodeago avatar fabb111 avatar adurhmn avatar chandan1499 avatar andrelmlins avatar oskarlebuda avatar cowane avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.