Merge pull request #29085 from jannikkeye/fastify-jwt

[@types/fastify-jwt] add types
This commit is contained in:
Nathan Shively-Sanders
2018-09-21 10:55:49 -07:00
committed by GitHub
4 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
import { IncomingMessage, Server, ServerResponse } from "http";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify-jwt";
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 req: FastifyRequest<IncomingMessage> = {
jwtVerify: (_options) => {
return req;
},
};
const res: FastifyReply<ServerResponse> = {
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");

26
types/fastify-jwt/index.d.ts vendored Normal file
View File

@@ -0,0 +1,26 @@
// Type definitions for fastify-jwt 0.4
// Project: https://github.com/fastify/fastify-jwt#readme
// Definitions by: My Self <https://github.com/jannikkeye>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { Secret, SignOptions, VerifyOptions, VerifyCallback, SignCallback, DecodeOptions } from 'jsonwebtoken';
export interface FastifyInstance<HttpServer, HttpRequest, HttpResponse> {
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<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;
}

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }