@clipboard-health/json-api v0.21.0
@clipboard-health/json-api
TypeScript-friendly utilities for adhering to the JSON:API specification.
Table of contents
Install
npm install @clipboard-health/json-apiUsage
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.
5 months ago
6 months ago
11 months ago
6 months ago
9 months ago
6 months ago
5 months ago
11 months ago
11 months ago
11 months ago
11 months ago
5 months ago
8 months ago
8 months ago
8 months ago
7 months ago
12 months ago
12 months ago
11 months ago
8 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago