From 47b0169e029b61128901ad575ba34d731558519e Mon Sep 17 00:00:00 2001 From: Duncan Beevers Date: Thu, 22 Jun 2023 06:30:33 -0700 Subject: [PATCH] chore: apiSpec may be const literal --- src/framework/types.ts | 22 ++++++++++++++++++++-- test/default-export.spec.ts | 30 ++++++++++++++++-------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/framework/types.ts b/src/framework/types.ts index 6f7c58a4..7c1e99da 100644 --- a/src/framework/types.ts +++ b/src/framework/types.ts @@ -1,6 +1,6 @@ import * as ajv from 'ajv'; import * as multer from 'multer'; -import { FormatsPluginOptions, FormatOptions } from 'ajv-formats'; +import { FormatsPluginOptions } from 'ajv-formats'; import { Request, Response, NextFunction } from 'express'; export { OpenAPIFrameworkArgs }; @@ -108,8 +108,26 @@ export type SerDesMap = { [format: string]: SerDes }; +type Primitive = undefined | null | boolean | string | number | Function + +type Immutable = + T extends Primitive ? T : + T extends Array ? ReadonlyArray : + T extends Map ? ReadonlyMap : Readonly + +type DeepImmutable = + T extends Primitive ? T : + T extends Array ? DeepImmutableArray : + T extends Map ? DeepImmutableMap : DeepImmutableObject + +interface DeepImmutableArray extends ReadonlyArray> {} +interface DeepImmutableMap extends ReadonlyMap, DeepImmutable> {} +type DeepImmutableObject = { + readonly [K in keyof T]: DeepImmutable +} + export interface OpenApiValidatorOpts { - apiSpec: OpenAPIV3.Document | string; + apiSpec: DeepImmutable | string; validateApiSpec?: boolean; validateResponses?: boolean | ValidateResponseOpts; validateRequests?: boolean | ValidateRequestOpts; diff --git a/test/default-export.spec.ts b/test/default-export.spec.ts index 6b4cd2fc..cacff56d 100644 --- a/test/default-export.spec.ts +++ b/test/default-export.spec.ts @@ -4,6 +4,21 @@ import { expect } from 'chai'; import * as request from 'supertest'; import * as path from 'path'; +const schema = { + openapi: '3.0.0', + info: { version: '1.0.0', title: 'test bug OpenApiValidator' }, + servers: [], + paths: { + '/': { + get: { + operationId: 'anything', + 'x-eov-operation-handler': 'controller-with-default', + responses: { 200: { description: 'home api' } } + } + }, + }, +} as const; + describe('default export resolver', () => { let server = null; let app = express(); @@ -11,20 +26,7 @@ describe('default export resolver', () => { before(async () => { app.use( OpenApiValidator.middleware({ - apiSpec: { - openapi: '3.0.0', - info: { version: '1.0.0', title: 'test bug OpenApiValidator' }, - paths: { - '/': { - get: { - operationId: 'anything', - // @ts-ignore - 'x-eov-operation-handler': 'controller-with-default', - responses: { 200: { description: 'home api' } } - } - }, - }, - }, + apiSpec: schema, operationHandlers: path.join(__dirname, 'resources'), }), );