Add new typings for keycloak-connect and express-ws-routes (#29205)

* [express-ws-routes] add typings

* [keycloak-connect] add typings

* removed empty files

* fixed tslint issues

* fixed tslint config

* [keycloak-connect] add tests

* [express-ws-routes] add tests

* [express-ws-routes] rename tests file

* [keycloak-connect] rename tests file

* [express-ws-routes] fixed lint issues

* [keycloak-connect] fixed lint issues
This commit is contained in:
Gregor Stamac
2018-10-05 20:06:04 +02:00
committed by Wesley Wigham
parent 875b67c35e
commit d2107afbb9
8 changed files with 293 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
import { Express as exExpress, Request, NextFunction } from 'express';
import * as WebSocket from 'ws';
import { Server as httpServer } from 'http';
import { Router as coreRouter } from 'express-serve-static-core';
import * as expresswsroutes from 'express-ws-routes';
const clientInfo: expresswsroutes.ClientInfo = undefined;
const origin: string = clientInfo.origin;
const secure: boolean = clientInfo.secure;
const request: Request = clientInfo.req;
const webSocketHandler: expresswsroutes.WebSocketHandler = undefined;
const socket: WebSocket = undefined;
webSocketHandler(socket);
const cbHandler: expresswsroutes.CbHandler = undefined;
cbHandler(webSocketHandler);
cbHandler(true);
const handler: expresswsroutes.WebSocketRouteHandler = undefined;
const next: NextFunction = undefined;
handler(clientInfo, cbHandler, next);
const server: expresswsroutes.Server = undefined;
const http: httpServer = server;
const wsServer: WebSocket.Server = server.wsServer;
const app: expresswsroutes.Express = expresswsroutes();
const e: exExpress = app;
const ws: expresswsroutes.Express = app.websocket('/path', handler);
const router: expresswsroutes.Router = undefined;
const r: coreRouter = router;
const wsr: expresswsroutes.Router = router.websocket('/path', handler);

33
types/express-ws-routes/index.d.ts vendored Normal file
View File

@@ -0,0 +1,33 @@
// Type definitions for express-ws-routes 1.1
// Project: https://github.com/amekkawi/express-ws-routes
// Definitions by: Gregor Stamać <https://github.com/gstamac>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { Express as exExpress, Request, NextFunction } from 'express';
import * as WebSocket from 'ws';
import { Server as httpServer } from 'http';
import { Router as coreRouter } from 'express-serve-static-core';
declare function expressWsRoutes(): expressWsRoutes.Express;
declare namespace expressWsRoutes {
interface ClientInfo { origin: string; secure: boolean; req: Request; }
type WebSocketHandler = (socket: WebSocket) => void;
type CbHandler = (connectHandler: WebSocketHandler | boolean) => void;
type WebSocketRouteHandler = (info: ClientInfo, cb: CbHandler, next: NextFunction) => void;
interface Server extends httpServer {
wsServer: WebSocket.Server;
}
interface Express extends exExpress {
websocket(route: string, handler: WebSocketRouteHandler): Express;
}
interface Router extends coreRouter {
websocket(route: string, handler: WebSocketRouteHandler): Router;
}
}
export = expressWsRoutes;

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",
"express-ws-routes-tests.ts"
]
}

View File

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

89
types/keycloak-connect/index.d.ts vendored Normal file
View File

