mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 22:30:01 +00:00
@types/permit - add typings
This commit is contained in:
32
types/permit/index.d.ts
vendored
Normal file
32
types/permit/index.d.ts
vendored
Normal 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 {}
|
||||
73
types/permit/permit-tests.ts
Normal file
73
types/permit/permit-tests.ts
Normal 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!';
|
||||
}
|
||||
23
types/permit/tsconfig.json
Normal file
23
types/permit/tsconfig.json
Normal 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
1
types/permit/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user