0.19.2 • Published 4 months ago

@clipboard-health/json-api-nestjs v0.19.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@clipboard-health/json-api-nestjs

TypeScript-friendly utilities for adhering to the JSON:API specification with NestJS.

Table of contents

Install

npm install @clipboard-health/json-api-nestjs

Usage

Query helpers

Create Zod schemas for your API's queries:

import { booleanString } from "@clipboard-health/contract-core";
import {
  cursorPaginationQuery,
  fieldsQuery,
  type FilterMap,
  filterQuery,
  includeQuery,
  sortQuery,
} from "@clipboard-health/json-api-nestjs";
import { z } from "zod";

import {
  type ArticleAttributeFields,
  type UserAttributeFields,
  type UserIncludeFields,
} from "../src/contract";

const articleFields = ["title"] as const satisfies readonly ArticleAttributeFields[];
const userFields = ["age", "dateOfBirth"] as const satisfies readonly UserAttributeFields[];
const userIncludeFields = [
  "articles",
  "articles.comments",
] as const satisfies readonly UserIncludeFields[];
const userFilterMap = {
  age: {
    filters: ["eq", "gt"],
    schema: z.coerce.number().int().positive().max(125),
  },
  dateOfBirth: {
    filters: ["gte"],
    schema: z.coerce.date().min(new Date("1900-01-01")).max(new Date()),
  },
  isActive: {
    filters: ["eq"],
    schema: booleanString,
  },
} as const satisfies FilterMap<UserAttributeFields>;

/**
 * Disclaimer: Just because JSON:API supports robust querying doesn’t mean your service should
 * implement them as they may require database indexes, which have a cost. **Implement only access
 * patterns required by clients.**
 *
 * The spec says that if clients provide fields the server doesn’t support, it **MUST** return 400
 * Bad Request, hence the `.strict()`.
 *\/
export const query = z
  .object({
    ...cursorPaginationQuery(),
    ...fieldsQuery({ article: articleFields, user: userFields }),
    ...filterQuery(userFilterMap),
    ...sortQuery(userFields),
    ...includeQuery(userIncludeFields),
  })
  .strict();

Local development commands

See package.json scripts for a list of commands.

0.19.0

4 months ago

0.17.2

6 months ago

0.15.4

9 months ago

0.19.1

4 months ago

0.17.3

4 months ago

0.15.5

9 months ago

0.19.2

4 months ago

0.17.4

4 months ago

0.15.6

9 months ago

0.15.7

9 months ago

0.15.8

8 months ago

0.15.9

8 months ago

0.13.0

11 months ago

0.13.1

11 months ago

0.15.0

11 months ago

0.15.1

11 months ago

0.17.0

6 months ago

0.15.2

11 months ago

0.17.1

6 months ago

0.15.3

9 months ago

0.16.10

6 months ago

0.16.11

6 months ago

0.16.3

7 months ago

0.16.4

7 months ago

0.16.5

7 months ago

0.16.6

6 months ago

0.16.7

6 months ago

0.16.8

6 months ago

0.16.9

6 months ago

0.12.0

11 months ago

0.12.1

11 months ago

0.14.0

11 months ago

0.12.2

11 months ago

0.12.3

11 months ago

0.16.0

8 months ago

0.16.1

7 months ago

0.18.0

4 months ago

0.16.2

7 months ago

0.15.10

8 months ago

0.15.11

8 months ago

0.11.11

11 months ago

0.11.8

11 months ago

0.11.9

11 months ago

0.11.7

11 months ago

0.11.10

11 months ago

0.10.0

11 months ago

0.11.0

11 months ago

0.9.0

11 months ago

0.11.1

11 months ago

0.8.0

11 months ago

0.7.1

11 months ago

0.11.2

11 months ago

0.11.3

11 months ago

0.11.4

11 months ago

0.11.5

11 months ago

0.11.6

11 months ago

0.7.0

11 months ago

0.6.0

11 months ago

0.5.1

12 months ago

0.4.1

12 months ago

0.4.0

12 months ago

0.3.1

12 months ago

0.3.0

12 months ago

0.2.0

12 months ago

0.1.0

12 months ago