brf/server/routes/api/users.ts

62 lines
1.5 KiB
TypeScript

import type { FastifyPluginCallbackZod } from 'fastify-type-provider-zod'
import knex from '../../lib/knex.ts'
import emitter from '../../lib/emitter.ts'
import Queries from '../../services/users/queries.ts'
const usersPlugin: FastifyPluginCallbackZod<{ addParentSchema: (schema: ANY) => void }> = (
fastify,
{ addParentSchema },
done,
) => {
const queries = Queries({ emitter, knex })
addParentSchema({
$id: 'user',
type: 'object',
properties: {
id: { type: 'integer' },
email: { type: 'string' },
password: { type: 'string' },
createdAt: { type: 'string' },
lastLoginAt: { type: 'string' },
lastLoginAttemptAt: { type: 'string' },
loginAttempts: { type: 'integer' },
bannedAt: { type: 'string' },
bannedById: { type: 'integer' },
blockedAt: { type: 'string' },
blockedById: { type: 'integer' },
emailVerifiedAt: { type: 'string' },
roles: { type: ['array', 'null'], items: { $ref: 'role' } },
},
})
addParentSchema({
$id: 'user-short',
type: 'object',
properties: {
id: { type: 'integer' },
email: { type: 'string' },
createdAt: { type: 'string' },
},
})
fastify.addHook('onRequest', fastify.auth)
fastify.route({
url: '/',
method: 'GET',
schema: {
response: {
200: { type: 'array', items: { $ref: 'user' } },
},
},
handler(request) {
return queries.find(request.query)
},
})
done()
}
export default usersPlugin