Skip to content

Commit bca02ad

Browse files
feat(plugin): route validation > controller validation
feat(package): use eslint instead of tslint
1 parent 80f0a63 commit bca02ad

File tree

13 files changed

+3508
-1922
lines changed

13 files changed

+3508
-1922
lines changed

.eslintrc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": [
3+
"airbnb-typescript/base"
4+
],
5+
"rules": {
6+
"import-name": 0,
7+
"variable-name": 0,
8+
"arrow-parens": 0,
9+
"max-classes-per-file": 0,
10+
"no-param-reassign": 0,
11+
"no-restricted-syntax": ["error", "ForInStatement", "LabeledStatement", "WithStatement"],
12+
"no-continue": 0
13+
},
14+
"parserOptions": {
15+
"ecmaVersion": 2017,
16+
"sourceType": "module",
17+
"project": "./tsconfig.test.json"
18+
},
19+
"ignorePatterns": ["/dist/**"]
20+
}

lib/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
import Plugin from './plugin';
2+
13
export { default as ResourceRouter } from './resource_router';
24
export * from './resource_router';
3-
import Plugin from './plugin';
45
export default Plugin;

lib/plugin.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,26 @@ function isSubscription(route: Route): route is SubscriptionRoute {
5050

5151
class Internals {
5252
options: PluginOptions;
53+
5354
controllerMap?: ControllerMap;
55+
5456
constructor(options: PluginOptions) {
5557
this.options = options;
5658
}
59+
5760
resolveController(name: string, ...args: any[]) {
5861
if (!(this.controllerMap && this.controllerMap[name])) {
5962
throw new Error(`Missing controller ${name}`);
6063
}
6164

6265
const controller = this.controllerMap[name];
6366
if (typeof controller === 'function') {
64-
return new controller(...args);
67+
const ControllerSubclass = controller;
68+
return new ControllerSubclass(...args);
6569
}
6670
return controller;
6771
}
72+
6873
resolveControllerForRoute(route: Route) {
6974
if (Array.isArray(route.controller)) {
7075
return this.resolveController(route.controller[0], ...route.controller.slice(1));
@@ -74,7 +79,8 @@ class Internals {
7479
}
7580
return route.controller!;
7681
}
77-
getHandler(route: Route, controller: Controller) {
82+
83+
static getHandler(route: Route, controller: Controller) {
7884
if (typeof route.action === 'string') {
7985
if (!(route.controller && controller[route.action])) {
8086
return null;
@@ -83,7 +89,8 @@ class Internals {
8389
}
8490
return route.action;
8591
}
86-
getSubscriptionHandler(
92+
93+
static getSubscriptionHandler(
8794
route: SubscriptionRoute,
8895
controller: Controller,
8996
name: 'filter'|'onSubscribe'|'onUnsubscribe',
@@ -97,13 +104,14 @@ class Internals {
97104
}
98105
return config;
99106
}
100-
resolveControllerValidator(
107+
108+
static resolveControllerValidator(
101109
route: Route,
102110
controller: Controller,
103111
key: 'params'|'query'|'response'|'payload',
104112
) {
105-
if (key === 'payload' && this.skipPayloadValidation(route)) {
106-
return;
113+
if (key === 'payload' && Internals.skipPayloadValidation(route)) {
114+
return undefined;
107115
}
108116

109117
// tslint:disable-next-line: max-line-length
@@ -113,11 +121,11 @@ class Internals {
113121
}
114122

115123
if (typeof route.action !== 'string') {
116-
return null;
124+
return undefined;
117125
}
118126

119127
if (!controller) {
120-
return null;
128+
return undefined;
121129
}
122130

123131
let validator = controller.validate;
@@ -126,12 +134,12 @@ class Internals {
126134
}
127135

128136
if (!validator) {
129-
return null;
137+
return undefined;
130138
}
131139

132140
const validationEntry = validator[key];
133141
if (!validationEntry) {
134-
return null;
142+
return undefined;
135143
}
136144

137145
let validate;
@@ -142,35 +150,38 @@ class Internals {
142150
}
143151
return validate;
144152
}
145-
buildValidate(route: Route, controller: Controller) {
153+
154+
static buildValidate(route: Route, controller: Controller) {
146155
return {
147-
params: this.resolveControllerValidator(
156+
params: Internals.resolveControllerValidator(
148157
route,
149158
controller,
150159
'params',
151160
),
152-
query: this.resolveControllerValidator(
161+
query: Internals.resolveControllerValidator(
153162
route,
154163
controller,
155164
'query',
156165
),
157-
response: this.resolveControllerValidator(
166+
response: Internals.resolveControllerValidator(
158167
route,
159168
controller,
160169
'response',
161170
),
162-
payload: this.resolveControllerValidator(
171+
payload: Internals.resolveControllerValidator(
163172
route,
164173
controller,
165174
'payload',
166175
),
167176
};
168177
}
169-
skipPayloadValidation(route: Route) {
178+
179+
static skipPayloadValidation(route: Route) {
170180
return skipPayloadValidationMethods.has(route.method);
171181
}
182+
172183
async onPostStart(server: Hapi.Server) {
173-
const routes = server.resources().routes;
184+
const { routes } = server.resources();
174185

175186
if (this.options.controllers) {
176187
if (typeof this.options.controllers === 'function') {
@@ -192,9 +203,9 @@ class Internals {
192203
path,
193204
{
194205
...route.config,
195-
filter: this.getSubscriptionHandler(route, controller, 'filter'),
196-
onSubscribe: this.getSubscriptionHandler(route, controller, 'onSubscribe'),
197-
onUnsubscribe: this.getSubscriptionHandler(route, controller, 'onUnsubscribe'),
206+
filter: Internals.getSubscriptionHandler(route, controller, 'filter'),
207+
onSubscribe: Internals.getSubscriptionHandler(route, controller, 'onSubscribe'),
208+
onUnsubscribe: Internals.getSubscriptionHandler(route, controller, 'onUnsubscribe'),
198209
},
199210
);
200211
}
@@ -204,7 +215,7 @@ class Internals {
204215
server.route({
205216
path,
206217
method: route.method,
207-
handler: this.getHandler(route, controller),
218+
handler: Internals.getHandler(route, controller),
208219
options: {
209220
id: name,
210221
description: route.description,
@@ -213,7 +224,7 @@ class Internals {
213224
tags: route.tags.all(),
214225
pre: route.pre.all(),
215226
payload: route.payload,
216-
validate: this.buildValidate(route, controller),
227+
validate: Internals.buildValidate(route, controller),
217228
plugins: {
218229
...route.plugins,
219230
resourceRouter: {

0 commit comments

Comments
 (0)