Comparing version 3.5.23 to 3.5.24
@@ -5,2 +5,7 @@ # Changelog | ||
## [3.5.24][] - 2022-08-30 | ||
- Add `isError(instance): boolean` | ||
- Support POST requests with body and custom headers in `fetch()` | ||
## [3.5.23][] - 2022-08-12 | ||
@@ -165,3 +170,4 @@ | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.23....HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.24....HEAD | ||
[3.5.24]: https://github.com/metarhia/metautil/compare/v3.5.23...v3.5.24 | ||
[3.5.23]: https://github.com/metarhia/metautil/compare/v3.5.22...v3.5.23 | ||
@@ -168,0 +174,0 @@ [3.5.22]: https://github.com/metarhia/metautil/compare/v3.5.21...v3.5.22 |
@@ -6,22 +6,42 @@ 'use strict'; | ||
const fetch = async (url) => { | ||
const prepareRequest = ({ body, headers = {} }) => { | ||
if (!body) return { headers }; | ||
const data = JSON.stringify(body); | ||
return { | ||
body: data, | ||
headers: { | ||
...headers, | ||
'Content-Type': 'application/json', | ||
'Content-Length': Buffer.byteLength(data), | ||
}, | ||
}; | ||
}; | ||
const prepareResponse = (resolve, reject) => async (res) => { | ||
const code = res.statusCode; | ||
if (code >= 400) { | ||
reject(new Error(`HTTP status code ${code}`)); | ||
return; | ||
} | ||
res.on('error', reject); | ||
const chunks = []; | ||
try { | ||
for await (const chunk of res) chunks.push(chunk); | ||
const json = Buffer.concat(chunks).toString(); | ||
const object = JSON.parse(json); | ||
resolve(object); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}; | ||
const fetch = (url, options = {}) => { | ||
const transport = url.startsWith('https') ? https : http; | ||
return new Promise((resolve, reject) => { | ||
transport.get(url, async (res) => { | ||
const code = res.statusCode; | ||
if (code !== 200) { | ||
reject(new Error(`HTTP status code ${code}`)); | ||
return; | ||
} | ||
res.on('error', reject); | ||
const chunks = []; | ||
try { | ||
for await (const chunk of res) chunks.push(chunk); | ||
const json = Buffer.concat(chunks).toString(); | ||
const object = JSON.parse(json); | ||
resolve(object); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
const { body, headers } = prepareRequest(options); | ||
const opt = { method: 'GET', ...options, headers }; | ||
const callback = prepareResponse(resolve, reject); | ||
const req = transport.request(url, opt, callback); | ||
if (body) req.write(body); | ||
req.end(); | ||
}); | ||
@@ -28,0 +48,0 @@ }; |
@@ -410,2 +410,4 @@ 'use strict'; | ||
const isError = (err) => err?.constructor?.name?.includes('Error') || false; | ||
module.exports = { | ||
@@ -449,2 +451,3 @@ random, | ||
unflatObject, | ||
isError, | ||
}; |
@@ -119,3 +119,9 @@ import { EventEmitter } from 'events'; | ||
export function fetch(url: string): Promise<string>; | ||
export type FetchOptions = { | ||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; | ||
headers?: object; | ||
body?: object; | ||
}; | ||
export function fetch(url: string, options?: FetchOptions): Promise<object>; | ||
export function jsonParse(buffer: Buffer): object | null; | ||
@@ -131,1 +137,3 @@ export function receiveBody(req: IncomingMessage): Promise<Buffer | null>; | ||
): object; | ||
export function isError(instance: object): boolean; |
{ | ||
"name": "metautil", | ||
"version": "3.5.23", | ||
"version": "3.5.24", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -41,4 +41,4 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^18.7.2", | ||
"eslint": "^8.21.0", | ||
"@types/node": "^18.7.14", | ||
"eslint": "^8.23.0", | ||
"eslint-config-metarhia": "^8.1.0", | ||
@@ -50,4 +50,4 @@ "eslint-config-prettier": "^8.5.0", | ||
"prettier": "^2.7.1", | ||
"typescript": "^4.7.4" | ||
"typescript": "^4.8.2" | ||
} | ||
} |
@@ -43,2 +43,3 @@ # Metarhia utilities | ||
- `delay(msec: number, signal?: EventEmitter): Promise<void>` | ||
- `isError(instance): boolean` | ||
@@ -45,0 +46,0 @@ ## Network utilities |
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
37362
846
87