@@ -0,0 +1,89 @@
// Type definitions for keycloak-connect 4.5
// Project: https://github.com/keycloak/keycloak-nodejs-connect
// Definitions by: Gregor Stamać <https://github.com/gstamac>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { RequestHandler, Request, Response } from 'express';
declare class Keycloak {
constructor(config: Keycloak.Config, keycloakConfig?: {} | string);
middleware(options?: Keycloak.MiddlewareOptions): RequestHandler;
protect(spec?: string | Keycloak.SpecHandler): RequestHandler;
authenticated: (request: Request) => void;
deauthenticated: (request: Request) => void;
accessDenied: (request: Request, response: Response) => void;
getGrant: (request: Request, response: Response) => Promise<Keycloak.Grant>;
storeGrant: (grant: Keycloak.Grant, request: Request, response: Response) => Keycloak.Grant;
unstoreGrant: (sessionId: string) => void;
getGrantFromCode: (code: string, request: Request, response: Response) => Promise<Keycloak.Grant>;
loginUrl: (uuid: string, redirectUrl: string) => string;
logoutUrl: (redirectUrl: string) => string;
accountUrl: () => string;
getAccount: (token: Keycloak.Token) => Promise<any>;
redirectToLogin: (request: Request) => boolean;
}
declare namespace Keycloak {
interface BaseConfig {
scope?: any;
}
interface StoreConfig extends BaseConfig {
store: any;
}
interface CookiesConfig extends BaseConfig {
cookies: any;
}
type Config = StoreConfig | CookiesConfig | BaseConfig;
interface MiddlewareOptions {
logout?: string;
admin?: string;
}
interface TokenContent {
exp: number;
resource_access?: any;
realm_access?: { roles?: string[] };
}
interface Token {
token: string;
clientId: string;
header?: any;
content: TokenContent;
signature?: Buffer;
signed?: string;
isExpired: () => boolean;
hasRole: (roleName: string) => boolean;
hasApplicationRole: (appName: string, roleName: string) => boolean;
hasRealmRole: (roleName: string) => boolean;
}
type SpecHandler = (token: Token, request: Request, response: Response) => boolean;
interface Grant {
access_token: Token;
refresh_token: Token;
id_token: Token;
expires_in: number;
token_type: string;
__raw: string;
update: (grant: Grant) => void;
toString: () => string;
isExpired: () => boolean;
store: (request: Request, response: Response) => void;
}
interface GrantedRequest extends Request {
kauth: { grant?: Grant };
}
}
export = Keycloak;

View File

@@ -0,0 +1,85 @@
import { RequestHandler, Request, Response } from 'express';
import * as Keycloak from 'keycloak-connect';
let _string: string;
let _number: number;
let _boolean: boolean;
let _any: any;
const config: Keycloak.Config = undefined;
let keycloak: Keycloak = new Keycloak(config);
keycloak = new Keycloak(config, {});
keycloak = new Keycloak(config, '');
let request: Request;
const response: Response = undefined;
const options: Keycloak.MiddlewareOptions = undefined;
const spec: Keycloak.SpecHandler = undefined;
let grant: Keycloak.Grant;
let token: Keycloak.Token;
let handler: RequestHandler = keycloak.middleware(options);
handler = keycloak.middleware();
handler = keycloak.protect(spec);
handler = keycloak.protect();
handler = keycloak.protect('');
keycloak.authenticated(request);
keycloak.deauthenticated(request);
keycloak.accessDenied(request, response);
let grantPromise: Promise<Keycloak.Grant> = keycloak.getGrant(request, response);
grant = keycloak.storeGrant(grant, request, response);
keycloak.unstoreGrant('sessionId');
grantPromise = keycloak.getGrantFromCode('code', request, response);
_string = keycloak.loginUrl('uuid', 'redirectUrl');
_string = keycloak.logoutUrl('redirectUrl');
_string = keycloak.accountUrl();
const account: Promise<any> = keycloak.getAccount(token);
_boolean = keycloak.redirectToLogin(request);
const baseConfig: Keycloak.BaseConfig = undefined;
_any = baseConfig.scope;
const storeConfig: Keycloak.StoreConfig = undefined;
_any = storeConfig.store;
const cookiesConfig: Keycloak.CookiesConfig = undefined;
_any = cookiesConfig.cookies;
const middlewareOptions: Keycloak.MiddlewareOptions = undefined;
_string = middlewareOptions.logout;
_string = middlewareOptions.admin;
let tokenContent: Keycloak.TokenContent;
_number = tokenContent.exp;
_any = tokenContent.resource_access;
const realm_access_roles: string[] = tokenContent.realm_access.roles;
_string = token.token;
_string = token.clientId;
_any = token.header;
tokenContent = token.content;
const signature: Buffer = token.signature;
_string = token.signed;
_boolean = token.isExpired();
_boolean = token.hasRole('roleName');
_boolean = token.hasApplicationRole('appName', 'roleName');
_boolean = token.hasRealmRole('roleName');
const specHandler: Keycloak.SpecHandler = undefined;
_boolean = specHandler(token, request, response);
token = grant.access_token;
token = grant.refresh_token;
token = grant.id_token;
_number = grant.expires_in;
_string = grant.token_type;
_string = grant.__raw;
grant.update(grant);
_string = grant.toString();
_boolean = grant.isExpired();
grant.store(request, response);
const grantedRequest: Keycloak.GrantedRequest = undefined;
request = grantedRequest;
grant = grantedRequest.kauth.grant;

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

View File

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