Skip to content

Commit

Permalink
refactor: move basePath to rest-crud config iface
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Sep 9, 2019
1 parent c9ce934 commit 4f74b98
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions _SPIKE_.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ configuration.
Example (`src/public-models/product.config.ts`):

```ts
import {ModelApiConfig} from '@loopback/rest-crud';
import {CrudRestApiConfig} from '@loopback/rest-crud';

module.exports = <ModelApiConfig>{
module.exports = <CrudRestApiConfig>{
model: 'Product',
pattern: 'CrudRest',
dataSource: 'db',
Expand Down
4 changes: 2 additions & 2 deletions examples/todo/src/public-models/todo.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {ModelApiConfig} from '@loopback/rest-crud';
import {CrudRestApiConfig} from '@loopback/rest-crud';

module.exports = <ModelApiConfig>{
module.exports = <CrudRestApiConfig>{
model: 'Todo',
pattern: 'CrudRest',
dataSource: 'db',
Expand Down
2 changes: 0 additions & 2 deletions packages/model-api-builder/src/model-api-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export type ModelApiConfig = {
pattern: string;
// E.g. 'db'
dataSource: string;
// E.g. '/products'
basePath: string;

[patternSpecificSetting: string]: unknown;
};
21 changes: 17 additions & 4 deletions packages/rest-crud/src/crud-rest-builder.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ import {defineRepositoryClass} from './repository-builder';

const debug = debugFactory('loopback:boot:crud-rest');

export interface CrudRestApiConfig extends ModelApiConfig {
// E.g. '/products'
basePath: string;
}

@bind(asModelApiBuilder)
export class CrudRestApiBuilder implements ModelApiBuilder {
readonly pattern: string = 'CrudRest';

build(
application: ApplicationWithRepositories,
modelClass: typeof Model & {prototype: Model},
config: ModelApiConfig,
cfg: ModelApiConfig,
): Promise<void> {
const modelName = modelClass.name;
const config = cfg as CrudRestApiConfig;
if (!config.basePath) {
throw new Error(
`Missing required field "basePath" in configuration for model ${modelName}.`,
);
}

if (!(modelClass.prototype instanceof Entity)) {
throw new Error(
`CrudRestController requires an Entity, Models are not supported. (Model name: ${modelClass.name})`,
`CrudRestController requires an Entity, Models are not supported. (Model name: ${modelName})`,
);
}
const entityClass = modelClass as typeof Entity & {prototype: Entity};
Expand All @@ -54,7 +67,7 @@ export class CrudRestApiBuilder implements ModelApiBuilder {

function setupCrudRepository(
entityClass: typeof Entity & {prototype: Entity},
modelConfig: ModelApiConfig,
modelConfig: CrudRestApiConfig,
): Class<EntityCrudRepository<Entity, unknown>> {
const repositoryClass = defineRepositoryClass(entityClass);

Expand All @@ -69,7 +82,7 @@ function setupCrudRepository(

function setupCrudRestController(
entityClass: typeof Entity & {prototype: Entity},
modelConfig: ModelApiConfig,
modelConfig: CrudRestApiConfig,
): ControllerClass {
const controllerClass = defineCrudRestController(
entityClass,
Expand Down
3 changes: 1 addition & 2 deletions packages/rest-crud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

// Re-export ModelApiConfig for use in application config files
export {ModelApiConfig} from '@loopback/model-api-builder';
export {CrudRestApiConfig} from './crud-rest-builder.plugin';
export * from './crud-rest.component';
export * from './crud-rest.controller';
export * from './repository-builder';

0 comments on commit 4f74b98

Please sign in to comment.