@viliaapro/apifetch 0.1.2 → 0.1.4
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 +10 -1
- package/dist/index.d.mts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +13 -8
- package/dist/index.mjs +11 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ npm install @viliaapro/apifetch @standard-schema/spec http-status-codes zod
|
|
|
20
20
|
## Usage
|
|
21
21
|
|
|
22
22
|
```typescript
|
|
23
|
-
import { apiFetch } from '@viliaapro/apifetch';
|
|
23
|
+
import { apiFetch, handleResponse } from '@viliaapro/apifetch';
|
|
24
24
|
import { StatusCodes } from 'http-status-codes';
|
|
25
25
|
import { z } from 'zod';
|
|
26
26
|
|
|
@@ -89,6 +89,15 @@ try {
|
|
|
89
89
|
|
|
90
90
|
Returns `Promise<R>` where `R` is inferred from the transform return types.
|
|
91
91
|
|
|
92
|
+
### `handleResponse<R>(response, handlers)`
|
|
93
|
+
|
|
94
|
+
| Parameter | Type | Description |
|
|
95
|
+
|------------|-----------------------|------------------------------------|
|
|
96
|
+
| `response` | `Response` | A fetch `Response` object |
|
|
97
|
+
| `handlers` | `ResponseHandlers<R>` | Map of status codes to handlers |
|
|
98
|
+
|
|
99
|
+
Returns `Promise<R>`. Useful when you already have a `Response` object from an external fetch client or middleware.
|
|
100
|
+
|
|
92
101
|
## License
|
|
93
102
|
|
|
94
103
|
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -8,9 +8,9 @@ type ResponseHandlers<R> = {
|
|
|
8
8
|
[S in number]?: StatusHandler<StandardSchemaV1, R>;
|
|
9
9
|
};
|
|
10
10
|
declare class ApiError extends Error {
|
|
11
|
-
readonly response
|
|
12
|
-
get status(): number
|
|
13
|
-
constructor(message: string, response
|
|
11
|
+
readonly response: Response;
|
|
12
|
+
get status(): number;
|
|
13
|
+
constructor(message: string, response: Response, options?: ErrorOptions);
|
|
14
14
|
}
|
|
15
15
|
declare class UnrecognizedStatusError extends ApiError {
|
|
16
16
|
constructor(response: Response);
|
|
@@ -19,6 +19,7 @@ declare class MalformedResponseError extends ApiError {
|
|
|
19
19
|
readonly issues: readonly StandardSchemaV1.Issue[];
|
|
20
20
|
constructor(response: Response, issues: readonly StandardSchemaV1.Issue[]);
|
|
21
21
|
}
|
|
22
|
+
declare function handleResponse<R>(response: Response, handlers: ResponseHandlers<R>): Promise<R>;
|
|
22
23
|
declare function apiFetch<R>(url: string, handlers: ResponseHandlers<R>, options?: RequestInit): Promise<R>;
|
|
23
24
|
|
|
24
|
-
export { ApiError, MalformedResponseError, type ResponseHandlers, type StatusHandler, UnrecognizedStatusError, apiFetch };
|
|
25
|
+
export { ApiError, MalformedResponseError, type ResponseHandlers, type StatusHandler, UnrecognizedStatusError, apiFetch, handleResponse };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,9 +8,9 @@ type ResponseHandlers<R> = {
|
|
|
8
8
|
[S in number]?: StatusHandler<StandardSchemaV1, R>;
|
|
9
9
|
};
|
|
10
10
|
declare class ApiError extends Error {
|
|
11
|
-
readonly response
|
|
12
|
-
get status(): number
|
|
13
|
-
constructor(message: string, response
|
|
11
|
+
readonly response: Response;
|
|
12
|
+
get status(): number;
|
|
13
|
+
constructor(message: string, response: Response, options?: ErrorOptions);
|
|
14
14
|
}
|
|
15
15
|
declare class UnrecognizedStatusError extends ApiError {
|
|
16
16
|
constructor(response: Response);
|
|
@@ -19,6 +19,7 @@ declare class MalformedResponseError extends ApiError {
|
|
|
19
19
|
readonly issues: readonly StandardSchemaV1.Issue[];
|
|
20
20
|
constructor(response: Response, issues: readonly StandardSchemaV1.Issue[]);
|
|
21
21
|
}
|
|
22
|
+
declare function handleResponse<R>(response: Response, handlers: ResponseHandlers<R>): Promise<R>;
|
|
22
23
|
declare function apiFetch<R>(url: string, handlers: ResponseHandlers<R>, options?: RequestInit): Promise<R>;
|
|
23
24
|
|
|
24
|
-
export { ApiError, MalformedResponseError, type ResponseHandlers, type StatusHandler, UnrecognizedStatusError, apiFetch };
|
|
25
|
+
export { ApiError, MalformedResponseError, type ResponseHandlers, type StatusHandler, UnrecognizedStatusError, apiFetch, handleResponse };
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,8 @@ __export(index_exports, {
|
|
|
23
23
|
ApiError: () => ApiError,
|
|
24
24
|
MalformedResponseError: () => MalformedResponseError,
|
|
25
25
|
UnrecognizedStatusError: () => UnrecognizedStatusError,
|
|
26
|
-
apiFetch: () => apiFetch
|
|
26
|
+
apiFetch: () => apiFetch,
|
|
27
|
+
handleResponse: () => handleResponse
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(index_exports);
|
|
29
30
|
var ApiError = class extends Error {
|
|
@@ -33,7 +34,7 @@ var ApiError = class extends Error {
|
|
|
33
34
|
this.name = this.constructor.name;
|
|
34
35
|
}
|
|
35
36
|
get status() {
|
|
36
|
-
return this.response
|
|
37
|
+
return this.response.status;
|
|
37
38
|
}
|
|
38
39
|
};
|
|
39
40
|
var UnrecognizedStatusError = class extends ApiError {
|
|
@@ -47,11 +48,7 @@ var MalformedResponseError = class extends ApiError {
|
|
|
47
48
|
this.issues = issues;
|
|
48
49
|
}
|
|
49
50
|
};
|
|
50
|
-
async function
|
|
51
|
-
const response = await fetch(url, {
|
|
52
|
-
...options,
|
|
53
|
-
headers: { "Content-Type": "application/json", ...options.headers }
|
|
54
|
-
});
|
|
51
|
+
async function handleResponse(response, handlers) {
|
|
55
52
|
const handler = handlers[response.status];
|
|
56
53
|
if (!handler) {
|
|
57
54
|
throw new UnrecognizedStatusError(response);
|
|
@@ -63,10 +60,18 @@ async function apiFetch(url, handlers, options = {}) {
|
|
|
63
60
|
}
|
|
64
61
|
return handler.transform(result.value);
|
|
65
62
|
}
|
|
63
|
+
async function apiFetch(url, handlers, options = {}) {
|
|
64
|
+
const response = await fetch(url, {
|
|
65
|
+
...options,
|
|
66
|
+
headers: { "Content-Type": "application/json", ...options.headers }
|
|
67
|
+
});
|
|
68
|
+
return handleResponse(response, handlers);
|
|
69
|
+
}
|
|
66
70
|
// Annotate the CommonJS export names for ESM import in node:
|
|
67
71
|
0 && (module.exports = {
|
|
68
72
|
ApiError,
|
|
69
73
|
MalformedResponseError,
|
|
70
74
|
UnrecognizedStatusError,
|
|
71
|
-
apiFetch
|
|
75
|
+
apiFetch,
|
|
76
|
+
handleResponse
|
|
72
77
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ var ApiError = class extends Error {
|
|
|
6
6
|
this.name = this.constructor.name;
|
|
7
7
|
}
|
|
8
8
|
get status() {
|
|
9
|
-
return this.response
|
|
9
|
+
return this.response.status;
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
12
|
var UnrecognizedStatusError = class extends ApiError {
|
|
@@ -20,11 +20,7 @@ var MalformedResponseError = class extends ApiError {
|
|
|
20
20
|
this.issues = issues;
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
|
-
async function
|
|
24
|
-
const response = await fetch(url, {
|
|
25
|
-
...options,
|
|
26
|
-
headers: { "Content-Type": "application/json", ...options.headers }
|
|
27
|
-
});
|
|
23
|
+
async function handleResponse(response, handlers) {
|
|
28
24
|
const handler = handlers[response.status];
|
|
29
25
|
if (!handler) {
|
|
30
26
|
throw new UnrecognizedStatusError(response);
|
|
@@ -36,9 +32,17 @@ async function apiFetch(url, handlers, options = {}) {
|
|
|
36
32
|
}
|
|
37
33
|
return handler.transform(result.value);
|
|
38
34
|
}
|
|
35
|
+
async function apiFetch(url, handlers, options = {}) {
|
|
36
|
+
const response = await fetch(url, {
|
|
37
|
+
...options,
|
|
38
|
+
headers: { "Content-Type": "application/json", ...options.headers }
|
|
39
|
+
});
|
|
40
|
+
return handleResponse(response, handlers);
|
|
41
|
+
}
|
|
39
42
|
export {
|
|
40
43
|
ApiError,
|
|
41
44
|
MalformedResponseError,
|
|
42
45
|
UnrecognizedStatusError,
|
|
43
|
-
apiFetch
|
|
46
|
+
apiFetch,
|
|
47
|
+
handleResponse
|
|
44
48
|
};
|