diff --git a/types/fastify-jwt/fastify-jwt-tests.ts b/types/fastify-jwt/fastify-jwt-tests.ts new file mode 100644 index 0000000000..9015990b25 --- /dev/null +++ b/types/fastify-jwt/fastify-jwt-tests.ts @@ -0,0 +1,85 @@ +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.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..801a135f84 --- /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 { Secret, SignOptions, VerifyOptions, VerifyCallback, SignCallback, DecodeOptions } from 'jsonwebtoken'; + +export interface FastifyInstance { + jwt: jwt; +} + +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; +} + +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; +} diff --git a/types/fastify-jwt/tsconfig.json b/types/fastify-jwt/tsconfig.json new file mode 100644 index 0000000000..a0149e0fe8 --- /dev/null +++ b/types/fastify-jwt/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "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" }