-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(typescript-fetch): runtime validation works (#287)
solves #286 - Use `--enable-runtime-response-validation` for the E2E tests - Refactor `typescript-fetch` response validation factory to share most of the implementation between schema builders - Fix the returned `res` of the response validation factory to be a `Proxy` to the actual `res` object, only intercepting the `json` method
- Loading branch information
Showing
10 changed files
with
237 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type {Res, StatusCode} from "./types" | ||
|
||
export function responseValidationFactoryFactory<Schema>( | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
parse: (schema: Schema, value: unknown) => any, | ||
possibleResponses: [string, Schema][], | ||
defaultResponse?: Schema, | ||
) { | ||
// Exploit the natural ordering matching the desired specificity of eg: 404 vs 4xx | ||
possibleResponses.sort((x, y) => (x[0] < y[0] ? -1 : 1)) | ||
|
||
return async ( | ||
whenRes: Promise<Res<StatusCode, unknown>>, | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
): Promise<any> => { | ||
const res = await whenRes | ||
|
||
const json = async () => { | ||
const status = res.status | ||
const value = await res.json() | ||
|
||
for (const [match, schema] of possibleResponses) { | ||
const isMatch = | ||
(/^\d+$/.test(match) && String(status) === match) || | ||
(/^\d[xX]{2}$/.test(match) && String(status)[0] === match[0]) | ||
|
||
if (isMatch) { | ||
return parse(schema, value) | ||
} | ||
} | ||
|
||
if (defaultResponse) { | ||
return parse(defaultResponse, value) | ||
} | ||
|
||
// TODO: throw on unmatched response? | ||
return value | ||
} | ||
|
||
return new Proxy(res, { | ||
get(target, prop, receiver) { | ||
if (prop === "json") { | ||
return json | ||
} | ||
|
||
return Reflect.get(target, prop, receiver) | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,21 @@ | ||
import type {Schema as JoiSchema} from "joi" | ||
import type {Res, StatusCode} from "./main" | ||
import {responseValidationFactoryFactory} from "./common" | ||
|
||
export function responseValidationFactory( | ||
possibleResponses: [string, JoiSchema][], | ||
defaultResponse?: JoiSchema, | ||
) { | ||
// Exploit the natural ordering matching the desired specificity of eg: 404 vs 4xx | ||
possibleResponses.sort((x, y) => (x[0] < y[0] ? -1 : 1)) | ||
|
||
// TODO: avoid any | ||
return async ( | ||
whenRes: Promise<Res<StatusCode, unknown>>, | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
): Promise<any> => { | ||
const res = await whenRes | ||
|
||
return { | ||
...res, | ||
json: async () => { | ||
const status = res.status | ||
const value = await res.json() | ||
|
||
for (const [match, schema] of possibleResponses) { | ||
const isMatch = | ||
(/^\d+$/.test(match) && String(status) === match) || | ||
(/^\d[xX]{2}$/.test(match) && String(status)[0] === match[0]) | ||
|
||
if (isMatch) { | ||
const result = schema.validate(value) | ||
|
||
if (result.error) { | ||
throw result.error | ||
} | ||
|
||
return result.value | ||
} | ||
} | ||
|
||
if (defaultResponse) { | ||
const result = defaultResponse.validate(value) | ||
|
||
if (result.error) { | ||
throw result.error | ||
} | ||
|
||
return result.value | ||
} | ||
|
||
// TODO: throw on unmatched response? | ||
return value | ||
}, | ||
} | ||
} | ||
return responseValidationFactoryFactory( | ||
(schema, value) => { | ||
const result = schema.validate(value) | ||
|
||
if (result.error) { | ||
throw result.error | ||
} | ||
|
||
return result.value | ||
}, | ||
possibleResponses, | ||
defaultResponse, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.