kayto_ts 0.1.8 → 0.1.10
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 +71 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -224,11 +224,71 @@ const clientWithHooks = clientApi<CatsEndpoints>({
|
|
|
224
224
|
});
|
|
225
225
|
```
|
|
226
226
|
|
|
227
|
+
## Success Handling (Cats API)
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
async function fetchCatsUI() {
|
|
231
|
+
let loading = true;
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
const result = await client.get("/api/cats");
|
|
235
|
+
|
|
236
|
+
if (!result.ok) {
|
|
237
|
+
throw result.error;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const cats = result.responses[200];
|
|
241
|
+
|
|
242
|
+
if (!cats) {
|
|
243
|
+
throw new Error("Cats response is empty");
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return cats;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
// UI can show toast/snackbar here
|
|
249
|
+
throw error;
|
|
250
|
+
} finally {
|
|
251
|
+
loading = false;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
async function createCatUI() {
|
|
258
|
+
let loading = true;
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
const result = await client.post("/api/cats", {
|
|
262
|
+
body: {
|
|
263
|
+
name: "Milo",
|
|
264
|
+
age: 2,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
if (!result.ok) {
|
|
269
|
+
throw result.error;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const created = result.responses[201] ?? result.responses[200];
|
|
273
|
+
|
|
274
|
+
if (!created) {
|
|
275
|
+
throw new Error("Create cat payload is empty");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return created;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
throw error;
|
|
281
|
+
} finally {
|
|
282
|
+
loading = false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
227
287
|
## Error Handling
|
|
228
288
|
|
|
229
289
|
All requests return a discriminated union:
|
|
230
290
|
|
|
231
|
-
- success: `{ ok: true,
|
|
291
|
+
- success: `{ ok: true, responses, response }`
|
|
232
292
|
- failure: `{ ok: false, error, response? }`
|
|
233
293
|
|
|
234
294
|
`error.kind` values:
|
|
@@ -246,6 +306,15 @@ You can also handle them generically via `message` / `status` / `cause` without
|
|
|
246
306
|
```ts
|
|
247
307
|
const result = await client.get("/api/cats");
|
|
248
308
|
|
|
309
|
+
if (result.ok) {
|
|
310
|
+
const success = result.responses[200];
|
|
311
|
+
console.log(success);
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
const result = await client.get("/api/cats");
|
|
317
|
+
|
|
249
318
|
if (!result.ok) {
|
|
250
319
|
console.error("Request failed:", result.error.message);
|
|
251
320
|
|
|
@@ -291,5 +360,5 @@ Client parses response body automatically by `content-type`:
|
|
|
291
360
|
## Notes
|
|
292
361
|
|
|
293
362
|
- Runtime shape validation is not built in yet (current typing is compile-time only).
|
|
294
|
-
- If you need runtime validation, validate `result.
|
|
363
|
+
- If you need runtime validation, validate `result.responses[statusCode]` in consumer code (ArkType/Zod/etc.).
|
|
295
364
|
- Current entrypoint is `src/index.ts`.
|