From a87e2aea638de3c58cad70380c7335edbe5c4b9a Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Fri, 21 Sep 2018 13:10:56 +0200 Subject: [PATCH 1/3] add types --- types/fastify-jwt/fastify-jwt-tests.ts | 84 ++++++++++++++++++++++++++ types/fastify-jwt/index.d.ts | 26 ++++++++ types/fastify-jwt/tsconfig.json | 23 +++++++ types/fastify-jwt/tslint.json | 1 + 4 files changed, 134 insertions(+) create mode 100644 types/fastify-jwt/fastify-jwt-tests.ts create mode 100644 types/fastify-jwt/index.d.ts create mode 100644 types/fastify-jwt/tsconfig.json create mode 100644 types/fastify-jwt/tslint.json diff --git a/types/fastify-jwt/fastify-jwt-tests.ts b/types/fastify-jwt/fastify-jwt-tests.ts new file mode 100644 index 0000000000..c84f068663 --- /dev/null +++ b/types/fastify-jwt/fastify-jwt-tests.ts @@ -0,0 +1,84 @@ +import { IncomingMessage, Server, ServerResponse } from "http"; +import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify-jwt"; + +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 req: FastifyRequest = { + jwtVerify: (_options) => { + return req; + }, +}; + +const res: FastifyReply = { + jwtSign: (_payload, _options, _next) => { + return res; + }, +}; + +fastify.jwt.sign({ a: "b" }, {}, (err, token) => { + if (err) { + throw err; + } + + return token; +}); + +fastify.jwt.sign("string", { algorithm: "some-algorithm" }, (err, token) => { + if (err) { + throw err; + } + + return token; +}); + +fastify.jwt.sign([], {}, (err, token) => { + if (err) { + throw err; + } + + return token; +}); + +fastify.jwt.sign(9999, {}, (err, token) => { + if (err) { + throw err; + } + + return token; +}); + +fastify.jwt.decode("some-token"); +fastify.jwt.secret = "some-secret"; +fastify.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 new file mode 100644 index 0000000000..19f0dbbf03 --- /dev/null +++ b/types/fastify-jwt/index.d.ts @@ -0,0 +1,26 @@ +// Type definitions for fastify-jwt 0.4 +// Project: https://github.com/fastify/fastify-jwt#readme +// Definitions by: My Self +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { SignOptions, VerifyOptions, VerifyCallback, SignCallback, DecodeOptions } from 'jsonwebtoken'; + +export interface FastifyInstance { + jwt: jwt; +} + +export interface jwt { + sign: (payload: any, options?: SignOptions, callback?: SignCallback) => string; + verify: (token: string, options?: VerifyOptions, callback?: VerifyCallback) => string; + decode: (token: string, options?: DecodeOptions) => string; + secret: string; +} + +export interface FastifyRequest { + jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: jwt["verify"]) => FastifyRequest; +} + +export interface FastifyReply { + jwtSign: (payload: any, options?: SignOptions | SignCallback, next?: jwt["sign"]) => FastifyReply; +} diff --git a/types/fastify-jwt/tsconfig.json b/types/fastify-jwt/tsconfig.json new file mode 100644 index 0000000000..b9df16a35f --- /dev/null +++ b/types/fastify-jwt/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "fastify-jwt-tests.ts" + ] +} diff --git a/types/fastify-jwt/tslint.json b/types/fastify-jwt/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/fastify-jwt/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 0e96ec20cbbd05af546c46cde2af1a86fcd417b9 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Fri, 21 Sep 2018 15:19:17 +0200 Subject: [PATCH 2/3] fix jwt.decode signature --- types/fastify-jwt/fastify-jwt-tests.ts | 1 + types/fastify-jwt/index.d.ts | 6 +++--- types/fastify-jwt/tsconfig.json | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/types/fastify-jwt/fastify-jwt-tests.ts b/types/fastify-jwt/fastify-jwt-tests.ts index c84f068663..9015990b25 100644 --- a/types/fastify-jwt/fastify-jwt-tests.ts +++ b/types/fastify-jwt/fastify-jwt-tests.ts @@ -60,6 +60,7 @@ 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) => { diff --git a/types/fastify-jwt/index.d.ts b/types/fastify-jwt/index.d.ts index 19f0dbbf03..d32d1fc6b5 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -4,7 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -import { SignOptions, VerifyOptions, VerifyCallback, SignCallback, DecodeOptions } from 'jsonwebtoken'; +import { Secret, SignOptions, VerifyOptions, VerifyCallback, SignCallback, DecodeOptions } from 'jsonwebtoken'; export interface FastifyInstance { jwt: jwt; @@ -13,8 +13,8 @@ export interface FastifyInstance { export interface jwt { sign: (payload: any, options?: SignOptions, callback?: SignCallback) => string; verify: (token: string, options?: VerifyOptions, callback?: VerifyCallback) => string; - decode: (token: string, options?: DecodeOptions) => string; - secret: string; + decode: (token: string, options?: DecodeOptions) => null | { [key: string]: any } | string; + secret: Secret; } export interface FastifyRequest { diff --git a/types/fastify-jwt/tsconfig.json b/types/fastify-jwt/tsconfig.json index b9df16a35f..a0149e0fe8 100644 --- a/types/fastify-jwt/tsconfig.json +++ b/types/fastify-jwt/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": true, + "strictNullChecks": false, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ From 3ec409c71eff14b37b726186fff589e18eec3b1c Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Fri, 21 Sep 2018 15:28:35 +0200 Subject: [PATCH 3/3] fix jwtVerify & jwtSign signatures --- types/fastify-jwt/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/fastify-jwt/index.d.ts b/types/fastify-jwt/index.d.ts index d32d1fc6b5..801a135f84 100644 --- a/types/fastify-jwt/index.d.ts +++ b/types/fastify-jwt/index.d.ts @@ -11,16 +11,16 @@ export interface FastifyInstance { } export interface jwt { - sign: (payload: any, options?: SignOptions, callback?: SignCallback) => string; - verify: (token: string, options?: VerifyOptions, callback?: VerifyCallback) => string; + 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; } export interface FastifyRequest { - jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: jwt["verify"]) => FastifyRequest; + jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: VerifyCallback) => Promise | null | { [key: string]: any } | string; } export interface FastifyReply { - jwtSign: (payload: any, options?: SignOptions | SignCallback, next?: jwt["sign"]) => FastifyReply; + jwtSign: (payload: any, options?: SignOptions | SignCallback, next?: SignCallback) => void; }