UNPKG

3.96 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2019. All Rights Reserved.
3// Node module: @loopback/boot
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.RestDefaults = exports.ModelApiBooter = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const model_api_builder_1 = require("@loopback/model-api-builder");
11const debug_1 = tslib_1.__importDefault(require("debug"));
12const path = tslib_1.__importStar(require("path"));
13const keys_1 = require("../keys");
14const types_1 = require("../types");
15const base_artifact_booter_1 = require("./base-artifact.booter");
16const debug = (0, debug_1.default)('loopback:boot:model-api');
17let ModelApiBooter = class ModelApiBooter extends base_artifact_booter_1.BaseArtifactBooter {
18 constructor(app, projectRoot, getModelApiBuilders, booterConfig = {}) {
19 // TODO assert that `app` has RepositoryMixin members
20 super(projectRoot,
21 // Set booter options if passed in via bootConfig
22 Object.assign({}, exports.RestDefaults, booterConfig));
23 this.app = app;
24 this.getModelApiBuilders = getModelApiBuilders;
25 this.booterConfig = booterConfig;
26 }
27 /**
28 * Load the the model config files
29 */
30 async load() {
31 // Important: don't call `super.load()` here, it would try to load
32 // classes via `loadClassesFromFiles` - that won't work for JSON files
33 await Promise.all(this.discovered.map(async (f) => {
34 try {
35 // It's important to await before returning,
36 // otherwise the catch block won't receive errors
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 * Set up the loaded model classes
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 * Retrieve the API builder that matches the pattern provided
61 * @param pattern - name of pattern for an API builder
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};
74ModelApiBooter = 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);
83exports.ModelApiBooter = ModelApiBooter;
84/**
85 * Default ArtifactOptions for ControllerBooter.
86 */
87exports.RestDefaults = {
88 dirs: ['model-endpoints'],
89 extensions: ['-config.js'],
90 nested: true,
91};
92//# sourceMappingURL=model-api.booter.js.map
\No newline at end of file