|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * HTTP code snippet generator for Node.js using node-fetch. |
| 4 | + * |
| 5 | + * @author |
| 6 | + * @hirenoble |
| 7 | + * |
| 8 | + * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict' |
| 12 | + |
| 13 | +var stringifyObject = require('stringify-object') |
| 14 | +var CodeBuilder = require('../../helpers/code-builder') |
| 15 | + |
| 16 | +module.exports = function (source, options) { |
| 17 | + var opts = Object.assign({ |
| 18 | + indent: ' ' |
| 19 | + }, options) |
| 20 | + |
| 21 | + var includeFS = false |
| 22 | + var code = new CodeBuilder(opts.indent) |
| 23 | + |
| 24 | + code.push('const fetch = require(\'node-fetch\');') |
| 25 | + var url = source.url |
| 26 | + var reqOpts = { |
| 27 | + method: source.method |
| 28 | + } |
| 29 | + |
| 30 | + if (Object.keys(source.queryObj).length) { |
| 31 | + reqOpts.qs = source.queryObj |
| 32 | + } |
| 33 | + |
| 34 | + if (Object.keys(source.headersObj).length) { |
| 35 | + reqOpts.headers = source.headersObj |
| 36 | + } |
| 37 | + |
| 38 | + switch (source.postData.mimeType) { |
| 39 | + case 'application/x-www-form-urlencoded': |
| 40 | + reqOpts.body = source.postData.paramsObj |
| 41 | + break |
| 42 | + |
| 43 | + case 'application/json': |
| 44 | + if (source.postData.jsonObj) { |
| 45 | + reqOpts.body = JSON.stringify(source.postData.jsonObj) |
| 46 | + } |
| 47 | + break |
| 48 | + |
| 49 | + case 'multipart/form-data': |
| 50 | + code.unshift('const FormData = require(\'form-data\');') |
| 51 | + code.push('const formData = new FormData();') |
| 52 | + source.postData.params.forEach(function (param) { |
| 53 | + if (!param.fileName && !param.fileName && !param.contentType) { |
| 54 | + code.push('formData.append(\'' + param.name + '\',\'' + param.value + '\');') |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if (param.fileName) { |
| 59 | + includeFS = true |
| 60 | + code.blank() |
| 61 | + code.push('formData.append(\'' + param.name + '\', fs.createReadStream(\'' + param.fileName + '\'));') |
| 62 | + } |
| 63 | + }) |
| 64 | + break |
| 65 | + |
| 66 | + default: |
| 67 | + if (source.postData.text) { |
| 68 | + reqOpts.body = source.postData.text |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // construct cookies argument |
| 73 | + if (source.cookies.length) { |
| 74 | + var cookies = '' |
| 75 | + source.cookies.forEach(function (cookie) { |
| 76 | + cookies = cookies + encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value) + '; ' |
| 77 | + }) |
| 78 | + if (reqOpts.headers) { |
| 79 | + reqOpts.headers.cookie = cookies |
| 80 | + } else { |
| 81 | + reqOpts.headers = {} |
| 82 | + reqOpts.headers.cookie = cookies |
| 83 | + } |
| 84 | + } |
| 85 | + code.blank() |
| 86 | + code.push('let url = \'' + url + '\';') |
| 87 | + .blank() |
| 88 | + code.push('let options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })) |
| 89 | + .blank() |
| 90 | + |
| 91 | + if (includeFS) { |
| 92 | + code.unshift('const fs = require(\'fs\');') |
| 93 | + } |
| 94 | + if (source.postData.mimeType === 'multipart/form-data') { |
| 95 | + code.push('options.body = formData;') |
| 96 | + .blank() |
| 97 | + } |
| 98 | + code.push('fetch(url, options)') |
| 99 | + .push(1, '.then(res => res.json())') |
| 100 | + .push(1, '.then(json => console.log(json))') |
| 101 | + .push(1, '.catch(err => console.error(\'error:\' + err));') |
| 102 | + |
| 103 | + return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")') |
| 104 | +} |
| 105 | + |
| 106 | +module.exports.info = { |
| 107 | + key: 'fetch', |
| 108 | + title: 'Fetch', |
| 109 | + link: 'https://github.com/bitinn/node-fetch', |
| 110 | + description: 'Simplified HTTP node-fetch client' |
| 111 | +} |
0 commit comments