Skip to content

Commit

Permalink
Export more interfaces
Browse files Browse the repository at this point in the history
Adds `Secret` and `UnlessOptions` interfaces, and reuses `Secret` in `SecretLoader`. Made these changes because I'd like to do something like this:

```ts
import jwt, { Options, UnlessOptions } from 'koa-jwt'

const jwtOptions: Options = {
  secret: 'its',
  key: 'a',
  algorithms: ['me'],
  cookie: 'mario',
  passthrough: true,
}

const jwtAllowList: UnlessOptions = { path: ['/ping'] }

export const jwtMiddleware = jwt(jwtOptions).unless(jwtAllowList)
```
  • Loading branch information
DylanTackoor authored and sdd committed Sep 23, 2021
1 parent 45bdca6 commit c667787
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare function jwt(options: jwt.Options): jwt.Middleware;

declare namespace jwt {
export interface Options {
secret: string | string[] | Buffer | Buffer[] | SecretLoader;
secret: Secret | SecretLoader;
key?: string;
tokenKey?: string;
getToken?(ctx: Koa.Context, opts: jwt.Options): string | null;
Expand All @@ -24,9 +24,11 @@ declare namespace jwt {
algorithms?: string[];
}

export type SecretLoader = (header: any, payload: any) => Promise<string | string[] | Buffer | Buffer[]>;
export type Secret = string | string[] | Buffer | Buffer[];
export type SecretLoader = (header: any, payload: any) => Promise<Secret>;

export type UnlessOptions = (params?: {custom?: (ctx: Koa.Context) => boolean, path?: string | RegExp | (string | RegExp)[], ext?: string | string[], method?: string | string[]}) => Koa.Middleware
export interface Middleware extends Koa.Middleware {
unless(params?: {custom?: (ctx: Koa.Context) => boolean, path?: string | RegExp | (string | RegExp)[], ext?: string | string[], method?: string | string[]}): Koa.Middleware;
unless: UnlessOptions;
}
}

0 comments on commit c667787

Please sign in to comment.