forked from hapi-swagger/hapi-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot-grouping.js
More file actions
137 lines (123 loc) · 3.3 KB
/
Copy pathdot-grouping.js
File metadata and controls
137 lines (123 loc) · 3.3 KB
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
'use strict';
// `dot-grouping.js`
// This file also shows the use of `pathReplacements` to group endpoints by replacing `.` with '/`
// in the group naming
const Hapi = require('hapi');
const Blipp = require('blipp');
const Inert = require('inert');
const Vision = require('vision');
const HapiSwagger = require('../');
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
const goodOptions = {
ops: {
interval: 1000
},
reporters: {
console: [
{
module: 'good-squeeze',
name: 'Squeeze',
args: [
{
log: '*',
response: '*'
}
]
},
{
module: 'good-console'
},
'stdout'
]
}
};
const swaggerOptions = {
pathPrefixSize: 2,
basePath: '/api/',
info: {
title: 'Test API Documentation',
description: 'This is a sample example of API documentation.'
},
pathReplacements: [
{
replaceIn: 'groups',
pattern: /[.].*$/,
replacement: '/'
}
]
};
// Start the server
server.register(
[
Inert,
Vision,
Blipp,
{
register: require('good'),
options: goodOptions
},
{
register: HapiSwagger,
options: swaggerOptions
}
],
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 users = [
{
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/user.get',
handler: function(request, reply) {
return reply(users);
},
config: {
tags: ['api']
}
});
server.route({
method: 'GET',
path: '/api/user.search',
handler: function(request, reply) {
return reply(users);
},
config: {
tags: ['api']
}
});
// 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);
});
}
);