@api-client/core 0.20.7 → 0.20.9

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.
@@ -1 +1 @@
1
- {"version":3,"file":"RuntimeModelGenerator.js","sourceRoot":"","sources":["../../../../src/modeling/generators/RuntimeModelGenerator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAuB,MAAM,gBAAgB,CAAA;AAG9D;;;;;GAKG;AACH,MAAM,OAAO,qBAAqB;IAChC,SAAS,CAAU;IACnB,UAAU,CAAgB;IAC1B,WAAW,GAAsC,EAAE,CAAA;IAEnD,YAAY,KAAgC;QAC1C,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;YAC9B,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;YACtB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,MAAM,EAAE,CAAA;QAClC,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,SAAS,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAA;YACpC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAA;QACzB,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,QAAQ;QACnB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;QAC7C,CAAC;QAED,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;YACrD,MAAM,OAAO,GAAG,MAAM,CAAC,yBAAyB,EAAE,CAAA;YAClD,MAAM,OAAO,GAAG,MAAM,CAAC,uBAAuB,EAAE,CAAA;YAChD,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBACpC,QAAQ,MAAM,CAAC,IAAI,EAAE,CAAC;oBACpB,KAAK,MAAM;wBACT,IAAI,OAAO;4BAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE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B;QAC7F,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,EAAE,CAAA;QACxC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC;YACnC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,GAAG,EAAE,CAAA;QACpC,CAAC;QACD,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC;YACjC,IAAI;YACJ,MAAM,EAAE,EAAE,gBAAgB,EAAE,UAAU,EAAE;SACzC,CAAC,CAAA;IACJ,CAAC;CACF","sourcesContent":["import { type ActionKind, type UpdateAction } from '../actions/index.js'\nimport { ApiModel, type ApiModelSchema } from '../ApiModel.js'\nimport { type RouteDefinition, type RuntimeApiModelSchema } from '../RuntimeApiModel.js'\n\n/**\n * A class that takes the API model and generates a runtime-optimized API model.\n *\n * Note, the API model must be already validated before passing it to this generator.\n * This class doesn't perform any validation, but it will throw an exception if the API model is invalid.\n */\nexport class RuntimeModelGenerator {\n #apiModel: ApiModel\n #apiSchema: ApiModelSchema\n #routingMap: Record<string, RouteDefinition[]> = {}\n\n constructor(input: ApiModel | ApiModelSchema) {\n if (input instanceof ApiModel) {\n this.#apiModel = input\n this.#apiSchema = input.toJSON()\n } else {\n this.#apiModel = new ApiModel(input)\n this.#apiSchema = input\n }\n }\n\n public async generate(): Promise<RuntimeApiModelSchema> {\n if (!this.#apiModel) {\n throw new Error('API model is not defined')\n }\n\n for (const expose of this.#apiModel.exposes.values()) {\n const colPath = expose.getAbsoluteCollectionPath()\n const resPath = expose.getAbsoluteResourcePath()\n for (const action of expose.actions) {\n switch (action.kind) {\n case 'list':\n if (colPath) this.addRoute('GET', colPath, expose.key, action.kind)\n break\n case 'create':\n if (colPath) this.addRoute('POST', colPath, expose.key, action.kind)\n break\n case 'search':\n if (colPath) this.addRoute('POST', `${colPath}/search`, expose.key, action.kind)\n break\n case 'read':\n if (resPath) this.addRoute('GET', resPath, expose.key, action.kind)\n break\n case 'update':\n if (resPath) {\n const updateAction = action as UpdateAction\n for (const method of updateAction.allowedMethods) {\n this.addRoute(method, resPath, expose.key, action.kind)\n }\n }\n break\n case 'delete':\n if (resPath) this.addRoute('DELETE', resPath, expose.key, action.kind)\n break\n }\n }\n }\n\n return {\n ...this.#apiSchema,\n routingMap: this.#routingMap,\n }\n }\n\n private addRoute(method: string, path: string, exposedEntityKey: string, actionKind: ActionKind): void {\n const upperMethod = method.toUpperCase()\n if (!this.#routingMap[upperMethod]) {\n this.#routingMap[upperMethod] = []\n }\n this.#routingMap[upperMethod].push({\n path,\n lookup: { exposedEntityKey, actionKind },\n })\n }\n}\n"]}
1
+ {"version":3,"file":"RuntimeModelGenerator.js","sourceRoot":"","sources":["../../../../src/modeling/generators/RuntimeModelGenerator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAuB,MAAM,gBAAgB,CAAA;AAI9D;;;;;GAKG;AACH,MAAM,OAAO,qBAAqB;IAChC,SAAS,CAAU;IACnB,UAAU,CAAgB;IAC1B,WAAW,GAAsC,EAAE,CAAA;IAEnD,YAAY,KAAgC;QAC1C,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;YAC9B,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;YACtB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,MAAM,EAAE,CAAA;QAClC,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,SAAS,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAA;YACpC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAA;QACzB,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,QAAQ;QACnB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;QAC7C,CAAC;QAED,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;YACrD,MAAM,OAAO,GAAG,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C,CAAA;YACJ,CAAC;YACD,QAAQ,GAAG,OAAO,CAAA;YAClB,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QACvF,CAAC;QACD,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC;YACjC,IAAI;YACJ,MAAM,EAAE;gBACN,gBAAgB,EAAE,MAAM,CAAC,GAAG;gBAC5B,iBAAiB,EAAE,YAAY,CAAC,IAAI,CAAC,IAAc;gBACnD,UAAU;gBACV,MAAM;aACP;SACF,CAAC,CAAA;IACJ,CAAC;CACF","sourcesContent":["import { type ActionKind, type UpdateAction } from '../actions/index.js'\nimport { ApiModel, type ApiModelSchema } from '../ApiModel.js'\nimport { ExposedEntity } from '../ExposedEntity.js'\nimport { RouteParam, type RouteDefinition, type RuntimeApiModelSchema } from '../RuntimeApiModel.js'\n\n/**\n * A class that takes the API model and generates a runtime-optimized API model.\n *\n * Note, the API model must be already validated before passing it to this generator.\n * This class doesn't perform any validation, but it will throw an exception if the API model is invalid.\n */\nexport class RuntimeModelGenerator {\n #apiModel: ApiModel\n #apiSchema: ApiModelSchema\n #routingMap: Record<string, RouteDefinition[]> = {}\n\n constructor(input: ApiModel | ApiModelSchema) {\n if (input instanceof ApiModel) {\n this.#apiModel = input\n this.#apiSchema = input.toJSON()\n } else {\n this.#apiModel = new ApiModel(input)\n this.#apiSchema = input\n }\n }\n\n public async generate(): Promise<RuntimeApiModelSchema> {\n if (!this.#apiModel) {\n throw new Error('API model is not defined')\n }\n\n for (const expose of this.#apiModel.exposes.values()) {\n const colPath = expose.getAbsoluteCollectionPath()\n const resPath = expose.getAbsoluteResourcePath()\n for (const action of expose.actions) {\n switch (action.kind) {\n case 'list':\n if (colPath) this.addRoute('GET', colPath, expose, action.kind)\n break\n case 'create':\n if (colPath) this.addRoute('POST', colPath, expose, action.kind)\n break\n case 'search':\n if (colPath) this.addRoute('POST', `${colPath}/search`, expose, action.kind)\n break\n case 'read':\n if (resPath) this.addRoute('GET', resPath, expose, action.kind)\n break\n case 'update':\n if (resPath) {\n const updateAction = action as UpdateAction\n for (const method of updateAction.allowedMethods) {\n this.addRoute(method, resPath, expose, action.kind)\n }\n }\n break\n case 'delete':\n if (resPath) this.addRoute('DELETE', resPath, expose, action.kind)\n break\n }\n }\n }\n\n return {\n ...this.#apiSchema,\n routingMap: this.#routingMap,\n }\n }\n\n private addRoute(method: string, path: string, expose: ExposedEntity, actionKind: ActionKind): void {\n const upperMethod = method.toUpperCase()\n if (!this.#routingMap[upperMethod]) {\n this.#routingMap[upperMethod] = []\n }\n\n const domainEntity = this.#apiModel.domain?.findEntity(expose.entity.key, expose.entity.domain)\n if (!domainEntity) throw new Error(`Entity ${expose.key} not found, this should never happen`)\n\n const params: RouteParam[] = []\n let current: ExposedEntity | undefined\n let previous: ExposedEntity | undefined = undefined\n if (['list', 'create', 'search'].includes(actionKind)) {\n // when starting with a collection, ignore self and start with parent (if any).\n // A collection won't have any path params anyway, so we can skip it.\n current = expose.parent ? this.#apiModel.exposes.get(expose.parent.key) : undefined\n // setting the previous to the current so the associations are captured.\n previous = expose\n } else {\n current = expose\n }\n\n while (current) {\n const match = current.resourcePath.match(/\\{([^}]+)\\}/)\n if (match) {\n const entity = this.#apiModel.domain?.findEntity(current.entity.key, current.entity.domain)\n if (!entity) throw new Error(`Entity ${current.entity.key} not found, this should never happen`)\n const pkName = entity.primaryKey()?.info.name\n if (!pkName) throw new Error(`Entity ${current.entity.key} has no primary key, this should never happen`)\n\n let associationName: string | undefined\n if (previous && previous.parent && previous.parent.key === current.key) {\n for (const assoc of entity.withInheritedAssociations()) {\n if (previous.parent?.association.key === assoc.key) {\n associationName = assoc.info.name\n break\n }\n }\n }\n\n params.push({\n paramName: match[1],\n exposedEntityKey: current.key,\n exposedEntityName: entity.info.name as string,\n propertyName: pkName,\n ...(associationName ? { associationName } : {}),\n })\n }\n previous = current\n current = current.parent ? this.#apiModel.exposes.get(current.parent.key) : undefined\n }\n this.#routingMap[upperMethod].push({\n path,\n lookup: {\n exposedEntityKey: expose.key,\n exposedEntityName: domainEntity.info.name as string,\n actionKind,\n params,\n },\n })\n }\n}\n"]}