Skip to content

Commit

Permalink
Add support for generics in extractor definition (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Jan 17, 2021
1 parent 8ca51f0 commit fe4eaa5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
22 changes: 18 additions & 4 deletions plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
/// <reference types="node" />

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<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
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<RouteGeneric, RawServer, RawRequest>) => string) | string,
getObj?: ((request: FastifyRequest<RouteGeneric, RawServer, RawRequest>) => string) | string,
getAct?: ((request: FastifyRequest<RouteGeneric, RawServer, RawRequest>) => string) | string
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion test/plugin.test-d.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -47,3 +47,18 @@ server.get('/entity', {
}
}
}, () => Promise.resolve('ok'))


interface ListRequest extends RequestGenericInterface {
Params: {
listID: string
}
}

server.get<ListRequest>('/', {
casbin: {
rest: {
getObj: (request) => request.params.listID,
}
}
}, () => Promise.resolve('ok'))

0 comments on commit fe4eaa5

Please sign in to comment.