-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test-d.ts
70 lines (62 loc) · 1.73 KB
/
index.test-d.ts
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
import Hapi from 'hapi';
import Joi from 'typesafe-joi';
const server = new Hapi.Server();
/**
* User's codebase
*/
const payloadSchema = Joi.object({
user: Joi.object({
name: Joi.string().required(),
email: Joi.string().required(),
}).required(),
}).required();
const querySchema = Joi.object({
search: Joi.string().optional().allow('', null),
}).required();
const paramsSchema = Joi.object({
id: Joi.number().required()
}).required();
const responseSchema = Joi.object({
id: Joi.number().required(),
name: Joi.string().required(),
email: Joi.string().required(),
search: Joi.string().optional().allow(null),
}).required();
server.route({
method: 'POST',
path: '/:id',
options: {
validate: {
payload: payloadSchema,
query: querySchema,
params: paramsSchema,
},
response: {
schema: responseSchema,
},
},
handler(request) {
// type of `payload` is automatically inferred based on `options.validate.payload` schema
const payload = request.payload; // $ExpectType { user: { name: string; email: string; } & {}; } & {}
const query = request.query; // $ExpectType {} & { search?: string | null | undefined; }
const params = request.params; // $ExpectType { id: number; } & {}
// return type is also automatically inferred based on `options.response.schema`
return {
id: params.id,
name: payload.user.name, // $ExpectType string
email: payload.user.email, // $ExpectType string
search: query.search, // $ExpectType string | null | undefined
};
},
});
server.route({
method: 'GET',
path: '/health-check',
options: {
description: 'Health check endpoint',
tags: ['api'],
},
handler(_request) {
return null;
},
});