0.21.0 • Published 7 months ago

@clipboard-health/json-api v0.21.0

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

@clipboard-health/json-api

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

Table of contents

Install

npm install @clipboard-health/json-api

Usage

Query helpers

From the client, call stringifyQuery to convert from JsonApiQuery to URLSearchParams:

import { deepEqual } from "node:assert/strict";

import { stringifyQuery } from "@clipboard-health/json-api";

import { type ClientJsonApiQuery } from "../src/lib/types";

const [date1, date2] = ["2024-01-01", "2024-01-02"];
const query: ClientJsonApiQuery = {
  fields: { user: ["age", "dateOfBirth"] },
  filter: {
    age: 2,
    dateOfBirth: {
      gt: date1,
      lt: date2,
    },
    isActive: true,
  },
  include: "article",
  page: {
    size: 10,
  },
  sort: "-age",
};

deepEqual(
  stringifyQuery(query),
  new URLSearchParams(
    `fields[user]=age,dateOfBirth&filter[age]=2&filter[dateOfBirth][gt]=${date1}&filter[dateOfBirth][lt]=${date2}&filter[isActive]=true&include=article&page[size]=10&sort=-age`,
  ).toString(),
);

From the server, call parseQuery to convert from URLSearchParams to ServerJsonApiQuery:

import { deepEqual } from "node:assert/strict";

import { parseQuery } from "@clipboard-health/json-api";

import { type ServerJsonApiQuery } from "../src/lib/types";

const [date1, date2] = ["2024-01-01", "2024-01-02"];
// The URLSearchParams constructor also supports URL-encoded strings
const searchParams = new URLSearchParams(
  `fields[user]=age,dateOfBirth&filter[age]=2&filter[dateOfBirth][gt]=${date1}&filter[dateOfBirth][lt]=${date2}&filter[isActive]=true&include=article&page[size]=10&sort=-age`,
);

const query: ServerJsonApiQuery = parseQuery(searchParams.toString());

deepEqual(query, {
  fields: { user: ["age", "dateOfBirth"] },
  filter: {
    age: "2",
    dateOfBirth: { gt: date1, lt: date2 },
    isActive: "true",
  },
  include: "article",
  page: {
    size: "10",
  },
  sort: "-age",
});

Local development commands

See package.json scripts for a list of commands.

0.20.0

7 months ago

0.19.0

8 months ago

0.17.2

1 year ago

0.19.1

8 months ago

0.17.3

11 months ago

0.19.2

8 months ago

0.19.3

7 months ago

0.15.0

1 year ago

0.15.1

1 year ago

0.17.0

1 year ago

0.17.1

1 year ago

0.21.0

7 months ago

0.18.1

10 months ago

0.18.2

10 months ago

0.18.3

10 months ago

0.18.4

9 months ago

0.14.0

1 year ago

0.14.1

1 year ago

0.16.0

1 year ago

0.18.0

10 months ago

0.13.6

1 year ago

0.13.7

1 year ago

0.13.8

1 year ago

0.11.0

1 year ago

0.12.0

1 year ago

0.13.0

1 year ago

0.13.1

1 year ago

0.13.2

1 year ago

0.13.3

1 year ago

0.13.4

1 year ago

0.13.5

1 year ago

0.10.0

1 year ago

0.9.0

1 year ago

0.8.0

1 year ago

0.9.1

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.0.0

1 year ago

0.2.1

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.9

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago