Skip to content

Commit

Permalink
chore: apiSpec may be const literal (#854)
Browse files Browse the repository at this point in the history
Co-authored-by: Carmine DiMascio <[email protected]>
  • Loading branch information
duncanbeevers and cdimascio authored Jun 2, 2024
1 parent a708132 commit e35a07c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
20 changes: 19 additions & 1 deletion src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,26 @@ export type SerDesMap = {
[format: string]: SerDes
};

type Primitive = undefined | null | boolean | string | number | Function

type Immutable<T> =
T extends Primitive ? T :
T extends Array<infer U> ? ReadonlyArray<U> :
T extends Map<infer K, infer V> ? ReadonlyMap<K, V> : Readonly<T>

type DeepImmutable<T> =
T extends Primitive ? T :
T extends Array<infer U> ? DeepImmutableArray<U> :
T extends Map<infer K, infer V> ? DeepImmutableMap<K, V> : DeepImmutableObject<T>

interface DeepImmutableArray<T> extends ReadonlyArray<DeepImmutable<T>> {}
interface DeepImmutableMap<K, V> extends ReadonlyMap<DeepImmutable<K>, DeepImmutable<V>> {}
type DeepImmutableObject<T> = {
readonly [K in keyof T]: DeepImmutable<T[K]>
}

export interface OpenApiValidatorOpts {
apiSpec: OpenAPIV3.Document | string;
apiSpec: DeepImmutable<OpenAPIV3.Document> | string;
validateApiSpec?: boolean;
validateResponses?: boolean | ValidateResponseOpts;
validateRequests?: boolean | ValidateRequestOpts;
Expand Down
30 changes: 16 additions & 14 deletions test/default-export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ 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();

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'),
}),
);
Expand Down

0 comments on commit e35a07c

Please sign in to comment.