├── .gitignore ├── Readme.md ├── cli.js ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # curl-to-fetch 3 | 4 | Parse curl commands and returns `fetch` API equivalent. 5 | 6 | ## Example 7 | 8 | Input: 9 | 10 | ``` 11 | curl --cookie 'species=sloth;type=galactic' slothy https://api.sloths.com 12 | ``` 13 | 14 | Output: 15 | 16 | ```js 17 | fetch( 18 | 'https://api.sloths.com', 19 | {headers:{"Set-Cookie":"species=sloth;type=galactic"}, 20 | method:'GET'} 21 | ) 22 | .then(console.log, console.error) 23 | ``` 24 | 25 | P.S: Supports all the `curl` flags that [parse-curl.js](https://www.npmjs.org/parse-curl/) has. 26 | 27 | ##CLI 28 | 29 | ```sh 30 | $ c2f "curl --cookie 'species=sloth;type=galactic' slothy https://api.sloths.com" 31 | ``` 32 | 33 | ```sh 34 | fetch( 35 | https://api.sloths.com, 36 | {headers:{"Set-Cookie":"species=sloth;type=galactic"}, 37 | method:'GET'} 38 | ) 39 | .then(console.log, console.error) 40 | ``` 41 | 42 | #API 43 | 44 | ```js 45 | 46 | const parse = require('curl-to-fetch'); 47 | 48 | const fetchCode = parse(`curl 'http://google.com/'`); 49 | 50 | console.log(fetchCode); 51 | 52 | ``` 53 | 54 | ## Badges 55 | 56 | ![](https://img.shields.io/badge/license-MIT-blue.svg) 57 | ![](https://img.shields.io/badge/status-stable-green.svg) 58 | 59 | --- 60 | 61 | > [h3manth.com](https://h3manth.com)  ·  62 | > GitHub [@hemanth](https://github.com/hemanth)  ·  63 | > Twitter [@gnumanth](https://twitter.com/gnumanth) 64 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict' 3 | let pkg = require('./package.json') 4 | let toFetch = require('./index') 5 | let input = process.argv[2] 6 | 7 | const helpString = 8 | `${pkg.description} 9 | Usage 10 | $ cat | c2f 11 | $ c2f 12 | 13 | Options 14 | -v, --version Print version 15 | -h, --help Display help message` 16 | 17 | function printHelp() { 18 | console.log(helpString) 19 | process.exit(0) 20 | } 21 | 22 | function printVersion() { 23 | console.log(pkg.version) 24 | process.exit(0) 25 | } 26 | 27 | function printError() { 28 | console.error('Couldn\'t generate the fetch code') 29 | process.exit(-1) 30 | } 31 | 32 | function init(data) { 33 | let res = toFetch(data) 34 | 35 | if (res) 36 | console.log(res) 37 | else 38 | printError() 39 | } 40 | 41 | process.argv.map(arg => { 42 | switch (arg) { 43 | case '-h': 44 | case '--help': 45 | printHelp() 46 | break 47 | case '-v': 48 | case '--version': 49 | printVersion() 50 | } 51 | }) 52 | 53 | if (process.stdin.isTTY) 54 | if (!input) 55 | printHelp() 56 | else 57 | init(input) 58 | else 59 | process.stdin.once('data', data => init(data)) 60 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 2 | const parse = require('./index') 3 | 4 | const fetchCode = parse(`curl 'http://google.com/'`) 5 | 6 | console.log(fetchCode) 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var parseCurl = require('parse-curl') 2 | 3 | module.exports = function (curl) { 4 | 5 | if (typeof curl !== 'string') 6 | throw new TypeError(`Expected String, Found ${typeof curl}`) 7 | 8 | var parsed = parseCurl(curl) 9 | 10 | return ` 11 | fetch( 12 | '${parsed.url}', 13 | {headers:${JSON.stringify(parsed.header)}, 14 | method:'${parsed.method}'} 15 | ) 16 | .then(console.log, console.error)` 17 | } 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "curl-to-fetch", 3 | "version": "1.2.1", 4 | "description": "Parse curl commands and returns `fetch` API equivalent.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test.js" 8 | }, 9 | "bin": { 10 | "c2f": "./cli.js" 11 | }, 12 | "keywords": [ 13 | "curl", 14 | "parse", 15 | "fetch", 16 | "codegen" 17 | ], 18 | "author": { 19 | "name": "Hemanth.HM", 20 | "email": "hemanth.hm@gmail.com", 21 | "url": "http://h3manth.com" 22 | }, 23 | "license": "MIT", 24 | "repository": "hemanth/curl-to-fetch", 25 | "engines": { 26 | "node": ">=4" 27 | }, 28 | "dependencies": { 29 | "parse-curl": "^0.2.3" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert') 2 | const parse = require('./index') 3 | 4 | const cases = [] 5 | 6 | cases.push({ 7 | input: "curl --cookie 'species=sloth;type=galactic' slothy https://api.sloths.com", 8 | output: ` 9 | fetch( 10 | 'https://api.sloths.com', 11 | {headers:{"Set-Cookie":"species=sloth;type=galactic"}, 12 | method:'GET'} 13 | ) 14 | .then(console.log, console.error)` 15 | }) 16 | 17 | cases.forEach(function (c) { 18 | const out = parse(c.input) 19 | 20 | const msg = ` 21 | input: ${c.input} 22 | expected: ${JSON.stringify(c.output)} 23 | received: ${JSON.stringify(out)} 24 | ` 25 | assert.deepEqual(out, c.output, msg) 26 | }) 27 | 28 | console.log('\n :)\n') --------------------------------------------------------------------------------