This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
sample2.js
119 lines (103 loc) · 3.84 KB
/
sample2.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
'use strict';
/**************************************************************************************************
* This sample demonstrates a few more advanced features of Swagger Express Middleware,
* such as setting a few options, initializing the mock data store, and adding custom middleware logic.
**************************************************************************************************/
const util = require('util');
const path = require('path');
const express = require('express');
const swagger = require('@apidevtools/swagger-express-middleware');
const Middleware = swagger.Middleware;
const MemoryDataStore = swagger.MemoryDataStore;
const Resource = swagger.Resource;
let app = express();
let middleware = new Middleware(app);
// Initialize Swagger Express Middleware with our Swagger file
let swaggerFile = path.join(__dirname, 'PetStore.yaml');
middleware.init(swaggerFile, (err) => {
// Create a custom data store with some initial mock data
let myDB = new MemoryDataStore();
myDB.save(
new Resource('/pets/Lassie', { name: 'Lassie', type: 'dog', tags: ['brown', 'white']}),
new Resource('/pets/Clifford', { name: 'Clifford', type: 'dog', tags: ['red', 'big']}),
new Resource('/pets/Garfield', { name: 'Garfield', type: 'cat', tags: ['orange']}),
new Resource('/pets/Snoopy', { name: 'Snoopy', type: 'dog', tags: ['black', 'white']}),
new Resource('/pets/Hello%20Kitty', { name: 'Hello Kitty', type: 'cat', tags: ['white']})
);
// Enable Express' case-sensitive and strict options
// (so "/pets/Fido", "/pets/fido", and "/pets/fido/" are all different)
app.enable('case sensitive routing');
app.enable('strict routing');
app.use(middleware.metadata());
app.use(middleware.files(
{
// Override the Express App's case-sensitive and strict-routing settings
// for the Files middleware.
caseSensitive: false,
strict: false
},
{
// Serve the Swagger API from "/swagger/api" instead of "/api-docs"
apiPath: '/swagger/api',
// Disable serving the "PetStore.yaml" file
rawFilesPath: false
}
));
app.use(middleware.parseRequest(
{
// Configure the cookie parser to use secure cookies
cookie: {
secret: 'MySuperSecureSecretKey'
},
// Don't allow JSON content over 100kb (default is 1mb)
json: {
limit: '100kb'
},
// Change the location for uploaded pet photos (default is the system's temp directory)
multipart: {
dest: path.join(__dirname, 'photos')
}
}
));
// These two middleware don't have any options (yet)
app.use(
middleware.CORS(),
middleware.validateRequest()
);
// Add custom middleware
app.patch('/pets/:petName', (req, res, next) => {
if (req.body.name !== req.params.petName) {
// The pet's name has changed, so change its URL.
// Start by deleting the old resource
myDB.delete(new Resource(req.path), (err, pet) => {
if (pet) {
// Merge the new data with the old data
pet.merge(req.body);
}
else {
pet = req.body;
}
// Save the pet with the new URL
myDB.save(new Resource('/pets', req.body.name, pet), (err, pet) => {
// Send the response
res.json(pet.data);
});
});
}
else {
next();
}
});
// The mock middleware will use our custom data store,
// which we already pre-populated with mock data
app.use(middleware.mock(myDB));
// Add a custom error handler that returns errors as HTML
app.use((err, req, res, next) => {
res.status(err.status);
res.type('html');
res.send(util.format('<html><body><h1>%d Error!</h1><p>%s</p></body></html>', err.status, err.message));
});
app.listen(8000, () => {
console.log('The Swagger Pet Store is now running at http://localhost:8000');
});
});