Merge pull request #29165 from jannikkeye/fix/@types/fastify-jwt

Fix/@types/fastify-jwt typings
This commit is contained in:
Andrew Casey
2018-09-26 13:08:22 -07:00
committed by GitHub
3 changed files with 82 additions and 62 deletions
+34 -46
View File
@@ -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<Server, IncomingMessage, ServerResponse> = {
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<IncomingMessage> = {
jwtVerify: (_options) => {
return req;
},
};
app.register<fastifyJwt.FastifyJwtOptions>(fastifyJwt, {
secret: "super-secret"
});
const res: FastifyReply<ServerResponse> = {
jwtSign: (_payload, _options, _next) => {
return res;
},
};
app.register<fastifyJwt.FastifyJwtOptions>(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");
+42 -16
View File
@@ -1,26 +1,52 @@
// Type definitions for fastify-jwt 0.4
// Project: https://github.com/fastify/fastify-jwt#readme
// Definitions by: My Self <https://github.com/jannikkeye>
// Definitions by: Jannik Keye <https://github.com/jannikkeye>
// 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<HttpServer, HttpRequest, HttpResponse> {
jwt: jwt;
declare module "fastify" {
interface FastifyInstance<HttpServer, HttpRequest, HttpResponse> {
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<HttpRequest> {
jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: VerifyCallback) => Promise<null | { [key: string]: any } | string> | null | { [key: string]: any } | string;
}
interface FastifyReply<HttpResponse> {
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<IncomingMessage>, reply: string | Buffer | object, callback?: VerifyCallback | SignCallback) => void;
interface FastifyJwtOptions {
secret: string | SecretCallback;
}
}
export interface FastifyRequest<HttpRequest> {
jwtVerify: (options?: VerifyOptions | VerifyCallback, next?: VerifyCallback) => Promise<null | { [key: string]: any } | string> | null | { [key: string]: any } | string;
}
export interface FastifyReply<HttpResponse> {
jwtSign: (payload: any, options?: SignOptions | SignCallback, next?: SignCallback) => void;
}
export = fastifyJwt;
+6
View File
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"fastify": "^1.11.2"
}
}