Skip to content

Commit

Permalink
lint cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 29, 2019
1 parent ff7d2a8 commit 8b0936f
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/framework/ajv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function createResponseAjv(
function createAjv(
openApiSpec: OpenAPIV3.Document,
options: ajv.Options = {},
request = true,
request: boolean = true,
): Ajv.Ajv {
const ajv = new Ajv({
...options,
Expand Down
13 changes: 11 additions & 2 deletions src/framework/openapi.schema.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import * as merge from 'lodash.merge';
import * as draftSchema from 'ajv/lib/refs/json-schema-draft-04.json';
// https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v3.0/schema.json
import * as openapi3Schema from './openapi.v3.schema.json';
import { OpenAPIV3 } from './types.js';

export class OpenAPISchemaValidator {
private validator: Ajv.ValidateFunction;
constructor({ version, extensions }: { version: string; extensions?: object }) {
constructor({
version,
extensions,
}: {
version: string;
extensions?: object;
}) {
const v = new Ajv({ schemaId: 'auto', allErrors: true });
v.addMetaSchema(draftSchema);

Expand All @@ -19,7 +26,9 @@ export class OpenAPISchemaValidator {
this.validator = v.compile(schema);
}

public validate(openapiDoc) {
public validate(
openapiDoc: OpenAPIV3.Document,
): { errors: Array<Ajv.ErrorObject> | null } {
const valid = this.validator(openapiDoc);
if (!valid) {
return { errors: this.validator.errors };
Expand Down
2 changes: 1 addition & 1 deletion src/framework/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ajv = require('ajv');
import * as ajv from 'ajv';
import { Request, Response, NextFunction } from 'express';
export { OpenAPIFrameworkArgs };

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/openapi.response.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ResponseValidator {
}

// TODO public for test only - fix me
_validate({ validators, body, statusCode, path }) {
public _validate({ validators, body, statusCode, path }) {
// find the validator for the 'status code' e.g 200, 2XX or 'default'
let validator;
const status = statusCode;
Expand Down
16 changes: 9 additions & 7 deletions src/middlewares/openapi.security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
OpenApiRequest,
SecurityHandlers,
OpenApiRequestMetadata,
OpenApiRequestHandler,
} from '../framework/types';
import { validationError } from './util';
import { OpenApiContext } from '../framework/openapi.context';
Expand All @@ -21,7 +22,7 @@ interface SecurityHandlerResult {
export function security(
context: OpenApiContext,
securityHandlers: SecurityHandlers,
) {
): OpenApiRequestHandler {
return async (req, res, next) => {
// TODO move the folllowing 3 check conditions to a dedicated upstream middleware
if (!req.openapi) {
Expand All @@ -31,12 +32,13 @@ export function security(
return next();
}

const expressRoute = req.openapi.expressRoute;
const openapi = <OpenApiRequestMetadata>req.openapi;
const expressRoute = openapi.expressRoute;
if (!expressRoute) {
return next(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 All @@ -47,9 +49,9 @@ export function security(

// use the local security object or fallbac to api doc's security or undefined
const securities: OpenAPIV3.SecurityRequirementObject[] =
req.openapi.schema.security ?? context.apiDoc.security;
openapi.schema.security ?? context.apiDoc.security;

const path: string = req.openapi.openApiRoute;
const path: string = openapi.openApiRoute;

if (!path || !Array.isArray(securities) || securities.length === 0) {
return next();
Expand Down Expand Up @@ -94,8 +96,8 @@ export function security(
class SecuritySchemes {
private securitySchemes;
private securityHandlers: SecurityHandlers;
private securities;
constructor(securitySchemes, securityHandlers: SecurityHandlers, securities) {
private securities: OpenAPIV3.SecurityRequirementObject[];
constructor(securitySchemes, securityHandlers: SecurityHandlers, securities: OpenAPIV3.SecurityRequirementObject[]) {
this.securitySchemes = securitySchemes;
this.securityHandlers = securityHandlers;
this.securities = securities;
Expand Down
39 changes: 22 additions & 17 deletions test/common/app.common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NextFunction, Request, Response } from 'express';
import * as http from 'http';
import * as express from 'express';

Expand All @@ -17,59 +18,61 @@ export function routes(app) {
const basePath = app.basePath;
const router1 = express
.Router()
.post('/', function(req, res, next) {
.post('/', function(req: Request, res: Response) {
res.json({
name: `${req.method}: /router_1`,
});
})
.get('/', function(req, res, next) {
.get('/', function(req: Request, res: Response) {
res.json({
name: `${req.method}: /router_1`,
});
})
.get('/:id', function(req, res, next) {
.get('/:id', function(req: Request, res: Response) {
res.json({
name: `${req.method}: /router_1/${req.params.id}`,
});
})
.get('/:id/best/:bid', function(req, res, next) {
.get('/:id/best/:bid', function(req: Request, res: Response) {
res.json({
name: `${req.method}: /router_1/${req.params.id}/best/${req.params.bid}`,
});
});

app.use(`${basePath}/router_1`, router1);

app.get(`${basePath}/pets`, function(req, res, next) {
app.get(`${basePath}/pets`, function(req: Request, res: Response) {
res.json({
test: 'hi',
...req.body,
});
});

app.post(`${basePath}/pets`, function(req, res, next) {
app.post(`${basePath}/pets`, function(req: Request, res: Response) {
res.json({
...req.body,
id: 'new-id',
});
});

app.get(`${basePath}/pets/:id`, function(req, res, next) {
app.get(`${basePath}/pets/:id`, function(req: Request, res: Response) {
res.json({
id: req.params.id,
});
});

app.get(`${basePath}/pets/:id/attributes`, function(req, res, next) {
app.get(`${basePath}/pets/:id/attributes`, function(
req: Request,
res: Response,
) {
res.json({
id: req.params.id,
});
});

app.get(`${basePath}/pets/:id/attributes/:attribute_id`, function(
req,
res,
next,
req: Request,
res: Response,
) {
res.json({
id: req.params.id,
Expand All @@ -78,22 +81,24 @@ export function routes(app) {
});

app.post(`${basePath}/route_defined_in_express_not_openapi`, function(
req,
res,
next,
req: Request,
res: Response,
) {
res.json({
id: req.params.id,
});
});

app.get('/not_under_an_openapi_basepath', function(req, res, next) {
app.get('/not_under_an_openapi_basepath', function(
req: Request,
res: Response,
) {
res.json({
id: '/not_under_an_openapi_basepath',
});
});

app.post('/v1/pets/:id/photos', function(req, res, next) {
app.post('/v1/pets/:id/photos', function(req: Request, res: Response) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
const files = req.files;
Expand All @@ -102,7 +107,7 @@ export function routes(app) {
metadata: req.body.metadata,
});
});
app.post('/v1/pets_charset', function (req: Request, res: any) {
app.post('/v1/pets_charset', function(req: Request, res: Response) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
res.json({
Expand Down
4 changes: 2 additions & 2 deletions test/common/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { OpenApiValidatorOpts } from '../../src/framework/types';

export async function createApp(
opts?: OpenApiValidatorOpts,
port = 3000,
port: number = 3000,
customRoutes = app => {},
useRoutes = true,
useRoutes: boolean = true,
) {
var app = express();
(<any>app).basePath = '/v1';
Expand Down
2 changes: 1 addition & 1 deletion test/openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createApp } from './common/app';
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let apps = [];
const apps = [];
let basePath = null;

before(() => {
Expand Down
1 change: 0 additions & 1 deletion test/path.level.parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
let basePath = null;

before(async () => {
// Set up the express app
Expand Down
1 change: 0 additions & 1 deletion test/query.params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
let basePath = null;

before(async () => {
// Set up the express app
Expand Down
2 changes: 1 addition & 1 deletion test/security.handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe('security.handlers', () => {
const validateSecurity = <ValidateSecurityOpts>eovConf.validateSecurity;
validateSecurity.handlers.OAuth2 = function(
req,
scopes,
scopes: string[],
schema: OpenAPIV3.OAuth2SecurityScheme,
) {
expect(schema.type).to.equal('oauth2');
Expand Down

0 comments on commit 8b0936f

Please sign in to comment.