fetch-extras
Advanced tools
+1
-1
| { | ||
| "name": "fetch-extras", | ||
| "version": "3.1.0", | ||
| "version": "3.2.0", | ||
| "description": "Useful utilities for working with Fetch", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -29,4 +29,5 @@ export {HttpError, throwIfHttpError, withHttpError} from './http-error.js'; | ||
| type StandardSchemaV1Issue, | ||
| type StandardSchemaV1Options, | ||
| } from './with-json-response.js'; | ||
| export {withResponse, type ResponseTransform} from './with-response.js'; | ||
| export {pipeline} from './pipeline.js'; |
@@ -25,4 +25,4 @@ // Standard Schema types (inlined from https://standardschema.dev) | ||
| type StandardSchemaV1Options = { | ||
| readonly libraryOptions?: Readonly<Record<string, unknown>> | undefined; | ||
| export type StandardSchemaV1Options = { | ||
| readonly libraryOptions?: Record<string, unknown> | undefined; | ||
| }; | ||
@@ -145,2 +145,3 @@ | ||
| @param options.schema - A Standard Schema object to validate response JSON against. | ||
| @param options.schemaOptions - Standard Schema options passed as the second argument to `schema['~standard'].validate()`. Use `schemaOptions.libraryOptions` for validator-specific options. | ||
| @returns A wrapper that takes a fetch function and returns a wrapped fetch function that returns the validated data. | ||
@@ -165,2 +166,21 @@ @throws {SyntaxError} When the response body is empty or is not valid JSON. | ||
| ``` | ||
| import {withJsonResponse} from 'fetch-extras'; | ||
| import {z} from 'zod'; | ||
| const fetchUser = withJsonResponse({ | ||
| schema: z.object({name: z.string()}), | ||
| schemaOptions: { | ||
| libraryOptions: { | ||
| // Validator-specific Standard Schema options. | ||
| }, | ||
| }, | ||
| })(fetch); | ||
| const user = await fetchUser('/api/user/1'); | ||
| console.log(user.name); | ||
| ``` | ||
| @example | ||
| ``` | ||
| import {pipeline, withHttpError, withTimeout, withJsonResponse} from 'fetch-extras'; | ||
@@ -184,5 +204,8 @@ import {z} from 'zod'; | ||
| >( | ||
| options?: {schema?: Schema}, | ||
| options?: { | ||
| schema?: Schema; | ||
| schemaOptions?: StandardSchemaV1Options; | ||
| }, | ||
| ): ( | ||
| fetchFunction: typeof fetch, | ||
| ) => (...arguments_: Parameters<typeof fetch>) => Promise<Schema extends StandardSchemaV1 ? StandardSchemaV1InferOutput<Schema> : unknown>; |
@@ -15,3 +15,3 @@ import {copyFetchMetadata} from './utilities.js'; | ||
| export function withJsonResponse({schema} = {}) { | ||
| export function withJsonResponse({schema, schemaOptions} = {}) { | ||
| if (schema !== undefined && typeof schema?.['~standard']?.validate !== 'function') { | ||
@@ -34,3 +34,3 @@ throw new TypeError('The `schema` option must be a Standard Schema object (https://standardschema.dev)'); | ||
| const result = await schema['~standard'].validate(jsonValue); | ||
| const result = await schema['~standard'].validate(jsonValue, schemaOptions); | ||
@@ -37,0 +37,0 @@ if (result.issues) { |
@@ -50,2 +50,23 @@ /** | ||
| ``` | ||
| import {withRetry} from 'fetch-extras'; | ||
| // Logging retries | ||
| const fetchWithRetry = withRetry({ | ||
| shouldRetry({error, response, attemptNumber, retriesLeft}) { | ||
| console.log('Retrying request', { | ||
| attemptNumber, | ||
| retriesLeft, | ||
| error, | ||
| status: response?.status, | ||
| }); | ||
| return true; | ||
| }, | ||
| })(fetch); | ||
| ``` | ||
| `shouldRetry` can also be used for retry logging or metrics. It is called after the built-in retry checks pass, before the retry delay. Return `true` to keep retrying. | ||
| @example | ||
| ``` | ||
| import {pipeline, withHttpError, withRetry, withBaseUrl, withTimeout} from 'fetch-extras'; | ||
@@ -106,2 +127,4 @@ | ||
| Can also be used for retry logging or metrics. Return `true` to keep retrying. | ||
| Do not consume the `response` body. If you need to inspect the body, clone the response first. | ||
@@ -108,0 +131,0 @@ */ |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
166130
0.73%3789
1.01%