|
| 1 | +import { z } from 'zod'; |
| 2 | +import { Kit } from './kit'; |
| 3 | + |
| 4 | +export async function fetchJson<schema extends undefined | z.ZodType = undefined>( |
| 5 | + url: string, |
| 6 | + requestInit?: RequestInit, |
| 7 | + schema?: schema, |
| 8 | +): Promise< |
| 9 | + (schema extends z.ZodType ? z.infer<schema> : Kit.Json.Value) | FetchJsonErrors.FetchJsonErrors |
| 10 | +> { |
| 11 | + const response = await fetch(url, requestInit) |
| 12 | + // @see https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#exceptions |
| 13 | + .catch(Kit.oneOf(error => error instanceof TypeError || error instanceof DOMException)); |
| 14 | + |
| 15 | + if (response instanceof TypeError) { |
| 16 | + return new FetchJsonErrors.FetchJsonRequestTypeError({ requestInit }, response); |
| 17 | + } |
| 18 | + |
| 19 | + if (response instanceof DOMException) { |
| 20 | + return new FetchJsonErrors.FetchJsonRequestNetworkError({}, response); |
| 21 | + } |
| 22 | + |
| 23 | + const json = await response |
| 24 | + .json() |
| 25 | + .then(value => value as Kit.Json.Value) |
| 26 | + // @see https://developer.mozilla.org/en-US/docs/Web/API/Response/json#exceptions |
| 27 | + .catch( |
| 28 | + Kit.oneOf( |
| 29 | + error => |
| 30 | + error instanceof SyntaxError || |
| 31 | + error instanceof TypeError || |
| 32 | + error instanceof DOMException, |
| 33 | + ), |
| 34 | + ); |
| 35 | + |
| 36 | + if (json instanceof DOMException) { |
| 37 | + return new FetchJsonErrors.FetchJsonRequestNetworkError({}, json); |
| 38 | + } |
| 39 | + |
| 40 | + if (json instanceof TypeError) { |
| 41 | + return new FetchJsonErrors.FetchJsonResponseTypeError({ response }, json); |
| 42 | + } |
| 43 | + |
| 44 | + if (json instanceof SyntaxError) { |
| 45 | + return new FetchJsonErrors.FetchJsonResponseSyntaxError({ response }, json); |
| 46 | + } |
| 47 | + |
| 48 | + if (schema) { |
| 49 | + const result = schema.safeParse(json); |
| 50 | + if (!result.success) { |
| 51 | + return new FetchJsonErrors.FetchJsonResponseSchemaError( |
| 52 | + { response, json, schema }, |
| 53 | + result.error, |
| 54 | + ); |
| 55 | + } |
| 56 | + return result.data as any; // z.infer<Exclude<schema, undefined>>; |
| 57 | + } |
| 58 | + |
| 59 | + return json as any; // Kit.Json.Value; |
| 60 | +} |
| 61 | + |
| 62 | +// ================================= |
| 63 | +// Error Classes |
| 64 | +// ================================= |
| 65 | + |
| 66 | +// eslint-disable-next-line @typescript-eslint/no-namespace |
| 67 | +export namespace FetchJsonErrors { |
| 68 | + export type FetchJsonErrors = FetchJsonResponseErrors | FetchJsonRequestErrors; |
| 69 | + |
| 70 | + // -------------------------------- |
| 71 | + // Response Error Classes |
| 72 | + // -------------------------------- |
| 73 | + |
| 74 | + export type FetchJsonRequestErrors = FetchJsonRequestTypeError | FetchJsonRequestNetworkError; |
| 75 | + |
| 76 | + export class FetchJsonRequestNetworkError extends Kit.Errors.ContextualError< |
| 77 | + 'FetchJsonRequestNetworkError', |
| 78 | + {}, |
| 79 | + DOMException |
| 80 | + > { |
| 81 | + message = 'Network failure.'; |
| 82 | + } |
| 83 | + |
| 84 | + export class FetchJsonRequestTypeError extends Kit.Errors.ContextualError< |
| 85 | + 'FetchJsonRequestTypeError', |
| 86 | + { requestInit?: RequestInit }, |
| 87 | + TypeError |
| 88 | + > { |
| 89 | + message = 'Invalid request.'; |
| 90 | + } |
| 91 | + |
| 92 | + // -------------------------------- |
| 93 | + // Response Error Classes |
| 94 | + // -------------------------------- |
| 95 | + |
| 96 | + export abstract class FetchJsonResponseError< |
| 97 | + $Name extends string, |
| 98 | + $Context extends { |
| 99 | + response: Response; |
| 100 | + }, |
| 101 | + $Cause extends z.ZodError | SyntaxError | TypeError | DOMException, |
| 102 | + > extends Kit.Errors.ContextualError<$Name, $Context, $Cause> { |
| 103 | + message = 'Invalid response.'; |
| 104 | + } |
| 105 | + |
| 106 | + export type FetchJsonResponseErrors = |
| 107 | + | FetchJsonResponseSyntaxError |
| 108 | + | FetchJsonResponseSchemaError |
| 109 | + | FetchJsonResponseTypeError; |
| 110 | + |
| 111 | + export class FetchJsonResponseTypeError extends FetchJsonResponseError< |
| 112 | + 'FetchJsonResponseTypeError', |
| 113 | + { response: Response }, |
| 114 | + TypeError |
| 115 | + > { |
| 116 | + message = 'Response is malformed.'; |
| 117 | + } |
| 118 | + |
| 119 | + export class FetchJsonResponseSyntaxError extends FetchJsonResponseError< |
| 120 | + 'FetchJsonResponseSyntaxError', |
| 121 | + { response: Response }, |
| 122 | + SyntaxError |
| 123 | + > { |
| 124 | + message = 'Response body is not valid JSON.'; |
| 125 | + } |
| 126 | + |
| 127 | + export class FetchJsonResponseSchemaError extends FetchJsonResponseError< |
| 128 | + 'FetchJsonResponseSchemaError', |
| 129 | + { response: Response; json: Kit.Json.Value; schema: z.ZodType }, |
| 130 | + z.ZodError |
| 131 | + > { |
| 132 | + message = 'Response body JSON violates the schema.'; |
| 133 | + } |
| 134 | +} |
0 commit comments