-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |