zodaxios
TypeScript icon, indicating that this package has built-in type declarations

0.7.0 • Public • Published

zodaxios

HTTP client with schema validation using Zod.

  • 🚀 Lightweight.
  • 🎉 No dependencies.
  • 🤖 Compatible with Axios API.
  • 🔄 Supports Interceptors.

Example

import zodaxios from 'zodaxios';

const schema = z.object({
  name: z.string()
});

const { data } = await zodaxios('/api', { schema });
//      ^? { name: string }

Creating an instance

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 });

Handling errors

import zodaxios, { ZodaxiosError } from 'zodaxios';

try {
  const { data } = await api.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 });
  }
}

Interceptors

// 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);
  }
);

Readme

Keywords

none

Package Sidebar

Install

npm i zodaxios

Weekly Downloads

2

Version

0.7.0

License

MIT

Unpacked Size

15.8 kB

Total Files

9

Last publish

Collaborators

  • petermekhaeil