一覧へ戻る
petermekhaeil/zodaxios(新しいタブで開きます)
主要言語: TypeScript
HTTP client with schema validation using Zod
- star 数
- 5
- watcher 数
- 1
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2023/03/09
主要言語: TypeScript
HTTP client with schema validation using Zod
HTTP client with schema validation using Zod.
import zodaxios from 'zodaxios'; const schema = z.object({ name: z.string() }); const { data } = await zodaxios.get('/api', { schema }); // ^? { name: string }
import zodaxios from 'zodaxios'; const api = zodaxios.create({ baseURL: 'https://example.com' }); const schema = z.object({ name: z.string() }); const { data } = await api.get('/api', { schema });
import zodaxios, { ZodaxiosError } from 'zodaxios'; try { const { data } = await zodaxios.get('/api', { schema }); } catch (error) { if (error instanceof ZodaxiosError) { // Zod validation failed. The schema did not match // the response data from the server. console.log('Validation failed', { error }); } else if (error.response) { // The server responded with an error console.log('Error from server', { response }); } }
// Add a request interceptor zodaxios.interceptors.request.use( function (config) { // Do something before request is sent return config; }, function (error) { // Do something with request error return Promise.reject(error); } ); // Add a response interceptor zodaxios.interceptors.response.use( function (response) { // Do something with response data return response; }, function (error) { // Do something with response error return Promise.reject(error); } );