Skip to content

Commit

Permalink
make and use a single validator type
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Jul 27, 2022
1 parent 71ee2e6 commit 7817c53
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 6 additions & 2 deletions extended_request_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ interface RequestInitDiff {
body?: Record<string, unknown> | BodyInit | null;
/** a function that will be called before returning response.
* can be used for validating response and throwing errors */
validator?:
((response: Response, init: ExtendedRequestInit) => void | Promise<void>);
validator?: Validator;
/** time in milliseconds which after the request should be cancelled and rejected */
timeout?: number;
}

export type Validator = (
response: Response,
init: ExtendedRequestInit,
) => void | Promise<void>;

export type ExtendedRequestInit =
& RequestInitDiff
& Omit<RequestInit, keyof RequestInitDiff>;
5 changes: 2 additions & 3 deletions fetch_wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as utils from "./utils.ts";
import { getHeader, setHeader } from "./header_utils.ts";
import { ExtendedRequestInit } from "./extended_request_init.ts";
import { ExtendedRequestInit, Validator } from "./extended_request_init.ts";

/**
* Transforms data and adds corresponding headers if possible.
Expand Down Expand Up @@ -80,8 +80,7 @@ export type WrapFetchOptions = {
/** user agent header string */
userAgent?: string;
/** validator to run after each response with this fetch */
validator?:
((response: Response, init: ExtendedRequestInit) => void | Promise<void>);
validator?: Validator;
/** if set, all requests will timeout after this amount of milliseconds passed */
timeout?: number;
};
Expand Down

0 comments on commit 7817c53

Please sign in to comment.