0.19.2 • Published 5 months ago

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

Weekly downloads
-
License
MIT
Repository
github
Last release
5 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

5 months ago

0.17.2

7 months ago

0.15.4

10 months ago

0.19.1

5 months ago

0.17.3

5 months ago

0.15.5

10 months ago

0.19.2

5 months ago

0.17.4

5 months ago

0.15.6

10 months ago

0.15.7

10 months ago

0.15.8

9 months ago

0.15.9

9 months ago

0.13.0

12 months ago

0.13.1

12 months ago

0.15.0

12 months ago

0.15.1

12 months ago

0.17.0

7 months ago

0.15.2

12 months ago

0.17.1

7 months ago

0.15.3

10 months ago

0.16.10

7 months ago

0.16.11

7 months ago

0.16.3

8 months ago

0.16.4

8 months ago

0.16.5

8 months ago

0.16.6

7 months ago

0.16.7

7 months ago

0.16.8

7 months ago

0.16.9

7 months ago

0.12.0

12 months ago

0.12.1

12 months ago

0.14.0

12 months ago

0.12.2

12 months ago

0.12.3

12 months ago

0.16.0

9 months ago

0.16.1

8 months ago

0.18.0

5 months ago

0.16.2

8 months ago

0.15.10

9 months ago

0.15.11

9 months ago

0.11.11

1 year ago

0.11.8

1 year ago

0.11.9

1 year ago

0.11.7

1 year ago

0.11.10

1 year ago

0.10.0

1 year ago

0.11.0

1 year ago

0.9.0

1 year ago

0.11.1

1 year ago

0.8.0

1 year ago

0.7.1

1 year ago

0.11.2

1 year ago

0.11.3

1 year ago

0.11.4

1 year ago

0.11.5

1 year ago

0.11.6

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.1

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago