Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 22, 2019
1 parent 48b5028 commit ca57fd7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 51 deletions.
19 changes: 19 additions & 0 deletions errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const methodNotAllowed = (path, method) => ({
status: 405,
errors: [
{
path,
message: `${method} method not allowed`,
},
],
});

export const notFoundError = path => ({
status: 404,
errors: [
{
path,
message: 'Not found',
},
],
});
4 changes: 2 additions & 2 deletions fw/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export function copy(obj) {
// return null;
// }

export function handleFilePath(filePath) {
export function loadSpecFile(filePath) {
if (typeof filePath === 'string') {
const absolutePath = path.resolve(process.cwd(), filePath);
if (fs.existsSync(absolutePath)) {
Expand All @@ -232,7 +232,7 @@ export function handleFilePath(filePath) {
}
}
}
return filePath;
return null;
}

export function handleYaml(apiDoc) {
Expand Down
57 changes: 8 additions & 49 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as _ from 'lodash';
import { ExpressApp } from 'express';
import * as fs from 'fs';
import * as path from 'path';
const jsYaml = require('js-yaml');
import OpenAPIFramework, {
OpenAPIFrameworkArgs,
OpenAPIFrameworkConstructorArgs,
} from './fw';
import OpenAPIRequestValidator from 'openapi-request-validator'; // OpenAPIRequestValidatorError,
import OpenAPIRequestValidator from 'openapi-request-validator';
import OpenAPIRequestCoercer from 'openapi-request-coercer';
import { OpenAPIFrameworkAPIContext } from './fw/types';
import { handleYaml, loadSpecFile } from './fw/util';
import { methodNotAllowed, notFoundError } from './errors';

// import { OpenAPIResponseValidatorError } from 'openapi-response-validator';
// import { SecurityHandlers } from 'openapi-security-handler';
// import { OpenAPI, OpenAPIV3 } from 'openapi-types';
import { OpenAPIFrameworkAPIContext } from './fw/types';

export interface ErrorResponse {
statusCode: number;
Expand All @@ -24,26 +24,6 @@ export interface OpenApiMiddlewareOpts extends OpenAPIFrameworkArgs {
errorTransform?: (validationResult: any) => ErrorResponse;
}

const methodNotAllowed = (path, method) => ({
status: 405,
errors: [
{
path,
message: `${method} method not allowed`,
},
],
});

const notFoundError = path => ({
status: 404,
errors: [
{
path,
message: 'Not found',
},
],
});

export function OpenApiMiddleware(opts: OpenApiMiddlewareOpts) {
if (!opts.apiSpecPath) throw new Error('apiSpecPath required');
const apiContents = loadSpecFile(opts.apiSpecPath);
Expand Down Expand Up @@ -83,13 +63,13 @@ OpenApiMiddleware.prototype.install = function(app: ExpressApp) {

// install param on routes with paths
for (const p of _.uniq(pathParms)) {
app.param(p, this.middleware()); //pathParamMiddleware);
app.param(p, this._middleware());
}
// install use on routes without paths
app.all(_.uniq(noPathParamRoutes), this.middleware());
app.all(_.uniq(noPathParamRoutes), this._middleware());
};

OpenApiMiddleware.prototype.middleware = function() {
OpenApiMiddleware.prototype._middleware = function() {
return (req, res, next) => {
const { path: rpath, method, route } = req;
var path = Array.isArray(route.path)
Expand Down Expand Up @@ -214,24 +194,3 @@ function buildRoutes(framework) {
});
return routes;
}

function loadSpecFile(filePath) {
if (typeof filePath === 'string') {
const absolutePath = path.resolve(process.cwd(), filePath);
if (fs.existsSync(absolutePath)) {
try {
// json or module
return require(absolutePath);
} catch (e) {
return fs.readFileSync(absolutePath, 'utf8');
}
}
}
return null;
}

function handleYaml(apiDoc) {
return typeof apiDoc === 'string'
? jsYaml.safeLoad(apiDoc, { json: true })
: apiDoc;
}

0 comments on commit ca57fd7

Please sign in to comment.