本文整理汇总了TypeScript中swagger-express-middleware.MemoryDataStore类的典型用法代码示例。如果您正苦于以下问题:TypeScript MemoryDataStore类的具体用法?TypeScript MemoryDataStore怎么用?TypeScript MemoryDataStore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MemoryDataStore类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: MemoryDataStore
middlewareInstance.init(path.join(__dirname, 'PetStore.yaml'), err => {
// Create a custom data store with some initial mock data
var 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(...middlewareInstance.metadata());
app.use(...middlewareInstance.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(...middlewareInstance.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(
...middlewareInstance.CORS(),
...middlewareInstance.validateRequest()
);
// Add custom middleware
app.patch('/pets/:petName', function(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), function(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), function(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(...middlewareInstance.mock(myDB));
// Add a custom error handler that returns errors as HTML
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
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, function() {
console.log('The Swagger Pet Store is now running at http://localhost:8000');
});
//.........这里部分代码省略.........
开发者ID:swissspidy,项目名称:typed-swagger-express-middleware,代码行数:101,代码来源:swagger-express-middleware-test.ts
示例2: Resource
myDB.delete(new Resource(req.path), function(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), function(err, pet) {
// Send the response
res.json(pet.data);
});
});
开发者ID:swissspidy,项目名称:typed-swagger-express-middleware,代码行数:15,代码来源:swagger-express-middleware-test.ts
示例3: function
app.patch('/pets/:petName', function(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), function(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), function(err, pet) {
// Send the response
res.json(pet.data);
});
});
}
else {
next();
}
});
开发者ID:swissspidy,项目名称:typed-swagger-express-middleware,代码行数:24,代码来源:swagger-express-middleware-test.ts
注:本文中的swagger-express-middleware.MemoryDataStore类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论