@clipboard-health/json-api 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -21
- package/package.json +1 -1
- package/src/lib/toJsonApiQuery.d.ts +3 -3
- package/src/lib/toJsonApiQuery.js +1 -1
- package/src/lib/toJsonApiQuery.js.map +1 -1
- package/src/lib/toSearchParams.d.ts +3 -3
- package/src/lib/toSearchParams.js +17 -7
- package/src/lib/toSearchParams.js.map +1 -1
- package/src/lib/types.d.ts +24 -6
package/README.md
CHANGED
|
@@ -18,50 +18,63 @@ npm install @clipboard-health/json-api
|
|
|
18
18
|
|
|
19
19
|
### Query helpers
|
|
20
20
|
|
|
21
|
-
From the client, call `toSearchParams` to convert from `
|
|
21
|
+
From the client, call `toSearchParams` to convert from `ClientJsonApiQuery` to `URLSearchParams`:
|
|
22
22
|
|
|
23
23
|
<!-- prettier-ignore -->
|
|
24
24
|
```ts
|
|
25
25
|
// ./examples/toSearchParams.ts
|
|
26
26
|
|
|
27
|
+
import { deepEqual } from "node:assert/strict";
|
|
28
|
+
|
|
27
29
|
import { toSearchParams } from "@clipboard-health/json-api";
|
|
28
30
|
|
|
29
|
-
import { type
|
|
31
|
+
import { type ClientJsonApiQuery } from "../src/lib/types";
|
|
30
32
|
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
const isoDate = "2024-01-01T15:00:00.000Z";
|
|
34
|
+
const query: ClientJsonApiQuery = {
|
|
35
|
+
fields: { dog: ["age"] },
|
|
36
|
+
filter: { age: [2], createdAt: { gte: new Date(isoDate) }, isGoodDog: true },
|
|
37
|
+
include: ["owner"],
|
|
38
|
+
page: { size: 10 },
|
|
36
39
|
sort: ["-age"],
|
|
37
40
|
};
|
|
38
41
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
deepEqual(
|
|
43
|
+
toSearchParams(query).toString(),
|
|
44
|
+
new URLSearchParams(
|
|
45
|
+
"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",
|
|
46
|
+
).toString(),
|
|
47
|
+
);
|
|
42
48
|
|
|
43
49
|
```
|
|
44
50
|
|
|
45
|
-
From the server, call `toJsonApiQuery` to convert from `URLSearchParams` to `
|
|
51
|
+
From the server, call `toJsonApiQuery` to convert from `URLSearchParams` to `ServerJsonApiQuery`:
|
|
46
52
|
|
|
47
53
|
<!-- prettier-ignore -->
|
|
48
54
|
```ts
|
|
49
55
|
// ./examples/toJsonApiQuery.ts
|
|
50
56
|
|
|
51
|
-
import {
|
|
57
|
+
import { deepEqual } from "node:assert/strict";
|
|
58
|
+
|
|
59
|
+
import { type ServerJsonApiQuery, toJsonApiQuery } from "@clipboard-health/json-api";
|
|
52
60
|
|
|
61
|
+
const isoDate = "2024-01-01T15:00:00.000Z";
|
|
62
|
+
// The URLSearchParams constructor also supports URL-encoded strings
|
|
53
63
|
const searchParams = new URLSearchParams(
|
|
54
|
-
|
|
64
|
+
`fields[dog]=age&filter[age]=2&filter[createdAt][gte]=${isoDate}&filter[isGoodDog]=true&include=owner&page[size]=10&sort=-age`,
|
|
55
65
|
);
|
|
56
66
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
67
|
+
const query: ServerJsonApiQuery = toJsonApiQuery(searchParams);
|
|
68
|
+
|
|
69
|
+
deepEqual(query, {
|
|
70
|
+
fields: { dog: ["age"] },
|
|
71
|
+
filter: { age: ["2"], createdAt: { gte: isoDate }, isGoodDog: ["true"] },
|
|
72
|
+
include: ["owner"],
|
|
73
|
+
page: {
|
|
74
|
+
size: "10",
|
|
75
|
+
},
|
|
76
|
+
sort: ["-age"],
|
|
77
|
+
});
|
|
65
78
|
|
|
66
79
|
```
|
|
67
80
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ServerJsonApiQuery } from "./types";
|
|
2
2
|
/**
|
|
3
|
-
* Call this function from clients to convert from {@link URLSearchParams} to {@link
|
|
3
|
+
* Call this function from clients to convert from {@link URLSearchParams} to {@link ServerJsonApiQuery}.
|
|
4
4
|
*
|
|
5
5
|
* @see [Example](https://github.com/ClipboardHealth/core-utils/blob/main/packages/json-api/examples/toJsonApiQuery.ts)
|
|
6
6
|
*/
|
|
7
|
-
export declare function toJsonApiQuery(searchParams: URLSearchParams):
|
|
7
|
+
export declare function toJsonApiQuery(searchParams: URLSearchParams): ServerJsonApiQuery;
|
|
@@ -10,7 +10,7 @@ const REGEX = {
|
|
|
10
10
|
sort: /^sort$/i,
|
|
11
11
|
};
|
|
12
12
|
/**
|
|
13
|
-
* Call this function from clients to convert from {@link URLSearchParams} to {@link
|
|
13
|
+
* Call this function from clients to convert from {@link URLSearchParams} to {@link ServerJsonApiQuery}.
|
|
14
14
|
*
|
|
15
15
|
* @see [Example](https://github.com/ClipboardHealth/core-utils/blob/main/packages/json-api/examples/toJsonApiQuery.ts)
|
|
16
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toJsonApiQuery.js","sourceRoot":"","sources":["../../../../../packages/json-api/src/lib/toJsonApiQuery.ts"],"names":[],"mappings":";;AAgBA,wCAgDC;AA9DD,MAAM,KAAK,GAAG;IACZ,MAAM,EAAE,mBAAmB;IAC3B,MAAM,EAAE,uBAAuB;IAC/B,UAAU,EAAE,2BAA2B;IACvC,OAAO,EAAE,YAAY;IACrB,IAAI,EAAE,iBAAiB;IACvB,IAAI,EAAE,SAAS;CACP,CAAC;AAEX;;;;GAIG;AACH,SAAgB,cAAc,CAAC,YAA6B;IAC1D,OAAO,CAAC,GAAG,YAAY,CAAC,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"toJsonApiQuery.js","sourceRoot":"","sources":["../../../../../packages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AAC,GAAG,MAAM,CAAC;YAClC,IAAI,KAAK,EAAE,CAAC;gBACV,OAAO;oBACL,GAAG,WAAW;oBACd,MAAM,EAAE;wBACN,GAAG,WAAW,CAAC,MAAM;wBACrB,CAAC,KAAK,CAAC,EAAE,SAAS;4BAChB,CAAC,CAAC,EAAE,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE;4BACxD,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC;qBACrB;iBACF,CAAC;YACJ,CAAC;QACH,CAAC;QAED,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YAC1C,OAAO,EAAE,GAAG,WAAW,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACtD,CAAC;QAED,IAAI,IAAI,KAAK,MAAM,IAAI,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACnC,OAAO;gBACL,GAAG,WAAW;gBACd,IAAI,EAAE,EAAE,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE;aAClD,CAAC;QACJ,CAAC;QAED,0BAA0B;QAC1B,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { URLSearchParams } from "node:url";
|
|
2
|
-
import { type
|
|
2
|
+
import { type ClientJsonApiQuery } from "./types";
|
|
3
3
|
/**
|
|
4
|
-
* Call this function from clients to convert from {@link
|
|
4
|
+
* Call this function from clients to convert from {@link ClientJsonApiQuery} to {@link URLSearchParams}.
|
|
5
5
|
*
|
|
6
6
|
* @see [Example](https://github.com/ClipboardHealth/core-utils/blob/main/packages/json-api/examples/toSearchParams.ts)
|
|
7
7
|
*/
|
|
8
|
-
export declare function toSearchParams(query:
|
|
8
|
+
export declare function toSearchParams(query: ClientJsonApiQuery): URLSearchParams;
|
|
@@ -2,8 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toSearchParams = toSearchParams;
|
|
4
4
|
const node_url_1 = require("node:url");
|
|
5
|
+
function filterValueString(value) {
|
|
6
|
+
return value instanceof Date ? value.toISOString() : String(value);
|
|
7
|
+
}
|
|
8
|
+
function join(values) {
|
|
9
|
+
return values.join(",");
|
|
10
|
+
}
|
|
5
11
|
/**
|
|
6
|
-
* Call this function from clients to convert from {@link
|
|
12
|
+
* Call this function from clients to convert from {@link ClientJsonApiQuery} to {@link URLSearchParams}.
|
|
7
13
|
*
|
|
8
14
|
* @see [Example](https://github.com/ClipboardHealth/core-utils/blob/main/packages/json-api/examples/toSearchParams.ts)
|
|
9
15
|
*/
|
|
@@ -11,31 +17,35 @@ function toSearchParams(query) {
|
|
|
11
17
|
const searchParams = new node_url_1.URLSearchParams();
|
|
12
18
|
if (query.fields) {
|
|
13
19
|
Object.entries(query.fields).forEach(([type, fields]) => {
|
|
14
|
-
searchParams.append(`fields[${type}]`,
|
|
20
|
+
searchParams.append(`fields[${type}]`, join(fields));
|
|
15
21
|
});
|
|
16
22
|
}
|
|
17
23
|
if (query.filter) {
|
|
18
24
|
Object.entries(query.filter).forEach(([field, values]) => {
|
|
25
|
+
const filterField = `filter[${field}]`;
|
|
19
26
|
if (Array.isArray(values)) {
|
|
20
|
-
searchParams.append(
|
|
27
|
+
searchParams.append(filterField, join(values.map((value) => filterValueString(value))));
|
|
28
|
+
}
|
|
29
|
+
else if (typeof values === "boolean") {
|
|
30
|
+
searchParams.append(filterField, String(values));
|
|
21
31
|
}
|
|
22
32
|
else if (typeof values === "object") {
|
|
23
33
|
Object.entries(values).forEach(([fieldType, value]) => {
|
|
24
|
-
searchParams.append(
|
|
34
|
+
searchParams.append(`${filterField}[${fieldType}]`, filterValueString(value));
|
|
25
35
|
});
|
|
26
36
|
}
|
|
27
37
|
});
|
|
28
38
|
}
|
|
29
39
|
if (query.include) {
|
|
30
|
-
searchParams.append("include", query.include
|
|
40
|
+
searchParams.append("include", join(query.include));
|
|
31
41
|
}
|
|
32
42
|
if (query.page) {
|
|
33
43
|
Object.entries(query.page).forEach(([key, value]) => {
|
|
34
|
-
searchParams.append(`page[${key}]`, value);
|
|
44
|
+
searchParams.append(`page[${key}]`, String(value));
|
|
35
45
|
});
|
|
36
46
|
}
|
|
37
47
|
if (query.sort) {
|
|
38
|
-
searchParams.append("sort", query.sort
|
|
48
|
+
searchParams.append("sort", join(query.sort));
|
|
39
49
|
}
|
|
40
50
|
return searchParams;
|
|
41
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toSearchParams.js","sourceRoot":"","sources":["../../../../../packages/json-api/src/lib/toSearchParams.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"toSearchParams.js","sourceRoot":"","sources":["../../../../../packages/json-api/src/lib/toSearchParams.ts"],"names":[],"mappings":";;AAiBA,wCAuCC;AAxDD,uCAA2C;AAI3C,SAAS,iBAAiB,CAAC,KAAqC;IAC9D,OAAO,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACrE,CAAC;AAED,SAAS,IAAI,CAAC,MAAgB;IAC5B,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC1B,CAAC;AAED;;;;GAIG;AACH,SAAgB,cAAc,CAAC,KAAyB;IACtD,MAAM,YAAY,GAAG,IAAI,0BAAe,EAAE,CAAC;IAE3C,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;QACjB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE;YACtD,YAAY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AC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YAClD,YAAY,CAAC,MAAM,CAAC,QAAQ,GAAG,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC;QACf,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
package/src/lib/types.d.ts
CHANGED
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Filter
|
|
2
|
+
* Filter types to build complex filter queries.
|
|
3
3
|
* - gt: Greater than
|
|
4
4
|
* - gte: Greater than or equal to
|
|
5
5
|
* - lt: Less than
|
|
6
6
|
* - lte: Less than or equal to
|
|
7
7
|
* - not: Not equal to
|
|
8
8
|
*/
|
|
9
|
-
type
|
|
9
|
+
type FilterType = "gt" | "gte" | "lt" | "lte" | "not";
|
|
10
|
+
type PageKey = "cursor" | "limit" | "number" | "offset" | "size";
|
|
11
|
+
interface JsonApiQueryTypes {
|
|
12
|
+
filterValue: unknown;
|
|
13
|
+
filterTypeValue: unknown;
|
|
14
|
+
pageValue: unknown;
|
|
15
|
+
}
|
|
16
|
+
export interface ClientTypes {
|
|
17
|
+
filterValue: Array<Date | number | string> | boolean;
|
|
18
|
+
filterTypeValue: Date | number | string;
|
|
19
|
+
pageValue: number | string;
|
|
20
|
+
}
|
|
21
|
+
interface ServerTypes {
|
|
22
|
+
filterValue: string[];
|
|
23
|
+
filterTypeValue: string;
|
|
24
|
+
pageValue: string;
|
|
25
|
+
}
|
|
10
26
|
/**
|
|
11
27
|
* A JSON:API URL query string.
|
|
12
28
|
*/
|
|
13
|
-
|
|
29
|
+
interface JsonApiQuery<T extends JsonApiQueryTypes> {
|
|
14
30
|
/**
|
|
15
31
|
* Fields to include in the response.
|
|
16
32
|
*
|
|
@@ -23,8 +39,8 @@ export interface JsonApiQuery {
|
|
|
23
39
|
* @see {@link https://jsonapi.org/recommendations/#filtering Filtering}
|
|
24
40
|
* @see {@link https://discuss.jsonapi.org/t/share-propose-a-filtering-strategy/257 Filtering strategy}
|
|
25
41
|
*/
|
|
26
|
-
filter?: Record<string,
|
|
27
|
-
[K in
|
|
42
|
+
filter?: Record<string, T["filterValue"] | {
|
|
43
|
+
[K in FilterType]?: T["filterTypeValue"];
|
|
28
44
|
}>;
|
|
29
45
|
/**
|
|
30
46
|
* Relationships to include in the response.
|
|
@@ -39,7 +55,7 @@ export interface JsonApiQuery {
|
|
|
39
55
|
* @see {@link https://jsonapi.org/examples/#pagination Pagination examples}
|
|
40
56
|
*/
|
|
41
57
|
page?: {
|
|
42
|
-
[K in
|
|
58
|
+
[K in PageKey]?: T["pageValue"];
|
|
43
59
|
};
|
|
44
60
|
/**
|
|
45
61
|
* Sorting data. Include the "-" prefix for descending order.
|
|
@@ -48,4 +64,6 @@ export interface JsonApiQuery {
|
|
|
48
64
|
*/
|
|
49
65
|
sort?: string[];
|
|
50
66
|
}
|
|
67
|
+
export type ClientJsonApiQuery = JsonApiQuery<ClientTypes>;
|
|
68
|
+
export type ServerJsonApiQuery = JsonApiQuery<ServerTypes>;
|
|
51
69
|
export {};
|