@23blocks/jsonapi-codec 3.0.1 → 3.1.1
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/dist/index.esm.js
CHANGED
|
@@ -77,7 +77,9 @@ import { BlockErrorException } from '@23blocks/contracts';
|
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* Decode a single resource from a JSON:API document
|
|
80
|
-
|
|
80
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
81
|
+
*/ function decodeOne(response, mapper) {
|
|
82
|
+
const document = response;
|
|
81
83
|
if (!isSingleResourceDocument(document)) {
|
|
82
84
|
throw new Error('Expected single resource document');
|
|
83
85
|
}
|
|
@@ -86,7 +88,9 @@ import { BlockErrorException } from '@23blocks/contracts';
|
|
|
86
88
|
}
|
|
87
89
|
/**
|
|
88
90
|
* Decode a single resource from a JSON:API document, returning null if not found
|
|
89
|
-
|
|
91
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
92
|
+
*/ function decodeOneOrNull(response, mapper) {
|
|
93
|
+
const document = response;
|
|
90
94
|
if (!document.data || Array.isArray(document.data)) {
|
|
91
95
|
return null;
|
|
92
96
|
}
|
|
@@ -95,7 +99,9 @@ import { BlockErrorException } from '@23blocks/contracts';
|
|
|
95
99
|
}
|
|
96
100
|
/**
|
|
97
101
|
* Decode multiple resources from a JSON:API document
|
|
98
|
-
|
|
102
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
103
|
+
*/ function decodeMany(response, mapper) {
|
|
104
|
+
const document = response;
|
|
99
105
|
if (!isCollectionDocument(document)) {
|
|
100
106
|
throw new Error('Expected collection document');
|
|
101
107
|
}
|
|
@@ -115,7 +121,9 @@ import { BlockErrorException } from '@23blocks/contracts';
|
|
|
115
121
|
}
|
|
116
122
|
/**
|
|
117
123
|
* Decode a paginated collection from a JSON:API document
|
|
118
|
-
|
|
124
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
125
|
+
*/ function decodePageResult(response, mapper) {
|
|
126
|
+
const document = response;
|
|
119
127
|
const data = isCollectionDocument(document) ? decodeMany(document, mapper) : [];
|
|
120
128
|
return {
|
|
121
129
|
data,
|
|
@@ -124,7 +132,9 @@ import { BlockErrorException } from '@23blocks/contracts';
|
|
|
124
132
|
}
|
|
125
133
|
/**
|
|
126
134
|
* Decode with a custom decoder function
|
|
127
|
-
|
|
135
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
136
|
+
*/ function decodeWith(response, decoder) {
|
|
137
|
+
const document = response;
|
|
128
138
|
const included = buildIncludedMap(document.included);
|
|
129
139
|
return decoder(document, included);
|
|
130
140
|
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { type JsonApiResource, type JsonApiRelationship, type JsonApiResourceIdentifier, type JsonApiLinks, type JsonApiLink, type JsonApiError, type JsonApiDocument, type JsonApiMeta, isSingleResourceDocument, isCollectionDocument, isErrorDocument, } from './lib/types.js';
|
|
2
|
-
export { type IncludedMap, type ResourceMapper, resourceKey, buildIncludedMap, getIncluded, resolveRelationship, resolveRelationshipMany, } from './lib/mapper.js';
|
|
2
|
+
export { type IncludedMap, type ResourceMapper, type ResourceMapper as JsonApiMapper, resourceKey, buildIncludedMap, getIncluded, resolveRelationship, resolveRelationshipMany, } from './lib/mapper.js';
|
|
3
3
|
export { decodeOne, decodeOneOrNull, decodeMany, extractPageMeta, decodePageResult, decodeWith, } from './lib/decode.js';
|
|
4
4
|
export { jsonApiErrorToBlockError, jsonApiErrorsToBlockError, blockErrorFromJsonApi, throwIfError, assertNotError, } from './lib/errors.js';
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,yBAAyB,EAC9B,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,wBAAwB,EACxB,oBAAoB,EACpB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,yBAAyB,EAC9B,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,wBAAwB,EACxB,oBAAoB,EACpB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,cAAc,EAEnB,KAAK,cAAc,IAAI,aAAa,EACpC,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,SAAS,EACT,eAAe,EACf,UAAU,EACV,eAAe,EACf,gBAAgB,EAChB,UAAU,GACX,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,wBAAwB,EACxB,yBAAyB,EACzB,qBAAqB,EACrB,YAAY,EACZ,cAAc,GACf,MAAM,iBAAiB,CAAC"}
|
package/dist/src/lib/decode.d.ts
CHANGED
|
@@ -3,26 +3,31 @@ import type { JsonApiDocument, JsonApiMeta } from './types.js';
|
|
|
3
3
|
import type { ResourceMapper, IncludedMap } from './mapper.js';
|
|
4
4
|
/**
|
|
5
5
|
* Decode a single resource from a JSON:API document
|
|
6
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
6
7
|
*/
|
|
7
|
-
export declare function decodeOne<T>(
|
|
8
|
+
export declare function decodeOne<T>(response: unknown, mapper: ResourceMapper<T>): T;
|
|
8
9
|
/**
|
|
9
10
|
* Decode a single resource from a JSON:API document, returning null if not found
|
|
11
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
10
12
|
*/
|
|
11
|
-
export declare function decodeOneOrNull<T>(
|
|
13
|
+
export declare function decodeOneOrNull<T>(response: unknown, mapper: ResourceMapper<T>): T | null;
|
|
12
14
|
/**
|
|
13
15
|
* Decode multiple resources from a JSON:API document
|
|
16
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
14
17
|
*/
|
|
15
|
-
export declare function decodeMany<T>(
|
|
18
|
+
export declare function decodeMany<T>(response: unknown, mapper: ResourceMapper<T>): T[];
|
|
16
19
|
/**
|
|
17
20
|
* Extract pagination metadata from JSON:API meta
|
|
18
21
|
*/
|
|
19
22
|
export declare function extractPageMeta(meta?: JsonApiMeta): PageMeta;
|
|
20
23
|
/**
|
|
21
24
|
* Decode a paginated collection from a JSON:API document
|
|
25
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
22
26
|
*/
|
|
23
|
-
export declare function decodePageResult<T>(
|
|
27
|
+
export declare function decodePageResult<T>(response: unknown, mapper: ResourceMapper<T>): PageResult<T>;
|
|
24
28
|
/**
|
|
25
29
|
* Decode with a custom decoder function
|
|
30
|
+
* Accepts unknown to handle transport layer responses that return unknown
|
|
26
31
|
*/
|
|
27
|
-
export declare function decodeWith<T>(
|
|
32
|
+
export declare function decodeWith<T>(response: unknown, decoder: (doc: JsonApiDocument, included: IncludedMap) => T): T;
|
|
28
33
|
//# sourceMappingURL=decode.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../src/lib/decode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG/D
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../src/lib/decode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG/D;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,QAAQ,EAAE,OAAO,EACjB,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GACxB,CAAC,CAQH;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAC/B,QAAQ,EAAE,OAAO,EACjB,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GACxB,CAAC,GAAG,IAAI,CAQV;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,QAAQ,EAAE,OAAO,EACjB,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GACxB,CAAC,EAAE,CAQL;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,IAAI,CAAC,EAAE,WAAW,GAAG,QAAQ,CAO5D;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,QAAQ,EAAE,OAAO,EACjB,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,GACxB,UAAU,CAAC,CAAC,CAAC,CAUf;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,CAAC,GAAG,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,KAAK,CAAC,GAC1D,CAAC,CAIH"}
|
package/dist/src/lib/types.d.ts
CHANGED
|
@@ -7,8 +7,8 @@ export interface JsonApiResource<TAttributes = Record<string, unknown>> {
|
|
|
7
7
|
type: string;
|
|
8
8
|
/** Resource ID */
|
|
9
9
|
id: string;
|
|
10
|
-
/** Resource attributes */
|
|
11
|
-
attributes
|
|
10
|
+
/** Resource attributes (defaults to empty object if not provided) */
|
|
11
|
+
attributes: TAttributes;
|
|
12
12
|
/** Resource relationships */
|
|
13
13
|
relationships?: Record<string, JsonApiRelationship>;
|
|
14
14
|
/** Resource links */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,eAAe,CAAC,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACpE,oBAAoB;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,kBAAkB;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,eAAe,CAAC,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACpE,oBAAoB;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,kBAAkB;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,qEAAqE;IACrE,UAAU,EAAE,WAAW,CAAC;IACxB,6BAA6B;IAC7B,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;IACpD,qBAAqB;IACrB,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB,oBAAoB;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,wBAAwB;IACxB,IAAI,EAAE,yBAAyB,GAAG,yBAAyB,EAAE,GAAG,IAAI,CAAC;IACrE,yBAAyB;IACzB,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB,wBAAwB;IACxB,IAAI,CAA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