From fe4eaa53916c0b0d82ed28561e43f25c3112702e Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Sun, 17 Jan 2021 23:35:03 +0200 Subject: [PATCH] Add support for generics in extractor definition (#13) --- plugin.d.ts | 22 ++++++++++++++++++---- test/plugin.test-d.ts | 17 ++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/plugin.d.ts b/plugin.d.ts index a562c83..926bdf0 100644 --- a/plugin.d.ts +++ b/plugin.d.ts @@ -1,14 +1,28 @@ /// import { FastifyInstance, FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify' +import { + ContextConfigDefault, + RawReplyDefaultExpression, + RawRequestDefaultExpression, + RawServerBase, + RawServerDefault +} from 'fastify/types/utils' +import { RouteGenericInterface } from 'fastify/types/route' declare module 'fastify' { - interface RouteShorthandOptions { + interface RouteShorthandOptions< + RawServer extends RawServerBase = RawServerDefault, + RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, + RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, + RouteGeneric extends RouteGenericInterface = RouteGenericInterface, + ContextConfig = ContextConfigDefault + > { casbin?: { rest?: boolean | { - getSub?: ((request: FastifyRequest) => string) | string, - getObj?: ((request: FastifyRequest) => string) | string, - getAct?: ((request: FastifyRequest) => string) | string + getSub?: ((request: FastifyRequest) => string) | string, + getObj?: ((request: FastifyRequest) => string) | string, + getAct?: ((request: FastifyRequest) => string) | string } } } diff --git a/test/plugin.test-d.ts b/test/plugin.test-d.ts index 3fcb448..1858699 100644 --- a/test/plugin.test-d.ts +++ b/test/plugin.test-d.ts @@ -1,4 +1,4 @@ -import fastify, { FastifyReply, FastifyRequest } from 'fastify' +import fastify, { FastifyReply, FastifyRequest, RequestGenericInterface } from 'fastify' import { expectType } from 'tsd' import casbinRest from '../plugin' @@ -47,3 +47,18 @@ server.get('/entity', { } } }, () => Promise.resolve('ok')) + + +interface ListRequest extends RequestGenericInterface { + Params: { + listID: string + } +} + +server.get('/', { + casbin: { + rest: { + getObj: (request) => request.params.listID, + } + } +}, () => Promise.resolve('ok'))