This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
125 lines (121 loc) · 3.32 KB
/
index.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
"use strict";
const fp = require("fastify-plugin");
const S = require("fluent-json-schema").default;
/**
* @author Frazer Smith
* @description Plugin that adds a collection of shared schemas for re-use throughout the server.
* @param {import("fastify").FastifyInstance} server - Fastify instance.
*/
async function plugin(server) {
/**
* NOTE: `.definition()` definitions have been replaced with `.prop()` properties, and
* `.id()` ids removed due to definitions breaking in v4.12.1 of fastify-swagger.
* @see {@link https://github.com/fastify/fastify-swagger/issues/524 | fastify-swagger Issue #524}
*/
// Response schemas
server.addSchema(
S.object()
.id("responses")
.title("Responses")
.description("Common response schemas")
.prop(
"badRequest",
S.object()
.title("400 Bad Request")
.prop("statusCode", S.number().const(400))
.prop("error", S.string().const("Bad Request"))
.prop(
"message",
S.string().examples([
"No valid query string parameters provided",
])
)
)
.prop(
"unauthorized",
S.object()
.title("401 Unauthorized")
.prop("statusCode", S.number().const(401))
.prop("error", S.string().const("Unauthorized"))
.prop(
"message",
S.string().examples(["missing authorization header"])
)
)
.prop(
"notFoundDbResults",
S.object()
.title("404 Not Found Response")
.prop("statusCode", S.number().const(404))
.prop("error", S.string().const("Not Found"))
.prop(
"message",
S.string().examples([
"Invalid or expired search results",
"Record does not exist or has already been deleted",
"User not found",
])
)
)
.prop(
"notAcceptable",
S.object()
.title("406 Not Acceptable Response")
.prop("statusCode", S.number().const(406))
.prop("error", S.string().const("Not Acceptable"))
.prop("message", S.string().const("Not Acceptable"))
)
.prop(
"unsupportedMediaType",
S.object()
.title("415 Unsupported Media Type")
.prop("statusCode", S.number().const(415))
.prop("error", S.string().const("Unsupported Media Type"))
.prop(
"message",
S.string().examples([
"Unsupported Media Type: application/xml",
])
)
)
.prop(
"tooManyRequests",
S.object()
.title("429 Too Many Requests Response")
.prop("statusCode", S.number().const(429))
.prop("error", S.string().const("Too Many Requests"))
.prop(
"message",
S.string().examples([
"Rate limit exceeded, retry in 1 minute",
])
)
)
.prop(
"internalServerError",
S.object()
.title("500 Internal Server Error Response")
.prop("statusCode", S.number().const(500))
.prop("error", S.string().const("Internal Server Error"))
.prop(
"message",
S.string().pattern(
/^(?:Internal Server Error|Error.*)$/u
)
)
)
.prop(
"serviceUnavailable",
S.object()
.title("503 Service Unavailable")
.prop("statusCode", S.number().const(503))
.prop("code", S.string().const("FST_UNDER_PRESSURE"))
.prop("error", S.string().const("Service Unavailable"))
.prop("message", S.string().const("Service Unavailable"))
)
);
}
module.exports = fp(plugin, {
fastify: "4.x",
name: "shared-schemas",
});