Socket
Socket
Sign inDemoInstall

ts-japi

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-japi - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

.github/ISSUE_TEMPLATE/bug_report.md

6

lib/classes/relator.d.ts

@@ -14,3 +14,3 @@ import { RelatorOptions } from "../interfaces/relator.interface";

*/
export default class Relator<PrimaryType, RelatedType = Dictionary<any>> {
export default class Relator<PrimaryType, RelatedType extends Dictionary<any> = any> {
/**

@@ -21,3 +21,3 @@ * Default options. Can be edited to change default options globally.

linkers: {};
serializer: Serializer<Record<string | number | symbol, any>, Record<string | number | symbol, any>>;
serializer: Serializer<Record<string | number | symbol, any>>;
};

@@ -38,3 +38,3 @@ /**

/** @internal Gets the related resources {@linkcode Serializer}. */
getRelatedSerializer(): Serializer<RelatedType, Record<string | number | symbol, any>> | undefined;
getRelatedSerializer(): Serializer<RelatedType> | undefined;
/** @internal Creates a {@linkcode Relationship}. */

@@ -41,0 +41,0 @@ getRelationship(data: PrimaryType): Promise<Relationships>;

@@ -15,3 +15,3 @@ import { DataDocument } from "../interfaces/document.interface";

*/
export default class Serializer<PrimaryType extends Dictionary<any>, RelatedType extends Dictionary<any> = Dictionary<any>> {
export default class Serializer<PrimaryType extends Dictionary<any>> {
/**

@@ -38,3 +38,3 @@ * Default options. Can be edited to change default options globally.

*/
private options;
options: SerializerOptions<PrimaryType>;
/**

@@ -46,7 +46,7 @@ * Creates a {@linkcode Serializer}.

*/
constructor(collectionName: string, options?: Partial<SerializerOptions<PrimaryType, RelatedType>>);
constructor(collectionName: string, options?: Partial<SerializerOptions<PrimaryType>>);
/** @internal Generates a `ResourceIdentifier`. */
constructResourceIdentity(data: PrimaryType, options?: Partial<SerializerOptions<PrimaryType, RelatedType>>): ResourceIdentifier;
constructResourceIdentity(data: PrimaryType, options?: Partial<SerializerOptions<PrimaryType>>): ResourceIdentifier;
/** @internal Generates a `Resource`. */
constructResource(data: PrimaryType, options?: Partial<SerializerOptions<PrimaryType, RelatedType>>): Promise<Resource<PrimaryType>>;
constructResource(data: PrimaryType, options?: Partial<SerializerOptions<PrimaryType>>): Promise<Resource<PrimaryType>>;
/**

@@ -58,4 +58,4 @@ * The actual serialization function.

*/
serialize(data?: SingleOrArray<PrimaryType>, options?: Partial<SerializerOptions<PrimaryType, RelatedType>>): Promise<DataDocument<PrimaryType>>;
serialize(data?: SingleOrArray<PrimaryType>, options?: Partial<SerializerOptions<PrimaryType>>): Promise<DataDocument<PrimaryType>>;
}
//# sourceMappingURL=serializer.d.ts.map

@@ -76,2 +76,6 @@ "use strict";

};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -160,4 +164,4 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

relationships = {};
if (!o.relators) return [3 /*break*/, 2];
relators = get_array_1.getArray(o.relators);
if (!relators) return [3 /*break*/, 2];
return [4 /*yield*/, Promise.all(relators.map(function (relator) {

@@ -197,3 +201,2 @@ var serializer = relator.getRelatedSerializer();

Serializer.prototype.serialize = function (data, options) {
var _a;
if (options === void 0) { options = {}; }

@@ -203,70 +206,89 @@ return __awaiter(this, void 0, void 0, function () {

return __awaiter(this, void 0, void 0, function () {
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) {
var queue, depth, i, len, _a, data_1, relators, relators_1, relators_1_1, relator, serializer, relatedData, newData, relatedData_1, relatedData_1_1, datum, uniqueId, _b, _c, _d, e_7_1, e_8_1;
var e_8, _e, e_7, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
relators = get_array_1.getArray(o.relators);
if (!(o.depth > 0 && relators)) return [3 /*break*/, 11];
document.included = document.included || [];
currentRelators = relators, currentData = data, currentDepth = o.depth;
_f.label = 1;
if (!(o.depth > 0 && o.relators)) return [3 /*break*/, 20];
queue = [[data, get_array_1.getArray(o.relators)]];
depth = o.depth;
_g.label = 1;
case 1:
if (!(currentRelators.length > 0 && currentData && currentDepth-- >= -1)) return [3 /*break*/, 11];
newRelators = [];
_f.label = 2;
if (!(queue.length > 0 && depth-- > 0)) return [3 /*break*/, 20];
i = 0, len = queue.length;
_g.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;
if (!(i < len)) return [3 /*break*/, 19];
_a = __read(queue[i], 2), data_1 = _a[0], relators = _a[1];
_g.label = 3;
case 3:
if (!!currentRelators_1_1.done) return [3 /*break*/, 7];
currentRelator = currentRelators_1_1.value;
serializer = currentRelator.getRelatedSerializer();
_g.trys.push([3, 16, 17, 18]);
relators_1 = (e_8 = void 0, __values(relators)), relators_1_1 = relators_1.next();
_g.label = 4;
case 4:
if (!!relators_1_1.done) return [3 /*break*/, 15];
relator = relators_1_1.value;
serializer = relator.getRelatedSerializer();
if (serializer === undefined)
return [3 /*break*/, 7];
return [4 /*yield*/, Promise.all(currentData.map(currentRelator.getRelatedData))];
case 4:
relatedData = _f.sent();
promises = [];
currentData = relatedData.flat();
try {
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;
promises.push(serializer.constructResource(datum));
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (currentData_1_1 && !currentData_1_1.done && (_e = currentData_1["return"])) _e.call(currentData_1);
}
finally { if (e_4) throw e_4.error; }
}
_a = document;
_c = (_b = document.included).concat;
return [4 /*yield*/, Promise.all(promises)];
return [3 /*break*/, 14];
return [4 /*yield*/, Promise.all(data_1.map(relator.getRelatedData))];
case 5:
_a.included = _c.apply(_b, [_f.sent()]);
if (serializer.options.relators)
newRelators.push(serializer.options.relators);
_f.label = 6;
relatedData = (_g.sent()).flat();
if (!(relatedData.length > 0)) return [3 /*break*/, 14];
newData = [];
_g.label = 6;
case 6:
currentRelators_1_1 = currentRelators_1.next();
return [3 /*break*/, 3];
case 7: return [3 /*break*/, 10];
_g.trys.push([6, 11, 12, 13]);
relatedData_1 = (e_7 = void 0, __values(relatedData)), relatedData_1_1 = relatedData_1.next();
_g.label = 7;
case 7:
if (!!relatedData_1_1.done) return [3 /*break*/, 10];
datum = relatedData_1_1.value;
uniqueId = serializer.collectionName + " " + datum[serializer.options.idKey];
if (!(!includedData.has(uniqueId) && !primaryData.has(uniqueId))) return [3 /*break*/, 9];
newData.push(datum);
_c = (_b = includedData).set;
_d = [uniqueId];
return [4 /*yield*/, serializer.constructResource(datum)];
case 8:
e_3_1 = _f.sent();
e_3 = { error: e_3_1 };
return [3 /*break*/, 10];
_c.apply(_b, _d.concat([_g.sent()]));
_g.label = 9;
case 9:
relatedData_1_1 = relatedData_1.next();
return [3 /*break*/, 7];
case 10: return [3 /*break*/, 13];
case 11:
e_7_1 = _g.sent();
e_7 = { error: e_7_1 };
return [3 /*break*/, 13];
case 12:
try {
if (currentRelators_1_1 && !currentRelators_1_1.done && (_d = currentRelators_1["return"])) _d.call(currentRelators_1);
if (relatedData_1_1 && !relatedData_1_1.done && (_f = relatedData_1["return"])) _f.call(relatedData_1);
}
finally { if (e_3) throw e_3.error; }
finally { if (e_7) throw e_7.error; }
return [7 /*endfinally*/];
case 10:
currentRelators = newRelators.flat();
return [3 /*break*/, 1];
case 11: return [2 /*return*/];
case 13:
if (newData.length > 0 && serializer.options.relators) {
queue.push([newData, get_array_1.getArray(serializer.options.relators)]);
}
_g.label = 14;
case 14:
relators_1_1 = relators_1.next();
return [3 /*break*/, 4];
case 15: return [3 /*break*/, 18];
case 16:
e_8_1 = _g.sent();
e_8 = { error: e_8_1 };
return [3 /*break*/, 18];
case 17:
try {
if (relators_1_1 && !relators_1_1.done && (_e = relators_1["return"])) _e.call(relators_1);
}
finally { if (e_8) throw e_8.error; }
return [7 /*endfinally*/];
case 18:
i++;
return [3 /*break*/, 2];
case 19: return [3 /*break*/, 1];
case 20: return [2 /*return*/];
}

@@ -276,6 +298,7 @@ });

}
var o, document, getIdentifiers, getResources, originallySingular, pagination, _b, resourceIdentifiers, relator, relationship, resources, resourceIdentifiers;
var o, includedData, primaryData, document, getIdentifiers, getResources, originallySingular, pagination, _a, resourceIdentifiers, relator, relationship, _b, _c, datum, resources, identifiers, resources_1, resources_1_1, resource, identifiers_1, identifiers_1_1, identifier, resources_2, resources_2_1, resource;
var e_3, _d, e_4, _e, e_5, _f, e_6, _g;
var _this = this;
return __generator(this, function (_c) {
switch (_c.label) {
return __generator(this, function (_h) {
switch (_h.label) {
case 0:

@@ -290,2 +313,4 @@ o = merge_1["default"]({}, this.options, options);

}
includedData = new Map();
primaryData = new Map();
document = {};

@@ -324,4 +349,4 @@ // Constructing base document.

}
_b = true;
switch (_b) {
_a = true;
switch (_a) {
case o.nullData: return [3 /*break*/, 1];

@@ -337,3 +362,3 @@ case o.onlyIdentifier: return [3 /*break*/, 2];

}
_c.label = 2;
_h.label = 2;
case 2:

@@ -345,7 +370,7 @@ {

}
_c.label = 3;
_h.label = 3;
case 3:
// Validate options.
if (o.relators === undefined) {
throw new TypeError("\"relator\" must be defined when using \"onlyRelationship\"");
throw new TypeError("\"relators\" must be defined when using \"onlyRelationship\"");
}

@@ -355,9 +380,9 @@ if (!originallySingular) {

}
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); });
relator = get_array_1.getArray(o.relators).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\"");
throw new TypeError("\"onlyRelationship\" is not the name of any collection name among the relators listed in \"relators\"");
}
return [4 /*yield*/, relator.getRelationship(data[0])];
case 4:
relationship = _c.sent();
relationship = _h.sent();
if (relationship.links) {

@@ -370,24 +395,79 @@ document.links = relationship.links;

if (!relationship.data) return [3 /*break*/, 6];
document.data = relationship.data;
try {
for (_b = __values(relationship.data), _c = _b.next(); !_c.done; _c = _b.next()) {
datum = _c.value;
primaryData.set(datum.type + " " + datum.id, datum);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_c && !_c.done && (_d = _b["return"])) _d.call(_b);
}
finally { if (e_3) throw e_3.error; }
}
return [4 /*yield*/, recurseResources(data)];
case 5:
_c.sent();
_c.label = 6;
_h.sent();
_h.label = 6;
case 6: return [3 /*break*/, 10];
case 7: return [4 /*yield*/, getResources(data)];
case 8:
resources = _c.sent();
resources = _h.sent();
if (o.asIncluded) {
resourceIdentifiers = getIdentifiers(data);
document.included = resources;
document.data = originallySingular ? resourceIdentifiers[0] : resourceIdentifiers;
identifiers = getIdentifiers(data);
try {
for (resources_1 = __values(resources), resources_1_1 = resources_1.next(); !resources_1_1.done; resources_1_1 = resources_1.next()) {
resource = resources_1_1.value;
includedData.set(resource.type + " " + resource.id, resource);
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (resources_1_1 && !resources_1_1.done && (_e = resources_1["return"])) _e.call(resources_1);
}
finally { if (e_4) throw e_4.error; }
}
try {
for (identifiers_1 = __values(identifiers), identifiers_1_1 = identifiers_1.next(); !identifiers_1_1.done; identifiers_1_1 = identifiers_1.next()) {
identifier = identifiers_1_1.value;
primaryData.set(identifier.type + " " + identifier.id, identifier);
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (identifiers_1_1 && !identifiers_1_1.done && (_f = identifiers_1["return"])) _f.call(identifiers_1);
}
finally { if (e_5) throw e_5.error; }
}
}
else {
document.data = originallySingular ? resources[0] : resources;
try {
for (resources_2 = __values(resources), resources_2_1 = resources_2.next(); !resources_2_1.done; resources_2_1 = resources_2.next()) {
resource = resources_2_1.value;
primaryData.set(resource.type + " " + resource.id, resource);
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (resources_2_1 && !resources_2_1.done && (_g = resources_2["return"])) _g.call(resources_2);
}
finally { if (e_6) throw e_6.error; }
}
}
return [4 /*yield*/, recurseResources(data)];
case 9:
_c.sent();
_c.label = 10;
case 10: return [2 /*return*/, document];
_h.sent();
_h.label = 10;
case 10:
if (includedData.size > 0) {
document.included = __spread(includedData.values());
}
if (primaryData.size > 0) {
document.data = originallySingular ? __spread(primaryData.values())[0] : __spread(primaryData.values());
}
return [2 /*return*/, document];
}

@@ -394,0 +474,0 @@ });

import Linker from "../classes/linker";
import Metaizer from "../classes/metaizer";
import Serializer from "../classes/serializer";
import { SingleOrArray } from "../types/global.types";
export interface RelatorOptions<PrimaryType, RelatedType> {
import { SingleOrArray, Dictionary } from "../types/global.types";
export interface RelatorOptions<PrimaryType, RelatedType extends Dictionary<any> = any> {
/**

@@ -7,0 +7,0 @@ * The `Serializer` to use for related data.

@@ -6,4 +6,4 @@ import Link from "../models/link.model";

export interface ResourceOptions<T> {
id?: string;
type?: string;
id: string;
type: string;
attributes?: Partial<T>;

@@ -13,2 +13,7 @@ relationships?: Relationships;

}
/** @internal */
export interface ResourceIdentifierOptions {
type: string;
id: string;
}
//# sourceMappingURL=resource.interface.d.ts.map

@@ -6,3 +6,3 @@ import Linker from "../classes/linker";

import { Dictionary, SingleOrArray } from "../types/global.types";
export interface SerializerOptions<PrimaryType extends Dictionary<any>, RelatedType extends Dictionary<any>> {
export interface SerializerOptions<PrimaryType extends Dictionary<any>> {
/**

@@ -37,3 +37,3 @@ * The key name for the identifier in the resource.

* {@linkcode SerializerOptions.depth | depth}, and
* {@linkcode SerializerOptions.relator | relator} (and all options they ignore
* {@linkcode SerializerOptions.relators | relators} (and all options they ignore
* except {@linkcode SerializerOptions.linkers | linkers.resource}

@@ -48,3 +48,3 @@ * and {@linkcode SerializerOptions.metaizers | metaizers.resource}).

* only. The value must be the name of a collection for a relator in the
* {@linkcode SerializerOptions.relator | relator} option.
* {@linkcode SerializerOptions.relators | relators} option.
*

@@ -91,3 +91,3 @@ * Only a single primary datum (as opposed to an array) **MUST**

*/
relators?: Relator<PrimaryType, RelatedType> | Relator<PrimaryType>[];
relators?: Relator<PrimaryType> | Array<Relator<PrimaryType>>;
/**

@@ -94,0 +94,0 @@ * A set of options for constructing [top-level links](https://jsonapi.org/format/#document-top-level).

@@ -1,12 +0,9 @@

import BaseResource from "./base-resource.model";
import Meta from "./meta.model";
export interface ResourceIdentifierOptions {
import { ResourceIdentifierOptions } from "../interfaces/resource.interface";
export default class ResourceIdentifier {
type: string;
id: string;
}
export default class ResourceIdentifier extends BaseResource {
type: string;
id: string;
meta?: Meta;
constructor(options: ResourceIdentifierOptions, meta?: Meta);
}
//# sourceMappingURL=resource-identifier.model.d.ts.map
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var base_resource_model_1 = __importDefault(require("./base-resource.model"));
var ResourceIdentifier = /** @class */ (function (_super) {
__extends(ResourceIdentifier, _super);
var ResourceIdentifier = /** @class */ (function () {
function ResourceIdentifier(options, meta) {
var _this = _super.call(this, options.type, options.id, meta) || this;
_this.type = options.type;
_this.id = options.id;
return _this;
this.type = options.type;
this.id = options.id;
if (meta)
this.meta = meta;
}
return ResourceIdentifier;
}(base_resource_model_1["default"]));
}());
exports["default"] = ResourceIdentifier;
//# sourceMappingURL=resource-identifier.model.js.map
import { LinkObject } from "../interfaces/document.interface";
import { ResourceOptions } from "../interfaces/resource.interface";
import { Dictionary } from "../types/global.types";
import BaseResource from "./base-resource.model";
import Meta from "./meta.model";
import Relationships from "./relationships.model";
export default class Resource<T = Dictionary<any>> extends BaseResource {
import ResourceIdentifier from "./resource-identifier.model";
export default class Resource<T = Dictionary<any>> extends ResourceIdentifier {
attributes?: Partial<T>;

@@ -9,0 +9,0 @@ relationships?: Relationships;

@@ -19,7 +19,7 @@ "use strict";

exports.__esModule = true;
var base_resource_model_1 = __importDefault(require("./base-resource.model"));
var resource_identifier_model_1 = __importDefault(require("./resource-identifier.model"));
var Resource = /** @class */ (function (_super) {
__extends(Resource, _super);
function Resource(options, meta) {
var _this = _super.call(this, options.type, options.id, meta) || this;
var _this = _super.call(this, { id: options.id, type: options.type }, meta) || this;
if (options.attributes)

@@ -34,4 +34,4 @@ _this.attributes = options.attributes;

return Resource;
}(base_resource_model_1["default"]));
}(resource_identifier_model_1["default"]));
exports["default"] = Resource;
//# sourceMappingURL=resource.model.js.map

@@ -1,2 +0,2 @@

export declare function getArray<T>(obj: T | T[] | undefined): T[] | undefined;
export declare function getArray<T>(obj: T | T[]): T[];
//# sourceMappingURL=get-array.d.ts.map

@@ -5,15 +5,5 @@ "use strict";

function getArray(obj) {
if (obj) {
if (Array.isArray(obj)) {
return obj;
}
else {
return [obj];
}
}
else {
return undefined;
}
return Array.isArray(obj) ? obj : [obj];
}
exports.getArray = getArray;
//# sourceMappingURL=get-array.js.map
{
"name": "ts-japi",
"version": "1.1.0",
"version": "1.1.1",
"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",

@@ -251,3 +251,3 @@ <br />

The [`ErrorSerializer`](https://jun-sheaf.github.io/ts-japi/classes/errorserializer.html) is used to serializer any object considered an error (the [`attributes`](https://jun-sheaf.github.io/ts-japi/interfaces/errorserializeroptions.html#attributes) option allows you to choose what attributes to use during serialization). *Alternatively* (**recommended**), you can construct custom errors by extending the [`JAPIError`](https://jun-sheaf.github.io/ts-japi/classes/japierror.html) class and use those for all server-to-client errors.
The [`ErrorSerializer`](https://jun-sheaf.github.io/ts-japi/classes/errorserializer.html) is used to serialize any object considered an error (the [`attributes`](https://jun-sheaf.github.io/ts-japi/interfaces/errorserializeroptions.html#attributes) option allows you to choose what attributes to use during serialization). *Alternatively* (**recommended**), you can construct custom errors by extending the [`JAPIError`](https://jun-sheaf.github.io/ts-japi/classes/japierror.html) class and use those for all server-to-client errors.

@@ -254,0 +254,0 @@ The [error serializer test](https://github.com/jun-sheaf/ts-japi/tree/master/test/error-serializer.test.ts) includes an example of the alternative solution.

@@ -75,2 +75,3 @@ {

],
"gaID": "UA-99757611-3",
"plugin": "none",

@@ -77,0 +78,0 @@ "theme": "../typedoc-classic-theme/bin/default",

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

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc