Comparing version 2.0.2 to 3.0.0
31
index.js
@@ -1,6 +0,7 @@ | ||
const http = require('http'); | ||
const https = require('https'); | ||
const ErrorWithObject = require('error-with-object'); | ||
import http from 'http'; | ||
import https from 'https'; | ||
function callarest (options, callback) { | ||
export * as callarestJson from './json.js'; | ||
export function callarest (options, callback) { | ||
const uri = new URL(options.url); | ||
@@ -11,6 +12,7 @@ | ||
if (typeof options.body !== 'string') { | ||
return callback(new ErrorWithObject({ | ||
message: 'You did not set the body property to an String. Did you mean to JSON.stringify an object it or use the callarest.json shortcut?', | ||
code: 'SENT_OBJECT_AS_BODY' | ||
})); | ||
return callback(Object.assign( | ||
new Error('callarest: You did not set the body property to an String. Did you mean to JSON.stringify an object it or use the callarest.json shortcut?'), | ||
{ | ||
code: 'SENT_OBJECT_AS_BODY' | ||
})); | ||
} | ||
@@ -46,7 +48,8 @@ headers['Content-Length'] = Buffer.byteLength(options.body); | ||
request.on('error', (error) => { | ||
callback(new ErrorWithObject({ | ||
code: 'REQUEST_ERROR', | ||
...error, | ||
request | ||
})); | ||
callback(Object.assign( | ||
new Error('callarest: request error'), { | ||
code: 'REQUEST_ERROR', | ||
...error, | ||
request | ||
})); | ||
}); | ||
@@ -60,2 +63,2 @@ | ||
module.exports = callarest; | ||
export default callarest; |
17
json.js
@@ -1,3 +0,2 @@ | ||
const ErrorWithObject = require('error-with-object'); | ||
const callarest = require('./'); | ||
import callarest from './index.js'; | ||
@@ -27,7 +26,9 @@ function callarestJson (options, callback) { | ||
} catch (error) { | ||
return callback(new ErrorWithObject({ | ||
code: 'RESPONSE_NOT_VALID_JSON', | ||
message: 'The response body could not be JSON.parsed', | ||
...rest | ||
})); | ||
return callback(Object.assign( | ||
new Error('The response body could not be JSON.parsed'), | ||
{ | ||
code: 'RESPONSE_NOT_VALID_JSON', | ||
...rest | ||
} | ||
)); | ||
} | ||
@@ -39,2 +40,2 @@ | ||
module.exports = callarestJson; | ||
export default callarestJson; |
{ | ||
"name": "callarest", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"type": "module", | ||
"keywords": [ | ||
@@ -15,3 +16,4 @@ "rest", | ||
"scripts": { | ||
"test": "c8 -o /tmp node test" | ||
"test": "c8 -o /tmp node test", | ||
"lint": "eslint ." | ||
}, | ||
@@ -22,4 +24,4 @@ "repository": { | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"author": "Mark Wylde <me@markwylde.com> (https://github.com/markwylde)", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -30,9 +32,10 @@ "url": "https://github.com/markwylde/callarest/issues" | ||
"devDependencies": { | ||
"basictap": "^4.0.2", | ||
"c8": "^7.2.1", | ||
"righto": "^6.1.3", | ||
"tape": "^5.0.1" | ||
"righto": "^6.1.3" | ||
}, | ||
"dependencies": { | ||
"error-with-object": "^1.1.0" | ||
"eslint": "^8.33.0", | ||
"eslint-config-standard": "^17.0.0" | ||
} | ||
} |
# Callarest | ||
[![Build Status](https://travis-ci.org/markwylde/callarest.svg?branch=master)](https://travis-ci.org/markwylde/callarest) | ||
[![David DM](https://david-dm.org/markwylde/callarest.svg)](https://david-dm.org/markwylde/callarest) | ||
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/markwylde/callarest) | ||
[![GitHub package.json version](https://img.shields.io/github/package-json/v/markwylde/callarest)](https://github.com/markwylde/callarest/releases) | ||
[![GitHub](https://img.shields.io/github/license/markwylde/callarest)](https://github.com/markwylde/callarest/blob/master/LICENSE) | ||
A simple tool to natively make http(s) requests in node | ||
@@ -13,3 +7,4 @@ | ||
```javascript | ||
const callarest = require('callarest') | ||
import { callarest } from 'callarest'; | ||
callarest({ | ||
@@ -29,3 +24,3 @@ method: 'post', | ||
console.log('The body was:', rest.body); | ||
// body will be a string | ||
@@ -37,3 +32,4 @@ }) | ||
```javascript | ||
const callarestJson = require('callarest/json) | ||
import { callarestJson } from 'callarest'; | ||
callarestJson({ | ||
@@ -50,3 +46,3 @@ method: 'post', | ||
console.log('The body was:', rest.body); | ||
// body will be a javascript Object | ||
@@ -53,0 +49,0 @@ }) |
@@ -1,3 +0,1 @@ | ||
const ErrorWithObject = require('error-with-object'); | ||
function parseBody (request, callback) { | ||
@@ -18,3 +16,12 @@ let body = []; | ||
} catch (error) { | ||
return callback(new ErrorWithObject({ code: 400, error, body })); | ||
const newError = Object.assign( | ||
new Error('callarest: could not parseBody'), | ||
{ | ||
code: 400, | ||
error, | ||
body | ||
} | ||
); | ||
callback(newError); | ||
return; | ||
} | ||
@@ -30,2 +37,2 @@ } | ||
module.exports = parseBody; | ||
export default parseBody; |
@@ -1,6 +0,6 @@ | ||
const http = require('http'); | ||
const parseBody = require('./parseBody'); | ||
import http from 'http'; | ||
import parseBody from './parseBody.js'; | ||
let server; | ||
function createServer (callback) { | ||
export function createServer (callback) { | ||
server = http.createServer((request, response) => { | ||
@@ -27,3 +27,3 @@ if (request.url === '/echo' && request.method === 'POST') { | ||
function createJsonServer (data, callback) { | ||
export function createJsonServer (data, callback) { | ||
if (arguments.length === 1) { | ||
@@ -42,6 +42,8 @@ callback = data; | ||
}); | ||
const dataToSend = data != null ? data : JSON.stringify({ | ||
a: 'you said', | ||
b: body | ||
}); | ||
const dataToSend = data != null | ||
? data | ||
: JSON.stringify({ | ||
a: 'you said', | ||
b: body | ||
}); | ||
response.end(dataToSend); | ||
@@ -60,10 +62,4 @@ }); | ||
function destroyServer () { | ||
export function destroyServer () { | ||
server.close(); | ||
} | ||
module.exports = { | ||
createServer, | ||
createJsonServer, | ||
destroyServer | ||
}; |
@@ -1,2 +0,2 @@ | ||
require('./raw'); | ||
require('./json'); | ||
import './raw.js'; | ||
import './json.js'; |
@@ -1,6 +0,6 @@ | ||
const test = require('tape'); | ||
const righto = require('righto'); | ||
import test from 'basictap'; | ||
import righto from 'righto'; | ||
const { createJsonServer, destroyServer } = require('./helpers/server'); | ||
const callarestJson = require('../json'); | ||
import { createJsonServer, destroyServer } from './helpers/server.js'; | ||
import callarestJson from '../json.js'; | ||
@@ -50,3 +50,3 @@ test('get -> success', t => { | ||
request(function (error, result) { | ||
t.equal(error.code, 'ENOTFOUND'); | ||
t.equal(error.code, 'EAI_AGAIN'); | ||
t.ok(error.request); | ||
@@ -88,3 +88,6 @@ t.notOk(result); | ||
t.ok(result); | ||
t.deepEqual(result.body, { a: 'you said', b: '{"b":1}' }); | ||
t.deepEqual(result.body, { | ||
a: 'you said', | ||
b: '{"b":1}' | ||
}); | ||
@@ -107,3 +110,6 @@ destroyServer(); | ||
if (error) { console.log(error); } | ||
t.deepEqual(result.body, { a: 'you said', b: '"something"' }); | ||
t.deepEqual(result.body, { | ||
a: 'you said', | ||
b: '"something"' | ||
}); | ||
@@ -110,0 +116,0 @@ destroyServer(); |
@@ -1,6 +0,6 @@ | ||
const test = require('tape'); | ||
const righto = require('righto'); | ||
import test from 'basictap'; | ||
import righto from 'righto'; | ||
const { createServer, destroyServer } = require('./helpers/server'); | ||
const callarest = require('../'); | ||
import { createServer, destroyServer } from './helpers/server.js'; | ||
import callarest from '../index.js'; | ||
@@ -14,3 +14,3 @@ test('get [https] -> success', t => { | ||
request(function (error, result) { | ||
request(function (error) { | ||
t.equal(error.code, 'ECONNREFUSED'); | ||
@@ -46,3 +46,3 @@ }); | ||
request(function (error, result) { | ||
t.equal(error.code, 'ENOTFOUND'); | ||
t.equal(error.code, 'EAI_AGAIN'); | ||
t.ok(error.request); | ||
@@ -86,3 +86,3 @@ t.notOk(result); | ||
request(function (error, result) { | ||
request(function (error) { | ||
t.equal(error.code, 'SENT_OBJECT_AS_BODY'); | ||
@@ -89,0 +89,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
12731
12
1
357
0
Yes
2
50
+ Addedeslint@^8.33.0
+ Added@eslint-community/eslint-utils@4.4.0(transitive)
+ Added@eslint-community/regexpp@4.11.1(transitive)
+ Added@eslint/eslintrc@2.1.4(transitive)
+ Added@eslint/js@8.57.1(transitive)
+ Added@humanwhocodes/config-array@0.13.0(transitive)
+ Added@humanwhocodes/module-importer@1.0.1(transitive)
+ Added@humanwhocodes/object-schema@2.0.3(transitive)
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Added@rtsao/scc@1.1.0(transitive)
+ Added@types/json5@0.0.29(transitive)
+ Added@ungap/structured-clone@1.2.0(transitive)
+ Addedacorn@8.12.1(transitive)
+ Addedacorn-jsx@5.3.2(transitive)
+ Addedajv@6.12.6(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedargparse@2.0.1(transitive)
+ Addedarray-buffer-byte-length@1.0.1(transitive)
+ Addedarray-includes@3.1.8(transitive)
+ Addedarray.prototype.findlastindex@1.2.5(transitive)
+ Addedarray.prototype.flat@1.3.2(transitive)
+ Addedarray.prototype.flatmap@1.3.2(transitive)
+ Addedarraybuffer.prototype.slice@1.0.3(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbuiltin-modules@3.3.0(transitive)
+ Addedbuiltins@5.1.0(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcallsites@3.1.0(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcross-spawn@7.0.3(transitive)
+ Addeddata-view-buffer@1.0.1(transitive)
+ Addeddata-view-byte-length@1.0.1(transitive)
+ Addeddata-view-byte-offset@1.0.0(transitive)
+ Addeddebug@3.2.74.3.7(transitive)
+ Addeddeep-is@0.1.4(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddoctrine@2.1.03.0.0(transitive)
+ Addedes-abstract@1.23.3(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedes-set-tostringtag@2.0.3(transitive)
+ Addedes-shim-unscopables@1.0.2(transitive)
+ Addedes-to-primitive@1.2.1(transitive)
+ Addedescape-string-regexp@4.0.0(transitive)
+ Addedeslint@8.57.1(transitive)
+ Addedeslint-compat-utils@0.5.1(transitive)
+ Addedeslint-config-standard@17.1.0(transitive)
+ Addedeslint-import-resolver-node@0.3.9(transitive)
+ Addedeslint-module-utils@2.12.0(transitive)
+ Addedeslint-plugin-es-x@7.8.0(transitive)
+ Addedeslint-plugin-import@2.30.0(transitive)
+ Addedeslint-plugin-n@16.6.2(transitive)
+ Addedeslint-plugin-promise@6.6.0(transitive)
+ Addedeslint-scope@7.2.2(transitive)
+ Addedeslint-visitor-keys@3.4.3(transitive)
+ Addedespree@9.6.1(transitive)
+ Addedesquery@1.6.0(transitive)
+ Addedesrecurse@4.3.0(transitive)
+ Addedestraverse@5.3.0(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedfast-levenshtein@2.0.6(transitive)
+ Addedfastq@1.17.1(transitive)
+ Addedfile-entry-cache@6.0.1(transitive)
+ Addedfind-up@5.0.0(transitive)
+ Addedflat-cache@3.2.0(transitive)
+ Addedflatted@3.3.1(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunction.prototype.name@1.1.6(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-symbol-description@1.0.2(transitive)
+ Addedget-tsconfig@4.8.1(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglob-parent@6.0.2(transitive)
+ Addedglobals@13.24.0(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedgraphemer@1.4.0(transitive)
+ Addedhas-bigints@1.0.2(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedimport-fresh@3.3.0(transitive)
+ Addedimurmurhash@0.1.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinternal-slot@1.0.7(transitive)
+ Addedis-array-buffer@3.0.4(transitive)
+ Addedis-bigint@1.0.4(transitive)
+ Addedis-boolean-object@1.1.2(transitive)
+ Addedis-builtin-module@3.2.1(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-data-view@1.0.1(transitive)
+ Addedis-date-object@1.0.5(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-negative-zero@2.0.3(transitive)
+ Addedis-number-object@1.0.7(transitive)
+ Addedis-path-inside@3.0.3(transitive)
+ Addedis-regex@1.1.4(transitive)
+ Addedis-shared-array-buffer@1.0.3(transitive)
+ Addedis-string@1.0.7(transitive)
+ Addedis-symbol@1.0.4(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedis-weakref@1.0.2(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjs-yaml@4.1.0(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stable-stringify-without-jsonify@1.0.1(transitive)
+ Addedjson5@1.0.2(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedlevn@0.4.1(transitive)
+ Addedlocate-path@6.0.0(transitive)
+ Addedlodash.merge@4.6.2(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedms@2.1.3(transitive)
+ Addednatural-compare@1.4.0(transitive)
+ Addedobject-inspect@1.13.2(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedobject.fromentries@2.0.8(transitive)
+ Addedobject.groupby@1.0.3(transitive)
+ Addedobject.values@1.2.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedoptionator@0.9.4(transitive)
+ Addedp-limit@3.1.0(transitive)
+ Addedp-locate@5.0.0(transitive)
+ Addedparent-module@1.0.1(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedprelude-ls@1.2.1(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedregexp.prototype.flags@1.5.2(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedresolve-from@4.0.0(transitive)
+ Addedresolve-pkg-maps@1.0.0(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrimraf@3.0.2(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedsafe-array-concat@1.1.2(transitive)
+ Addedsafe-regex-test@1.0.3(transitive)
+ Addedsemver@6.3.17.6.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstring.prototype.trim@1.2.9(transitive)
+ Addedstring.prototype.trimend@1.0.8(transitive)
+ Addedstring.prototype.trimstart@1.0.8(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedstrip-bom@3.0.0(transitive)
+ Addedstrip-json-comments@3.1.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtext-table@0.2.0(transitive)
+ Addedtsconfig-paths@3.15.0(transitive)
+ Addedtype-check@0.4.0(transitive)
+ Addedtype-fest@0.20.2(transitive)
+ Addedtyped-array-buffer@1.0.2(transitive)
+ Addedtyped-array-byte-length@1.0.1(transitive)
+ Addedtyped-array-byte-offset@1.0.2(transitive)
+ Addedtyped-array-length@1.0.6(transitive)
+ Addedunbox-primitive@1.0.2(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwhich-boxed-primitive@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
+ Addedword-wrap@1.2.5(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyocto-queue@0.1.0(transitive)
- Removederror-with-object@^1.1.0
- Removederror-with-object@1.1.0(transitive)