@zayne-labs/callapi 1.11.9 → 1.11.11
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 +60 -57
- package/dist/esm/{common-DvPxUh-h.d.ts → common-7RMbhCz1.d.ts} +204 -128
- package/dist/esm/common-CXyKVpND.js +649 -0
- package/dist/esm/common-CXyKVpND.js.map +1 -0
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -423
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/external/index.d.ts +2 -80
- package/dist/esm/utils/external/index.js +2 -2
- package/package.json +1 -1
- package/dist/esm/body-BVMFJoeo.js +0 -228
- package/dist/esm/body-BVMFJoeo.js.map +0 -1
package/README.md
CHANGED
|
@@ -59,8 +59,8 @@ const { data } = await callApi("/api/data"); // JSON? Parsed.
|
|
|
59
59
|
const { data, error } = await callApi("/api/users");
|
|
60
60
|
|
|
61
61
|
if (error) {
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
console.log(error.name); // "HTTPError", "ValidationError"
|
|
63
|
+
console.log(error.errorData); // Actual API response
|
|
64
64
|
}
|
|
65
65
|
```
|
|
66
66
|
|
|
@@ -68,9 +68,9 @@ if (error) {
|
|
|
68
68
|
|
|
69
69
|
```js
|
|
70
70
|
await callApi("/api/data", {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
retryAttempts: 3,
|
|
72
|
+
retryStrategy: "exponential",
|
|
73
|
+
retryStatusCodes: [429, 500, 502, 503],
|
|
74
74
|
});
|
|
75
75
|
```
|
|
76
76
|
|
|
@@ -82,32 +82,35 @@ import { createFetchClient } from "@zayne-labs/callapi";
|
|
|
82
82
|
import { defineSchema } from "@zayne-labs/callapi/utils";
|
|
83
83
|
|
|
84
84
|
const callMainApi = createFetchClient({
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
schema: defineSchema({
|
|
86
|
+
"/users/:id": {
|
|
87
|
+
data: z.object({
|
|
88
|
+
id: z.number(),
|
|
89
|
+
name: z.string(),
|
|
90
|
+
}),
|
|
91
|
+
},
|
|
92
|
+
}),
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
// Fully typed + validated
|
|
96
|
+
const user = await callMainApi("/users/:id", {
|
|
97
|
+
params: { id: 123 },
|
|
98
|
+
});
|
|
96
99
|
```
|
|
97
100
|
|
|
98
101
|
**Hooks** - Intercept at any point.
|
|
99
102
|
|
|
100
103
|
```js
|
|
101
104
|
const api = createFetchClient({
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
onRequest: ({ request }) => {
|
|
106
|
+
request.headers.set("Authorization", `Bearer ${token}`);
|
|
107
|
+
},
|
|
108
|
+
onError: ({ error }) => {
|
|
109
|
+
Sentry.captureException(error);
|
|
110
|
+
},
|
|
111
|
+
onResponseStream: ({ event }) => {
|
|
112
|
+
console.log(`Downloaded ${event.progress}%`);
|
|
113
|
+
},
|
|
111
114
|
});
|
|
112
115
|
```
|
|
113
116
|
|
|
@@ -115,38 +118,38 @@ const api = createFetchClient({
|
|
|
115
118
|
|
|
116
119
|
```js
|
|
117
120
|
const metricsPlugin = definePlugin({
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
121
|
+
id: "metrics",
|
|
122
|
+
name: "Metrics Plugin",
|
|
123
|
+
|
|
124
|
+
setup: ({ options }) => ({
|
|
125
|
+
options: {
|
|
126
|
+
...options,
|
|
127
|
+
meta: { startTime: Date.now() },
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
|
|
131
|
+
hooks: {
|
|
132
|
+
onSuccess: ({ options }) => {
|
|
133
|
+
const duration = Date.now() - options.meta.startTime;
|
|
134
|
+
|
|
135
|
+
console.info(`Request took ${duration}ms`);
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
middlewares: {
|
|
140
|
+
fetchMiddleware: (ctx) => async (input, init) => {
|
|
141
|
+
console.info("→", input);
|
|
142
|
+
|
|
143
|
+
const response = await ctx.fetchImpl(input, init);
|
|
144
|
+
|
|
145
|
+
console.info("←", response.status);
|
|
146
|
+
return response;
|
|
147
|
+
},
|
|
148
|
+
},
|
|
146
149
|
});
|
|
147
150
|
|
|
148
151
|
const api = createFetchClient({
|
|
149
|
-
|
|
152
|
+
plugins: [metricsPlugin],
|
|
150
153
|
});
|
|
151
154
|
```
|
|
152
155
|
|
|
@@ -174,10 +177,10 @@ const { data } = await callApi("/api/users");
|
|
|
174
177
|
|
|
175
178
|
// Configured
|
|
176
179
|
const api = createFetchClient({
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
180
|
+
baseURL: "https://api.example.com",
|
|
181
|
+
retryAttempts: 2,
|
|
182
|
+
timeout: 10000,
|
|
183
|
+
onError: ({ error }) => trackError(error),
|
|
181
184
|
});
|
|
182
185
|
```
|
|
183
186
|
|
|
@@ -185,7 +188,7 @@ const api = createFetchClient({
|
|
|
185
188
|
|
|
186
189
|
```html
|
|
187
190
|
<script type="module">
|
|
188
|
-
|
|
191
|
+
import { callApi } from "https://esm.run/@zayne-labs/callapi";
|
|
189
192
|
</script>
|
|
190
193
|
```
|
|
191
194
|
|