1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.RestDefaults = exports.ModelApiBooter = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const core_1 = require("@loopback/core");
|
10 | const model_api_builder_1 = require("@loopback/model-api-builder");
|
11 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
12 | const path = tslib_1.__importStar(require("path"));
|
13 | const keys_1 = require("../keys");
|
14 | const types_1 = require("../types");
|
15 | const base_artifact_booter_1 = require("./base-artifact.booter");
|
16 | const debug = (0, debug_1.default)('loopback:boot:model-api');
|
17 | let ModelApiBooter = class ModelApiBooter extends base_artifact_booter_1.BaseArtifactBooter {
|
18 | constructor(app, projectRoot, getModelApiBuilders, booterConfig = {}) {
|
19 |
|
20 | super(projectRoot,
|
21 |
|
22 | Object.assign({}, exports.RestDefaults, booterConfig));
|
23 | this.app = app;
|
24 | this.getModelApiBuilders = getModelApiBuilders;
|
25 | this.booterConfig = booterConfig;
|
26 | }
|
27 | |
28 |
|
29 |
|
30 | async load() {
|
31 |
|
32 |
|
33 | await Promise.all(this.discovered.map(async (f) => {
|
34 | try {
|
35 |
|
36 |
|
37 | await this.setupModel(f);
|
38 | }
|
39 | catch (err) {
|
40 | const shortPath = path.relative(this.projectRoot, f);
|
41 | err.message += ` (while loading ${shortPath})`;
|
42 | throw err;
|
43 | }
|
44 | }));
|
45 | }
|
46 | |
47 |
|
48 |
|
49 | async setupModel(configFile) {
|
50 | const cfg = require(configFile);
|
51 | debug('Loaded model config from %s', path.relative(this.projectRoot, configFile), cfg);
|
52 | const modelClass = cfg.model;
|
53 | if (typeof modelClass !== 'function') {
|
54 | throw new Error(`Invalid "model" field. Expected a Model class, found ${modelClass}`);
|
55 | }
|
56 | const builder = await this.getApiBuilderForPattern(cfg.pattern);
|
57 | await builder.build(this.app, modelClass, cfg);
|
58 | }
|
59 | |
60 |
|
61 |
|
62 |
|
63 | async getApiBuilderForPattern(pattern) {
|
64 | const allBuilders = await this.getModelApiBuilders();
|
65 | const builder = allBuilders.find(b => b.pattern === pattern);
|
66 | if (!builder) {
|
67 | const availableBuilders = allBuilders.map(b => b.pattern).join(', ');
|
68 | throw new Error(`Unsupported API pattern "${pattern}". ` +
|
69 | `Available patterns: ${availableBuilders || '<none>'}`);
|
70 | }
|
71 | return builder;
|
72 | }
|
73 | };
|
74 | ModelApiBooter = tslib_1.__decorate([
|
75 | (0, types_1.booter)('modelApi'),
|
76 | (0, core_1.extensionPoint)(model_api_builder_1.MODEL_API_BUILDER_PLUGINS),
|
77 | tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
|
78 | tslib_1.__param(1, (0, core_1.inject)(keys_1.BootBindings.PROJECT_ROOT)),
|
79 | tslib_1.__param(2, (0, core_1.extensions)()),
|
80 | tslib_1.__param(3, (0, core_1.config)()),
|
81 | tslib_1.__metadata("design:paramtypes", [Object, String, Function, Object])
|
82 | ], ModelApiBooter);
|
83 | exports.ModelApiBooter = ModelApiBooter;
|
84 |
|
85 |
|
86 |
|
87 | exports.RestDefaults = {
|
88 | dirs: ['model-endpoints'],
|
89 | extensions: ['-config.js'],
|
90 | nested: true,
|
91 | };
|
92 |
|
\ | No newline at end of file |