From 7536b1c7d82d7e98e0547603ffa76b557eff871d Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Sat, 22 Sep 2018 11:38:03 +0200 Subject: [PATCH 1/5] fix typings and add fastify dependency --- types/fastify-jwt/fastify-jwt-tests.ts | 2 +- types/fastify-jwt/index.d.ts | 37 ++++++++++++++++---------- types/fastify-jwt/package.json | 6 +++++ 3 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 types/fastify-jwt/package.json diff --git a/types/fastify-jwt/fastify-jwt-tests.ts b/types/fastify-jwt/fastify-jwt-tests.ts index 9015990b25..814ed20301 100644 --- a/types/fastify-jwt/fastify-jwt-tests.ts +++ b/types/fastify-jwt/fastify-jwt-tests.ts @@ -1,5 +1,5 @@ import { IncomingMessage, Server, ServerResponse } from "http"; -import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify-jwt"; +import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; const fastify: FastifyInstance = { jwt: { diff --git a/types/fastify-jwt/index.d.ts b/types/fastify-jwt/index.d.ts index 801a135f84..1fa14c30a1 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -3,24 +3,33 @@ // Definitions by: My Self // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 +import fastify = require("fastify"); import { Secret, SignOptions, VerifyOptions, VerifyCallback, SignCallback, DecodeOptions } from 'jsonwebtoken'; -export interface FastifyInstance { - jwt: jwt; +declare module "fastify" { + interface FastifyInstance { + jwt: jwt; + } + + 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; + } + + interface FastifyRequest { + jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: VerifyCallback) => Promise | null | { [key: string]: any } | string; + } + + interface FastifyReply { + jwtSign: (payload: any, 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; -export interface FastifyRequest { - jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: VerifyCallback) => Promise | null | { [key: string]: any } | string; -} +declare namespace fastifyJwt {} -export interface FastifyReply { - jwtSign: (payload: any, options?: SignOptions | SignCallback, next?: SignCallback) => void; -} +export = fastifyJwt; \ No newline at end of file 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 From 1ce0d8b169ae1ef8c64b6149d4f17ddbfff0ebd4 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Sat, 22 Sep 2018 13:19:15 +0200 Subject: [PATCH 2/5] fix jwt function signatures --- types/fastify-jwt/index.d.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/types/fastify-jwt/index.d.ts b/types/fastify-jwt/index.d.ts index 1fa14c30a1..a5c435cda0 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -11,11 +11,25 @@ declare module "fastify" { interface FastifyInstance { jwt: jwt; } - + + interface SignFunction { + (payload: any, options?: SignOptions): string; + (payload: any, options?: SignOptions, callback?: SignCallback): void; + } + + interface VerifyFunction { + (token: string, options?: VerifyOptions, callback?: VerifyCallback): void; + (token: string, options?: VerifyOptions): object | string; + } + + interface DecodeFunction { + (token: string, options?: DecodeOptions): null | { [key: string]: any } | string; + } + 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; + sign: SignFunction; + verify: VerifyFunction; + decode: DecodeFunction; secret: Secret; } From 831a4b4e416293818b083842d64293743fcac7d6 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Sat, 22 Sep 2018 13:22:10 +0200 Subject: [PATCH 3/5] fix decode signature --- types/fastify-jwt/index.d.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/types/fastify-jwt/index.d.ts b/types/fastify-jwt/index.d.ts index a5c435cda0..76b16bc485 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -22,14 +22,10 @@ declare module "fastify" { (token: string, options?: VerifyOptions): object | string; } - interface DecodeFunction { - (token: string, options?: DecodeOptions): null | { [key: string]: any } | string; - } - interface jwt { sign: SignFunction; verify: VerifyFunction; - decode: DecodeFunction; + decode(token: string, options?: DecodeOptions): null | { [key: string]: any } | string; secret: Secret; } From 21a6230627cdb3555e2da51dd757d836a052214e Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Sun, 23 Sep 2018 10:02:46 +0200 Subject: [PATCH 4/5] fix my name --- types/fastify-jwt/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/fastify-jwt/index.d.ts b/types/fastify-jwt/index.d.ts index 76b16bc485..9f262583c3 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -1,6 +1,6 @@ // 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 import fastify = require("fastify"); From a36af80067944494959eafc44a84492cd2f44ac7 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Mon, 24 Sep 2018 22:08:12 +0200 Subject: [PATCH 5/5] fix typings --- types/fastify-jwt/fastify-jwt-tests.ts | 80 +++++++++++--------------- types/fastify-jwt/index.d.ts | 25 +++++--- 2 files changed, 50 insertions(+), 55 deletions(-) diff --git a/types/fastify-jwt/fastify-jwt-tests.ts b/types/fastify-jwt/fastify-jwt-tests.ts index 814ed20301..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"; +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 9f262583c3..94e6249a3c 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -2,10 +2,11 @@ // Project: https://github.com/fastify/fastify-jwt#readme // 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'; declare module "fastify" { interface FastifyInstance { @@ -13,8 +14,8 @@ declare module "fastify" { } interface SignFunction { - (payload: any, options?: SignOptions): string; - (payload: any, options?: SignOptions, callback?: SignCallback): void; + (payload: string | Buffer | object, options?: SignOptions): string; + (payload: string | Buffer | object, options?: SignOptions, callback?: SignCallback): void; } interface VerifyFunction { @@ -28,18 +29,24 @@ declare module "fastify" { 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: any, options?: SignOptions | SignCallback, next?: SignCallback) => void; + jwtSign: (payload: string | Buffer | object, options?: SignOptions | SignCallback, next?: SignCallback) => void; } } -declare function fastifyJwt (): void; +declare function fastifyJwt(): void; -declare namespace fastifyJwt {} +declare namespace fastifyJwt { + type SecretCallback = (request: fastify.FastifyRequest, reply: string | Buffer | object, callback?: VerifyCallback | SignCallback) => void; -export = fastifyJwt; \ No newline at end of file + interface FastifyJwtOptions { + secret: string | SecretCallback; + } +} + +export = fastifyJwt;