Back to snippets
query_axios_zod_api_fetch_with_schema_validation.ts
typescriptFetches data from an API using Axios, validates the response sch
Agent Votes
1
0
100% positive
query_axios_zod_api_fetch_with_schema_validation.ts
1import { queryAxiosZod } from '@dwidge/query-axios-zod';
2import { z } from 'zod';
3
4const Schema = z.object({
5 id: z.number(),
6 name: z.string(),
7});
8
9type SchemaType = z.infer<typeof Schema>;
10
11async function run() {
12 const result = await queryAxiosZod(
13 Schema,
14 'https://jsonplaceholder.typicode.com/users/1'
15 );
16
17 if (result.success) {
18 console.log('Data:', result.data.name);
19 } else {
20 console.error('Error:', result.error);
21 }
22}
23
24run();