@osskit/fetch-enhancers
Advanced tools
Comparing version 0.0.28 to 1.0.1
@@ -1,3 +0,3 @@ | ||
import { Request, RequestInit, Response } from 'node-fetch'; | ||
export declare type FetchAPI = (url: string | Request, init?: RequestInit) => Promise<Response>; | ||
export declare const authorizedFetch: (getToken: () => Promise<string> | string, fetch?: FetchAPI | undefined, init?: RequestInit) => (url: string | Request, innerInit?: RequestInit) => Promise<Response>; | ||
import type { Request, RequestInit, Response } from 'node-fetch'; | ||
export declare type FetchAPI = (url: Request | string, init?: RequestInit) => Promise<Response>; | ||
export declare const authorizedFetch: (getToken: () => Promise<string> | string, fetch: FetchAPI, init?: RequestInit) => (url: Request | string, innerInit?: RequestInit) => Promise<Response>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.authorizedFetch = void 0; | ||
const node_fetch_1 = __importDefault(require("node-fetch")); | ||
const authorizedFetch = (getToken, fetch, init = {}) => async (url, innerInit = {}) => { | ||
const innerFetch = fetch || node_fetch_1.default; | ||
const innerFetch = fetch; | ||
const token = await getToken(); | ||
@@ -11,0 +7,0 @@ return innerFetch(url, { |
@@ -1,2 +0,2 @@ | ||
import { Fetch } from '../types'; | ||
import type { Fetch } from '../types'; | ||
export interface BasicAuthenticationParams { | ||
@@ -3,0 +3,0 @@ username: string; |
@@ -16,3 +16,3 @@ "use strict"; | ||
const responseText = await response.text(); | ||
throw new types_1.FetchError(responseText ?? 'fetch error', url.toString()); | ||
throw new types_1.FetchError(responseText ?? 'fetch error', JSON.stringify(url)); | ||
} | ||
@@ -19,0 +19,0 @@ return response; |
@@ -1,2 +0,2 @@ | ||
import { Fetch } from '../types'; | ||
import type { Fetch } from '../types'; | ||
export declare const withHeaders: (fetch: Fetch, headers: Record<string, string>) => Fetch; |
@@ -1,3 +0,4 @@ | ||
import { Fetch, FetchError } from '../types'; | ||
export declare type RetryOptions = { | ||
import type { Fetch } from '../types'; | ||
import { FetchError } from '../types'; | ||
export interface RetryOptions { | ||
minTimeout?: number; | ||
@@ -7,3 +8,3 @@ retries?: number; | ||
onRetry?: (error: FetchError) => void; | ||
}; | ||
export declare const withRetry: (fetch: Fetch, options?: RetryOptions) => Fetch; | ||
} | ||
export declare const withRetry: (fetch: Fetch, options?: RetryOptions | undefined) => Fetch; |
@@ -9,5 +9,6 @@ "use strict"; | ||
const types_1 = require("../types"); | ||
const withRetry = (fetch, options = { minTimeout: 10, retries: 3, factor: 5, onRetry: () => { } }) => (url, init) => { | ||
const withRetry = (fetch, options) => (url, init) => { | ||
const finalOptions = { minTimeout: 10, retries: 3, factor: 5, ...options }; | ||
return (0, async_retry_1.default)(async (_bail, attempt) => { | ||
const isRetry = attempt < (options.retries ?? 3); | ||
const isRetry = attempt < (finalOptions.retries ?? 3); | ||
const response = await fetch(url, init); | ||
@@ -18,9 +19,9 @@ if (response.status < 500 || response.status >= 600 || !isRetry) { | ||
const responseText = await response.text(); | ||
throw new types_1.FetchError(responseText ?? 'fetch error', url.toString(), { | ||
throw new types_1.FetchError(responseText ?? 'fetch error', JSON.stringify(url), { | ||
attempt: String(attempt), | ||
status: String(response.status), | ||
}); | ||
}, { ...options, onRetry: (err) => options.onRetry?.(err) }); | ||
}, { ...finalOptions, onRetry: (err) => finalOptions.onRetry?.(err) }); | ||
}; | ||
exports.withRetry = withRetry; | ||
//# sourceMappingURL=withRetry.js.map |
@@ -1,2 +0,2 @@ | ||
import { Fetch } from '../types'; | ||
import type { Fetch } from '../types'; | ||
export declare const withThrow: (fetch: Fetch) => Fetch; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.withThrow = void 0; | ||
const node_fetch_1 = require("node-fetch"); | ||
const types_1 = require("../types"); | ||
const withThrow = (fetch) => async (url, init) => { | ||
@@ -9,3 +9,3 @@ const response = await fetch(url, init); | ||
const responseText = await response.text(); | ||
throw new node_fetch_1.FetchError(responseText ?? 'fetch error', url.toString()); | ||
throw new types_1.FetchError(responseText ?? 'fetch error', JSON.stringify(url)); | ||
} | ||
@@ -12,0 +12,0 @@ return response; |
@@ -1,6 +0,6 @@ | ||
import { RequestInfo, RequestInit } from 'node-fetch'; | ||
import { Fetch } from '../types'; | ||
export declare type TimeoutOptions = { | ||
import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
import type { Fetch } from '../types'; | ||
export interface TimeoutOptions { | ||
requestTimeoutMs: number; | ||
}; | ||
} | ||
export declare const withTimeout: (fetch: Fetch, options: TimeoutOptions) => (url: RequestInfo, init?: RequestInit | undefined) => Promise<import("node-fetch").Response>; |
@@ -8,11 +8,13 @@ "use strict"; | ||
const abort_controller_1 = __importDefault(require("abort-controller")); | ||
const node_fetch_1 = require("node-fetch"); | ||
const types_1 = require("../types"); | ||
const withTimeout = (fetch, options) => async (url, init) => { | ||
const controller = new abort_controller_1.default(); | ||
const timeoutId = setTimeout(() => controller.abort(), options.requestTimeoutMs); | ||
const timeoutId = setTimeout(() => { | ||
controller.abort(); | ||
}, options.requestTimeoutMs); | ||
try { | ||
return await fetch(url, Object.assign({ signal: controller.signal }, init)); | ||
return await fetch(url, { signal: controller.signal, ...init }); | ||
} | ||
catch (error) { | ||
throw new node_fetch_1.FetchError(error.message ?? 'fetch error', url.toString()); | ||
throw new types_1.FetchError(error.message ?? 'fetch error', JSON.stringify(url)); | ||
} | ||
@@ -19,0 +21,0 @@ finally { |
import type { RequestInfo, RequestInit, Response, Request } from 'node-fetch'; | ||
export declare type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
export declare class FetchError extends Error { | ||
message: string; | ||
url: string; | ||
@@ -12,7 +11,7 @@ data?: Record<string, string>; | ||
}; | ||
export declare type EnhancedFetch<T> = (url: string | Request, init?: RequestInit & EnhancedFetchRequestInit<T>) => Promise<Response>; | ||
export declare type FetchEnhancer<T1> = <T2 extends {}>(fetch: Fetch | EnhancedFetch<T2>, options?: T1) => EnhancedFetch<T1 & T2>; | ||
export declare type FetchEnhancerWithMandatoryOptions<T1> = <T2 extends {}>(fetch: Fetch | EnhancedFetch<T2>, options: T1) => EnhancedFetch<T1 & T2>; | ||
export declare type EnhancedFetch<T> = (url: Request | string, init?: EnhancedFetchRequestInit<T> & RequestInit) => Promise<Response>; | ||
export declare type FetchEnhancer<T1> = <T2 extends {}>(fetch: EnhancedFetch<T2> | Fetch, options?: T1) => EnhancedFetch<T1 & T2>; | ||
export declare type FetchEnhancerWithMandatoryOptions<T1> = <T2 extends {}>(fetch: EnhancedFetch<T2> | Fetch, options: T1) => EnhancedFetch<T1 & T2>; | ||
export declare class FetchAuthorizationError extends Error { | ||
constructor(message: string, url: string | Request, requestInit: RequestInit); | ||
constructor(message: string, url: Request | string, requestInit: RequestInit); | ||
status: number; | ||
@@ -19,0 +18,0 @@ url: string; |
@@ -5,3 +5,2 @@ "use strict"; | ||
class FetchError extends Error { | ||
message; | ||
url; | ||
@@ -11,5 +10,5 @@ data; | ||
super(message); | ||
this.message = message; | ||
this.url = url; | ||
this.data = data; | ||
this.name = 'FetchError'; | ||
} | ||
@@ -21,6 +20,6 @@ } | ||
super(message); | ||
Object.setPrototypeOf(this, FetchAuthorizationError.prototype); | ||
this.url = url.toString(); | ||
this.url = JSON.stringify(url); | ||
this.requestInit = requestInit; | ||
this.status = 403; | ||
this.name = 'FetchAuthorizationError'; | ||
} | ||
@@ -27,0 +26,0 @@ status; |
109
package.json
{ | ||
"name": "@osskit/fetch-enhancers", | ||
"version": "0.0.28", | ||
"repository": { | ||
"url": "https://github.com/osskit/monitor" | ||
}, | ||
"main": "dist/src/index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "jest --runInBand --forceExit --config tests/jest.config.ts", | ||
"lint": "eslint src --fix --ext .ts", | ||
"format": "prettier --write '**/*.{ts,js,json}'", | ||
"prepare": "husky install" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/async-retry": "^1.4.3", | ||
"@types/jest": "^27.0.2", | ||
"@types/lodash.clonedeep": "^4.5.6", | ||
"@types/node": "^16.11.6", | ||
"@types/node-fetch": "^2.5.12", | ||
"@types/uuid": "^8.3.1", | ||
"@typescript-eslint/eslint-plugin": "^5.2.0", | ||
"eslint": "^8.1.0", | ||
"eslint-config-noamokman": "^10.3.11", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.25.2", | ||
"eslint-plugin-jest": "^25.2.2", | ||
"eslint-plugin-lodash": "^7.3.0", | ||
"eslint-plugin-unicorn": "^37.0.1", | ||
"husky": "^7.0.4", | ||
"jest": "^27.3.1", | ||
"lint-staged": "^11.2.6", | ||
"node-fetch": "^2.6.6", | ||
"npm-link-check": "^4.0.0", | ||
"prettier": "^2.4.1", | ||
"ts-jest": "^27.0.7", | ||
"ts-node": "^10.4.0", | ||
"tsc": "^2.0.3", | ||
"tslint": "^6.1.3", | ||
"tslint-clean-code": "^0.2.10", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-immutable": "^6.0.1", | ||
"typescript": "^4.4.4", | ||
"uuid": "^8.3.2" | ||
}, | ||
"dependencies": { | ||
"abort-controller": "^3.0.0", | ||
"async-retry": "^1.3.3", | ||
"eslint": "^7.30.0", | ||
"lodash.clonedeep": "^4.5.0" | ||
}, | ||
"lint-staged": { | ||
"*.ts": "eslint --fix", | ||
"*.{ts,js,json}": "prettier --write" | ||
} | ||
"name": "@osskit/fetch-enhancers", | ||
"version": "1.0.1", | ||
"repository": { | ||
"url": "https://github.com/osskit/fetch-enhancers" | ||
}, | ||
"main": "dist/src/index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "jest --runInBand --forceExit --config tests/jest.config.ts", | ||
"lint": "eslint src --fix --ext .ts", | ||
"format": "prettier --write '**/*.{ts,js,json}'", | ||
"prepare": "husky install" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"devDependencies": { | ||
"@osskit/eslint-config": "^0.0.6", | ||
"@osskit/prettier-config": "^0.0.1", | ||
"@types/async-retry": "^1.4.3", | ||
"@types/jest": "^27.0.2", | ||
"@types/lodash.clonedeep": "^4.5.6", | ||
"@types/node": "^16.11.6", | ||
"@types/node-fetch": "^2.5.12", | ||
"@types/uuid": "^8.3.1", | ||
"@typescript-eslint/eslint-plugin": "^4.32.0", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-jest": "^24.5.0", | ||
"eslint-plugin-react": "^7.26.0", | ||
"eslint-plugin-unicorn": "^36.0.0", | ||
"eslint": "^7.32.0", | ||
"husky": "^7.0.4", | ||
"jest": "^27.3.1", | ||
"lint-staged": "^11.2.6", | ||
"node-fetch": "^2.6.6", | ||
"npm-link-check": "^4.0.0", | ||
"prettier": "^2.4.1", | ||
"ts-jest": "^27.0.7", | ||
"ts-node": "^10.4.0", | ||
"tsc": "^2.0.3", | ||
"typescript": "^4.4.4", | ||
"uuid": "^8.3.2" | ||
}, | ||
"dependencies": { | ||
"abort-controller": "^3.0.0", | ||
"async-retry": "^1.3.3", | ||
"lodash.clonedeep": "^4.5.0" | ||
}, | ||
"lint-staged": { | ||
"*.ts": "eslint --fix", | ||
"*.{ts,js,json}": "prettier --write" | ||
} | ||
} |
@@ -1,19 +0,21 @@ | ||
import nodeFetch, { Request, RequestInit, Response } from 'node-fetch'; | ||
import type { Request, RequestInit, Response } from 'node-fetch'; | ||
export type FetchAPI = (url: string | Request, init?: RequestInit) => Promise<Response>; | ||
export type FetchAPI = (url: Request | string, init?: RequestInit) => Promise<Response>; | ||
export const authorizedFetch = | ||
(getToken: () => Promise<string> | string, fetch?: FetchAPI, init: RequestInit = {}) => | ||
async (url: string | Request, innerInit: RequestInit = {}): Promise<Response> => { | ||
const innerFetch = fetch || nodeFetch; | ||
const token = await getToken(); | ||
return innerFetch(url, { | ||
...innerInit, | ||
...init, | ||
headers: { | ||
...innerInit.headers, | ||
...init.headers, | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
}; | ||
(getToken: () => Promise<string> | string, fetch: FetchAPI, init: RequestInit = {}) => | ||
async (url: Request | string, innerInit: RequestInit = {}): Promise<Response> => { | ||
const innerFetch = fetch; | ||
const token = await getToken(); | ||
return innerFetch(url, { | ||
...innerInit, | ||
...init, | ||
headers: { | ||
...innerInit.headers, | ||
...init.headers, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
}; |
@@ -1,28 +0,32 @@ | ||
import { RequestInfo, RequestInit } from 'node-fetch'; | ||
import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
import { Fetch, FetchError } from '../types'; | ||
import type { Fetch } from '../types'; | ||
import { FetchError } from '../types'; | ||
export interface BasicAuthenticationParams { | ||
username: string; | ||
password: string; | ||
username: string; | ||
password: string; | ||
} | ||
export const withBasicAuth = | ||
(fetch: Fetch, options: BasicAuthenticationParams): Fetch => | ||
async (url: RequestInfo, init?: RequestInit) => { | ||
const { username, password } = options; | ||
(fetch: Fetch, options: BasicAuthenticationParams): Fetch => | ||
async (url: RequestInfo, init?: RequestInit) => { | ||
const { username, password } = options; | ||
const response = await fetch(url, { | ||
...init, | ||
headers: { | ||
...init?.headers, | ||
Authorization: `Basic ${Buffer.from(`${username}:${password}`, 'binary').toString('base64')}`, | ||
}, | ||
}); | ||
const response = await fetch(url, { | ||
...init, | ||
headers: { | ||
...init?.headers, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
Authorization: `Basic ${Buffer.from(`${username}:${password}`, 'binary').toString('base64')}`, | ||
}, | ||
}); | ||
if (!response.ok) { | ||
const responseText = await response.text(); | ||
throw new FetchError(responseText ?? 'fetch error', url.toString()); | ||
} | ||
return response; | ||
}; | ||
if (!response.ok) { | ||
const responseText = await response.text(); | ||
throw new FetchError(responseText ?? 'fetch error', JSON.stringify(url)); | ||
} | ||
return response; | ||
}; |
@@ -1,14 +0,14 @@ | ||
import { RequestInfo, RequestInit } from 'node-fetch'; | ||
import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
import { Fetch } from '../types'; | ||
import type { Fetch } from '../types'; | ||
export const withHeaders = | ||
(fetch: Fetch, headers: Record<string, string>): Fetch => | ||
(url: RequestInfo, init?: RequestInit) => | ||
fetch(url, { | ||
...init, | ||
headers: { | ||
...init?.headers, | ||
...headers, | ||
}, | ||
}); | ||
(fetch: Fetch, headers: Record<string, string>): Fetch => | ||
(url: RequestInfo, init?: RequestInit) => | ||
fetch(url, { | ||
...init, | ||
headers: { | ||
...init?.headers, | ||
...headers, | ||
}, | ||
}); |
import retry from 'async-retry'; | ||
import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
import { Fetch, FetchError } from '../types'; | ||
import type { Fetch } from '../types'; | ||
import { FetchError } from '../types'; | ||
export type RetryOptions = { | ||
minTimeout?: number; | ||
retries?: number; | ||
factor?: number; | ||
onRetry?: (error: FetchError) => void; | ||
}; | ||
export interface RetryOptions { | ||
minTimeout?: number; | ||
retries?: number; | ||
factor?: number; | ||
onRetry?: (error: FetchError) => void; | ||
} | ||
export const withRetry = | ||
(fetch: Fetch, options: RetryOptions = { minTimeout: 10, retries: 3, factor: 5, onRetry: () => {} }): Fetch => | ||
(url: RequestInfo, init?: RequestInit) => { | ||
return retry( | ||
async (_bail, attempt) => { | ||
const isRetry = attempt < (options.retries ?? 3); | ||
const response = await fetch(url, init); | ||
(fetch: Fetch, options?: RetryOptions): Fetch => | ||
(url: RequestInfo, init?: RequestInit) => { | ||
const finalOptions = { minTimeout: 10, retries: 3, factor: 5, ...options }; | ||
if (response.status < 500 || response.status >= 600 || !isRetry) { | ||
return response; | ||
} | ||
return retry( | ||
async (_bail, attempt) => { | ||
const isRetry = attempt < (finalOptions.retries ?? 3); | ||
const response = await fetch(url, init); | ||
const responseText = await response.text(); | ||
if (response.status < 500 || response.status >= 600 || !isRetry) { | ||
return response; | ||
} | ||
throw new FetchError(responseText ?? 'fetch error', url.toString(), { | ||
attempt: String(attempt), | ||
status: String(response.status), | ||
}); | ||
}, | ||
{ ...options, onRetry: (err) => options.onRetry?.(err as FetchError) }, | ||
); | ||
}; | ||
const responseText = await response.text(); | ||
throw new FetchError(responseText ?? 'fetch error', JSON.stringify(url), { | ||
attempt: String(attempt), | ||
status: String(response.status), | ||
}); | ||
}, | ||
{ ...finalOptions, onRetry: (err) => finalOptions.onRetry?.(err as FetchError) }, | ||
); | ||
}; |
import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
import { FetchError } from 'node-fetch'; | ||
import { Fetch } from '../types'; | ||
import type { Fetch } from '../types'; | ||
import { FetchError } from '../types'; | ||
export const withThrow = | ||
(fetch: Fetch): Fetch => | ||
async (url: RequestInfo, init?: RequestInit) => { | ||
const response = await fetch(url, init); | ||
if (!response.ok) { | ||
const responseText = await response.text(); | ||
throw new FetchError(responseText ?? 'fetch error', url.toString()); | ||
} | ||
return response; | ||
}; | ||
(fetch: Fetch): Fetch => | ||
async (url: RequestInfo, init?: RequestInit) => { | ||
const response = await fetch(url, init); | ||
if (!response.ok) { | ||
const responseText = await response.text(); | ||
throw new FetchError(responseText ?? 'fetch error', JSON.stringify(url)); | ||
} | ||
return response; | ||
}; |
import AbortController from 'abort-controller'; | ||
import { FetchError, RequestInfo, RequestInit } from 'node-fetch'; | ||
import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
import { Fetch } from '../types'; | ||
import type { Fetch } from '../types'; | ||
import { FetchError } from '../types'; | ||
export type TimeoutOptions = { | ||
requestTimeoutMs: number; | ||
}; | ||
export interface TimeoutOptions { | ||
requestTimeoutMs: number; | ||
} | ||
export const withTimeout = (fetch: Fetch, options: TimeoutOptions) => async (url: RequestInfo, init?: RequestInit) => { | ||
const controller = new AbortController(); | ||
const timeoutId = setTimeout(() => controller.abort(), options.requestTimeoutMs); | ||
const controller = new AbortController(); | ||
const timeoutId = setTimeout(() => { | ||
controller.abort(); | ||
}, options.requestTimeoutMs); | ||
try { | ||
return await fetch(url, Object.assign({ signal: controller.signal }, init)); | ||
} catch (error) { | ||
throw new FetchError((error as any).message ?? 'fetch error', url.toString()); | ||
} finally { | ||
clearTimeout(timeoutId); | ||
} | ||
try { | ||
return await fetch(url, { signal: controller.signal, ...init }); | ||
} catch (error) { | ||
throw new FetchError((error as Error).message ?? 'fetch error', JSON.stringify(url)); | ||
} finally { | ||
clearTimeout(timeoutId); | ||
} | ||
}; |
@@ -6,42 +6,40 @@ import type { RequestInfo, RequestInit, Response, Request } from 'node-fetch'; | ||
export class FetchError extends Error { | ||
message: string; | ||
url: string; | ||
data?: Record<string, string>; | ||
constructor(message: string, url: string, data?: Record<string, string>) { | ||
super(message); | ||
this.message = message; | ||
this.url = url; | ||
this.data = data; | ||
} | ||
url: string; | ||
data?: Record<string, string>; | ||
constructor(message: string, url: string, data?: Record<string, string>) { | ||
super(message); | ||
this.url = url; | ||
this.data = data; | ||
this.name = 'FetchError'; | ||
} | ||
} | ||
export type EnhancedFetchRequestInit<T> = RequestInit & { | ||
enhancers?: T; | ||
enhancers?: T; | ||
}; | ||
export type EnhancedFetch<T> = ( | ||
url: string | Request, | ||
init?: RequestInit & EnhancedFetchRequestInit<T>, | ||
) => Promise<Response>; | ||
export type EnhancedFetch<T> = (url: Request | string, init?: EnhancedFetchRequestInit<T> & RequestInit) => Promise<Response>; | ||
export type FetchEnhancer<T1> = <T2 extends {}>( | ||
fetch: Fetch | EnhancedFetch<T2>, | ||
options?: T1, | ||
) => EnhancedFetch<T1 & T2>; | ||
export type FetchEnhancer<T1> = <T2 extends {}>(fetch: EnhancedFetch<T2> | Fetch, options?: T1) => EnhancedFetch<T1 & T2>; | ||
export type FetchEnhancerWithMandatoryOptions<T1> = <T2 extends {}>( | ||
fetch: Fetch | EnhancedFetch<T2>, | ||
options: T1, | ||
fetch: EnhancedFetch<T2> | Fetch, | ||
options: T1, | ||
) => EnhancedFetch<T1 & T2>; | ||
export class FetchAuthorizationError extends Error { | ||
constructor(message: string, url: string | Request, requestInit: RequestInit) { | ||
super(message); | ||
Object.setPrototypeOf(this, FetchAuthorizationError.prototype); | ||
this.url = url.toString(); | ||
this.requestInit = requestInit; | ||
this.status = 403; | ||
} | ||
status: number; | ||
url: string; | ||
requestInit: RequestInit; | ||
constructor(message: string, url: Request | string, requestInit: RequestInit) { | ||
super(message); | ||
this.url = JSON.stringify(url); | ||
this.requestInit = requestInit; | ||
this.status = 403; | ||
this.name = 'FetchAuthorizationError'; | ||
} | ||
status: number; | ||
url: string; | ||
requestInit: RequestInit; | ||
} |
import type { InitialOptionsTsJest } from 'ts-jest/dist/types'; | ||
const config: InitialOptionsTsJest = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
rootDir: '../', | ||
testMatch: ['<rootDir>/tests/specs/**.spec.ts'], | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: { | ||
target: 'ESNext', | ||
lib: ['DOM', 'ESNext'], | ||
skipLibCheck: true, | ||
strictPropertyInitialization: false, | ||
noUnusedLocals: false, | ||
noUnusedParameters: false, | ||
isolatedModules: true, | ||
}, | ||
}, | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
rootDir: '../', | ||
testMatch: ['<rootDir>/tests/specs/**.spec.ts'], | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: { | ||
target: 'ESNext', | ||
lib: ['DOM', 'ESNext'], | ||
skipLibCheck: true, | ||
strictPropertyInitialization: false, | ||
noUnusedLocals: false, | ||
noUnusedParameters: false, | ||
isolatedModules: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
export default config; |
@@ -11,30 +11,28 @@ import { createServer } from 'http'; | ||
test('mixed ', async () => { | ||
const retryOpts = { onRetry: (_: Error) => console.log('hi!') }; | ||
const timeoutOpts = { | ||
requestTimeoutMs: 10000, | ||
}; | ||
const basicAuthOpts = { | ||
username: 'hi', | ||
password: 'bye', | ||
}; | ||
const enhancedFetch = withThrow( | ||
withBasicAuth(withTimeout(withRetry(fetch, retryOpts), timeoutOpts), basicAuthOpts), | ||
); | ||
const server = createServer((_, res) => { | ||
res.writeHead(200); | ||
res.end(); | ||
}); | ||
const retryOpts = { onRetry: (_: Error) => console.log('hi!') }; | ||
const timeoutOpts = { | ||
requestTimeoutMs: 10000, | ||
}; | ||
const basicAuthOpts = { | ||
username: 'hi', | ||
password: 'bye', | ||
}; | ||
const enhancedFetch = withThrow(withBasicAuth(withTimeout(withRetry(fetch, retryOpts), timeoutOpts), basicAuthOpts)); | ||
const server = createServer((_, res) => { | ||
res.writeHead(200); | ||
res.end(); | ||
}); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await enhancedFetch(`http://127.0.0.1:${port}`); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await enhancedFetch(`http://127.0.0.1:${port}`); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
}); | ||
}); |
@@ -10,44 +10,44 @@ import { createServer } from 'http'; | ||
test('retries upon 500', async () => { | ||
let i = 0; | ||
const server = createServer((_, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(500); | ||
res.end(); | ||
} else { | ||
res.end('ha'); | ||
} | ||
}); | ||
let i = 0; | ||
const server = createServer((_, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(500); | ||
res.end(); | ||
} else { | ||
res.end('ha'); | ||
} | ||
}); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
const res = await retryFetch(`http://127.0.0.1:${port}`); | ||
expect(await res.text()).toBe('ha'); | ||
server.close(); | ||
resolve(); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
server.on('error', reject); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
const res = await retryFetch(`http://127.0.0.1:${port}`); | ||
expect(await res.text()).toBe('ha'); | ||
server.close(); | ||
resolve(); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
server.on('error', reject); | ||
}); | ||
}); | ||
test('resolves on > MAX_RETRIES', async () => { | ||
const server = createServer((_, res) => { | ||
res.writeHead(500); | ||
res.end(); | ||
}); | ||
const server = createServer((_, res) => { | ||
res.writeHead(500); | ||
res.end(); | ||
}); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
const res = await retryFetch(`http://127.0.0.1:${port}`); | ||
expect(res.status).toBe(500); | ||
server.close(); | ||
return resolve(); | ||
}); | ||
server.on('error', reject); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
const res = await retryFetch(`http://127.0.0.1:${port}`); | ||
expect(res.status).toBe(500); | ||
server.close(); | ||
return resolve(); | ||
}); | ||
server.on('error', reject); | ||
}); | ||
}); |
@@ -8,23 +8,23 @@ import { createServer } from 'http'; | ||
test('throws error when fetch fails ', async () => { | ||
const throwingFetch = withThrow(fetch); | ||
const server = createServer((_, res) => { | ||
res.writeHead(500); | ||
res.end(); | ||
}); | ||
const throwingFetch = withThrow(fetch); | ||
const server = createServer((_, res) => { | ||
res.writeHead(500); | ||
res.end(); | ||
}); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await throwingFetch(`http://127.0.0.1:${port}`); | ||
reject(); | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await throwingFetch(`http://127.0.0.1:${port}`); | ||
reject(); | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
}); | ||
}); |
@@ -8,51 +8,51 @@ import { createServer } from 'http'; | ||
test('single request configuration - times out when server does not respond in time ', async () => { | ||
const timeoutFetch = withTimeout(fetch, { requestTimeoutMs: 100 }); | ||
const server = createServer((_, res) => { | ||
setTimeout(() => { | ||
res.writeHead(200); | ||
res.end(); | ||
}, 2000); | ||
}); | ||
const timeoutFetch = withTimeout(fetch, { requestTimeoutMs: 100 }); | ||
const server = createServer((_, res) => { | ||
setTimeout(() => { | ||
res.writeHead(200); | ||
res.end(); | ||
}, 2000); | ||
}); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await timeoutFetch(`http://127.0.0.1:${port}`); | ||
reject(); | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await timeoutFetch(`http://127.0.0.1:${port}`); | ||
reject(); | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
}); | ||
}); | ||
test('global configuration - times out when server does not respond in time ', async () => { | ||
const timeoutFetch = withTimeout(fetch, { requestTimeoutMs: 100 }); | ||
const server = createServer((_, res) => { | ||
setTimeout(() => { | ||
res.writeHead(200); | ||
res.end(); | ||
}, 2000); | ||
}); | ||
const timeoutFetch = withTimeout(fetch, { requestTimeoutMs: 100 }); | ||
const server = createServer((_, res) => { | ||
setTimeout(() => { | ||
res.writeHead(200); | ||
res.end(); | ||
}, 2000); | ||
}); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await timeoutFetch(`http://127.0.0.1:${port}`); | ||
reject(); | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
return new Promise<void>((resolve, reject) => { | ||
server.listen(async () => { | ||
const { port } = server.address() as AddressInfo; | ||
try { | ||
await timeoutFetch(`http://127.0.0.1:${port}`); | ||
reject(); | ||
} catch (err) { | ||
expect(err instanceof FetchError).toBeTruthy(); | ||
resolve(); | ||
} finally { | ||
server.close(); | ||
} | ||
}); | ||
server.on('error', reject); | ||
}); | ||
}); |
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"lib": ["esnext"], | ||
"module": "CommonJS", | ||
"moduleResolution": "Node", | ||
"outDir": "dist", | ||
"removeComments": true, | ||
"strict": true, | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": false, | ||
"skipLibCheck": true, | ||
"declaration": true, | ||
"sourceMap": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noImplicitReturns": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"experimentalDecorators": true | ||
}, | ||
"include": ["src/**/*", "tests/**/*"] | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"lib": ["esnext"], | ||
"module": "CommonJS", | ||
"moduleResolution": "Node", | ||
"outDir": "dist", | ||
"removeComments": true, | ||
"strict": true, | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": false, | ||
"skipLibCheck": true, | ||
"declaration": true, | ||
"sourceMap": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noImplicitReturns": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"experimentalDecorators": true | ||
}, | ||
"include": ["src/**/*", "tests/**/*"] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3
25
1
45024
811
- Removedeslint@^7.30.0
- Removed@babel/code-frame@7.12.11(transitive)
- Removed@babel/helper-validator-identifier@7.25.9(transitive)
- Removed@babel/highlight@7.25.9(transitive)
- Removed@eslint/eslintrc@0.4.3(transitive)
- Removed@humanwhocodes/config-array@0.5.0(transitive)
- Removed@humanwhocodes/object-schema@1.2.1(transitive)
- Removedacorn@7.4.1(transitive)
- Removedacorn-jsx@5.3.2(transitive)
- Removedajv@6.12.68.17.1(transitive)
- Removedansi-colors@4.1.3(transitive)
- Removedansi-regex@5.0.1(transitive)
- Removedansi-styles@3.2.14.3.0(transitive)
- Removedargparse@1.0.10(transitive)
- Removedastral-regex@2.0.0(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcallsites@3.1.0(transitive)
- Removedchalk@2.4.24.1.2(transitive)
- Removedcolor-convert@1.9.32.0.1(transitive)
- Removedcolor-name@1.1.31.1.4(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedcross-spawn@7.0.6(transitive)
- Removeddebug@4.4.0(transitive)
- Removeddeep-is@0.1.4(transitive)
- Removeddoctrine@3.0.0(transitive)
- Removedemoji-regex@8.0.0(transitive)
- Removedenquirer@2.4.1(transitive)
- Removedescape-string-regexp@1.0.54.0.0(transitive)
- Removedeslint@7.32.0(transitive)
- Removedeslint-scope@5.1.1(transitive)
- Removedeslint-utils@2.1.0(transitive)
- Removedeslint-visitor-keys@1.3.02.1.0(transitive)
- Removedespree@7.3.1(transitive)
- Removedesprima@4.0.1(transitive)
- Removedesquery@1.6.0(transitive)
- Removedesrecurse@4.3.0(transitive)
- Removedestraverse@4.3.05.3.0(transitive)
- Removedesutils@2.0.3(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfast-levenshtein@2.0.6(transitive)
- Removedfast-uri@3.0.5(transitive)
- Removedfile-entry-cache@6.0.1(transitive)
- Removedflat-cache@3.2.0(transitive)
- Removedflatted@3.3.2(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedfunctional-red-black-tree@1.0.1(transitive)
- Removedglob@7.2.3(transitive)
- Removedglob-parent@5.1.2(transitive)
- Removedglobals@13.24.0(transitive)
- Removedhas-flag@3.0.04.0.0(transitive)
- Removedignore@4.0.6(transitive)
- Removedimport-fresh@3.3.0(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-fullwidth-code-point@3.0.0(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedisexe@2.0.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedjson-buffer@3.0.1(transitive)
- Removedjson-schema-traverse@0.4.11.0.0(transitive)
- Removedjson-stable-stringify-without-jsonify@1.0.1(transitive)
- Removedkeyv@4.5.4(transitive)
- Removedlevn@0.4.1(transitive)
- Removedlodash.merge@4.6.2(transitive)
- Removedlodash.truncate@4.4.2(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedms@2.1.3(transitive)
- Removednatural-compare@1.4.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedoptionator@0.9.4(transitive)
- Removedparent-module@1.0.1(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-key@3.1.1(transitive)
- Removedpicocolors@1.1.1(transitive)
- Removedprelude-ls@1.2.1(transitive)
- Removedprogress@2.0.3(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedregexpp@3.2.0(transitive)
- Removedrequire-from-string@2.0.2(transitive)
- Removedresolve-from@4.0.0(transitive)
- Removedrimraf@3.0.2(transitive)
- Removedsemver@7.6.3(transitive)
- Removedshebang-command@2.0.0(transitive)
- Removedshebang-regex@3.0.0(transitive)
- Removedslice-ansi@4.0.0(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedstring-width@4.2.3(transitive)
- Removedstrip-ansi@6.0.1(transitive)
- Removedstrip-json-comments@3.1.1(transitive)
- Removedsupports-color@5.5.07.2.0(transitive)
- Removedtable@6.9.0(transitive)
- Removedtext-table@0.2.0(transitive)
- Removedtype-check@0.4.0(transitive)
- Removedtype-fest@0.20.2(transitive)
- Removeduri-js@4.4.1(transitive)
- Removedv8-compile-cache@2.4.0(transitive)
- Removedwhich@2.0.2(transitive)
- Removedword-wrap@1.2.5(transitive)
- Removedwrappy@1.0.2(transitive)