Skip to content

Commit

Permalink
cleanup types
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Nov 25, 2019
1 parent 9ee7d2f commit 5b576de
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/framework/base.path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ServerUrlValues {
default?: string;
}

export default class BasePath {
export class BasePath {
public readonly variables: ServerUrlVariables = {};
public readonly path: string = '';
private allPaths: string[] = null;
Expand Down
15 changes: 3 additions & 12 deletions src/framework/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import * as fs from 'fs';
import * as jsYaml from 'js-yaml';
import * as path from 'path';
import $RefParser from './json.ref.schema';
import { $RefParser } from './json.ref.schema';
import { OpenAPISchemaValidator } from './openapi.schema.validator';
import BasePath from './base.path';
import { BasePath } from './base.path';
import {
OpenAPIFrameworkAPIContext,
OpenAPIFrameworkArgs,
OpenAPIFrameworkPathObject,
OpenAPIFrameworkVisitor,
OpenAPIV3,
} from './types';

export {
BasePath,
OpenAPIFrameworkArgs,
OpenAPIFrameworkPathObject,
OpenAPIFrameworkAPIContext,
};

export default class OpenAPIFramework {
export class OpenAPIFramework {
public readonly apiDoc: OpenAPIV3.Document;
public readonly basePaths: string[];

Expand Down
6 changes: 3 additions & 3 deletions src/framework/json.ref.schema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as $RefParser from 'json-schema-ref-parser';
import * as RefParser from 'json-schema-ref-parser';
import { loopWhile } from 'deasync';

export default {
export const $RefParser = {
bundle: (schema, options?) => {
var savedError,
savedResult,
done = false;

$RefParser.bundle(schema, options, (error, result) => {
RefParser.bundle(schema, options, (error, result) => {
savedError = error;
savedResult = result;
done = true;
Expand Down
2 changes: 1 addition & 1 deletion src/framework/openapi.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class OpenApiContext {
public readonly apiDoc: OpenAPIV3.Document;
public readonly expressRouteMap = {};
public readonly openApiRouteMap = {};
public readonly routes = [];
public readonly routes: RouteMetadata[] = [];
private basePaths: string[];

constructor(spec: Spec) {
Expand Down
10 changes: 7 additions & 3 deletions src/framework/openapi.spec.loader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as _ from 'lodash';
import OpenAPIFramework, { OpenAPIFrameworkArgs } from './index';
import { OpenAPIFrameworkAPIContext, OpenAPIV3 } from './types';
import { OpenAPIFramework } from './index';
import {
OpenAPIFrameworkAPIContext,
OpenAPIV3,
OpenAPIFrameworkArgs,
} from './types';

export interface Spec {
apiDoc: OpenAPIV3.Document;
basePaths: string[];
routes: RouteMetadata[];
routes: RouteMetadata[];
}

export interface RouteMetadata {
Expand Down
13 changes: 10 additions & 3 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from 'express';
import BasePath from './base.path';
import ajv = require('ajv');
import { Request, Response, NextFunction } from 'express';
import { BasePath } from './base.path';
export { OpenAPIFrameworkArgs };

export type SecurityHandlers = {
Expand Down Expand Up @@ -361,8 +361,15 @@ export interface OpenAPIFrameworkVisitor {
visitApi?(context: OpenAPIFrameworkAPIContext): void;
}

export interface OpenApiRequestMetadata {
expressRoute: string;
openApiRoute: string;
pathParams: string[];
schema: OpenAPIV3.OperationObject;
}

export interface OpenApiRequest extends Request {
openapi;
openapi?: OpenApiRequestMetadata | {};
}

export type OpenApiRequestHandler = (
Expand Down
27 changes: 16 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ValidateResponseOpts,
OpenApiRequest,
OpenApiRequestHandler,
OpenApiRequestMetadata,
} from './framework/types';

export class OpenApiValidator {
Expand Down Expand Up @@ -69,7 +70,7 @@ export class OpenApiValidator {
}

private installPathParams(app: Application): void {
const pathParams = [];
const pathParams: string[] = [];
for (const route of this.context.routes) {
if (route.pathParams.length > 0) {
pathParams.push(...route.pathParams);
Expand All @@ -87,9 +88,10 @@ export class OpenApiValidator {
value: any,
name: string,
) => {
if (req.openapi.pathParams) {
const { pathParams } = <OpenApiRequestMetadata>req.openapi;
if (pathParams) {
// override path params
req.params[name] = req.openapi.pathParams[name] || req.params[name];
req.params[name] = pathParams[name] || req.params[name];
}
next();
},
Expand Down Expand Up @@ -118,14 +120,17 @@ export class OpenApiValidator {
const { allowUnknownQueryParameters } = <ValidateRequestOpts>(
validateRequests
);
const requestValidator = new middlewares.RequestValidator(this.context.apiDoc, {
nullable: true,
coerceTypes,
removeAdditional: false,
useDefaults: true,
unknownFormats,
allowUnknownQueryParameters,
});
const requestValidator = new middlewares.RequestValidator(
this.context.apiDoc,
{
nullable: true,
coerceTypes,
removeAdditional: false,
useDefaults: true,
unknownFormats,
allowUnknownQueryParameters,
},
);
const requestValidationHandler: OpenApiRequestHandler = (req, res, next) =>
requestValidator.validate(req, res, next);

Expand Down
22 changes: 13 additions & 9 deletions src/middlewares/openapi.metadata.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { pathToRegexp } from 'path-to-regexp';
import * as _ from 'lodash';
import { OpenApiContext } from '../framework/openapi.context';
import { OpenApiRequest, OpenApiRequestHandler } from '../framework/types';
import {
OpenApiRequest,
OpenApiRequestHandler,
OpenApiRequestMetadata,
} from '../framework/types';

export function applyOpenApiMetadata(
openApiContext: OpenApiContext,
): OpenApiRequestHandler {
return (req, res, next): any => {
return (req: OpenApiRequest, res, next): any => {
const matched = lookupRoute(req);

if (matched) {
req.openapi = {};
const { expressRoute, openApiRoute, pathParams, schema } = matched;
req.openapi.expressRoute = expressRoute;
req.openapi.openApiRoute = openApiRoute;
req.openapi.pathParams = pathParams;
req.openapi.schema = schema;
req.openapi = {
expressRoute: expressRoute,
openApiRoute: openApiRoute,
pathParams: pathParams,
schema: schema,
};
req.params = pathParams;
} else if (openApiContext.isManagedRoute(req.path)) {
req.openapi = {};
}
-next();
};

function lookupRoute(req: OpenApiRequest) {
function lookupRoute(req: OpenApiRequest): OpenApiRequestMetadata {
const path = req.path;
const method = req.method;
const routeEntries = Object.entries(openApiContext.expressRouteMap);
Expand Down
22 changes: 15 additions & 7 deletions src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { OpenApiContext } from '../framework/openapi.context';
import { validationError } from './util';
import { Request } from 'express';
import { OpenApiRequest, OpenApiRequestHandler } from '../framework/types';
import {
OpenApiRequest,
OpenApiRequestHandler,
OpenApiRequestMetadata,
OpenAPIV3,
} from '../framework/types';
const multer = require('multer');

export function multipart(
Expand Down Expand Up @@ -50,12 +55,15 @@ function isValidContentType(req: Request): boolean {
}

function isMultipart(req: OpenApiRequest): boolean {
return (
req.openapi &&
req.openapi.schema &&
req.openapi.schema.requestBody &&
req.openapi.schema.requestBody.content &&
req.openapi.schema.requestBody.content['multipart/form-data']
const openapi = <OpenApiRequestMetadata>req.openapi;
return !!(
openapi &&
openapi.schema &&
openapi.schema.requestBody &&
(<OpenAPIV3.RequestBodyObject>openapi.schema.requestBody).content &&
(<OpenAPIV3.RequestBodyObject>openapi.schema.requestBody).content[
'multipart/form-data'
]
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
OpenApiRequest,
RequestValidatorOptions,
ValidateRequestOpts,
OpenApiRequestMetadata,
} from '../framework/types';
import { Ajv } from 'ajv';

Expand Down Expand Up @@ -47,12 +48,13 @@ export class RequestValidator {
return next();
}

const path = req.openapi.expressRoute;
const openapi = <OpenApiRequestMetadata>req.openapi;
const path = openapi.expressRoute;
if (!path) {
throw validationError(404, req.path, 'not found');
}

const pathSchema = req.openapi.schema;
const pathSchema = openapi.schema;
if (!pathSchema) {
// add openapi metadata to make this case more clear
// its not obvious that missig schema means methodNotAllowed
Expand Down
4 changes: 3 additions & 1 deletion src/middlewares/openapi.security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
OpenAPIV3,
OpenApiRequest,
SecurityHandlers,
OpenApiRequestMetadata,
} from '../framework/types';
import { validationError } from './util';
import { OpenApiContext } from '../framework/openapi.context';
Expand Down Expand Up @@ -169,9 +170,10 @@ class AuthValidator {
private path: string;
private scopes: string[];
constructor(req: OpenApiRequest, scheme, scopes: string[] = []) {
const openapi = <OpenApiRequestMetadata>req.openapi;
this.req = req;
this.scheme = scheme;
this.path = req.openapi.openApiRoute;
this.path = openapi.openApiRoute;
this.scopes = scopes;
}

Expand Down

0 comments on commit 5b576de

Please sign in to comment.