Skip to content

Commit

Permalink
pass init to validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Feb 9, 2021
1 parent 4d46e4a commit c878abb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# Changelog

# v1.5.0

- pass init to validator

# v1.4.0

- add validator option
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Also adds `Accept` header with value `application/json, text/plain, */*` if not
you can import `wrapFetch` from `mod.ts` file.

```js
export { wrapFetch } from 'https://deno.land/x/fetch_goody@v1.4.0/mod.ts';
export { wrapFetch } from 'https://deno.land/x/fetch_goody@v1.5.0/mod.ts';
```

### wrapFetch
Expand Down Expand Up @@ -58,7 +58,7 @@ const resp3 = await wrappedfetch("url",{
// adding a response validator where you can throw errors

const resp4 = await wrappedfetch("url",{
validator(response: Response) {
validator(response: Response, init: ExtendedRequestInit) {
if (response.status > 200) {
throw new Error('yada');
}
Expand Down
3 changes: 2 additions & 1 deletion extended_request_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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) => void | Promise<void>);
validator?:
((response: Response, init: ExtendedRequestInit) => void | Promise<void>);
}

export type ExtendedRequestInit =
Expand Down
2 changes: 1 addition & 1 deletion fetch_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function wrapFetch(
"validator" in interceptedInit &&
typeof interceptedInit.validator === "function"
) {
await interceptedInit.validator(response);
await interceptedInit.validator(response, interceptedInit);
}

return response;
Expand Down
12 changes: 11 additions & 1 deletion fetch_wrapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,19 +306,29 @@ Deno.test("WrappedFetch runs validator option with passigng response if set", as
const wrappedFetch = wrapFetch();

let validatorRan = false;
let initPassed = false;

// for string
const paramsString = await wrappedFetch(serverOneUrl + "/user-agent", {
validator(response) {
validator(response, init) {
assertStrictEquals(response.status, 200);
validatorRan = true;
initPassed = init.method === "delete";
},
body: {
"baz": "zab",
},
method: "delete",
}).then((r) => r.text());

assertStrictEquals(
validatorRan,
true,
);
assertStrictEquals(
initPassed,
true,
);
} finally {
await closeServers();
}
Expand Down

0 comments on commit c878abb

Please sign in to comment.