-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
122 lines (114 loc) · 2.89 KB
/
example.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
var express = require('express')
, flair = require('./lib/index')
, joi = require('joi')
, flairui = require('flair-ui')
, app = express()
, cheeses = [
{ id: 1, name: 'edam' }
, { id: 2, name: 'gouda' }
, { id: 3, name: 'cheddar' }
, { id: 4, name: 'emmental' }
];
function byId(value) {
return function(item) {
return item.id == value;
};
}
//to validate against a json schema with a custom mime type we need
//this middleware
app.use(flair.jsonBodyParser());
//register our schema with flair
flair.addContentTypes([
{
type: "application/vnd.cheese+json",
schema: {
id: "cheese",
type: "object",
properties: {
name: {
description: "The name of the cheese",
type: "string",
required: true
},
id: {
description: "The id of the cheese",
type: "integer",
required: false //if a cheese has no id, we'll generate one
}
},
//no null or undefined cheeses, thank you
required: true
}
},
{
type: "application/vnd.cheese-list+json",
schema: {
id: "cheeseList",
type: "Array",
items: {
//this reference will work in swagger, but not
//real json-schema
"$ref": "cheese"
}
}
}
]);
app.get(
'/cheese',
flair.describe("cheeseList", "All cheeses", "Returns all the cheeses"),
flair.produces("application/vnd.cheese-list+json"),
function(req, res) {
res.json(cheeses);
}
);
app.post(
'/cheese',
flair.describe("createCheese", "Create cheese", "Adds a new cheese to the system"),
flair.consumes("application/vnd.cheese+json"),
function(req, res) {
var newCheese = req.body;
cheeses.push(newCheese);
res.send(201);
}
);
app.get(
'/cheese/:id',
flair.describe("getCheese", "Get cheese", "Returns a single cheese, selected by id"),
flair.validate({
id: joi.types.Number().integer().required().notes("path").description("cheese id")
}),
flair.produces("application/vnd.cheese+json"),
function(req, res) {
var cheese = cheeses.filter(byId(req.param("id")))[0];
if (cheese) {
res.header('content-type', 'application/vnd.cheese+json');
res.json(cheese);
} else {
res.send(404);
}
}
);
app.put(
'/cheese/:id',
flair.describe("updateCheese", "Update cheese", "Update an existing cheese"),
flair.validate({
id: joi.types.Number().integer().required().notes("path").description("cheese id")
}),
flair.consumes("application/vnd.cheese+json"),
function(req, res) {
var cheese = cheeses.filter(byId(req.param("id")))[0];
if (cheese) {
cheese.name = req.body.name;
res.json(cheese);
} else {
res.send(404);
}
}
);
app.use(flair.swagger(app, {
docPath: '/api-docs',
basePath: "http://localhost:3000/",
version: '0.1'
}));
app.use('/docs', flairui('/api-docs', { supportHeaderParams: true }));
app.listen(3000);