Skip to content

Commit

Permalink
relocate framework files
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Sep 15, 2019
1 parent 7c27713 commit a3129e2
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 55 deletions.
39 changes: 0 additions & 39 deletions src/errors/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/openapi.context.ts → src/framework/openapi.context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenApiSpecLoader } from './openapi.spec.loader';
import { OpenAPIFrameworkArgs } from './framework';
import { OpenAPIFrameworkArgs } from './index';

export class OpenApiContext {
// TODO cleanup structure (group related functionality)
Expand All @@ -13,7 +13,7 @@ export class OpenApiContext {
const { apiDoc, basePaths, routes } = openApiRouteDiscovery.load();

this.apiDoc = apiDoc;
this.basePaths = <Set<string>> basePaths;
this.basePaths = <Set<string>>basePaths;
this.routes = this.initializeRoutes(routes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as _ from 'lodash';
import OpenAPIFramework, {
OpenAPIFrameworkArgs,
OpenAPIFrameworkConstructorArgs,
} from './framework';
import { OpenAPIFrameworkAPIContext } from './framework/types';
} from './index';
import { OpenAPIFrameworkAPIContext } from './types';

export class OpenApiSpecLoader {
private opts: OpenAPIFrameworkArgs;
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as _ from 'lodash';
import { Application, Request, Response, NextFunction } from 'express';
import { OpenApiContext } from './openapi.context';
import * as middlewares from './middlewares';
import ono from 'ono';
import * as _ from 'lodash';
import { Application } from 'express';
import { OpenApiContext } from './framework/openapi.context';
import { OpenAPIV3, OpenApiRequest } from './framework/types';
import * as middlewares from './middlewares';

export interface OpenApiValidatorOpts {
apiSpec: OpenAPIV3.Document | string;
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/openapi.metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as pathToRegexp from 'path-to-regexp';
import * as _ from 'lodash';
import { OpenApiContext } from '../openapi.context';
import { OpenApiContext } from '../framework/openapi.context';

export function applyOpenApiMetadata(openApiContext: OpenApiContext) {
return (req, res, next) => {
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenApiContext } from '../openapi.context';
import { validationError } from '../errors';
import { OpenApiContext } from '../framework/openapi.context';
import { validationError } from './util';
import * as multer from 'multer';

export function multipart(openApiContext: OpenApiContext, multerOpts: {} = {}) {
Expand Down
7 changes: 5 additions & 2 deletions src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createRequestAjv } from './ajv';
import { extractContentType } from './util';
import { validationError, ajvErrorsToValidatorError } from '../errors';
import {
extractContentType,
validationError,
ajvErrorsToValidatorError,
} from './util';
import ono from 'ono';

const TYPE_JSON = 'application/json';
Expand Down
5 changes: 2 additions & 3 deletions src/middlewares/openapi.response.validator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import ono from 'ono';
import * as Ajv from 'ajv';
import * as mung from 'express-mung';
import { validationError, ajvErrorsToValidatorError } from '../errors';
import ono from 'ono';
import { createResponseAjv } from './ajv';
import { extractContentType } from './util';
import { extractContentType, ajvErrorsToValidatorError } from './util';

const TYPE_JSON = 'application/json';

Expand Down
40 changes: 40 additions & 0 deletions src/middlewares/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ono from 'ono';

export function extractContentType(req) {
let contentType = req.headers['content-type'] || 'not_provided';
let end = contentType.indexOf(';');
Expand All @@ -7,3 +9,41 @@ export function extractContentType(req) {
}
return contentType;
}

const _validationError = (
status: number,
path: string,
message: string,
errors?: any, // TODO rename - normalize...something else
) => ({
status,
errors: [
{
path,
message,
...({ errors } || {}),
},
],
});

export function validationError(status, path, message) {
const err = _validationError(status, path, message);
return ono(err, message);
}

export function ajvErrorsToValidatorError(status, errors) {
return {
status,
errors: errors.map(e => {
const required =
e.params &&
e.params.missingProperty &&
e.dataPath + '.' + e.params.missingProperty;
return {
path: required || e.dataPath || e.schemaPath,
message: e.message,
errorCode: `${e.keyword}.openapi.validation`,
};
}),
};
}

0 comments on commit a3129e2

Please sign in to comment.