-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.ts
63 lines (54 loc) · 1.43 KB
/
routes.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
import { error } from 'console'
import { FastifyInstance } from 'fastify'
interface TQuerystring {
int1: number
int2: number
limit: number
str1: string
str2: string
}
const queryStringSchema = {
schema: {
querystring: {
type: 'object',
properties: {
int1: { type: 'number' },
int2: { type: 'number' },
limit: { type: 'number' },
str1: { type: 'string' },
str2: { type: 'string' },
},
},
},
}
// Example: curl http://127.0.0.1:3000/fizzbuzz\?int1\=13\&int2\=4\&limit\=100\&str1\=fizz\&str2\=buzz
async function routes(fastify: FastifyInstance) {
fastify.get<{ Querystring: TQuerystring }>(
'/fizzbuzz',
queryStringSchema,
(request, reply) => {
const { int1, int2, limit, str1, str2 } = request.query
switch (true) {
case int1 == null:
case int2 == null:
case limit == null:
case str1 == null || str1 == '':
case str2 == null || str2 == '':
reply.status(404).send('Wrong parameter')
default:
}
const output: (number | string)[] = []
for (let i = 1; i <= limit; i++) {
if (i % int1 === 0 && i % int2 === 0) {
output.push(str1 + str2)
} else if (i % int2 === 0) {
output.push(str2)
} else if (i % int1 === 0) {
output.push(str1)
} else output.push(i)
}
reply.send({ output })
}
)
}
export { routes }