This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.ts.hbs
59 lines (53 loc) · 1.62 KB
/
service.ts.hbs
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
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
{{#if shouldBeAuthenticated}}
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
{{/if}}
const body{{sentenceCase httpMethod}}ServiceSchema = Type.Object({
property: Type.String()
})
type Body{{sentenceCase httpMethod}}ServiceSchemaType = Static<typeof body{{sentenceCase httpMethod}}ServiceSchema>
const {{lowerCase httpMethod}}ServiceSchema: FastifySchema = {
description: '{{description}}',
tags: ['{{tag}}'] as string[],
{{#if shouldBeAuthenticated}}
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
{{/if}}
body: body{{sentenceCase httpMethod}}ServiceSchema,
response: {
200: Type.Object({}),
400: fastifyErrors[400],
{{#if shouldBeAuthenticated}}
401: fastifyErrors[401],
403: fastifyErrors[403],
{{/if}}
500: fastifyErrors[500]
}
} as const
export const {{lowerCase httpMethod}}Service: FastifyPluginAsync = async (fastify) => {
{{#if shouldBeAuthenticated}}
await fastify.register(authenticateUser)
{{/if}}
fastify.route<{
Body: Body{{sentenceCase httpMethod}}ServiceSchemaType
}>({
method: '{{httpMethod}}',
url: '{{url}}',
schema: {{lowerCase httpMethod}}ServiceSchema,
handler: async (request, reply) => {
{{#if shouldBeAuthenticated}}
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
{{/if}}
reply.statusCode = 200
return {}
}
})
}