kayto_ts 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/index.js +1 -1
- package/dist/types.d.ts +23 -3
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +10 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -305,6 +305,8 @@ All requests return a discriminated union:
|
|
|
305
305
|
You can handle errors by `kind`, but it is optional.
|
|
306
306
|
You can also handle them generically via `message` / `status` / `cause` without branching by `kind`.
|
|
307
307
|
|
|
308
|
+
For `http` errors, `error.data` contains the parsed backend payload and is typed from the endpoint's non-2xx responses (for example `400`, `404`, `422`).
|
|
309
|
+
|
|
308
310
|
```ts
|
|
309
311
|
const result = await client.get("/api/cats");
|
|
310
312
|
|
|
@@ -343,6 +345,7 @@ if (!result.ok) {
|
|
|
343
345
|
break;
|
|
344
346
|
case "http":
|
|
345
347
|
console.error("HTTP error", result.error.status);
|
|
348
|
+
console.error("Backend payload", result.error.data);
|
|
346
349
|
break;
|
|
347
350
|
default:
|
|
348
351
|
console.error(result.error.message, result.error.cause);
|
package/dist/index.js
CHANGED
|
@@ -100,7 +100,7 @@ export function clientApi(config = {}) {
|
|
|
100
100
|
if (!response.ok) {
|
|
101
101
|
return {
|
|
102
102
|
ok: false,
|
|
103
|
-
error: makeClientError("http", `Request failed with status ${response.status}`, undefined, response.status),
|
|
103
|
+
error: makeClientError("http", `Request failed with status ${response.status}`, undefined, response.status, bodyResult.result),
|
|
104
104
|
response,
|
|
105
105
|
};
|
|
106
106
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -20,13 +20,32 @@ export type EndpointResult<E> = {
|
|
|
20
20
|
responses: EndpointResponseMap<E>;
|
|
21
21
|
};
|
|
22
22
|
export type ErrorKind = "network" | "timeout" | "aborted" | "http" | "parse" | "hook";
|
|
23
|
-
|
|
23
|
+
type ErrorStatusCode<R extends Record<PropertyKey, unknown>> = Exclude<Extract<keyof R, number>, 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226>;
|
|
24
|
+
type HttpErrorDataByStatus<R extends Record<PropertyKey, unknown>> = {
|
|
25
|
+
[S in ErrorStatusCode<R>]: [R[S]] extends [never] ? never : {
|
|
26
|
+
status: S;
|
|
27
|
+
data: R[S];
|
|
28
|
+
};
|
|
29
|
+
}[ErrorStatusCode<R>];
|
|
30
|
+
type HttpErrorPayload<E> = [EndpointResponses<E>] extends [never] ? {
|
|
31
|
+
status: number;
|
|
32
|
+
data?: unknown;
|
|
33
|
+
} : [HttpErrorDataByStatus<EndpointResponses<E>>] extends [never] ? {
|
|
34
|
+
status: number;
|
|
35
|
+
data?: unknown;
|
|
36
|
+
} : HttpErrorDataByStatus<EndpointResponses<E>>;
|
|
37
|
+
export type ClientError<E = unknown> = {
|
|
24
38
|
kind: ErrorKind;
|
|
25
39
|
message: string;
|
|
26
40
|
cause?: unknown;
|
|
41
|
+
} & ({
|
|
42
|
+
kind: "network" | "timeout" | "aborted" | "parse" | "hook";
|
|
27
43
|
status?: number;
|
|
28
|
-
|
|
29
|
-
|
|
44
|
+
data?: undefined;
|
|
45
|
+
} | ({
|
|
46
|
+
kind: "http";
|
|
47
|
+
} & HttpErrorPayload<E>));
|
|
48
|
+
export type FetchResult<E, Err = ClientError<E>> = {
|
|
30
49
|
ok: true;
|
|
31
50
|
responses: EndpointResponseMap<E>;
|
|
32
51
|
response: Response;
|
|
@@ -81,3 +100,4 @@ export type ClientHooks = {
|
|
|
81
100
|
export type ClientConfig = ClientHooks & {
|
|
82
101
|
baseUrl?: string;
|
|
83
102
|
};
|
|
103
|
+
export {};
|
package/dist/utils.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export declare const HTTP_METHOD: {
|
|
|
6
6
|
readonly patch: "PATCH";
|
|
7
7
|
readonly delete: "DELETE";
|
|
8
8
|
};
|
|
9
|
-
export declare function makeClientError(kind: ErrorKind, message: string, cause?: unknown, status?: number): ClientError
|
|
9
|
+
export declare function makeClientError<E = unknown>(kind: ErrorKind, message: string, cause?: unknown, status?: number, data?: unknown): ClientError<E>;
|
|
10
10
|
export declare function createFetchSignal(options: RequestOptions): {
|
|
11
11
|
signal?: AbortSignal;
|
|
12
12
|
cleanup: () => void;
|
package/dist/utils.js
CHANGED
|
@@ -6,7 +6,16 @@ export const HTTP_METHOD = {
|
|
|
6
6
|
patch: "PATCH",
|
|
7
7
|
delete: "DELETE",
|
|
8
8
|
};
|
|
9
|
-
export function makeClientError(kind, message, cause, status) {
|
|
9
|
+
export function makeClientError(kind, message, cause, status, data) {
|
|
10
|
+
if (kind === "http") {
|
|
11
|
+
return {
|
|
12
|
+
kind,
|
|
13
|
+
message,
|
|
14
|
+
cause,
|
|
15
|
+
status: status ?? 0,
|
|
16
|
+
data,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
10
19
|
return { kind, message, cause, status };
|
|
11
20
|
}
|
|
12
21
|
export function createFetchSignal(options) {
|