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

@kyrisu/jsonapi-fastify

v0.1.9

Published

A node JSON:API server, powered by fastify

Downloads

2

Readme

jsonapi-fastify

Coverage Status

Installation

npm install jsonapi-fastify

About

Inspired by projects such as jagql-framework and jsonapi-server, jsonapi-fastify allows developers to stand up a {json:api} compliant API server quickly and easily. As the name implies, jsonapi-fastify uses fastify to handle request routing and configuration.

Motivation / Justification / Rationale

This framework solves the challenges of json:api without coupling us to any one ORM solution. Every other module out there is either tightly coupled to a database implementation, tracking an old version of the json:api spec, or is merely a helper library for a small feature. If you're building an API and your use case only involves reading and writing to a data store... well count yourself lucky. For everyone else, this framework provides the flexibility to provide a complex API without being confined to any one technology.

A config driven approach to building an API enables:

  • [x] Enforced json:api responses
  • [ ] Automatic GraphQL schema generation (coming soon)
  • [x] Request validation
  • [x] Payload validation
  • [x] Automatic documentation generation
  • [x] Automatic inclusions
  • [x] Automatic routing
  • [x] Automatic handling of relationships

Ultimately, the only things users of this framework need to care about are:

  • What are your resources called
  • What properties yours resources have
  • For each resource, implement a handler to:
    • create a resource
    • delete a resource
    • search for multiple resources
    • find a specific resource
    • update a specific resource

Handlers

  • memory-handler - an in-memory data store to enable rapid prototyping. This ships as a part of jsonapi-fastify and powers the core test suite.
  • resource-handler - a database agnostic handler that allows users to fully customize handler behavior. Useful for resources that do not stem from stores with CRUD interfaces or are composed from multiple sources.

Full documentation

Quick Start: Server

const { jsonapiFastify, define, MemoryHandler } = require("jsonapi-fastify");
const { nanoid } = require("nanoid");

const server = jsonapiFastify({
  openapi: {
    info: {
      version: "1.0.0",
      title: "test server",
      description: "a jsonapi server",
      contact: {
        url: "https://jsonapi.org",
        email: "[email protected]",
      },
      license: {
        name: "MIT",
        url: "https://jsonapi.org/license",
      },
    },
  },
  definitions: [
    define((schema) => ({
      resource: "people",
      idGenerator: () => nanoid(),
      handler: MemoryHandler(),
      fields: {
        firstname: schema.attribute(),
        lastname: schema.attribute(),
        articles: schema.belongsToOne({
          resource: "articles",
          as: "author",
        }),
      },
      examples: [
        {
          id: "42",
          type: "people",
          firstname: "John",
          lastname: "Doe",
        },
        {
          id: "24",
          type: "people",
          firstname: "Jane",
          lastname: "Doe",
        },
        {
          id: "22",
          type: "people",
          firstname: "Billy",
          lastname: "Idol",
        },
      ],
      defaultPageSize: 100,
    })),
  ],
});

server.listen(3000, (err, address) => {
  if (err) {
    console.error(err);
  } else {
    console.log(`Ready at address ${address}!`);
  }
});

Quick Start: Serverless (AWS Lambda)

# serverless.yml
provider:
  name: aws
  stage: dev
functions:
  handler: src/handler.main
  environment:
    STAGE: ${self:provider.stage}
  events:
    - http:
        method: any
        path: api/{proxy+}
// src/handler.js

import awsLambdaFastify from "aws-lambda-fastify";
import { jsonapiFastify, define, MemoryHandler } from "jsonapi-fastify";
import { nanoid } from "nanoid";

const app = jsonapiFastify({
  urlPrefixAlias: `https://api.example.com/${process.env.STAGE}`,
  prefix: "/api",
  openapi: {
    info: {
      version: "1.0.0",
      title: "test server",
      description: "a jsonapi server",
      contact: {
        url: "https://jsonapi.org",
        email: "[email protected]",
      },
      license: {
        name: "MIT",
        url: "https://jsonapi.org/license",
      },
    },
  },
  definitions: [
    define((schema) => ({
      resource: "people",
      idGenerator: () => nanoid(),
      handler: MemoryHandler(),
      fields: {
        firstname: schema.attribute({
          description: "The person's first name",
          type: (z) => z.string(),
        }),
        lastname: schema.attribute({
          description: "The person's last name",
          type: (z) => z.string(),
        }),
        articles: schema.belongsToOne({
          resource: "articles",
          as: "author",
        }),
      },
      examples: [
        {
          id: "42",
          type: "people",
          firstname: "John",
          lastname: "Doe",
        },
      ],
      defaultPageSize: 100,
    })),
  ],
});

export const main = awsLambdaFastify(app);