@clipboard-health/json-api 0.14.0 → 0.15.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 +2 -0
- package/package.json +1 -1
- package/src/lib/query/toClientSearchParams.d.ts +31 -2
- package/src/lib/query/toClientSearchParams.js +31 -2
- package/src/lib/query/toClientSearchParams.js.map +1 -1
- package/src/lib/query/toServerJsonApiQuery.d.ts +29 -2
- package/src/lib/query/toServerJsonApiQuery.js +29 -2
- package/src/lib/query/toServerJsonApiQuery.js.map +1 -1
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@ From the client, call `toClientSearchParams` to convert from `ClientJsonApiQuery
|
|
|
25
25
|
```ts
|
|
26
26
|
// ./examples/toClientSearchParams.ts
|
|
27
27
|
|
|
28
|
+
// packages/json-api/src/lib/query/toClientSearchParams.ts
|
|
28
29
|
import { deepEqual } from "node:assert/strict";
|
|
29
30
|
|
|
30
31
|
import { toClientSearchParams } from "@clipboard-health/json-api";
|
|
@@ -61,6 +62,7 @@ From the server, call `toServerJsonApiQuery` to convert from `URLSearchParams` t
|
|
|
61
62
|
```ts
|
|
62
63
|
// ./examples/toServerJsonApiQuery.ts
|
|
63
64
|
|
|
65
|
+
// packages/json-api/src/lib/query/toServerJsonApiQuery.ts
|
|
64
66
|
import { deepEqual } from "node:assert/strict";
|
|
65
67
|
|
|
66
68
|
import { type ServerJsonApiQuery, toServerJsonApiQuery } from "@clipboard-health/json-api";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/json-api",
|
|
3
3
|
"description": "TypeScript-friendly utilities for adhering to the JSON:API specification.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.15.0",
|
|
5
5
|
"bugs": "https://github.com/clipboardhealth/core-utils/issues",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"tslib": "2.8.0"
|
|
@@ -2,8 +2,37 @@ import { type ClientJsonApiQuery } from "../types";
|
|
|
2
2
|
/**
|
|
3
3
|
* Call this function from clients to convert from {@link ClientJsonApiQuery} to {@link URLSearchParams}.
|
|
4
4
|
*
|
|
5
|
-
* @
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* // packages/json-api/examples/toClientSearchParams.ts
|
|
8
|
+
* import { deepEqual } from "node:assert/strict";
|
|
6
9
|
*
|
|
7
|
-
*
|
|
10
|
+
* import { toClientSearchParams } from "@clipboard-health/json-api";
|
|
11
|
+
*
|
|
12
|
+
* import { type ClientJsonApiQuery } from "../src/lib/types";
|
|
13
|
+
*
|
|
14
|
+
* const [date1, date2] = ["2024-01-01", "2024-01-02"];
|
|
15
|
+
* const query: ClientJsonApiQuery = {
|
|
16
|
+
* fields: { user: ["age", "dateOfBirth"] },
|
|
17
|
+
* filter: {
|
|
18
|
+
* age: { eq: ["2"] },
|
|
19
|
+
* dateOfBirth: { gt: [date1], lt: [date2] },
|
|
20
|
+
* isActive: { eq: ["true"] },
|
|
21
|
+
* },
|
|
22
|
+
* include: ["article"],
|
|
23
|
+
* page: {
|
|
24
|
+
* size: "10",
|
|
25
|
+
* },
|
|
26
|
+
* sort: ["-age"],
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* deepEqual(
|
|
30
|
+
* toClientSearchParams(query).toString(),
|
|
31
|
+
* new URLSearchParams(
|
|
32
|
+
* `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`,
|
|
33
|
+
* ).toString(),
|
|
34
|
+
* );
|
|
35
|
+
*
|
|
36
|
+
* ```
|
|
8
37
|
*/
|
|
9
38
|
export declare function toClientSearchParams(query: ClientJsonApiQuery): URLSearchParams;
|
|
@@ -10,9 +10,38 @@ function join(values) {
|
|
|
10
10
|
/**
|
|
11
11
|
* Call this function from clients to convert from {@link ClientJsonApiQuery} to {@link URLSearchParams}.
|
|
12
12
|
*
|
|
13
|
-
* @
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // packages/json-api/examples/toClientSearchParams.ts
|
|
16
|
+
* import { deepEqual } from "node:assert/strict";
|
|
14
17
|
*
|
|
15
|
-
*
|
|
18
|
+
* import { toClientSearchParams } from "@clipboard-health/json-api";
|
|
19
|
+
*
|
|
20
|
+
* import { type ClientJsonApiQuery } from "../src/lib/types";
|
|
21
|
+
*
|
|
22
|
+
* const [date1, date2] = ["2024-01-01", "2024-01-02"];
|
|
23
|
+
* const query: ClientJsonApiQuery = {
|
|
24
|
+
* fields: { user: ["age", "dateOfBirth"] },
|
|
25
|
+
* filter: {
|
|
26
|
+
* age: { eq: ["2"] },
|
|
27
|
+
* dateOfBirth: { gt: [date1], lt: [date2] },
|
|
28
|
+
* isActive: { eq: ["true"] },
|
|
29
|
+
* },
|
|
30
|
+
* include: ["article"],
|
|
31
|
+
* page: {
|
|
32
|
+
* size: "10",
|
|
33
|
+
* },
|
|
34
|
+
* sort: ["-age"],
|
|
35
|
+
* };
|
|
36
|
+
*
|
|
37
|
+
* deepEqual(
|
|
38
|
+
* toClientSearchParams(query).toString(),
|
|
39
|
+
* new URLSearchParams(
|
|
40
|
+
* `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`,
|
|
41
|
+
* ).toString(),
|
|
42
|
+
* );
|
|
43
|
+
*
|
|
44
|
+
* ```
|
|
16
45
|
*/
|
|
17
46
|
function toClientSearchParams(query) {
|
|
18
47
|
const searchParams = new URLSearchParams();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toClientSearchParams.js","sourceRoot":"","sources":["../../../../../../packages/json-api/src/lib/query/toClientSearchParams.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"toClientSearchParams.js","sourceRoot":"","sources":["../../../../../../packages/json-api/src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AAK,EAAE,EAAE,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC,CACrD,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;QAClB,YAAY,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IACtD,CAAC;IAED,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC;QACf,MAAM,CAAC,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"}
|
|
@@ -2,8 +2,35 @@ import { type ServerJsonApiQuery } from "../types";
|
|
|
2
2
|
/**
|
|
3
3
|
* Call this function from servers to convert from {@link URLSearchParams} to {@link ServerJsonApiQuery}.
|
|
4
4
|
*
|
|
5
|
-
* @
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* // packages/json-api/examples/toServerJsonApiQuery.ts
|
|
8
|
+
* import { deepEqual } from "node:assert/strict";
|
|
6
9
|
*
|
|
7
|
-
*
|
|
10
|
+
* import { type ServerJsonApiQuery, toServerJsonApiQuery } from "@clipboard-health/json-api";
|
|
11
|
+
*
|
|
12
|
+
* const [date1, date2] = ["2024-01-01", "2024-01-02"];
|
|
13
|
+
* // The URLSearchParams constructor also supports URL-encoded strings
|
|
14
|
+
* const searchParams = new URLSearchParams(
|
|
15
|
+
* `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`,
|
|
16
|
+
* );
|
|
17
|
+
*
|
|
18
|
+
* const query: ServerJsonApiQuery = toServerJsonApiQuery(searchParams);
|
|
19
|
+
*
|
|
20
|
+
* deepEqual(query, {
|
|
21
|
+
* fields: { user: ["age", "dateOfBirth"] },
|
|
22
|
+
* filter: {
|
|
23
|
+
* age: { eq: ["2"] },
|
|
24
|
+
* dateOfBirth: { gt: [date1], lt: [date2] },
|
|
25
|
+
* isActive: { eq: ["true"] },
|
|
26
|
+
* },
|
|
27
|
+
* include: ["article"],
|
|
28
|
+
* page: {
|
|
29
|
+
* size: "10",
|
|
30
|
+
* },
|
|
31
|
+
* sort: ["-age"],
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* ```
|
|
8
35
|
*/
|
|
9
36
|
export declare function toServerJsonApiQuery(searchParams: URLSearchParams): ServerJsonApiQuery;
|
|
@@ -12,9 +12,36 @@ const REGEX = {
|
|
|
12
12
|
/**
|
|
13
13
|
* Call this function from servers to convert from {@link URLSearchParams} to {@link ServerJsonApiQuery}.
|
|
14
14
|
*
|
|
15
|
-
* @
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* // packages/json-api/examples/toServerJsonApiQuery.ts
|
|
18
|
+
* import { deepEqual } from "node:assert/strict";
|
|
16
19
|
*
|
|
17
|
-
*
|
|
20
|
+
* import { type ServerJsonApiQuery, toServerJsonApiQuery } from "@clipboard-health/json-api";
|
|
21
|
+
*
|
|
22
|
+
* const [date1, date2] = ["2024-01-01", "2024-01-02"];
|
|
23
|
+
* // The URLSearchParams constructor also supports URL-encoded strings
|
|
24
|
+
* const searchParams = new URLSearchParams(
|
|
25
|
+
* `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`,
|
|
26
|
+
* );
|
|
27
|
+
*
|
|
28
|
+
* const query: ServerJsonApiQuery = toServerJsonApiQuery(searchParams);
|
|
29
|
+
*
|
|
30
|
+
* deepEqual(query, {
|
|
31
|
+
* fields: { user: ["age", "dateOfBirth"] },
|
|
32
|
+
* filter: {
|
|
33
|
+
* age: { eq: ["2"] },
|
|
34
|
+
* dateOfBirth: { gt: [date1], lt: [date2] },
|
|
35
|
+
* isActive: { eq: ["true"] },
|
|
36
|
+
* },
|
|
37
|
+
* include: ["article"],
|
|
38
|
+
* page: {
|
|
39
|
+
* size: "10",
|
|
40
|
+
* },
|
|
41
|
+
* sort: ["-age"],
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* ```
|
|
18
45
|
*/
|
|
19
46
|
function toServerJsonApiQuery(searchParams) {
|
|
20
47
|
return [...searchParams].reduce((accumulator, [key, value]) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toServerJsonApiQuery.js","sourceRoot":"","sources":["../../../../../../packages/json-api/src/lib/query/toServerJsonApiQuery.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"toServerJsonApiQuery.js","sourceRoot":"","sources":["../../../../../../packages/json-api/src/lib/query/toServerJsonApiQuery.ts"],"names":[],"mappings":";;AA6CA,oDAiDC;AA5FD,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,SAAgB,oBAAoB,CAAC,YAA6B;IAChE,OAAO,CAAC,GAAG,YAAY,CAAC,CAAC,MAAM,CAAqB,CAAC,WAAW,EAAE,CAAC,GAAG,EAAE,K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