forked from hapi-swagger/hapi-swagger
-
Notifications
You must be signed in to change notification settings - Fork 2
/
swagger-client.js
184 lines (165 loc) · 3.73 KB
/
swagger-client.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
// `swagger-client.js` - how to plug-in to build an interface with `swagger-client`
const Hapi = require('@hapi/hapi');
const Joi = require('@hapi/joi');
const Swagger = require('swagger-client');
const HapiSwagger = require('../');
let swaggerOptions = {
documentationPage: false,
swaggerUI: false,
tags: [
{
name: 'sum'
},
{
name: 'math'
},
{
name: 'mathematics'
}
],
deReference: true
};
const defaultHandler = function(request, h) {
let a = parseFloat(request.params.a);
let b = parseFloat(request.params.b);
return h.response({
a: a,
b: b,
operator: '+',
equals: a + b,
created: new Date().toISOString(),
modified: new Date().toISOString()
});
};
const routes = [
{
method: 'PUT',
path: '/sum/add/{a}/{b}',
options: {
handler: defaultHandler,
description: 'Add',
tags: ['api'],
plugins: {
'hapi-swagger': {
id: 'add' // refer to notes above
}
},
validate: {
params: {
a: Joi.number()
.required()
.description('the first number'),
b: Joi.number()
.required()
.description('the second number')
}
}
}
},
{
method: 'PUT',
path: '/math/add/{a}/{b}',
options: {
handler: defaultHandler,
description: 'Add',
tags: ['api'],
plugins: {
'hapi-swagger': {
id: 'add' // refer to notes above
}
},
validate: {
params: {
a: Joi.number()
.required()
.description('the first number'),
b: Joi.number()
.required()
.description('the second number')
}
}
}
},
{
method: 'PUT',
path: '/mathematics/add/{a}/{b}',
options: {
handler: defaultHandler,
description: 'Add',
tags: ['api'],
validate: {
params: {
a: Joi.number()
.required()
.description('the first number'),
b: Joi.number()
.required()
.description('the second number')
}
}
}
}
];
const ser = async () => {
const server = Hapi.Server({
host: 'localhost',
port: 3000
});
// Blipp - Needs updating for Hapi v17.x
await server.register([
{
plugin: HapiSwagger,
options: swaggerOptions
}
]);
server.route(routes);
await server.start();
return server;
};
ser()
.then(server => {
console.log(`Server listening on ${server.info.uri}`);
// create swagger client using json output from plugin
Swagger(server.info.uri + '/swagger.json').then(client => {
// Three calls to API using swagger client
client.apis.math
.add1({ a: 8, b: 8 }, { responseContentType: 'application/json' })
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
client.apis.sum
.add2({ a: 7, b: 7 }, { responseContentType: 'application/json' })
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
client.apis.mathematics
.putMathematicsAddAB({ a: 9, b: 9 }, { responseContentType: 'application/json' })
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
client
.execute({
operationId: 'add',
parameters: { a: 9, b: 9 }
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
});
})
.catch(err => {
console.error(err);
process.exit(1);
});