@clipboard-health/json-api
Advanced tools
Comparing version 0.2.9 to 0.3.0
{ | ||
"name": "@clipboard-health/json-api", | ||
"description": "Utilities for adhering to the JSON:API specification.", | ||
"version": "0.2.9", | ||
"version": "0.3.0", | ||
"bugs": "https://github.com/clipboardhealth/core-utils/issues", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
@@ -9,2 +9,3 @@ # @clipboard-health/json-api | ||
- [Usage](#usage) | ||
- [Query helpers](#query-helpers) | ||
- [Local development commands](#local-development-commands) | ||
@@ -22,20 +23,26 @@ | ||
From the client, call `toSearchParams` to convert from `ClientJsonApiQuery` to `URLSearchParams`: | ||
From the client, call `toClientSearchParams` to convert from `ClientJsonApiQuery` to `URLSearchParams`: | ||
<!-- prettier-ignore --> | ||
```ts | ||
// ./examples/toSearchParams.ts | ||
// ./examples/toClientSearchParams.ts | ||
import { deepEqual } from "node:assert/strict"; | ||
import { toSearchParams } from "@clipboard-health/json-api"; | ||
import { toClientSearchParams } from "@clipboard-health/json-api"; | ||
import { type ClientJsonApiQuery } from "../src/lib/types"; | ||
const isoDate = "2024-01-01T15:00:00.000Z"; | ||
const [date1, date2] = ["2024-01-01", "2024-01-02"]; | ||
const query: ClientJsonApiQuery = { | ||
fields: { dog: ["age"] }, | ||
filter: { age: [2], createdAt: { gte: new Date(isoDate) }, isGoodDog: true }, | ||
fields: { dog: ["name", "age"] }, | ||
filter: { | ||
age: { eq: ["2"] }, | ||
createdAt: { gt: [date1], lt: [date2] }, | ||
isGoodDog: { eq: ["true"] }, | ||
}, | ||
include: ["owner"], | ||
page: { size: 10 }, | ||
page: { | ||
size: "10", | ||
}, | ||
sort: ["-age"], | ||
@@ -45,5 +52,5 @@ }; | ||
deepEqual( | ||
toSearchParams(query).toString(), | ||
toClientSearchParams(query).toString(), | ||
new URLSearchParams( | ||
"fields[dog]=age&filter[age]=2&filter[createdAt][gte]=2024-01-01T15:00:00.000Z&filter[isGoodDog]=true&include=owner&page[size]=10&sort=-age", | ||
`fields[dog]=name,age&filter[age]=2&filter[createdAt][gt]=${date1}&filter[createdAt][lt]=${date2}&filter[isGoodDog]=true&include=owner&page[size]=10&sort=-age`, | ||
).toString(), | ||
@@ -54,23 +61,27 @@ ); | ||
From the server, call `toJsonApiQuery` to convert from `URLSearchParams` to `ServerJsonApiQuery`: | ||
From the server, call `toServerJsonApiQuery` to convert from `URLSearchParams` to `ServerJsonApiQuery`: | ||
<!-- prettier-ignore --> | ||
```ts | ||
// ./examples/toJsonApiQuery.ts | ||
// ./examples/toServerJsonApiQuery.ts | ||
import { deepEqual } from "node:assert/strict"; | ||
import { type ServerJsonApiQuery, toJsonApiQuery } from "@clipboard-health/json-api"; | ||
import { type ServerJsonApiQuery, toServerJsonApiQuery } from "@clipboard-health/json-api"; | ||
const isoDate = "2024-01-01T15:00:00.000Z"; | ||
const [date1, date2] = ["2024-01-01", "2024-01-02"]; | ||
// The URLSearchParams constructor also supports URL-encoded strings | ||
const searchParams = new URLSearchParams( | ||
`fields[dog]=age&filter[age]=2&filter[createdAt][gte]=${isoDate}&filter[isGoodDog]=true&include=owner&page[size]=10&sort=-age`, | ||
`fields[dog]=name,age&filter[age]=2&filter[createdAt][gt]=${date1}&filter[createdAt][lt]=${date2}&filter[isGoodDog]=true&include=owner&page[size]=10&sort=-age`, | ||
); | ||
const query: ServerJsonApiQuery = toJsonApiQuery(searchParams); | ||
const query: ServerJsonApiQuery = toServerJsonApiQuery(searchParams); | ||
deepEqual(query, { | ||
fields: { dog: ["age"] }, | ||
filter: { age: ["2"], createdAt: { gte: isoDate }, isGoodDog: ["true"] }, | ||
fields: { dog: ["name", "age"] }, | ||
filter: { | ||
age: { eq: ["2"] }, | ||
createdAt: { gt: [date1], lt: [date2] }, | ||
isGoodDog: { eq: ["true"] }, | ||
}, | ||
include: ["owner"], | ||
@@ -77,0 +88,0 @@ page: { |
@@ -1,3 +0,3 @@ | ||
export * from "./lib/toJsonApiQuery"; | ||
export * from "./lib/toSearchParams"; | ||
export * from "./lib/query/toClientSearchParams"; | ||
export * from "./lib/query/toServerJsonApiQuery"; | ||
export * from "./lib/types"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./lib/toJsonApiQuery"), exports); | ||
tslib_1.__exportStar(require("./lib/toSearchParams"), exports); | ||
tslib_1.__exportStar(require("./lib/query/toClientSearchParams"), exports); | ||
tslib_1.__exportStar(require("./lib/query/toServerJsonApiQuery"), exports); | ||
tslib_1.__exportStar(require("./lib/types"), exports); | ||
//# sourceMappingURL=index.js.map |
/** | ||
* Filter types to build complex filter queries. | ||
* - gt: Greater than | ||
* - gte: Greater than or equal to | ||
* - lt: Less than | ||
* - lte: Less than or equal to | ||
* - not: Not equal to | ||
* - eq: Equal to; the default when no filter type is provided, do not explicitly include it. | ||
* - ne: Not equal to. | ||
* - gt: Greater than. | ||
* - gte: Greater than or equal to. | ||
* - lt: Less than. | ||
* - lte: Less than or equal to. | ||
* - not: Not. | ||
*/ | ||
type FilterType = "gt" | "gte" | "lt" | "lte" | "not"; | ||
type FilterType = "eq" | "ne" | "gt" | "gte" | "lt" | "lte" | "not"; | ||
type PageKey = "cursor" | "limit" | "number" | "offset" | "size"; | ||
interface JsonApiQueryTypes { | ||
filterValue: unknown; | ||
filterTypeValue: unknown; | ||
pageValue: unknown; | ||
} | ||
export interface ClientTypes { | ||
filterValue: Array<Date | number | string> | boolean; | ||
filterTypeValue: Date | number | string; | ||
filterValue: Array<Date | number | string | boolean>; | ||
pageValue: number | string; | ||
@@ -23,3 +23,2 @@ } | ||
filterValue: string[]; | ||
filterTypeValue: string; | ||
pageValue: string; | ||
@@ -43,4 +42,4 @@ } | ||
*/ | ||
filter?: Record<string, T["filterValue"] | { | ||
[K in FilterType]?: T["filterTypeValue"]; | ||
filter?: Record<string, { | ||
[K in FilterType]?: T["filterValue"]; | ||
}>; | ||
@@ -47,0 +46,0 @@ /** |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
95
14276
196