metautil
Advanced tools
Comparing version 4.0.0 to 4.0.1
'use strict'; | ||
class Error extends global.Error { | ||
class Error extends globalThis.Error { | ||
constructor(message, options = {}) { | ||
@@ -5,0 +5,0 @@ super(message); |
@@ -6,11 +6,2 @@ 'use strict'; | ||
const prepareRequest = ({ body, headers = {} }) => { | ||
if (!body) return { headers }; | ||
const content = { | ||
'Content-Type': 'application/json', | ||
'Content-Length': Buffer.byteLength(body), | ||
}; | ||
return { body, headers: { ...content, ...headers } }; | ||
}; | ||
const receiveBody = async (stream) => { | ||
@@ -22,26 +13,2 @@ const chunks = []; | ||
const prepareResponse = (resolve, reject) => (res) => { | ||
const code = res.statusCode; | ||
if (code >= 400) return void reject(new Error(`HTTP status code ${code}`)); | ||
res.on('error', reject); | ||
const parse = (data) => async () => JSON.parse(data.toString()); | ||
const text = (data) => async () => data.toString(); | ||
receiveBody(res) | ||
.then((data) => resolve({ json: parse(data), text: text(data) })) | ||
.catch(reject); | ||
}; | ||
const fetchPolyfill = (url, options = {}) => { | ||
const protocol = url.startsWith('https') ? https : http; | ||
return new Promise((resolve, reject) => { | ||
const { body, headers } = prepareRequest(options); | ||
const opt = { method: 'GET', ...options, headers }; | ||
const callback = prepareResponse(resolve, reject); | ||
const req = protocol.request(url, opt, callback); | ||
req.on('error', reject); | ||
if (body) req.write(body); | ||
req.end(); | ||
}); | ||
}; | ||
const IPV4_OCTETS = 4; | ||
@@ -103,3 +70,2 @@ | ||
module.exports = { | ||
fetch: global.fetch || fetchPolyfill, | ||
receiveBody, | ||
@@ -106,0 +72,0 @@ ipToInt, |
@@ -127,12 +127,2 @@ import { EventEmitter } from 'node:events'; | ||
export type FetchOptions = { | ||
method?: HttpMethod; | ||
headers?: Headers; | ||
body?: Body; | ||
}; | ||
export interface Response { | ||
json(): Promise<Dictionary>; | ||
} | ||
export type ApiOptions = { | ||
@@ -143,3 +133,2 @@ method?: HttpMethod; | ||
export function fetch(url: string, options?: FetchOptions): Promise<Response>; | ||
export function receiveBody(stream: IncomingMessage): Promise<Buffer | null>; | ||
@@ -146,0 +135,0 @@ export function ipToInt(ip?: string): number; |
{ | ||
"name": "metautil", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -19,2 +19,3 @@ "license": "MIT", | ||
"lib/", | ||
"dist.js", | ||
"metautil.d.ts" | ||
@@ -21,0 +22,0 @@ ], |
@@ -70,3 +70,3 @@ # Metarhia utilities | ||
- `fetch(url: string, options?: FetchOptions): Promise<Response>` | ||
- Deprecated in 4.x: `fetch(url: string, options?: FetchOptions): Promise<Response>` | ||
- `receiveBody(stream: IncomingMessage): Promise<Buffer | null>` | ||
@@ -73,0 +73,0 @@ - `ipToInt(ip?: string): number` |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
19
0
40277
1043