UNPKG

json-api-nestjs

Version:
107 lines 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.zodOperationRel = exports.zodRemove = exports.zodUpdate = exports.zodAdd = exports.Operation = void 0; exports.zodInputOperation = zodInputOperation; const zod_1 = require("zod"); const nestjs_shared_1 = require("../../../../utils/nestjs-shared"); const constants_1 = require("../../constants"); const helper_1 = require("../../../mixin/helper"); var Operation; (function (Operation) { Operation["add"] = "add"; Operation["update"] = "update"; Operation["remove"] = "remove"; })(Operation || (exports.Operation = Operation = {})); const literalSchema = zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()]); const jsonSchema = zod_1.z.lazy(() => zod_1.z.union([literalSchema, zod_1.z.array(jsonSchema), zod_1.z.record(jsonSchema)])); const zodGeneralData = jsonSchema.nullable(); const zodAdd = (type) => zod_1.z .object({ op: zod_1.z.literal(Operation.add), ref: zod_1.z .object({ type: zod_1.z.literal(type), tmpId: zod_1.z.union([zod_1.z.number(), zod_1.z.string()]).optional(), }) .strict(), data: zodGeneralData, }) .strict(); exports.zodAdd = zodAdd; const zodUpdate = (type) => zod_1.z .object({ op: zod_1.z.literal(Operation.update), ref: zod_1.z .object({ type: zod_1.z.literal(type), id: zod_1.z.string(), }) .strict(), data: zodGeneralData, }) .strict(); exports.zodUpdate = zodUpdate; const zodRemove = (type) => zod_1.z .object({ op: zod_1.z.literal(Operation.remove), ref: zod_1.z .object({ type: zod_1.z.literal(type), id: zod_1.z.string(), }) .strict(), }) .strict(); exports.zodRemove = zodRemove; const zodOperationRel = (type, rel, typeOperation) => { const literalArray = rel.map((i) => zod_1.z.literal(i)); return zod_1.z .object({ op: zod_1.z.literal(typeOperation), ref: zod_1.z .object({ type: zod_1.z.literal(type), id: zod_1.z.string(), relationship: zod_1.z.union(literalArray), }) .strict(), data: zodGeneralData, }) .strict(); }; exports.zodOperationRel = zodOperationRel; function zodInputOperation(mapController, entityMapProps) { const array = []; for (const [entity, controller] of mapController.entries()) { const typeName = (0, nestjs_shared_1.camelToKebab)((0, helper_1.getEntityName)(entity)); const entityMap = entityMapProps.get(entity); if (!entityMap) throw new Error('Entity not found in map'); const { relations } = entityMap; const hasOwnProperty = (props) => Object.prototype.hasOwnProperty.call(controller.prototype, props); if (hasOwnProperty('postOne')) { array.push((0, exports.zodAdd)(typeName)); } if (hasOwnProperty('patchOne')) { array.push((0, exports.zodUpdate)(typeName)); } if (hasOwnProperty('deleteOne')) { array.push((0, exports.zodRemove)(typeName)); } if (hasOwnProperty('postRelationship')) { array.push((0, exports.zodOperationRel)(typeName, relations, Operation.add)); } if (hasOwnProperty('deleteRelationship')) { array.push((0, exports.zodOperationRel)(typeName, relations, Operation.remove)); } if (hasOwnProperty('patchRelationship')) { array.push((0, exports.zodOperationRel)(typeName, relations, Operation.update)); } } return zod_1.z .object({ [constants_1.KEY_MAIN_INPUT_SCHEMA]: zod_1.z.array(zod_1.z.union(array)).nonempty(), }) .strict(); } //# sourceMappingURL=zod-helper.js.map