Comparing version 1.0.1 to 1.1.0
import { RelatorOptions } from "../interfaces/relator.interface"; | ||
import Relationships from "../models/relationships.model"; | ||
import { SingleOrArray } from "../types/global.types"; | ||
import { Dictionary, SingleOrArray } from "../types/global.types"; | ||
import Serializer from "./serializer"; | ||
@@ -14,3 +14,3 @@ /** | ||
*/ | ||
export default class Relator<PrimaryType, RelatedType> { | ||
export default class Relator<PrimaryType, RelatedType = Dictionary<any>> { | ||
/** | ||
@@ -17,0 +17,0 @@ * Default options. Can be edited to change default options globally. |
@@ -84,3 +84,3 @@ "use strict"; | ||
var metaizer_1 = __importDefault(require("./metaizer")); | ||
var relator_1 = __importDefault(require("./relator")); | ||
var get_array_1 = require("../utils/get-array"); | ||
/** | ||
@@ -120,6 +120,6 @@ * The {@linkcode Serializer} class is the main class used to serializer data | ||
return __awaiter(this, void 0, void 0, function () { | ||
var o, id, attributes, _a, _b, _c, key, value, _d, _e, _f, key, value, _g, _h, _j; | ||
var e_1, _k, e_2, _l; | ||
return __generator(this, function (_m) { | ||
switch (_m.label) { | ||
var o, id, attributes, _a, _b, _c, key, value, _d, _e, _f, key, value, relationships, relators; | ||
var e_1, _g, e_2, _h; | ||
return __generator(this, function (_j) { | ||
switch (_j.label) { | ||
case 0: | ||
@@ -140,3 +140,3 @@ o = merge_1["default"]({}, this.options, options); | ||
try { | ||
if (_b && !_b.done && (_k = _a["return"])) _k.call(_a); | ||
if (_b && !_b.done && (_g = _a["return"])) _g.call(_a); | ||
} | ||
@@ -156,3 +156,3 @@ finally { if (e_1) throw e_1.error; } | ||
try { | ||
if (_e && !_e.done && (_l = _d["return"])) _l.call(_d); | ||
if (_e && !_e.done && (_h = _d["return"])) _h.call(_d); | ||
} | ||
@@ -162,21 +162,28 @@ finally { if (e_2) throw e_2.error; } | ||
} | ||
relationships = {}; | ||
relators = get_array_1.getArray(o.relators); | ||
if (!relators) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, Promise.all(relators.map(function (relator) { | ||
var serializer = relator.getRelatedSerializer(); | ||
if (serializer) { | ||
return relator | ||
.getRelationship(data) | ||
.then(function (rship) { return (relationships[serializer.collectionName] = rship); }); | ||
} | ||
else | ||
return; | ||
}))]; | ||
case 1: | ||
_j.sent(); | ||
_j.label = 2; | ||
case 2: | ||
// Delete the ID field. | ||
delete attributes[o.idKey]; | ||
_g = resource_model_1["default"].bind; | ||
_h = { | ||
type: this.collectionName, | ||
id: id, | ||
attributes: attributes | ||
}; | ||
if (!o.relator) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, o.relator.getRelationship(data)]; | ||
case 1: | ||
_j = _m.sent(); | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
_j = undefined; | ||
_m.label = 3; | ||
case 3: return [2 /*return*/, new (_g.apply(resource_model_1["default"], [void 0, (_h.relationships = _j, | ||
_h.links = o.linkers.resource ? { self: o.linkers.resource.link(data) } : undefined, | ||
_h), o.metaizers.resource ? o.metaizers.resource.metaize(data) : undefined]))()]; | ||
return [2 /*return*/, new resource_model_1["default"]({ | ||
type: this.collectionName, | ||
id: id, | ||
attributes: attributes, | ||
relationships: Object.keys(relationships).length > 0 ? relationships : undefined, | ||
links: o.linkers.resource ? { self: o.linkers.resource.link(data) } : undefined | ||
}, o.metaizers.resource ? o.metaizers.resource.metaize(data) : undefined)]; | ||
} | ||
@@ -193,2 +200,3 @@ }); | ||
Serializer.prototype.serialize = function (data, options) { | ||
var _a; | ||
if (options === void 0) { options = {}; } | ||
@@ -198,23 +206,33 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var currentRelator, currentData, currentDepth, serializer, relatedData, promises, currentData_1, currentData_1_1, datum, _a, _b, _c; | ||
var e_3, _d; | ||
return __generator(this, function (_e) { | ||
switch (_e.label) { | ||
var relators, currentRelators, currentData, currentDepth, newRelators, currentRelators_1, currentRelators_1_1, currentRelator, serializer, relatedData, promises, currentData_1, currentData_1_1, datum, _a, _b, _c, e_3_1; | ||
var e_3, _d, e_4, _e; | ||
return __generator(this, function (_f) { | ||
switch (_f.label) { | ||
case 0: | ||
if (!(o.depth > 0)) return [3 /*break*/, 4]; | ||
relators = get_array_1.getArray(o.relators); | ||
if (!(o.depth > 0 && relators)) return [3 /*break*/, 11]; | ||
document.included = document.included || []; | ||
currentRelator = o.relator, currentData = data, currentDepth = o.depth; | ||
_e.label = 1; | ||
currentRelators = relators, currentData = data, currentDepth = o.depth; | ||
_f.label = 1; | ||
case 1: | ||
if (!(currentRelator && currentData && currentDepth-- >= -1)) return [3 /*break*/, 4]; | ||
if (!(currentRelators.length > 0 && currentData && currentDepth-- >= -1)) return [3 /*break*/, 11]; | ||
newRelators = []; | ||
_f.label = 2; | ||
case 2: | ||
_f.trys.push([2, 8, 9, 10]); | ||
currentRelators_1 = (e_3 = void 0, __values(currentRelators)), currentRelators_1_1 = currentRelators_1.next(); | ||
_f.label = 3; | ||
case 3: | ||
if (!!currentRelators_1_1.done) return [3 /*break*/, 7]; | ||
currentRelator = currentRelators_1_1.value; | ||
serializer = currentRelator.getRelatedSerializer(); | ||
if (!serializer) | ||
return [3 /*break*/, 4]; | ||
if (serializer === undefined) | ||
return [3 /*break*/, 7]; | ||
return [4 /*yield*/, Promise.all(currentData.map(currentRelator.getRelatedData))]; | ||
case 2: | ||
relatedData = _e.sent(); | ||
case 4: | ||
relatedData = _f.sent(); | ||
promises = []; | ||
currentData = relatedData.flat(); | ||
try { | ||
for (currentData_1 = (e_3 = void 0, __values(currentData)), currentData_1_1 = currentData_1.next(); !currentData_1_1.done; currentData_1_1 = currentData_1.next()) { | ||
for (currentData_1 = (e_4 = void 0, __values(currentData)), currentData_1_1 = currentData_1.next(); !currentData_1_1.done; currentData_1_1 = currentData_1.next()) { | ||
datum = currentData_1_1.value; | ||
@@ -224,8 +242,8 @@ promises.push(serializer.constructResource(datum)); | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
catch (e_4_1) { e_4 = { error: e_4_1 }; } | ||
finally { | ||
try { | ||
if (currentData_1_1 && !currentData_1_1.done && (_d = currentData_1["return"])) _d.call(currentData_1); | ||
if (currentData_1_1 && !currentData_1_1.done && (_e = currentData_1["return"])) _e.call(currentData_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
finally { if (e_4) throw e_4.error; } | ||
} | ||
@@ -235,7 +253,25 @@ _a = document; | ||
return [4 /*yield*/, Promise.all(promises)]; | ||
case 3: | ||
_a.included = _c.apply(_b, [_e.sent()]); | ||
currentRelator = serializer.options.relator; | ||
case 5: | ||
_a.included = _c.apply(_b, [_f.sent()]); | ||
if (serializer.options.relators) | ||
newRelators.push(serializer.options.relators); | ||
_f.label = 6; | ||
case 6: | ||
currentRelators_1_1 = currentRelators_1.next(); | ||
return [3 /*break*/, 3]; | ||
case 7: return [3 /*break*/, 10]; | ||
case 8: | ||
e_3_1 = _f.sent(); | ||
e_3 = { error: e_3_1 }; | ||
return [3 /*break*/, 10]; | ||
case 9: | ||
try { | ||
if (currentRelators_1_1 && !currentRelators_1_1.done && (_d = currentRelators_1["return"])) _d.call(currentRelators_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
return [7 /*endfinally*/]; | ||
case 10: | ||
currentRelators = newRelators.flat(); | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/]; | ||
case 11: return [2 /*return*/]; | ||
} | ||
@@ -245,6 +281,6 @@ }); | ||
} | ||
var o, document, getIdentifiers, getResources, originallySingular, pagination, _a, resourceIdentifiers, relationship, resources, resourceIdentifiers; | ||
var o, document, getIdentifiers, getResources, originallySingular, pagination, _b, resourceIdentifiers, relator, relationship, resources, resourceIdentifiers; | ||
var _this = this; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
@@ -292,7 +328,7 @@ o = merge_1["default"]({}, this.options, options); | ||
} | ||
_a = true; | ||
switch (_a) { | ||
_b = true; | ||
switch (_b) { | ||
case o.nullData: return [3 /*break*/, 1]; | ||
case o.onlyIdentifier: return [3 /*break*/, 2]; | ||
case o.onlyRelationship: return [3 /*break*/, 3]; | ||
case !!o.onlyRelationship: return [3 /*break*/, 3]; | ||
} | ||
@@ -305,3 +341,3 @@ return [3 /*break*/, 7]; | ||
} | ||
_b.label = 2; | ||
_c.label = 2; | ||
case 2: | ||
@@ -313,6 +349,6 @@ { | ||
} | ||
_b.label = 3; | ||
_c.label = 3; | ||
case 3: | ||
// Validate options. | ||
if (!(o.relator instanceof relator_1["default"])) { | ||
if (o.relators === undefined) { | ||
throw new TypeError("\"relator\" must be defined when using \"onlyRelationship\""); | ||
@@ -323,5 +359,9 @@ } | ||
} | ||
return [4 /*yield*/, o.relator.getRelationship(data[0])]; | ||
relator = (_a = get_array_1.getArray(o.relators)) === null || _a === void 0 ? void 0 : _a.find(function (relator) { var _a; return !!(((_a = relator.getRelatedSerializer()) === null || _a === void 0 ? void 0 : _a.collectionName) === o.onlyRelationship); }); | ||
if (relator === undefined) { | ||
throw new TypeError("\"onlyRelationship\" is not the name of any collection name among the relators listed in \"relator\""); | ||
} | ||
return [4 /*yield*/, relator.getRelationship(data[0])]; | ||
case 4: | ||
relationship = _b.sent(); | ||
relationship = _c.sent(); | ||
if (relationship.links) { | ||
@@ -337,8 +377,8 @@ document.links = relationship.links; | ||
case 5: | ||
_b.sent(); | ||
_b.label = 6; | ||
_c.sent(); | ||
_c.label = 6; | ||
case 6: return [3 /*break*/, 10]; | ||
case 7: return [4 /*yield*/, getResources(data)]; | ||
case 8: | ||
resources = _b.sent(); | ||
resources = _c.sent(); | ||
if (o.asIncluded) { | ||
@@ -354,4 +394,4 @@ resourceIdentifiers = getIdentifiers(data); | ||
case 9: | ||
_b.sent(); | ||
_b.label = 10; | ||
_c.sent(); | ||
_c.label = 10; | ||
case 10: return [2 /*return*/, document]; | ||
@@ -358,0 +398,0 @@ } |
@@ -44,7 +44,7 @@ import Linker from "../classes/linker"; | ||
/** | ||
* Whether to only serialize the [resource linkages](https://jsonapi.org/format/#document-resource-object-linkage) | ||
* obtained from primary data. | ||
* This is used to serialize the [resource linkages](https://jsonapi.org/format/#document-resource-object-linkage) | ||
* only. The value must be the name of a collection for a relator in the | ||
* {@linkcode SerializerOptions.relator | relator} option. | ||
* | ||
* A {@linkcode SerializerOptions.relator | relator} option **MUST** be | ||
* defined, and only a single primary datum (as opposed to an array) **MUST** | ||
* Only a single primary datum (as opposed to an array) **MUST** | ||
* be serialized. | ||
@@ -58,3 +58,3 @@ * | ||
*/ | ||
onlyRelationship: boolean; | ||
onlyRelationship: string; | ||
/** | ||
@@ -90,3 +90,3 @@ * Whether to make primary data as an [included resource](https://jsonapi.org/format/#document-compound-documents) | ||
*/ | ||
relator?: Relator<PrimaryType, RelatedType>; | ||
relators?: Relator<PrimaryType, RelatedType> | Relator<PrimaryType>[]; | ||
/** | ||
@@ -93,0 +93,0 @@ * A set of options for constructing [top-level links](https://jsonapi.org/format/#document-top-level). |
{ | ||
"name": "ts-japi", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
179129
114
1960