Skip to content

Commit

Permalink
plugin types with tests (#36)
Browse files Browse the repository at this point in the history
* wip: plugin types

* wip: declare `fastify` decorators

* fix: plugin is a `FastifyPluginCallback` - not `async`

* wip: `import type`s

* wip: initial tests for plugin

* wip: refactor types test

* wip: shorten type names

* wip: add decorator tests

* wip: test shape of plugin

* wip: add types to `package.json`
  • Loading branch information
mikaelkaron authored Aug 7, 2023
1 parent 1110d4f commit 8705d76
Show file tree
Hide file tree
Showing 4 changed files with 1,511 additions and 2 deletions.
48 changes: 48 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { type FastifyJWTOptions, type VerifyPayloadType } from '@fastify/jwt'
import { type FastifyPluginCallback, type FastifyReply, type FastifyRequest } from 'fastify'
import { type GetJwksOptions } from 'get-jwks'
import { type URL, type UrlObject } from 'url'

export interface JWTOptions extends FastifyJWTOptions {
namespace?: string
jwks?: boolean | GetJwksOptions
}

export interface WebhookOptions {
url: string | URL | UrlObject
}

export type CreateSession = (request?: FastifyRequest, reply?: FastifyReply) => Promise<void>

export interface AuthStrategy {
name: string,
createSession: CreateSession
}

export interface FastifyUserPluginOptions {
jwt?: JWTOptions
webhook?: WebhookOptions
authStrategies?: AuthStrategy[]
}

export type AddAuthStrategyDecorator = (strategy: AuthStrategy) => void
export type ExtractUserDecorator = () => Promise<any>
export type CreateSessionDecorator = () => Promise<void>
export type CreateJWTSessionDecorator = () => Promise<VerifyPayloadType>
export type CreateWebhookSessionDecorator = () => Promise<void>

declare module 'fastify' {
interface FastifyInstance {
addAuthStrategy: AddAuthStrategyDecorator
}
interface FastifyRequest {
extractUser: ExtractUserDecorator
createSession: CreateSessionDecorator
createJWTSession: CreateJWTSessionDecorator
createWebhookSession: CreateWebhookSessionDecorator
}
}

declare const fastifyUser: FastifyPluginCallback<FastifyUserPluginOptions>

export default fastifyUser
Loading

0 comments on commit 8705d76

Please sign in to comment.