Skip to content

Commit

Permalink
types (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
meghein authored Jan 24, 2024
1 parent db3886b commit 518325e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
25 changes: 19 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import type { FastifyPluginCallback } from 'fastify'
import type { FastifyPluginCallback, FastifyRequest } from 'fastify'
import { Oso } from 'oso'

export interface PluginOptions {
mandatory: string
export type SetupOsoFunction = (oso: Oso) => Promise<Oso>

export type AuthorizeRequestFunction = (actor: object, request: FastifyRequest) => Promise<void>
export interface FastifyOsoOptions {
setupOso: SetupOsoFunction
}

declare module 'fastify' {
interface FastifyInstance {
oso: Oso
}
interface FastifyRequest {
authorizeRequest: AuthorizeRequestFunction
}
}

declare const fastifyPluginTemplate: FastifyPluginCallback<PluginOptions>
declare const fastifyOso: FastifyPluginCallback<FastifyOsoOptions>

export default fastifyPluginTemplate
export { fastifyPluginTemplate }
export default fastifyOso
export { fastifyOso }
25 changes: 19 additions & 6 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import fastify, { FastifyInstance } from 'fastify'
import fastify, { FastifyInstance, FastifyRequest } from 'fastify'
import { Oso } from 'oso'
// eslint-disable-next-line import/no-unresolved
import { expectAssignable, expectError } from 'tsd'

import fastifyPluginTemplate from '..'
import fastifyOso from '..'

import type { AuthorizeRequestFunction } from './index.d.ts'

const app = fastify()

const opt1 = {
mandatory: 'string'
async setupOso (oso: Oso) {
return oso
}
}

expectAssignable<FastifyInstance>(app.register(fastifyPluginTemplate, opt1))
expectAssignable<FastifyInstance>(app.register(fastifyOso, opt1))

app.register(fastifyOso, opt1).after(() => {
app.addHook('onRequest', async (request: FastifyRequest) => {
const actor = { id: '123', role: 'user' }
expectAssignable<AuthorizeRequestFunction>(request.authorizeRequest)
await expectAssignable<Promise<void>>(request.authorizeRequest(actor, request))
})
})

const errOpt1 = {
error: true
setupOso: {}
}

expectError(app.register(fastifyPluginTemplate, errOpt1))
expectError(app.register(fastifyOso, errOpt1))

0 comments on commit 518325e

Please sign in to comment.