forked from hapi-swagger/hapi-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversions.js
254 lines (234 loc) · 7.17 KB
/
versions.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
console.log('The plugin "hapi-api-version" does not yet support HAPI v17.x. This example will be updated at a later date.');
/*
// `versions.js` - how to use plug-in with `hapi-api-version` for versioning of an API
// This file also shows the use of `pathReplacements` to remove all version numbers in paths
// The routes are versioned ie '/api/v1/users' and '/api/v2/users'
const Hapi = require('hapi');
const Joi = require('joi');
const Blipp = require('blipp');
const Inert = require('inert');
const Vision = require('vision');
const HapiApiVersion = require('hapi-api-version');
const HapiSwagger = require('../');
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
const options = {
basePath: '/api/',
validVersions: [1, 2],
defaultVersion: 2
};
const goodOptions = {
ops: {
interval: 1000
},
reporters: {
console: [
{
module: 'good-squeeze',
name: 'Squeeze',
args: [
{
log: '*',
response: '*'
}
]
},
{
module: 'good-console'
},
'stdout'
]
}
};
const versionOptions = {
basePath: options.basePath,
validVersions: options.validVersions,
defaultVersion: options.defaultVersion,
vendorName: 'mysuperapi'
};
const swaggerOptions = {
pathPrefixSize: 2,
basePath: options.basePath,
info: {
title: 'Test API Documentation',
description: 'This is a sample example of API documentation.'
},
pathReplacements: [
{
replaceIn: 'all',
pattern: /v([0-9]+)\//,
replacement: ''
}
],
deReference: false
};
// Start the server
server.register(
[
Inert,
Vision,
Blipp,
{
register: require('good'),
options: goodOptions
},
{
register: HapiSwagger,
options: swaggerOptions
},
{
register: HapiApiVersion,
options: versionOptions
}
],
err => {
if (err) {
throw err;
}
// Add a route - handler and route definition is the same for all versions
server.route({
method: 'GET',
path: '/version',
handler: function(request, reply) {
// Return the api-version which was requested
return reply({
version: request.pre.apiVersion
});
}
});
const usersVersion1Model = Joi.array()
.items(
Joi.object({
name: Joi.string().label('name')
}).label('User v1')
)
.label('Users v1');
const usersVersion1 = [
{
name: 'Peter Miller'
}
];
const usersVersion2Model = Joi.array()
.items(
Joi.object({
firstname: Joi.string().label('firstname'),
lastname: Joi.string().label('lastname')
}).label('User v2')
)
.label('Users v2');
const usersVersion2 = [
{
firstname: 'Peter',
lastname: 'Miller'
}
];
// Add a versioned route - which is actually two routes with prefix '/v1' and '/v2'. Not only the
// handlers are different, but also the route definition itself (like here with response validation).
server.route({
method: 'GET',
path: '/api/v1/users',
handler: function(request, reply) {
return reply(usersVersion1).type(
'application/vnd.mysuperapi.v1+json'
);
},
config: {
tags: ['api', 'v1'],
plugins: {
'hapi-swagger': {
responses: {
'200': {
description: 'Success',
schema: usersVersion1Model
}
}
}
},
validate: {
headers: Joi.object({
accept: Joi.string().valid([
'application/vnd.mysuperapi.v1+json'
])
}).unknown()
}
}
});
server.route({
method: 'GET',
path: '/api/v2/users',
handler: function(request, reply) {
return reply(usersVersion2).type(
'application/vnd.mysuperapi.v2+json'
);
},
config: {
tags: ['api', 'v1', 'v2'],
plugins: {
'hapi-swagger': {
responses: {
'200': {
description: 'Success',
schema: usersVersion2Model
}
}
}
},
validate: {
headers: Joi.object({
accept: Joi.string().valid([
'application/vnd.mysuperapi.v2+json',
'application/vnd.mysuperapi.v1+json'
])
}).unknown()
}
}
});
server.route({
method: 'GET',
path: '/api/v2/users/{id}',
handler: function(request, reply) {
return reply(usersVersion2[0]).type(
'application/vnd.mysuperapi.v2+json'
);
},
config: {
tags: ['api', 'v2'],
plugins: {
'hapi-swagger': {
responses: {
'200': {
description: 'Success',
schema: usersVersion2Model
}
}
}
},
validate: {
params: {
id: Joi.number().required().description('id of user')
},
headers: Joi.object({
accept: Joi.string().valid([
'application/vnd.mysuperapi.v2+json'
])
}).unknown()
}
}
});
// Add a versioned route - This is a simple version of the '/users' route where just the handlers
// differ and even those just a little. It maybe is the preferred option if just the formatting of
// the response differs between versions.
// Start the server
server.start(err => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
}
);
*/