Skip to main content
Deno 2 is finally here 🎉️
Learn more

zod_api

Configure API clients using Zod schemas for type enforcement and interpreted action methods.

Api Client

import { zodApiClient, zodApiResource } from "zod_api"

const apiClient = zodApiClient({
  baseUrl: "https://someapi.com/v1",
  resources: {
    foo: zodApiResource("/foo", {
      actions: {
        get: {
          dataSchema: z.object({
            bar: z.string(),
            baz: z.number(),
          }),
        },
      },
    }),
    baz: zodApiResource("/baz/:id", {
      // URL parameters schema is enforced by the given path, /baz/[id] pattern is also supported
      urlParamsSchema: z.object({
        id: z.number(),
      }),
      actions: {
        get: {
          searchParamsSchema: z.object({
            q: z.string().optional(),
          }),
          headersSchema: z.object({
            "x-key": z.string(),
            "x-secret": z.string(),
          }),
          dataSchema: z.object({
            foo: z.string(),
            bar: z.number(),
          }),
        },
      },
    }),
  },
})