diff --git a/types/permit/index.d.ts b/types/permit/index.d.ts new file mode 100644 index 0000000000..229aa62b22 --- /dev/null +++ b/types/permit/index.d.ts @@ -0,0 +1,32 @@ +// Type definitions for permit 0.2 +// Project: https://github.com/ianstormtaylor/permit#readme +// Definitions by: My Self +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +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 {} diff --git a/types/permit/permit-tests.ts b/types/permit/permit-tests.ts new file mode 100644 index 0000000000..63f60eb161 --- /dev/null +++ b/types/permit/permit-tests.ts @@ -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!'; +} diff --git a/types/permit/tsconfig.json b/types/permit/tsconfig.json new file mode 100644 index 0000000000..3dba051ab5 --- /dev/null +++ b/types/permit/tsconfig.json @@ -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" + ] +} diff --git a/types/permit/tslint.json b/types/permit/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/permit/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }