Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generics in extractor definition #13

Merged
merged 1 commit into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'))