├── .babelrc ├── .github ├── FUNDING.yml └── workflows │ ├── codeql-analysis.yml │ ├── nodejs.yml │ ├── publish.yml │ └── testCoverage.yml ├── .gitignore ├── .nvmrc ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── playground.html ├── rollup.config.js ├── src └── main.js ├── test └── main.test.js ├── types └── main.d.ts └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["@babel/preset-env", { 4 | "targets": { 5 | "node": "8" 6 | } 7 | }] 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: leoek 2 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ master ] 20 | schedule: 21 | - cron: '39 18 * * 6' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'javascript' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 37 | # Learn more: 38 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 39 | 40 | steps: 41 | - name: Checkout repository 42 | uses: actions/checkout@v2 43 | 44 | # Initializes the CodeQL tools for scanning. 45 | - name: Initialize CodeQL 46 | uses: github/codeql-action/init@v1 47 | with: 48 | languages: ${{ matrix.language }} 49 | # If you wish to specify custom queries, you can do so here or in a config file. 50 | # By default, queries listed here will override any specified in a config file. 51 | # Prefix the list here with "+" to use these queries and those in the config file. 52 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 53 | 54 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 55 | # If this step fails, then you should remove it and run the build manually (see below) 56 | - name: Autobuild 57 | uses: github/codeql-action/autobuild@v1 58 | 59 | # ℹ️ Command-line programs to run using the OS shell. 60 | # 📚 https://git.io/JvXDl 61 | 62 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 63 | # and modify them (or add more) to build your code if your project 64 | # uses a compiled language 65 | 66 | #- run: | 67 | # make bootstrap 68 | # make release 69 | 70 | - name: Perform CodeQL Analysis 71 | uses: github/codeql-action/analyze@v1 72 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: Test & Build 5 | 6 | on: 7 | push: 8 | branches: [ master, develop ] 9 | pull_request: 10 | branches: [ master, develop ] 11 | 12 | jobs: 13 | testAndBuild: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Read .nvmrc 18 | id: node_version 19 | run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc) 20 | - name: Setup node (with version from .nvmrc) 21 | uses: actions/setup-node@v1 22 | with: 23 | node-version: ${{ steps.node_version.outputs.NODE_VERSION }} 24 | - run: yarn install --frozen-lockfile 25 | - run: yarn test 26 | - run: yarn build 27 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will run tests using node and then publish a package when a release is created 2 | 3 | name: Test, Build & Publish 4 | 5 | on: 6 | release: 7 | types: [created] 8 | 9 | jobs: 10 | 11 | publish-npm: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Read .nvmrc 16 | id: node_version 17 | run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc) 18 | - name: Setup node (with version from .nvmrc) 19 | uses: actions/setup-node@v1 20 | with: 21 | node-version: ${{ steps.node_version.outputs.NODE_VERSION }} 22 | registry-url: https://registry.npmjs.org/ 23 | - run: yarn install --frozen-lockfile 24 | - run: yarn test 25 | - run: yarn build 26 | - run: yarn publish 27 | env: 28 | NODE_AUTH_TOKEN: ${{secrets.npm_token}} 29 | 30 | -------------------------------------------------------------------------------- /.github/workflows/testCoverage.yml: -------------------------------------------------------------------------------- 1 | name: Test & Report Coverage 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | uploadCodeCov: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Read .nvmrc 15 | id: node_version 16 | run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc) 17 | - name: Setup node (with version from .nvmrc) 18 | uses: actions/setup-node@v1 19 | with: 20 | node-version: ${{ steps.node_version.outputs.NODE_VERSION }} 21 | - name: install dev dependencies 22 | run: yarn install --frozen-lockfile 23 | - name: install codecov 24 | run: yarn add --dev codecov 25 | - name: create coverage report 26 | run: yarn test --coverage 27 | - name: upload coverage report to codecov 28 | run: yarn codecov --token="${{ secrets.CODECOV_TOKEN }}" 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib 3 | yarn-error.log 4 | coverage 5 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 12.10.0 -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "10" 4 | - "8" 5 | - "6" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Leonard Krause 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 | [](https://opensource.org/licenses/mit-license.php) 2 |  3 | [](https://codecov.io/gh/leoek/fetch-to-curl) 4 |  5 | 6 | 7 | 8 | # fetch request to curl 9 | 10 | This module was inspired by [http-to-curl](https://github.com/drgx/http-to-curl). Use it to generate curl requests with the inputs you would usually use for javascripts fetch. However it does not patch any modules like http-to-curl. It is just a wrapper to generate the curl string. This ensures that there are no side effects with your actual usage of fetch. 11 | 12 | Also note that the goal of this library is to be as simple and minimal as possible. This also means that there are zero dependencies :tada: 13 | 14 | ## Installation 15 | 16 | ```sh 17 | yarn add fetch-to-curl 18 | ``` 19 | 20 | or 21 | 22 | ```sh 23 | npm install fetch-to-curl 24 | ``` 25 | 26 | ## Usage 27 | 28 | ```js 29 | import { fetchToCurl } from 'fetch-to-curl'; 30 | // or In case there is no support for Es Modules in your environment: 31 | // const { fetchToCurl } = require("fetch-to-curl") 32 | 33 | const url = 'https://jsonplaceholder.typicode.com/posts/1'; 34 | const options = { 35 | headers: { 36 | Authorization: "BASIC SOMEBASE64STRING" 37 | }, 38 | method: 'get' 39 | }; 40 | // Log yopur request 41 | console.log(fetchToCurl(url, options)); 42 | // Do your request 43 | fetch(url, options); 44 | 45 | // Output 46 | curl "https://jsonplaceholder.typicode.com/posts/1" -X GET -H "Authorization: BASIC SOMEBASE64STRING" 47 | 48 | // You can also pass a single Request object 49 | console.log(fetchToCurl({ 50 | url: "https://jsonplaceholder.typicode.com/posts/1" 51 | headers: { 52 | Authorization: "BASIC SOMEBASE64STRING" 53 | }, 54 | method: 'get' 55 | })); 56 | 57 | // and/or a Headers object as you would to with fetch 58 | console.log(fetchToCurl({ 59 | url: "https://jsonplaceholder.typicode.com/posts/1" 60 | headers: new Headers({ 61 | Authorization: "BASIC SOMEBASE64STRING" 62 | }), 63 | method: 'get' 64 | })) 65 | 66 | ``` 67 | 68 | ## Playground and usage without package manager 69 | 70 | There is a minimal example of usage without package manager available which allows to [directly test this in the browser console](https://leoek.github.io/fetch-to-curl/playground.html). [(view source - playground.html)](https://github.com/leoek/fetch-to-curl/blob/master/playground.html) 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fetch-to-curl", 3 | "version": "0.6.0", 4 | "description": "Convert fetch HTTP request to curl", 5 | "main": "lib/bundle.js", 6 | "author": "Leonard Krause", 7 | "license": "MIT", 8 | "scripts": { 9 | "build": "rollup -c", 10 | "test": "jest" 11 | }, 12 | "files": [ 13 | "lib/bundle.js", 14 | "types" 15 | ], 16 | "types": "types/main.d.ts", 17 | "dependencies": {}, 18 | "devDependencies": { 19 | "@babel/core": "^7.12.10", 20 | "@babel/preset-env": "^7.12.11", 21 | "jest": "^26.6.3", 22 | "rollup": "^2.35.1", 23 | "rollup-plugin-babel-minify": "^10.0.0", 24 | "rollup-plugin-terser": "^7.0.2" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "git+https://github.com/leoek/fetch-to-curl.git" 29 | }, 30 | "bugs": { 31 | "url": "https://github.com/leoek/fetch-to-curl/issues" 32 | }, 33 | "homepage": "https://github.com/leoek/fetch-to-curl#readme", 34 | "keywords": [ 35 | "curl", 36 | "fetch", 37 | "http", 38 | "request-http", 39 | "debug", 40 | "react-native" 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /playground.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 11 | 14 | 18 | 34 | 35 | 36 | 37 |39 | Usually this library should be used together with a package manager like 40 | yarn or 41 | npm. (please 42 | refer to the 43 | README 46 | for general usage information) 47 |
48 |49 | Note the creation of the exports object in line 11 of this html file, 50 | which mitigates the missing module support of the browser. 51 |
52 |fetchToCurl("https://example.com", { headers: { "x-test": "test"
58 | }})
60 | or
61 | console.log(fetchToCurl("https://example.com", { headers: { "x-test":
63 | "test" }}))
65 | in your browser console
66 |