diff --git a/types/fastify-jwt/fastify-jwt-tests.ts b/types/fastify-jwt/fastify-jwt-tests.ts index 9015990b25..6b92026b04 100644 --- a/types/fastify-jwt/fastify-jwt-tests.ts +++ b/types/fastify-jwt/fastify-jwt-tests.ts @@ -1,34 +1,35 @@ import { IncomingMessage, Server, ServerResponse } from "http"; -import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify-jwt"; +import fastifyJwt = require("fastify-jwt"); +import fastify = require("fastify"); -const fastify: FastifyInstance = { - jwt: { - decode: (token, options) => { - return token; - }, - secret: "some-string", - sign: (payload, options) => { - return JSON.stringify(payload); - }, - verify: (token, options) => { - return token; - }, - }, -}; +const app = fastify(); -const req: FastifyRequest = { - jwtVerify: (_options) => { - return req; - }, -}; +app.register(fastifyJwt, { + secret: "super-secret" +}); -const res: FastifyReply = { - jwtSign: (_payload, _options, _next) => { - return res; - }, -}; +app.register(fastifyJwt, { + secret: (request, reply, callback) => { + return ""; + } +}); -fastify.jwt.sign({ a: "b" }, {}, (err, token) => { +app.get("/path", (request, reply) => { + request.jwtVerify(); + request.jwtVerify({}); + request.jwtVerify({}, () => "string"); + request.jwtVerify(() => "string", () => "string"); + + reply.jwtSign("payload"); + reply.jwtSign("payload", {}); + reply.jwtSign("payload", {}, () => "string"); + reply.jwtSign({ a: "b" }, {}, () => "string"); + reply.jwtSign([], {}, () => "string"); + reply.jwtSign(new Buffer("buffer"), {}, () => "string"); + reply.jwtSign("payload", () => "string", () => "string"); +}); + +app.jwt.sign({ a: "b" }, {}, (err, token) => { if (err) { throw err; } @@ -36,7 +37,7 @@ fastify.jwt.sign({ a: "b" }, {}, (err, token) => { return token; }); -fastify.jwt.sign("string", { algorithm: "some-algorithm" }, (err, token) => { +app.jwt.sign("string", { algorithm: "some-algorithm" }, (err, token) => { if (err) { throw err; } @@ -44,7 +45,7 @@ fastify.jwt.sign("string", { algorithm: "some-algorithm" }, (err, token) => { return token; }); -fastify.jwt.sign([], {}, (err, token) => { +app.jwt.sign([], {}, (err, token) => { if (err) { throw err; } @@ -52,7 +53,7 @@ fastify.jwt.sign([], {}, (err, token) => { return token; }); -fastify.jwt.sign(9999, {}, (err, token) => { +app.jwt.sign(new Buffer("buffer"), {}, (err, token) => { if (err) { throw err; } @@ -60,26 +61,13 @@ fastify.jwt.sign(9999, {}, (err, token) => { return token; }); -fastify.jwt.decode("some-token"); -fastify.jwt.decode("some-token"); -fastify.jwt.secret = "some-secret"; -fastify.jwt.verify("some-token", {}, (err) => { +app.jwt.decode("some-token"); +app.jwt.decode("some-token"); +app.jwt.secret = "some-secret"; +app.jwt.verify("some-token", {}, (err) => { if (err) { throw err; } return true; }); - -req.jwtVerify(); -req.jwtVerify({}); -req.jwtVerify({}, () => "string"); -req.jwtVerify(() => "string", () => "string"); - -res.jwtSign("payload"); -res.jwtSign("payload", {}); -res.jwtSign("payload", {}, () => "string"); -res.jwtSign({ a: "b" }, {}, () => "string"); -res.jwtSign([], {}, () => "string"); -res.jwtSign(9999, {}, () => "string"); -res.jwtSign("payload", () => "string", () => "string"); diff --git a/types/fastify-jwt/index.d.ts b/types/fastify-jwt/index.d.ts index 801a135f84..94e6249a3c 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -1,26 +1,52 @@ // Type definitions for fastify-jwt 0.4 // Project: https://github.com/fastify/fastify-jwt#readme -// Definitions by: My Self +// Definitions by: Jannik Keye // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.4 +import fastify = require("fastify"); import { Secret, SignOptions, VerifyOptions, VerifyCallback, SignCallback, DecodeOptions } from 'jsonwebtoken'; +import { IncomingMessage, ServerResponse } from 'http'; -export interface FastifyInstance { - jwt: jwt; +declare module "fastify" { + interface FastifyInstance { + jwt: jwt; + } + + interface SignFunction { + (payload: string | Buffer | object, options?: SignOptions): string; + (payload: string | Buffer | object, options?: SignOptions, callback?: SignCallback): void; + } + + interface VerifyFunction { + (token: string, options?: VerifyOptions, callback?: VerifyCallback): void; + (token: string, options?: VerifyOptions): object | string; + } + + interface jwt { + sign: SignFunction; + verify: VerifyFunction; + decode(token: string, options?: DecodeOptions): null | { [key: string]: any } | string; + secret: Secret; + } + + interface FastifyRequest { + jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: VerifyCallback) => Promise | null | { [key: string]: any } | string; + } + + interface FastifyReply { + jwtSign: (payload: string | Buffer | object, options?: SignOptions | SignCallback, next?: SignCallback) => void; + } } -export interface jwt { - sign: (payload: any, options?: SignOptions, callback?: SignCallback) => void; - verify: (token: string, options?: VerifyOptions, callback?: VerifyCallback) => void; - decode: (token: string, options?: DecodeOptions) => null | { [key: string]: any } | string; - secret: Secret; +declare function fastifyJwt(): void; + +declare namespace fastifyJwt { + type SecretCallback = (request: fastify.FastifyRequest, reply: string | Buffer | object, callback?: VerifyCallback | SignCallback) => void; + + interface FastifyJwtOptions { + secret: string | SecretCallback; + } } -export interface FastifyRequest { - jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: VerifyCallback) => Promise | null | { [key: string]: any } | string; -} - -export interface FastifyReply { - jwtSign: (payload: any, options?: SignOptions | SignCallback, next?: SignCallback) => void; -} +export = fastifyJwt; diff --git a/types/fastify-jwt/package.json b/types/fastify-jwt/package.json new file mode 100644 index 0000000000..948a91ff5b --- /dev/null +++ b/types/fastify-jwt/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "fastify": "^1.11.2" + } +} \ No newline at end of file