feat: migrate from express to fastify

This commit is contained in:
Divlo
2021-10-24 04:18:18 +02:00
parent 714cc643ba
commit b77e602358
281 changed files with 19768 additions and 22895 deletions

View File

@ -0,0 +1,53 @@
/** @type {import('node-plop').PlopGeneratorConfig} */
exports.serviceGenerator = {
description: 'REST API endpoint',
prompts: [
{
type: 'input',
name: 'url',
message: 'url'
},
{
type: 'list',
name: 'httpMethod',
message: 'httpMethod',
choices: ['GET', 'POST', 'PUT', 'DELETE']
},
{
type: 'input',
name: 'description',
message: 'description'
},
{
type: 'list',
name: 'tag',
message: 'tag',
choices: [
'users',
'guilds',
'channels',
'invitations',
'messages',
'members',
'uploads'
]
},
{
type: 'confirm',
name: 'shouldBeAuthenticated',
message: 'shouldBeAuthenticated'
}
],
actions: [
{
type: 'add',
path: 'src/services/{{url}}/{{lowerCase httpMethod}}.ts',
templateFile: 'generators/service/service.ts.hbs'
},
{
type: 'add',
path: 'src/services/{{url}}/__test__/{{lowerCase httpMethod}}.test.ts',
templateFile: 'generators/service/service.test.ts.hbs'
}
]
}

View File

@ -0,0 +1,26 @@
import { application } from 'application.js'
{{#if shouldBeAuthenticated}}
import { authenticateUserTest } from '__test__/utils/authenticateUserTest.js'
{{/if}}
import { prismaMock } from '__test__/setup.js'
describe('{{httpMethod}} {{url}}', () => {
it('succeeds', async () => {
// prismaMock.service.findUnique.mockResolvedValue(null)
{{#if shouldBeAuthenticated}}
const { accessToken, user } = await authenticateUserTest()
{{/if}}
const response = await application.inject({
method: '{{httpMethod}}',
url: '{{url}}',
{{#if shouldBeAuthenticated}}
headers: {
authorization: `Bearer ${accessToken}`
},
{{/if}}
payload: {}
})
// const responseJson = response.json()
expect(response.statusCode).toEqual(200)
})
})

View File

@ -0,0 +1,59 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from 'tools/database/prisma.js'
import { fastifyErrors } from 'models/utils.js'
{{#if shouldBeAuthenticated}}
import authenticateUser from '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 {}
}
})
}