@types/permit - add typings

This commit is contained in:
Jannik Keye
2018-09-20 18:56:18 +02:00
parent 14d0a6329a
commit f8ea36a1fb
4 changed files with 129 additions and 0 deletions

32
types/permit/index.d.ts vendored Normal file
View File

@@ -0,0 +1,32 @@
// Type definitions for permit 0.2
// Project: https://github.com/ianstormtaylor/permit#readme
// Definitions by: My Self <https://github.com/jannikkeye>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { IncomingMessage, ServerResponse } from "http";
export interface PermitOptions {
scheme?: string;
proxy?: string;
realm?: string;
}
export interface BearerOptions extends PermitOptions {
basic?: string;
header?: string;
query?: string;
}
export class Permit {
constructor(options: PermitOptions);
check(req: IncomingMessage): string;
fail(res: ServerResponse): void;
}
export class Bearer extends Permit {
constructor(options: BearerOptions);
}
export class Basic extends Permit {}

View File

@@ -0,0 +1,73 @@
import { IncomingMessage, ServerResponse } from "http";
import { Permit, Basic, Bearer } from 'permit';
const permit = new Permit({
scheme: "some-scheme",
proxy: "some-proxy",
realm: "auth"
});
const basic = new Basic({
scheme: "some-scheme",
proxy: "some-proxy",
realm: "auth"
});
const bearer = new Bearer({
basic: 'username',
query: 'access_token',
});
function permitHandler(req: IncomingMessage, res: ServerResponse) {
const token = permit.check(req);
if (!token) {
permit.fail(res);
throw new Error(`Authentication required!`);
}
const user = "some-user";
if (!user) {
permit.fail(res);
throw new Error(`Authentication invalid!`);
}
return 'Success!';
}
function basichHndler(req: IncomingMessage, res: ServerResponse) {
const token = basic.check(req);
if (!token) {
basic.fail(res);
throw new Error(`Authentication required!`);
}
const user = "some-user";
if (!user) {
basic.fail(res);
throw new Error(`Authentication invalid!`);
}
return 'Success!';
}
function bearerHandler(req: IncomingMessage, res: ServerResponse) {
const token = bearer.check(req);
if (!token) {
bearer.fail(res);
throw new Error(`Authentication required!`);
}
const user = "some-user";
if (!user) {
bearer.fail(res);
throw new Error(`Authentication invalid!`);
}
return 'Success!';
}

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",
"permit-tests.ts"
]
}

1
types/permit/tslint.json Normal file
View File

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