-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
crud-controller.ts
133 lines (121 loc) · 3.31 KB
/
crud-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
repository,
CrudRepository,
ValueObject,
DataObject,
Options,
Where,
} from '@loopback/repository';
import {
api,
getControllerSpec,
ControllerSpec,
HttpErrors,
put,
param,
} from '@loopback/rest';
import {expect} from '@loopback/testlab';
import {CrudController} from '../..';
describe('CrudController', () => {
class Address extends ValueObject {}
@api({basePath: '/addresses', paths: {}})
class AddressController extends CrudController<Address> {
constructor(@repository('addressRepo') repo: CrudRepository<Address>) {
super();
this.repository = repo;
}
}
it('registers CRUD operations', () => {
const spec = getControllerSpec(AddressController);
const ops = getOperations(spec);
expect(ops).to.eql([
'get /: find',
'post /: createAll',
'post /updateAll: updateAll',
'post /deleteAll: deleteAll',
'get /count: count',
]);
});
});
describe('CrudController with overrides', () => {
class Address extends ValueObject {}
@api({basePath: '/addresses', paths: {}})
class AddressController extends CrudController<Address> {
constructor(@repository('addressRepo') repo: CrudRepository<Address>) {
super();
this.repository = repo;
}
/**
* An example to add pre/post processing logic for the base method
*/
async create(
dataObject: DataObject<Address>,
options?: Options,
): Promise<Address> {
console.log('Creating address %j', dataObject);
const address = await super.create(dataObject, options);
console.log('Address created: %j', address);
return address;
}
/**
* An example to disable an HTTP route exposed by the base method
*/
deleteAll() {
// Disable the `deleteAll` route
return Promise.reject(new HttpErrors.NotFound());
}
@put(`/updateAll`)
updateAll(
@param({name: '', in: 'body'})
dataObject: DataObject<Address>,
@param({name: 'where', required: false, in: 'query'})
where?: Where,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<number> {
return super.updateAll(dataObject, where, options);
}
}
it('registers CRUD operations', () => {
const spec = getControllerSpec(AddressController);
const ops = getOperations(spec);
expect(ops).to.eql([
'get /: find',
'post /: createAll',
'put /updateAll: updateAll',
'post /deleteAll: deleteAll',
'get /count: count',
]);
});
});
/**
* Build an array of readable routes from the controller spec
* @param spec Controller spec
*/
function getOperations(spec: ControllerSpec): string[] {
const operations: string[] = [];
for (const p in spec.paths) {
const path = spec.paths[p];
let verb, operationName;
for (const v of [
'delete',
'get',
'head',
'options',
'patch',
'post',
'put',
]) {
if (v in path) {
verb = v;
operationName = path[v]['x-operation-name'];
operations.push(`${verb} ${p}: ${operationName}`);
}
}
}
return operations;
}