New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@thrty/api-zod

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thrty/api-zod

Middleware for validation and metadata describing request body, response body and query parameters

latest
Source
npmnpm
Version
3.0.0-alpha.11
Version published
Maintainers
1
Created
Source

thirty
@thrty/api-zod

Middlewares for validation and metadata describing request body, response body and query parameters with zod

Installation

npm install @thrty/api-zod

Usage

import { APIGatewayProxyHandler } from 'aws-lambda';
import { compose, types } from '@thrty/core';
import { authorizer, get } from '@thrty/api';
import { inject } from '@thrty/inject';
import { responseBody } from '@thrty/api-zod';
import { NotFoundError } from '@thrty/http-errors';
import { httpErrorHandler } from '@thrty/http-error-handler';

export const handler = compose(
  typesOf<APIGatewayProxyHandler>(),
  inject({
    ...todoRepositoryProviders,
  }),
  httpErrorHandler(),
  get('/todos/{todoId}'),
  authorizer('default'),
  responseBody(z.object({
    id: z.string(),
    title: z.string(),
    completed: z.boolean(),
    createdAt: z.string(),
    updatedAt: z.string(),
  })),
)(async event => {
  const { todoRepository } = event.deps;
  const todo = await todoRepository.findById(event.routeParams.todoId);

  if (!todo) {
    throw new NotFoundError('Todo not found');
  }

  return {
    statusCode: 200,
    body: todo,
  };
});

Middlewares

requestBody

Middleware to parse, validate and infer the type of request body using Zod. If request body is not valid, it will throw a ZodBadRequestError with the validation errors.

export const handler = compose(
  /* ... */
  requestBody(z.object({
    title: z.string(),
  })),
)(async event => {
  // validated request body is available in event.requestBody
  event.requestBody
  /* ... */
});

responseBody

Middleware to serialize, infer the type and optionally validate the response body using Zod. If returned body does not satisfy inferred type, a TypeScript throws an error. If validation is enabled, response body is not valid, it will throw a Error with the validation errors.

export const handler = compose(
  /* ... */
  responseBody(z.object({
    id: z.string(),
    title: z.string(),
    completed: z.boolean(),
    createdAt: z.string(),
    updatedAt: z.string(),
  })),
)(async event => {
  // validated request body is available in event.requestBody
  return {
    statusCode: 200,
    body: {
      id: 'cCYfM67dbtx',
      title: 'Implementing Zod middleware',
      completed: true,
      createdAt: '2025-01-01T00:00:00.000Z',
      updatedAt: '2025-01-01T00:00:00.000Z',
    }
  }
});

To enable validation pass { validate: true } as second argument to responseBody:

queryParams

Middleware to parse, validate and infer the type of query parameters using Zod. If query parameters are not valid, it will throw a ZodBadRequestError with the validation errors.

import { queryParams } from '@thrty/api-zod/src';

export const handler = compose(
  /* ... */
  queryParams(z.object({
    limit: z.string().transform(Number),
    page: z.string().transform(Number),
    sortBy: z.array(z.string()),
  })),
)(async event => {
  // validated query parameters are available in event.queryParams
  event.queryParams.limit
  /* ... */
});

FAQs

Package last updated on 06 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts