-
Notifications
You must be signed in to change notification settings - Fork 35
/
config.js
173 lines (154 loc) · 4.44 KB
/
config.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
const Hoek = require('hoek');
const Joi = require('joi');
const _ = require('lodash');
const internals = {
defaults: {
query: {
page: {
name: 'page',
default: 1
},
limit: {
name: 'limit',
default: 25
},
pagination: {
name: 'pagination',
default: true
},
invalid: 'defaults'
},
meta: {
baseUri: '',
name: 'meta',
count: {
active: true,
name: 'count'
},
totalCount: {
active: true,
name: 'totalCount'
},
pageCount: {
active: true,
name: 'pageCount'
},
self: {
active: true,
name: 'self'
},
previous: {
active: true,
name: 'previous'
},
next: {
active: true,
name: 'next'
},
first: {
active: true,
name: 'first'
},
last: {
active: true,
name: 'last'
},
// name == default.query.page.name
page: {
active: false
},
// name == default.query.limit.name
limit: {
active: false
}
},
results: {
name: 'results'
},
reply: {
paginate: 'paginate'
},
routes: {
include: ['*'],
exclude: []
}
},
schemas: {}
};
internals.schemas.metaParameter = Joi.object({
active: Joi.boolean().required(),
name: Joi.string().required()
});
internals.schemas.options = Joi.object({
query: Joi.object({
page: Joi.object({
name: Joi.string().required(),
default: Joi.number().integer().positive().required()
}),
limit: Joi.object({
name: Joi.string().required(),
default: Joi.number().integer().positive().required()
}),
pagination: Joi.object({
name: Joi.string().required(),
default: Joi.boolean().required()
}),
invalid: Joi.string().required()
}),
meta: Joi.object({
baseUri: Joi.string().allow(''),
name: Joi.string().required(),
count: internals.schemas.metaParameter,
totalCount: internals.schemas.metaParameter,
pageCount: internals.schemas.metaParameter,
self: internals.schemas.metaParameter,
previous: internals.schemas.metaParameter,
next: internals.schemas.metaParameter,
first: internals.schemas.metaParameter,
last: internals.schemas.metaParameter,
page: Joi.object({
active: Joi.boolean().required()
}),
limit: Joi.object({
active: Joi.boolean().required()
})
}),
results: Joi.object({
name: Joi.string().required()
}),
reply: Joi.object({
paginate: Joi.string().required()
}),
routes: Joi.object({
include: Joi.array().items(Joi.alternatives().try(Joi.object().type(RegExp), Joi.string())), // May register no routes...
exclude: Joi.array().items(Joi.alternatives().try(Joi.object().type(RegExp), Joi.string()))
})
});
internals.schemas.routeOptions = Joi.object({
enabled: Joi.boolean(),
defaults: Joi.object({
page: Joi.number().integer().positive(),
limit: Joi.number().integer().positive(),
pagination: Joi.boolean()
})
});
module.exports = {
getConfig: function (server, options) {
const config = Hoek.applyToDefaults(internals.defaults, options);
const routeSettings = _.map(server.table()[0].table, (item) => item.settings);
// Check main config
const result = Joi.validate(config, internals.schemas.options);
if (result.error) {
throw result.error;
}
// Check each route settings if config is valid
_.each(routeSettings, (setting) => {
const res = Joi.validate(setting.plugins.pagination, internals.schemas.routeOptions);
if (res.error) {
throw res.error;
}
});
return result.value;
}
};