From d2107afbb96f9fa21921b04047b233d334f45fb1 Mon Sep 17 00:00:00 2001 From: Gregor Stamac <1668205+gstamac@users.noreply.github.com> Date: Fri, 5 Oct 2018 20:06:04 +0200 Subject: [PATCH] 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 --- .../express-ws-routes-tests.ts | 34 +++++++ types/express-ws-routes/index.d.ts | 33 +++++++ types/express-ws-routes/tsconfig.json | 23 +++++ types/express-ws-routes/tslint.json | 3 + types/keycloak-connect/index.d.ts | 89 +++++++++++++++++++ .../keycloak-connect-tests.ts | 85 ++++++++++++++++++ types/keycloak-connect/tsconfig.json | 23 +++++ types/keycloak-connect/tslint.json | 3 + 8 files changed, 293 insertions(+) create mode 100644 types/express-ws-routes/express-ws-routes-tests.ts create mode 100644 types/express-ws-routes/index.d.ts create mode 100644 types/express-ws-routes/tsconfig.json create mode 100644 types/express-ws-routes/tslint.json create mode 100644 types/keycloak-connect/index.d.ts create mode 100644 types/keycloak-connect/keycloak-connect-tests.ts create mode 100644 types/keycloak-connect/tsconfig.json create mode 100644 types/keycloak-connect/tslint.json diff --git a/types/express-ws-routes/express-ws-routes-tests.ts b/types/express-ws-routes/express-ws-routes-tests.ts new file mode 100644 index 0000000000..1909238090 --- /dev/null +++ b/types/express-ws-routes/express-ws-routes-tests.ts @@ -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); diff --git a/types/express-ws-routes/index.d.ts b/types/express-ws-routes/index.d.ts new file mode 100644 index 0000000000..13dd408f3b --- /dev/null +++ b/types/express-ws-routes/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for express-ws-routes 1.1 +// Project: https://github.com/amekkawi/express-ws-routes +// Definitions by: Gregor Stamać +// 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; diff --git a/types/express-ws-routes/tsconfig.json b/types/express-ws-routes/tsconfig.json new file mode 100644 index 0000000000..7c01178554 --- /dev/null +++ b/types/express-ws-routes/tsconfig.json @@ -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" + ] +} \ No newline at end of file diff --git a/types/express-ws-routes/tslint.json b/types/express-ws-routes/tslint.json new file mode 100644 index 0000000000..6746359dda --- /dev/null +++ b/types/express-ws-routes/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/keycloak-connect/index.d.ts b/types/keycloak-connect/index.d.ts new file mode 100644 index 0000000000..de24fb84e9 --- /dev/null +++ b/types/keycloak-connect/index.d.ts @@ -0,0 +1,89 @@ +// Type definitions for keycloak-connect 4.5 +// Project: https://github.com/keycloak/keycloak-nodejs-connect +// Definitions by: Gregor Stamać +// 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; + storeGrant: (grant: Keycloak.Grant, request: Request, response: Response) => Keycloak.Grant; + unstoreGrant: (sessionId: string) => void; + getGrantFromCode: (code: string, request: Request, response: Response) => Promise; + loginUrl: (uuid: string, redirectUrl: string) => string; + logoutUrl: (redirectUrl: string) => string; + accountUrl: () => string; + getAccount: (token: Keycloak.Token) => Promise; + 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; diff --git a/types/keycloak-connect/keycloak-connect-tests.ts b/types/keycloak-connect/keycloak-connect-tests.ts new file mode 100644 index 0000000000..2e8d1869df --- /dev/null +++ b/types/keycloak-connect/keycloak-connect-tests.ts @@ -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.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 = 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; diff --git a/types/keycloak-connect/tsconfig.json b/types/keycloak-connect/tsconfig.json new file mode 100644 index 0000000000..94b756188e --- /dev/null +++ b/types/keycloak-connect/tsconfig.json @@ -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" + ] +} \ No newline at end of file diff --git a/types/keycloak-connect/tslint.json b/types/keycloak-connect/tslint.json new file mode 100644 index 0000000000..6746359dda --- /dev/null +++ b/types/keycloak-connect/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}