[jwt-then] Add Types (#28928)

Please fill in this template.

- [x] Use a meaningful title for the pull request. Include the name of the package modified.
- [x] Test the change in your own code. (Compile and run.)
- [x] Add or edit tests to reflect the change. (Run with `npm test`.)
- [x] Follow the advice from the [readme](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#make-a-pull-request).
- [x] Avoid [common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#common-mistakes).
- [x] Run `npm run lint package-name` (or `tsc` if no `tslint.json` is present).
- [x] The package does not already provide its own types, or cannot have its `.d.ts` files generated via `--declaration`
- [x] If this is for an NPM package, match the name. If not, do not conflict with the name of an NPM package.
- [x] Create it with `dts-gen --dt`, not by basing it on an existing project.
- [x] `tslint.json` should be present, and `tsconfig.json` should have `noImplicitAny`, `noImplicitThis`, `strictNullChecks`, and `strictFunctionTypes` set to `true`.
This commit is contained in:
Max
2018-09-17 01:04:55 -07:00
committed by Ryan Cavanaugh
parent 84d10599aa
commit 3fdd79cae9
4 changed files with 98 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
// Type definitions for jwt-then 1.0
// Project: https://github.com/fl0w/jwt-then#readme
// Definitions by: Definitions by: Max Uetrecht <https://github.com/phenomax>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
/// <reference types="node" />
export type Secret = string | Buffer | { key: string; passphrase: string };
export interface SignOptions {
/**
* Signature algorithm. Could be one of these values :
* - HS256: HMAC using SHA-256 hash algorithm (default)
* - HS384: HMAC using SHA-384 hash algorithm
* - HS512: HMAC using SHA-512 hash algorithm
* - RS256: RSASSA using SHA-256 hash algorithm
* - RS384: RSASSA using SHA-384 hash algorithm
* - RS512: RSASSA using SHA-512 hash algorithm
* - ES256: ECDSA using P-256 curve and SHA-256 hash algorithm
* - ES384: ECDSA using P-384 curve and SHA-384 hash algorithm
* - ES512: ECDSA using P-521 curve and SHA-512 hash algorithm
* - none: No digital signature or MAC value included
*/
algorithm?: string;
keyid?: string;
/** expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms.js). Eg: 60, "2 days", "10h", "7d" */
expiresIn?: string | number;
/** expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms.js). Eg: 60, "2 days", "10h", "7d" */
notBefore?: string | number;
audience?: string | string[];
subject?: string;
issuer?: string;
jwtid?: string;
noTimestamp?: boolean;
header?: object;
encoding?: string;
}
/**
* Sign the given payload into a JSON Web Token string
* @param payload - Payload to sign, could be an literal, buffer or string
* @param secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
* @param [options] - Options for the signature
* @returns A proiose containing the JSON Web Token string
*/
export function sign(
payload: string | Buffer | object,
secretOrPrivateKey: Secret,
options?: SignOptions,
): Promise<string>;
/**
* Verify given token using a secret or a public key to get a decoded token
* @param token - JWT string to verify
* @param secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.
* @param [options] - Options for the verification
* @returns A promise containing either an error or the decoded JSON Web Token string
*/
export function verify(
token: string,
secretOrPublicKey: string | Buffer,
): Promise<object | string>;
+11
View File
@@ -0,0 +1,11 @@
import jwt = require("jwt-then");
import fs = require("fs");
const testObject = { foo: "bar" };
(async () => {
const token = await jwt.sign(testObject, "foobar");
const verified = await jwt.verify(token, "foobar");
console.log(token, verified);
})();
+23
View File
@@ -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",
"jwt-then-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }