├── .github └── workflows │ └── pr-validation.yaml ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── dist ├── bodies │ ├── body.d.ts │ ├── file-body.d.ts │ ├── form-body.d.ts │ ├── json-body.d.ts │ └── raw-body.d.ts ├── curl-generator.cjs.js ├── curl-generator.esm.js ├── curl-generator.umd.js └── main.d.ts ├── example └── parameters.ts ├── package-lock.json ├── package.json ├── rollup.config.js ├── src ├── bodies │ ├── body.ts │ ├── file-body.ts │ ├── form-body.ts │ ├── json-body.ts │ └── raw-body.ts └── main.ts ├── test.txt ├── test ├── __snapshots__ │ └── main.test.ts.snap └── main.test.ts └── tsconfig.json /.github/workflows/pr-validation.yaml: -------------------------------------------------------------------------------- 1 | name: Validate PR 2 | 3 | on: 4 | workflow_dispatch: 5 | pull_request: 6 | branches: [master] 7 | 8 | jobs: 9 | checks: 10 | name: PR checks 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Checkout repository 14 | uses: actions/checkout@v4 15 | 16 | - name: Install Node.js 17 | uses: actions/setup-node@v4 18 | with: 19 | node-version: 20 20 | 21 | - name: Install dependencies 22 | run: npm ci 23 | 24 | - name: "Typescript: type checking" 25 | run: "npm run typecheck" 26 | 27 | - name: "Unit tests" 28 | run: "npm run test" 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | coverage 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry = "https://registry.npmjs.com/" 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Alberto De Agostini 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CurlGenerator 2 | 3 | CurlGenerator is a **small** (~1kb), dependecy free, library to generate curl snippets. 4 | Currently it has only 1 API, but I'm open to suggestion on how to improve and to integrate with different http request libraries. 5 | 6 | Buy Me A Coffee 7 | 8 | ## Getting started 9 | 10 | install in your project 11 | ```bash 12 | npm install curl-generator 13 | ``` 14 | 15 | Or you can download the file you need from the dist folder 16 | 17 | #### Using it 18 | ```js 19 | import {CurlGenerator} from "curl-generator"; 20 | 21 | const curlSnippet = CurlGenerator({url: "https://jsonplaceholder.typicode.com/posts/1"}); 22 | // curlSnippet => curl "https://jsonplaceholder.typicode.com/posts/101" 23 | ``` 24 | 25 | If you are going to import it with in node via require 26 | ```js 27 | const CurlGenerator = require("curl-generator").CurlGenerator; 28 | ``` 29 | 30 | #### API 31 | 32 | Currently the library export just CurlGenerator, and it's a function with just 1 object parameter with the following description: 33 | ```js 34 | /** 35 | * @param {string} url - the request url 36 | * @param {string} [param.method] - a value between ("GET" | "POST" | "PUT" | "PATCH" | "DELETE") it's case insensitive 37 | * @param {Object} [param.headers] - an object containing the headers of the request 38 | * @param {Object} [body] - the body of the request 39 | */ 40 | ``` 41 | 42 | Example of a more "andvanced" use 43 | ```js 44 | import {CurlGenerator} from "curl-generator"; 45 | 46 | const params = { 47 | url: "https://jsonplaceholder.typicode.com/posts", 48 | method: "POST", 49 | headers: { 50 | "Content-type": "application/json; charset=UTF-8" 51 | }, 52 | body: { 53 | "id": "123-456-789", 54 | "key1": "value 1", 55 | "key2": `a "complex" value` 56 | } 57 | } 58 | const curlSnippet = CurlGenerator(params); 59 | // 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\"}" 60 | ``` 61 | 62 | You can also pass Curl options as additional options: 63 | ```js 64 | import {CurlGenerator} from "curl-generator"; 65 | 66 | const params = { 67 | url: "https://jsonplaceholder.typicode.com/posts/1" 68 | } 69 | const options = { 70 | output: "test.txt", 71 | silent: true 72 | } 73 | const curlSnippet = CurlGenerator(params, options); 74 | // curl "https://jsonplaceholder.typicode.com/posts/1" --output test.txt --silent 75 | ``` 76 | Currently the following options are supported (you can submit a PR if you need others): 77 | | Name | Type | 78 | |:-----------------:|:-------------:| 79 | | compressed | boolean | 80 | | compressedSsh | boolean | 81 | | fail | boolean | 82 | | failEarly | boolean | 83 | | head | boolean | 84 | | include | boolean | 85 | | insecure | boolean | 86 | | ipv4 | boolean | 87 | | ipv6 | boolean | 88 | | listOnly | boolean | 89 | | location | boolean | 90 | | locationTrusted | boolean | 91 | | noKeepalive | boolean | 92 | | output | string | 93 | | showError | boolean | 94 | | silent | boolean | 95 | | ssl | boolean | 96 | | sslv2 | boolean | 97 | | sslv3 | boolean | 98 | | verbose | boolean | 99 | 100 | ## Contributing 101 | 102 | 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 103 | Dev build with watch 104 | ```bash 105 | npm run dev 106 | ``` 107 | Build bundle 108 | ```bash 109 | npm run build 110 | ``` 111 | Test (I plan to move testing with jest if the library grows) 112 | ```bash 113 | npm run test 114 | ``` 115 | 116 | The build generate three files: 117 | * `dist/curl-generator.cjs.js` 118 | A CommonJS bundle, suitable for use in Node.js, that `require`s the external dependency. This corresponds to the `"main"` field in package.json 119 | * `dist/curl-generator.esm.js` 120 | an ES module bundle, suitable for use in other people's libraries and applications, that `import`s the external dependency. This corresponds to the `"module"` field in package.json 121 | * `dist/curl-generator.umd.js` 122 | a UMD build, suitable for use in any environment (including the browser, as a `