1 | import { expectType } from 'tsd'
|
2 | import createHttpError from 'http-errors'
|
3 |
|
4 | import Fastify, {
|
5 | FastifyReply,
|
6 | FastifyRequest,
|
7 | preHandlerHookHandler
|
8 | } from 'fastify'
|
9 | import fastifyGuard from '.'
|
10 |
|
11 | const fastify = Fastify()
|
12 |
|
13 | fastify.register(fastifyGuard, {
|
14 | errorHandler: (result, req, reply) => {
|
15 | return reply.send('string')
|
16 | },
|
17 | requestProperty: 'string',
|
18 | roleProperty: 'string',
|
19 | scopeProperty: 'string'
|
20 | })
|
21 |
|
22 | ;(request: FastifyRequest, reply: FastifyReply) => {
|
23 | expectType<boolean | createHttpError.HttpError>(
|
24 | fastify.guard.hasRole(request, 'user')
|
25 | )
|
26 |
|
27 | expectType<boolean | createHttpError.HttpError>(
|
28 | fastify.guard.hasScope(request, 'read')
|
29 | )
|
30 | }
|
31 | expectType<preHandlerHookHandler>(fastify.guard.role('ceo'))
|
32 | expectType<preHandlerHookHandler>(fastify.guard.role('ceo', 'cto'))
|
33 | expectType<preHandlerHookHandler>(fastify.guard.role(['string']))
|
34 | expectType<preHandlerHookHandler>(fastify.guard.scope('profile'))
|
35 | expectType<preHandlerHookHandler>(fastify.guard.scope('profile', 'blog'))
|
36 | expectType<preHandlerHookHandler>(fastify.guard.scope(['string']))
|