@jphil/bookwhen-client 0.4.0 → 0.4.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.d.ts +28 -0
- package/dist/index.js +53 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -217,6 +217,34 @@ declare interface JsonApiResponse<T> {
|
|
|
217
217
|
};
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Generic JSON:API relationship resolver utility
|
|
222
|
+
*
|
|
223
|
+
* This utility resolves relationships between data and included arrays in JSON:API responses.
|
|
224
|
+
* It matches resources by both id and type to ensure correct resolution.
|
|
225
|
+
*/
|
|
226
|
+
/**
|
|
227
|
+
* Resolves relationships in a JSON:API response by matching data references with included resources.
|
|
228
|
+
*
|
|
229
|
+
* @param data - The main data array from the JSON:API response
|
|
230
|
+
* @param included - The included resources array from the JSON:API response
|
|
231
|
+
* @returns A new array with resolved relationships
|
|
232
|
+
*/
|
|
233
|
+
export declare function resolveJsonApiRelationships<T extends {
|
|
234
|
+
relationships?: Record<string, any>;
|
|
235
|
+
}>(data: T[], included: any[]): T[];
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Resolves a single JSON:API resource with its relationships
|
|
239
|
+
*
|
|
240
|
+
* @param data - The main data object from the JSON:API response
|
|
241
|
+
* @param included - The included resources array from the JSON:API response
|
|
242
|
+
* @returns A new object with resolved relationships
|
|
243
|
+
*/
|
|
244
|
+
export declare function resolveJsonApiResource<T extends {
|
|
245
|
+
relationships?: Record<string, any>;
|
|
246
|
+
}>(data: T, included: any[]): T;
|
|
247
|
+
|
|
220
248
|
export declare type Resource = string;
|
|
221
249
|
|
|
222
250
|
export declare type Resources = Resource[];
|
package/dist/index.js
CHANGED
|
@@ -6569,7 +6569,59 @@ function createBookwhenClient(options) {
|
|
|
6569
6569
|
}
|
|
6570
6570
|
return new BookwhenClient(axiosInstance);
|
|
6571
6571
|
}
|
|
6572
|
+
function resolveJsonApiRelationships(data, included) {
|
|
6573
|
+
return data.map((item) => {
|
|
6574
|
+
const resolved = { ...item };
|
|
6575
|
+
Object.keys(resolved.relationships || {}).forEach((relationKey) => {
|
|
6576
|
+
const relation = resolved.relationships[relationKey];
|
|
6577
|
+
if (relation.data) {
|
|
6578
|
+
if (relation.data.id && relation.data.type) {
|
|
6579
|
+
const resolvedData = included.find(
|
|
6580
|
+
(inc) => inc.id === relation.data.id && inc.type === relation.data.type
|
|
6581
|
+
);
|
|
6582
|
+
if (resolvedData) {
|
|
6583
|
+
resolved.relationships[relationKey].data = resolvedData;
|
|
6584
|
+
}
|
|
6585
|
+
} else if (Array.isArray(relation.data)) {
|
|
6586
|
+
resolved.relationships[relationKey].data = relation.data.map((ref) => {
|
|
6587
|
+
const resolvedItem = included.find(
|
|
6588
|
+
(inc) => inc.id === ref.id && inc.type === ref.type
|
|
6589
|
+
);
|
|
6590
|
+
return resolvedItem || ref;
|
|
6591
|
+
});
|
|
6592
|
+
}
|
|
6593
|
+
}
|
|
6594
|
+
});
|
|
6595
|
+
return resolved;
|
|
6596
|
+
});
|
|
6597
|
+
}
|
|
6598
|
+
function resolveJsonApiResource(data, included) {
|
|
6599
|
+
const resolved = { ...data };
|
|
6600
|
+
Object.keys(resolved.relationships || {}).forEach((relationKey) => {
|
|
6601
|
+
const relation = resolved.relationships[relationKey];
|
|
6602
|
+
if (relation.data) {
|
|
6603
|
+
if (relation.data.id && relation.data.type) {
|
|
6604
|
+
const resolvedData = included.find(
|
|
6605
|
+
(inc) => inc.id === relation.data.id && inc.type === relation.data.type
|
|
6606
|
+
);
|
|
6607
|
+
if (resolvedData) {
|
|
6608
|
+
resolved.relationships[relationKey].data = resolvedData;
|
|
6609
|
+
}
|
|
6610
|
+
} else if (Array.isArray(relation.data)) {
|
|
6611
|
+
resolved.relationships[relationKey].data = relation.data.map((ref) => {
|
|
6612
|
+
const resolvedItem = included.find(
|
|
6613
|
+
(inc) => inc.id === ref.id && inc.type === ref.type
|
|
6614
|
+
);
|
|
6615
|
+
return resolvedItem || ref;
|
|
6616
|
+
});
|
|
6617
|
+
}
|
|
6618
|
+
}
|
|
6619
|
+
});
|
|
6620
|
+
return resolved;
|
|
6621
|
+
}
|
|
6572
6622
|
export {
|
|
6573
|
-
createBookwhenClient
|
|
6623
|
+
createBookwhenClient,
|
|
6624
|
+
resolveJsonApiRelationships,
|
|
6625
|
+
resolveJsonApiResource
|
|
6574
6626
|
};
|
|
6575
6627
|
//# sourceMappingURL=index.js.map
|