Skip to content
Open
69 changes: 42 additions & 27 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import AjvDraft4 from 'ajv-draft-04';
import Ajv2020 from 'ajv/dist/2020';
export { OpenAPIFrameworkArgs };

export type AjvInstance = AjvDraft4 | Ajv2020
export type AjvInstance = AjvDraft4 | Ajv2020;

export type BodySchema =
| OpenAPIV3.ReferenceObject
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface Options extends ajv.Options {
ajvFormats?: FormatsPluginOptions;
}

export interface RequestValidatorOptions extends Options, ValidateRequestOpts { }
export interface RequestValidatorOptions extends Options, ValidateRequestOpts {}

export type ValidateRequestOpts = {
/**
Expand Down Expand Up @@ -125,32 +125,42 @@ export class SerDesSingleton implements SerDes {
serialize: param.serialize,
};
}
};
}

export type SerDesMap = {
[format: string]: SerDes
[format: string]: SerDes;
};

type Primitive = undefined | null | boolean | string | number | Function
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 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>
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>> {}
interface DeepImmutableMap<K, V>
extends ReadonlyMap<DeepImmutable<K>, DeepImmutable<V>> {}
type DeepImmutableObject<T> = {
readonly [K in keyof T]: DeepImmutable<T[K]>
}
readonly [K in keyof T]: DeepImmutable<T[K]>;
};

export interface OpenApiValidatorOpts {
apiSpec: DeepImmutable<OpenAPIV3.DocumentV3> | DeepImmutable<OpenAPIV3.DocumentV3_1> | string;
apiSpec:
| DeepImmutable<OpenAPIV3.DocumentV3>
| DeepImmutable<OpenAPIV3.DocumentV3_1>
| string;
validateApiSpec?: boolean;
validateResponses?: boolean | ValidateResponseOpts;
validateRequests?: boolean | ValidateRequestOpts;
Expand Down Expand Up @@ -204,18 +214,19 @@ export namespace OpenAPIV3 {
externalDocs?: ExternalDocumentationObject;
}

interface ComponentsV3_1 extends ComponentsObject {
pathItems?: { [path: string]: PathItemObject | ReferenceObject }
export interface ComponentsV3_1 extends ComponentsObject {
pathItems?: { [path: string]: PathItemObject | ReferenceObject };
}

export interface DocumentV3_1 extends Omit<DocumentV3, 'paths' | 'info' | 'components'| "openapi" > {
export interface DocumentV3_1
extends Omit<DocumentV3, 'paths' | 'info' | 'components' | 'openapi'> {
openapi: `3.1.${string}`;
paths?: DocumentV3['paths'];
info: InfoObjectV3_1;
components: ComponentsV3_1;
webhooks: {
[name: string]: PathItemObject | ReferenceObject
}
[name: string]: PathItemObject | ReferenceObject;
};
}

export interface InfoObject {
Expand Down Expand Up @@ -299,7 +310,7 @@ export namespace OpenAPIV3 {
in: string;
}

export interface HeaderObject extends ParameterBaseObject { }
export interface HeaderObject extends ParameterBaseObject {}

interface ParameterBaseObject {
description?: string;
Expand All @@ -323,14 +334,18 @@ export namespace OpenAPIV3 {
| 'integer';
export type ArraySchemaObjectType = 'array';

export type SchemaObject = ArraySchemaObject | NonArraySchemaObject | CompositionSchemaObject;
export type SchemaObject =
| ArraySchemaObject
| NonArraySchemaObject
| CompositionSchemaObject;

export interface ArraySchemaObject extends BaseSchemaObject<ArraySchemaObjectType> {
export interface ArraySchemaObject
extends BaseSchemaObject<ArraySchemaObjectType> {
items: ReferenceObject | SchemaObject;
}

export interface NonArraySchemaObject extends BaseSchemaObject<NonArraySchemaObjectType> {
}
export interface NonArraySchemaObject
extends BaseSchemaObject<NonArraySchemaObjectType> {}

export interface CompositionSchemaObject extends BaseSchemaObject<undefined> {
// JSON schema allowed properties, adjusted for OpenAPI
Expand Down
Loading