Skip to content

Commit

Permalink
type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Nov 25, 2019
1 parent 9a9fa25 commit 9ee7d2f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 62 deletions.
14 changes: 6 additions & 8 deletions src/framework/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import BasePath from './base.path';
import {
OpenAPIFrameworkAPIContext,
OpenAPIFrameworkArgs,
OpenAPIFrameworkConstructorArgs,
OpenAPIFrameworkPathObject,
OpenAPIFrameworkVisitor,
OpenAPIV3,
Expand All @@ -16,7 +15,6 @@ import {
export {
BasePath,
OpenAPIFrameworkArgs,
OpenAPIFrameworkConstructorArgs,
OpenAPIFrameworkPathObject,
OpenAPIFrameworkAPIContext,
};
Expand All @@ -30,7 +28,7 @@ export default class OpenAPIFramework {
private readonly basePathObs: BasePath[];
private readonly loggingPrefix = 'openapi.validator: ';

constructor(args = {} as OpenAPIFrameworkConstructorArgs) {
constructor(args: OpenAPIFrameworkArgs) {
this.apiDoc = this.copy(this.loadSpec(args.apiDoc));
this.basePathObs = this.getBasePathsFromServers(this.apiDoc.servers);
this.basePaths = Array.from(
Expand Down Expand Up @@ -62,7 +60,7 @@ export default class OpenAPIFramework {
}
}

public initialize(visitor: OpenAPIFrameworkVisitor) {
public initialize(visitor: OpenAPIFrameworkVisitor): void {
const getApiDoc = () => {
return this.copy(this.apiDoc);
};
Expand Down Expand Up @@ -104,14 +102,14 @@ export default class OpenAPIFramework {
return $RefParser.bundle(filePath);
}

private copy(obj) {
private copy<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}

private sortApiDocTags(apiDoc) {
private sortApiDocTags(apiDoc: OpenAPIV3.Document): void {
if (apiDoc && Array.isArray(apiDoc.tags)) {
apiDoc.tags.sort((a, b) => {
return a.name > b.name;
apiDoc.tags.sort((a, b): number => {
return a.name < b.name ? -1 : 1;
});
}
}
Expand Down
31 changes: 18 additions & 13 deletions src/framework/openapi.context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { OpenAPIV3 } from './types';
import { Spec } from './openapi.spec.loader';
import { Spec, RouteMetadata } from './openapi.spec.loader';

export interface RoutePair {
expressRoute: string;
openApiRoute: string;
}
export class OpenApiContext {
public readonly apiDoc: OpenAPIV3.Document;
public readonly expressRouteMap = {};
Expand All @@ -14,7 +18,8 @@ export class OpenApiContext {
this.routes = this.initializeRoutes(spec.routes);
}

private initializeRoutes(routes) {
// side-effecting builds express/openapi route maps
private initializeRoutes(routes: RouteMetadata[]): RouteMetadata[] {
for (const route of routes) {
const routeMethods = this.expressRouteMap[route.expressRoute];
if (routeMethods) {
Expand All @@ -34,14 +39,14 @@ export class OpenApiContext {
return routes;
}

isManagedRoute(path) {
public isManagedRoute(path: string): boolean {
for (const bp of this.basePaths) {
if (path.startsWith(bp)) return true;
}
return false;
}

routePair(route) {
public routePair(route: string): RoutePair | null {
const methods = this.methods(route);
if (methods) {
return {
Expand All @@ -52,19 +57,19 @@ export class OpenApiContext {
return null;
}

methods(route) {
private methods(route: string) {
const expressRouteMethods = this.expressRouteMap[route];
if (expressRouteMethods) return expressRouteMethods;
const openApiRouteMethods = this.openApiRouteMap[route];
return openApiRouteMethods;
}

schema(route, method) {
const methods = this.methods(route);
if (methods) {
const schema = methods[method];
return schema;
}
return null;
}
// private schema(route: string, method: string) {
// const methods = this.methods(route);
// if (methods) {
// const schema = methods[method];
// return schema;
// }
// return null;
// }
}
54 changes: 23 additions & 31 deletions src/framework/openapi.spec.loader.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
import * as _ from 'lodash';
import OpenAPIFramework, {
OpenAPIFrameworkArgs,
OpenAPIFrameworkConstructorArgs,
} from './index';
import OpenAPIFramework, { OpenAPIFrameworkArgs } from './index';
import { OpenAPIFrameworkAPIContext, OpenAPIV3 } from './types';

export interface Spec {
apiDoc: OpenAPIV3.Document;
basePaths: string[];
routes: any[]; // TODO create tye
routes: RouteMetadata[];
}

export interface RouteMetadata {
expressRoute: string;
openApiRoute: string;
method: string;
pathParams: string[];
schema: OpenAPIV3.OperationObject;
}
export class OpenApiSpecLoader {
private opts: OpenAPIFrameworkArgs;
private readonly framework: OpenAPIFramework;
constructor(opts: OpenAPIFrameworkArgs) {
this.opts = opts;
this.framework = new OpenAPIFramework(opts);
}

public load(): Spec {
const framework = this.createFramework(this.opts);
const routes = this.discoverRoutes(framework, framework.basePaths);
const { apiDoc, basePaths } = this.framework;
const routes = this.discoverRoutes();
return {
apiDoc: framework.apiDoc,
basePaths: framework.basePaths,
apiDoc,
basePaths,
routes,
};
}

private createFramework(args: OpenAPIFrameworkArgs): OpenAPIFramework {
const frameworkArgs: OpenAPIFrameworkConstructorArgs = {
featureType: 'middleware',
name: 'express-openapi-validator',
...(args as OpenAPIFrameworkArgs),
};

const framework = new OpenAPIFramework(frameworkArgs);
return framework;
}

private discoverRoutes(
framework: OpenAPIFramework,
basePaths: string[],
): any[] { // TODO create type
const routes = [];
private discoverRoutes(): RouteMetadata[] {
const routes: RouteMetadata[] = [];
const toExpressParams = this.toExpressParams;
framework.initialize({
const basePaths = this.framework.basePaths;
this.framework.initialize({
visitApi(ctx: OpenAPIFrameworkAPIContext) {
const apiDoc = ctx.getApiDoc();
for (const bpa of basePaths) {
Expand All @@ -57,11 +49,11 @@ export class OpenApiSpecLoader {
(schema.parameters || []).forEach(parameter =>
schemaParameters.add(parameter),
);
((methods as any).parameters || []).forEach(parameter =>
(methods.parameters || []).forEach(parameter =>
schemaParameters.add(parameter),
);
schema.parameters = Array.from(schemaParameters);
const pathParams = new Set();
const pathParams = new Set<string>();
for (const param of schema.parameters) {
if (param.in === 'path') {
pathParams.add(param.name);
Expand All @@ -88,7 +80,7 @@ export class OpenApiSpecLoader {
return routes;
}

private toExpressParams(part) {
private toExpressParams(part: string): string {
return part.replace(/\{([^}]+)}/g, ':$1');
}
}
12 changes: 2 additions & 10 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import BasePath from './base.path';
import ajv = require('ajv');
export {
OpenAPIFrameworkArgs,
OpenAPIFrameworkConstructorArgs,
};
export { OpenAPIFrameworkArgs };

export type SecurityHandlers = {
[key: string]: (
Expand Down Expand Up @@ -350,11 +347,6 @@ export interface OpenAPIFrameworkPathObject {
module?: any;
}

interface OpenAPIFrameworkConstructorArgs extends OpenAPIFrameworkArgs {
featureType: string;
name: string;
}

interface OpenAPIFrameworkArgs {
apiDoc: OpenAPIV3.Document | string;
validateApiDoc?: boolean;
Expand Down Expand Up @@ -431,4 +423,4 @@ export interface ValidationErrorItem {
path: string;
message: string;
error_code?: string;
}
}

0 comments on commit 9ee7d2f

Please sign in to comment.