npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-jsonapi-zod

v3.2.4

Published

JSON:API support for Koa using Zod

Downloads

180

Readme

JSON:API support for Koa with Zod

Release

This package provides support for working with JSON:API in Koa, taking all guesswork out of the equation.

Installation

npm

npm i koa-jsonapi-zod

pnpm

pnpm add koa-jsonapi-zod

Usage

In order to serialize your entities into valid JSON:API responses, you have to set up a serialize manager which is composed of several entity serializers. You also have access to several utility functions meant for parsing incoming requests, as well as a middleware which takes care of handling requests and responses.

For a complete example, have a look under examples/complete.

Middlewares

This package exports two middlewares, jsonApiRequestMiddleware and jsonApiErrorMiddleware.

The former takes care of validating incoming requests and formatting responses accordingly. You can exclude specific paths from being handled by the middleware (e.g. RPC endpoints). This middleware should be registered as early as possible.

The error middleware is a convenience middleware which traps all errors and creates appropriate error responses. You can also supply a logger function as an option, which allows you to log specific errors. This middleware should be registered right after the request middleware.

For more details, see index.ts in the complete example.

Features not covered in the example

Serialization context

In some instances your serializers might require context about a request. One instance of this is where you want to expose specific fields of an entity only when the user has permission to access these. In that case you should define your serialization context first:

type SerializerContext = {
    permissions?: string[];
};

Each serializer can define its own context, and it will be accessible separately for each serializer. Since the context is always an optional property, you can then perform checks like this in e.g. your getAttributes() implementation:

const attributes: Record<string, unknown> = {
    foo: "bar",
    baz: "bat",
};

if (options.context?.permissions?.includes("read:secret")) {
    attributes.secret = "super secrtet information";
}

You can then serialize your entities in the following way:

serializeManager.createResourceDocument("my_entity", entity, {
    context: {my_entity: {permissions: "foo"}},
});

Additionally, when you define a relationship in a serializer as an entity, you can pass down additional context which will be merged with the parent context.

Filtering

You can add filters to list handlers as well. JSON:API does not define the structure of filters itself, except that the filter property should be used for this. Whether you make that property an object or a plain string is up to you. Simply provide a Zod schema to the filterSchema property, and you will get a filter property on the result back.

Pagination

Similar to filters you can also supply a pageSchema property, which will result in a page result.