@sibipro/ask
Advanced tools
Comparing version 1.1.5 to 1.2.0
41
ask.js
const fetch = require('node-fetch'); | ||
module.exports = async function ask(url, config) { | ||
try { | ||
const defaults = { | ||
responseType: 'json', | ||
method: 'get' | ||
}; | ||
const { responseType, ...init } = { ...defaults, ...config }; | ||
const response = await fetch(url, init); | ||
const defaults = { | ||
responseType: 'json', | ||
method: 'get', | ||
}; | ||
if (!response.ok) { | ||
const err = new Error(response.statusText); | ||
err.statusCode = response.statusCode; | ||
throw err; | ||
} | ||
const isObject = v => typeof v === 'object' && !Array.isArray(v) && v !== null; | ||
if (responseType === 'json') return response.json(); | ||
if (responseType === 'text') return response.text(); | ||
const encodeBody = config => { | ||
if (!isObject(config.body)) return config; | ||
return response; | ||
} catch (err) { | ||
console.log('internal-query', url, err); | ||
// if body is an object, stringify it | ||
return { ...config, body: JSON.stringify(config.body) }; | ||
}; | ||
module.exports = async function ask(url, config) { | ||
const { responseType, ...init } = { ...defaults, ...encodeBody(config) }; | ||
const response = await fetch(url, init); | ||
if (!response.ok) { | ||
const err = new Error(response.statusText); | ||
err.statusCode = response.statusCode; | ||
throw err; | ||
} | ||
if (responseType === 'json') return response.json(); | ||
if (responseType === 'text') return response.text(); | ||
return response; | ||
}; |
@@ -19,3 +19,3 @@ const fetch = require('node-fetch'); | ||
ok: true, | ||
json: jest.fn(() => ['url 1', 'url 2']) | ||
json: jest.fn(() => ['url 1', 'url 2']), | ||
}); | ||
@@ -26,5 +26,5 @@ | ||
headers: { | ||
id: '' | ||
id: '', | ||
}, | ||
responseType: 'json' | ||
responseType: 'json', | ||
}); | ||
@@ -38,3 +38,3 @@ | ||
ok: true, | ||
text: jest.fn(() => ['url 1', 'url 2']) | ||
text: jest.fn(() => ['url 1', 'url 2']), | ||
}); | ||
@@ -45,5 +45,5 @@ | ||
headers: { | ||
id: '' | ||
id: '', | ||
}, | ||
responseType: 'text' | ||
responseType: 'text', | ||
}); | ||
@@ -57,3 +57,3 @@ | ||
ok: true, | ||
body: ['url 1', 'url 2'] | ||
body: ['url 1', 'url 2'], | ||
}); | ||
@@ -64,5 +64,5 @@ | ||
headers: { | ||
id: '' | ||
id: '', | ||
}, | ||
responseType: 'raw' | ||
responseType: 'raw', | ||
}); | ||
@@ -78,18 +78,63 @@ | ||
statusCode: 500, | ||
json: jest.fn(() => ['url 1', 'url 2']) | ||
json: jest.fn(() => ['url 1', 'url 2']), | ||
}); | ||
try { | ||
return await ask('test/fetch/url', { | ||
await ask('test/fetch/url', { | ||
method: 'get', | ||
headers: { | ||
id: '' | ||
id: '', | ||
}, | ||
responseType: 'json' | ||
responseType: 'json', | ||
}); | ||
} catch (err) { | ||
// eslint-disable-next-line jest/no-try-expect | ||
expect(err).toBeDefined(); | ||
// eslint-disable-next-line jest/no-try-expect | ||
expect(err.message).toEqual('Error thrown'); | ||
// eslint-disable-next-line jest/no-try-expect | ||
expect(err.statusCode).toEqual(500); | ||
} | ||
}); | ||
describe('object body', () => { | ||
beforeEach(() => { | ||
fetch.mockResolvedValue({ ok: true, json: jest.fn() }); | ||
}); | ||
test('stringifies the object body', async () => { | ||
const body = { stringify: 'me' }; | ||
await ask('test/fetch/url', { | ||
method: 'get', | ||
responseType: 'json', | ||
body, | ||
}); | ||
expect(fetch).toHaveBeenCalledWith( | ||
'test/fetch/url', | ||
expect.objectContaining({ | ||
body: JSON.stringify(body), | ||
}) | ||
); | ||
}); | ||
test('does nothing if body is a string', async () => { | ||
await ask('test/fetch/url', { | ||
method: 'get', | ||
responseType: 'json', | ||
body: '{}', | ||
headers: { | ||
'X-Sent-From': 'jest', | ||
}, | ||
}); | ||
expect(fetch).toHaveBeenCalledWith('test/fetch/url', { | ||
body: '{}', | ||
headers: { | ||
'X-Sent-From': 'jest', | ||
}, | ||
method: 'get', | ||
}); | ||
}); | ||
}); | ||
}); |
module.exports = { | ||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
testEnvironment: 'node' | ||
testEnvironment: 'node', | ||
}; |
{ | ||
"name": "@sibipro/ask", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"main": "index.js", | ||
@@ -8,3 +8,4 @@ "license": "MIT", | ||
"test": "NODE_ENV=development jest", | ||
"test:watch": "yarn test --watchAll" | ||
"test:watch": "yarn test --watchAll", | ||
"lint": "eslint *.js" | ||
}, | ||
@@ -19,3 +20,7 @@ "repository": { | ||
"devDependencies": { | ||
"jest": "24.9.0" | ||
"@sibipro/eslint-config": "^1.1.0", | ||
"@sibipro/eslint-config-jest": "^1.1.1", | ||
"eslint": "^6.8.0", | ||
"jest": "24.9.0", | ||
"prettier": "^1.19.1" | ||
}, | ||
@@ -26,2 +31,8 @@ "jest": { | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"jsxBracketSameLine": true, | ||
"printWidth": 120, | ||
"trailingComma": "es5" | ||
}, | ||
"keywords": [ | ||
@@ -28,0 +39,0 @@ "node-fetch", |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
6578
8
145
5
10