@applitools/req
Advanced tools
Comparing version 1.1.25 to 1.1.26
@@ -33,4 +33,4 @@ "use strict"; | ||
const https_1 = require("https"); | ||
const node_fetch_1 = __importStar(require("node-fetch")); | ||
const proxy_agent_1 = __importDefault(require("proxy-agent")); | ||
const node_fetch_1 = __importStar(require("node-fetch")); | ||
const utils = __importStar(require("@applitools/utils")); | ||
@@ -47,6 +47,12 @@ const stop = Symbol('stop retry'); | ||
headers: { ...baseOption.headers, ...options === null || options === void 0 ? void 0 : options.headers }, | ||
retry: [...(baseOption.retry ? [].concat(baseOption.retry) : []), ...((options === null || options === void 0 ? void 0 : options.retry) ? [].concat(options.retry) : [])], | ||
hooks: [...(baseOption.hooks ? [].concat(baseOption.hooks) : []), ...((options === null || options === void 0 ? void 0 : options.hooks) ? [].concat(options.hooks) : [])], | ||
retry: [ | ||
...(baseOption.retry ? [].concat(baseOption.retry) : []), | ||
...((options === null || options === void 0 ? void 0 : options.retry) ? [].concat(options.retry) : []), | ||
], | ||
hooks: [ | ||
...(baseOption.hooks ? [].concat(baseOption.hooks) : []), | ||
...((options === null || options === void 0 ? void 0 : options.hooks) ? [].concat(options.hooks) : []), | ||
], | ||
}; | ||
return mergedOptions.hooks.reduce((options, hooks) => { var _a; return ((_a = hooks.afterOptionsMerged) === null || _a === void 0 ? void 0 : _a.call(hooks, { options })) || options; }, mergedOptions); | ||
return mergedOptions.hooks.reduce((options, hooks) => { var _a, _b; return (_b = (_a = hooks.afterOptionsMerged) === null || _a === void 0 ? void 0 : _a.call(hooks, { options })) !== null && _b !== void 0 ? _b : options; }, mergedOptions); | ||
} | ||
@@ -63,3 +69,3 @@ exports.mergeOptions = mergeOptions; | ||
async function req(input, options) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; | ||
const fetch = (_a = options === null || options === void 0 ? void 0 : options.fetch) !== null && _a !== void 0 ? _a : node_fetch_1.default; | ||
@@ -111,2 +117,4 @@ if (options === null || options === void 0 ? void 0 : options.hooks) | ||
const controller = new abort_controller_1.AbortController(); | ||
if (options === null || options === void 0 ? void 0 : options.signal) | ||
options.signal.onabort = () => controller.abort(); | ||
const url = new URL(String((_b = input.url) !== null && _b !== void 0 ? _b : input), options === null || options === void 0 ? void 0 : options.baseUrl); | ||
@@ -126,7 +134,7 @@ if (options === null || options === void 0 ? void 0 : options.query) { | ||
body: utils.types.isPlainObject(options === null || options === void 0 ? void 0 : options.body) || utils.types.isArray(options === null || options === void 0 ? void 0 : options.body) | ||
? JSON.stringify(options.body) | ||
? JSON.stringify(options === null || options === void 0 ? void 0 : options.body) | ||
: (_f = options === null || options === void 0 ? void 0 : options.body) !== null && _f !== void 0 ? _f : input.body, | ||
agent: url => { | ||
var _a, _b; | ||
const proxy = utils.types.isFunction(options === null || options === void 0 ? void 0 : options.proxy) ? options.proxy(url) : options === null || options === void 0 ? void 0 : options.proxy; | ||
const proxy = utils.types.isFunction(options === null || options === void 0 ? void 0 : options.proxy) ? options === null || options === void 0 ? void 0 : options.proxy(url) : options === null || options === void 0 ? void 0 : options.proxy; | ||
if (proxy) { | ||
@@ -137,4 +145,5 @@ const proxyUrl = new URL(proxy.url); | ||
const agent = new proxy_agent_1.default({ ...(0, url_1.parse)(proxyUrl.href), rejectUnauthorized: false }); | ||
const originalCallback = agent.callback.bind(agent); | ||
agent.callback = (request, options, callback) => originalCallback(request, { ...options, rejectUnauthorized: false }, callback); | ||
agent.callback = utils.general.wrap(agent.callback.bind(agent), (fn, request, options, ...rest) => { | ||
return fn(request, { ...options, rejectUnauthorized: false }, ...rest); | ||
}); | ||
return agent; | ||
@@ -149,15 +158,22 @@ } | ||
request = await beforeRequest({ request, options }); | ||
const timer = (options === null || options === void 0 ? void 0 : options.timeout) > 0 ? setTimeout(() => controller.abort(), options.timeout) : null; | ||
const timer = (options === null || options === void 0 ? void 0 : options.timeout) ? setTimeout(() => controller.abort(), options.timeout) : null; | ||
try { | ||
let response = await fetch(request); | ||
// if the request has to be retried due to status code | ||
const retry = (_g = options === null || options === void 0 ? void 0 : options.retry) === null || _g === void 0 ? void 0 : _g.find(retry => { | ||
var _a, _b; | ||
return (((_a = retry.statuses) === null || _a === void 0 ? void 0 : _a.includes(response.status)) || ((_b = retry.validate) === null || _b === void 0 ? void 0 : _b.call(retry, { response }))) && | ||
(!retry.limit || !retry.attempt || retry.attempt < retry.limit); | ||
}); | ||
const retry = await ((_g = options === null || options === void 0 ? void 0 : options.retry) === null || _g === void 0 ? void 0 : _g.reduce((prev, retry) => { | ||
return prev.then(async (result) => { | ||
var _a, _b; | ||
return (result !== null && result !== void 0 ? result : ((((_a = retry.statuses) === null || _a === void 0 ? void 0 : _a.includes(response.status)) || (await ((_b = retry.validate) === null || _b === void 0 ? void 0 : _b.call(retry, { response })))) && | ||
(!retry.limit || !retry.attempt || retry.attempt < retry.limit) | ||
? retry | ||
: null)); | ||
}); | ||
}, Promise.resolve(null))); | ||
if (retry) { | ||
(_h = retry.attempt) !== null && _h !== void 0 ? _h : (retry.attempt = 0); | ||
let delay = response.headers.has('Retry-After') ? Number(response.headers.get('Retry-After')) * 1000 : null; | ||
delay !== null && delay !== void 0 ? delay : (delay = utils.types.isArray(retry.timeout) ? retry.timeout[Math.min(retry.attempt, retry.timeout.length)] : retry.timeout); | ||
const delay = response.headers.has('Retry-After') | ||
? Number(response.headers.get('Retry-After')) * 1000 | ||
: utils.types.isArray(retry.timeout) | ||
? retry.timeout[Math.min(retry.attempt, retry.timeout.length)] | ||
: (_j = retry.timeout) !== null && _j !== void 0 ? _j : 0; | ||
await utils.general.sleep(delay); | ||
@@ -175,12 +191,16 @@ retry.attempt += 1; | ||
// if the request has to be retried due to network error | ||
const retry = (_j = options === null || options === void 0 ? void 0 : options.retry) === null || _j === void 0 ? void 0 : _j.find(retry => { | ||
var _a, _b; | ||
return (((_a = retry.codes) === null || _a === void 0 ? void 0 : _a.includes(error.code)) || ((_b = retry.validate) === null || _b === void 0 ? void 0 : _b.call(retry, { error }))) && | ||
(!retry.limit || !retry.attempt || retry.attempt < retry.limit); | ||
}); | ||
const retry = await ((_k = options === null || options === void 0 ? void 0 : options.retry) === null || _k === void 0 ? void 0 : _k.reduce((prev, retry) => { | ||
return prev.then(async (result) => { | ||
var _a, _b; | ||
return (result !== null && result !== void 0 ? result : ((((_a = retry.codes) === null || _a === void 0 ? void 0 : _a.includes(error.code)) || (await ((_b = retry.validate) === null || _b === void 0 ? void 0 : _b.call(retry, { error })))) && | ||
(!retry.limit || !retry.attempt || retry.attempt < retry.limit))) | ||
? retry | ||
: null; | ||
}); | ||
}, Promise.resolve(null))); | ||
if (retry) { | ||
(_k = retry.attempt) !== null && _k !== void 0 ? _k : (retry.attempt = 0); | ||
(_l = retry.attempt) !== null && _l !== void 0 ? _l : (retry.attempt = 0); | ||
const delay = utils.types.isArray(retry.timeout) | ||
? retry.timeout[Math.min(retry.attempt, retry.timeout.length)] | ||
: retry.timeout; | ||
: (_m = retry.timeout) !== null && _m !== void 0 ? _m : 0; | ||
await utils.general.sleep(delay); | ||
@@ -197,3 +217,6 @@ retry.attempt = retry.attempt + 1; | ||
finally { | ||
clearTimeout(timer); | ||
if (timer) | ||
clearTimeout(timer); | ||
if (options === null || options === void 0 ? void 0 : options.signal) | ||
options.signal.onabort = null; | ||
} | ||
@@ -200,0 +223,0 @@ } |
{ | ||
"name": "@applitools/req", | ||
"version": "1.1.25", | ||
"version": "1.1.26", | ||
"description": "Applitools fetch-based request library", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
"lint": "eslint '**/*.ts'", | ||
"build": "tsc", | ||
"build": "tsc --project ./tsconfig.build.json", | ||
"test": "mocha --no-timeouts -r ts-node/register ./test/**/*.spec.ts", | ||
@@ -62,3 +62,3 @@ "test:sanity": "yarn test", | ||
"dependencies": { | ||
"@applitools/utils": "1.3.24", | ||
"@applitools/utils": "1.3.25", | ||
"@types/node-fetch": "2.6.2", | ||
@@ -74,6 +74,6 @@ "abort-controller": "3.0.0", | ||
"@types/node": "12", | ||
"@typescript-eslint/eslint-plugin": "^5.22.0", | ||
"@typescript-eslint/parser": "^5.22.0", | ||
"eslint": "^8.16.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"@typescript-eslint/eslint-plugin": "^5.48.1", | ||
"@typescript-eslint/parser": "^5.48.1", | ||
"eslint": "^8.31.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"eslint-plugin-mocha-no-only": "^1.1.1", | ||
@@ -85,5 +85,5 @@ "eslint-plugin-node": "^11.1.0", | ||
"nock": "^13.2.9", | ||
"prettier": "^2.6.2", | ||
"prettier": "^2.8.2", | ||
"ts-node": "^10.8.0", | ||
"typescript": "^4.7.2" | ||
"typescript": "^4.9.4" | ||
}, | ||
@@ -90,0 +90,0 @@ "engines": { |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { type AbortSignal } from 'abort-controller'; | ||
import globalFetch, { Request, Response } from 'node-fetch'; | ||
declare const stop: unique symbol; | ||
export declare type Fetch = typeof globalFetch; | ||
export declare type Options = { | ||
export type Fetch = typeof globalFetch; | ||
export interface Options { | ||
/** | ||
@@ -28,3 +29,3 @@ * Providing this value will allow usage of relative urls for input | ||
*/ | ||
headers?: Record<string, string | undefined>; | ||
headers?: Record<string, string | string[] | undefined>; | ||
/** | ||
@@ -35,3 +36,3 @@ * Body of the request, plain objects will be transformed to JSON strings | ||
*/ | ||
body?: NodeJS.ReadableStream | ArrayBufferView | string | Record<string, any>; | ||
body?: NodeJS.ReadableStream | ArrayBufferView | string | Record<string, any> | any[]; | ||
/** | ||
@@ -57,6 +58,7 @@ * Proxy settings for the request. Auth credentials specified in the object will override ones specified in url | ||
*/ | ||
hooks?: Hooks | Hooks[]; | ||
hooks?: Hooks<this> | Hooks<this>[]; | ||
signal?: AbortSignal; | ||
fetch?: Fetch; | ||
}; | ||
export declare type Retry = { | ||
} | ||
export interface Retry { | ||
/** | ||
@@ -79,3 +81,3 @@ * Max number of attempts for specified condition | ||
error?: Error; | ||
}) => boolean; | ||
}) => boolean | Promise<boolean>; | ||
/** | ||
@@ -96,9 +98,9 @@ * Status codes of the response to retry on. | ||
attempt?: number; | ||
}; | ||
export declare type Proxy = { | ||
} | ||
export interface Proxy { | ||
url: string; | ||
username?: string; | ||
password?: string; | ||
}; | ||
export declare type Hooks<TOptions extends Options = Options> = { | ||
} | ||
export interface Hooks<TOptions extends Options = Options> { | ||
/** | ||
@@ -131,3 +133,3 @@ * Hook that will be executed after options are merged, it will not be executed if no merge takes place | ||
request: Request; | ||
options: TOptions; | ||
options?: TOptions; | ||
}): Request | void | Promise<Request | void>; | ||
@@ -154,3 +156,3 @@ /** | ||
error?: Error; | ||
options: TOptions; | ||
options?: TOptions; | ||
}): Request | typeof stop | void | Promise<Request | void | typeof stop>; | ||
@@ -171,3 +173,3 @@ /** | ||
response: Response; | ||
options: TOptions; | ||
options?: TOptions; | ||
}): Response | void | Promise<Response | void>; | ||
@@ -188,10 +190,10 @@ /** | ||
error: Error; | ||
options: TOptions; | ||
options?: TOptions; | ||
}): Error | void | Promise<Error | void>; | ||
}; | ||
export declare type Req<TOptions extends Options = Options> = (input: string | URL | Request, options?: TOptions) => Promise<Response>; | ||
} | ||
export type Req<TOptions extends Options = Options> = (input: string | URL | Request, options?: TOptions) => Promise<Response>; | ||
/** | ||
* Helper function that will properly merge two {@link Options} objects | ||
*/ | ||
export declare function mergeOptions<TOptions extends Options>(baseOption: TOptions, options: TOptions): TOptions; | ||
export declare function mergeOptions<TOptions extends Options>(baseOption: TOptions, options?: TOptions): TOptions; | ||
/** | ||
@@ -201,4 +203,4 @@ * Helper function that will create {@link req} function with predefined options | ||
*/ | ||
export declare function makeReq<TOptions extends Options = Options>(baseOption?: Partial<Options>): Req<TOptions>; | ||
export declare function makeReq<TOptions extends Options = Options, TBaseOptions extends TOptions = TOptions>(baseOption: Partial<TBaseOptions>): Req<TOptions>; | ||
export declare function req(input: string | URL | Request, options?: Options): Promise<Response>; | ||
export default req; |
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
38320
445
+ Added@applitools/utils@1.3.25(transitive)
- Removed@applitools/utils@1.3.24(transitive)
Updated@applitools/utils@1.3.25