@samchon/openapi
Advanced tools
Comparing version 0.2.2 to 0.3.0-dev.20240703
@@ -16,11 +16,11 @@ import { IMigrateRoute } from "./IMigrateRoute"; | ||
*/ | ||
export interface IMigrateDocument { | ||
export interface IMigrateDocument<Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, Operation extends OpenApi.IOperation<Schema> = OpenApi.IOperation<Schema>> { | ||
/** | ||
* List of routes for migration. | ||
*/ | ||
routes: IMigrateRoute[]; | ||
routes: IMigrateRoute<Schema, Operation>[]; | ||
/** | ||
* List of errors occurred during the migration. | ||
*/ | ||
errors: IMigrateDocument.IError[]; | ||
errors: IMigrateDocument.IError<Operation>[]; | ||
} | ||
@@ -31,7 +31,7 @@ export declare namespace IMigrateDocument { | ||
*/ | ||
interface IError { | ||
interface IError<Operation extends OpenApi.IOperation<any> = OpenApi.IOperation<OpenApi.IJsonSchema>> { | ||
/** | ||
* Target operation causing the error. | ||
*/ | ||
operation: () => OpenApi.IOperation; | ||
operation: () => Operation; | ||
/** | ||
@@ -38,0 +38,0 @@ * Method of the operation. |
@@ -15,3 +15,3 @@ import { OpenApi } from "./OpenApi"; | ||
*/ | ||
export interface IMigrateRoute { | ||
export interface IMigrateRoute<Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, Operation extends OpenApi.IOperation<Schema> = OpenApi.IOperation<Schema>> { | ||
/** | ||
@@ -61,3 +61,3 @@ * Method of the route. | ||
* | ||
* - Exxample 1 | ||
* - Example 1 | ||
* - path: `POST /shopping/sellers/sales` | ||
@@ -73,3 +73,3 @@ * - accessor: `shopping.sellers.sales.post` | ||
*/ | ||
parameters: IMigrateRoute.IParameter[]; | ||
parameters: IMigrateRoute.IParameter<Schema>[]; | ||
/** | ||
@@ -90,3 +90,3 @@ * Metadata of headers. | ||
*/ | ||
headers: IMigrateRoute.IHeaders | null; | ||
headers: IMigrateRoute.IHeaders<Schema> | null; | ||
/** | ||
@@ -107,3 +107,3 @@ * Metadata of query values. | ||
*/ | ||
query: IMigrateRoute.IQuery | null; | ||
query: IMigrateRoute.IQuery<Schema> | null; | ||
/** | ||
@@ -118,3 +118,3 @@ * Metadata of request body. | ||
*/ | ||
body: IMigrateRoute.IBody | null; | ||
body: IMigrateRoute.IBody<Schema> | null; | ||
/** | ||
@@ -129,3 +129,3 @@ * Metadata of response body for success case. | ||
*/ | ||
success: IMigrateRoute.IBody | null; | ||
success: IMigrateRoute.IBody<Schema> | null; | ||
/** | ||
@@ -142,3 +142,3 @@ * Metadata of response body for exceptional status cases. | ||
*/ | ||
exceptions: Record<string, IMigrateRoute.IException>; | ||
exceptions: Record<string, IMigrateRoute.IException<Schema>>; | ||
/** | ||
@@ -165,32 +165,32 @@ * Description comment for the route function. | ||
*/ | ||
operation: () => OpenApi.IOperation; | ||
operation: () => Operation; | ||
} | ||
export declare namespace IMigrateRoute { | ||
interface IParameter { | ||
interface IParameter<Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema> { | ||
name: string; | ||
key: string; | ||
schema: OpenApi.IJsonSchema; | ||
schema: Schema; | ||
description?: string; | ||
} | ||
interface IHeaders { | ||
interface IHeaders<Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema> { | ||
name: string; | ||
key: string; | ||
schema: OpenApi.IJsonSchema.IObject | OpenApi.IJsonSchema.IReference; | ||
schema: Schema; | ||
} | ||
interface IQuery { | ||
interface IQuery<Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema> { | ||
name: string; | ||
key: string; | ||
schema: OpenApi.IJsonSchema.IObject | OpenApi.IJsonSchema.IReference; | ||
schema: Schema; | ||
} | ||
interface IBody { | ||
interface IBody<Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema> { | ||
name: string; | ||
key: string; | ||
type: "text/plain" | "application/json" | "application/x-www-form-urlencoded" | "multipart/form-data"; | ||
schema: OpenApi.IJsonSchema; | ||
schema: Schema; | ||
"x-nestia-encrypted"?: boolean; | ||
} | ||
interface IException { | ||
interface IException<Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema> { | ||
description?: string; | ||
schema: OpenApi.IJsonSchema; | ||
schema: Schema; | ||
} | ||
} |
import { IMigrateDocument } from "../IMigrateDocument"; | ||
import { OpenApi } from "../OpenApi"; | ||
export declare namespace MigrateConverter { | ||
const convert: (document: OpenApi.IDocument) => IMigrateDocument; | ||
const convert: <Schema extends OpenApi.IJsonSchema, Operation extends OpenApi.IOperation<Schema>>(document: OpenApi.IDocument<Schema, Operation>) => IMigrateDocument<Schema, Operation>; | ||
} |
import { IMigrateRoute } from "../IMigrateRoute"; | ||
import { OpenApi } from "../OpenApi"; | ||
export declare namespace MigrateRouteAccessor { | ||
const overwrite: (routes: IMigrateRoute[]) => void; | ||
const overwrite: <Schema extends OpenApi.IJsonSchema, Operation extends OpenApi.IOperation<Schema>>(routes: IMigrateRoute<Schema, Operation>[]) => void; | ||
} |
@@ -46,3 +46,3 @@ import { IMigrateDocument } from "./IMigrateDocument"; | ||
*/ | ||
function convert(input: SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument | OpenApi.IDocument): IDocument; | ||
function convert<Schema extends IJsonSchema = IJsonSchema, Operation extends IOperation<Schema> = IOperation<Schema>>(input: SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument | OpenApi.IDocument<Schema, Operation>): IDocument<Schema, Operation>; | ||
/** | ||
@@ -57,12 +57,21 @@ * Downgrade to Swagger v2.0 document. | ||
*/ | ||
function downgrade(document: IDocument, version: "2.0"): SwaggerV2.IDocument; | ||
function downgrade(document: IDocument, version: "3.0"): OpenApiV3.IDocument; | ||
function migrate(document: IDocument): IMigrateDocument; | ||
interface IDocument { | ||
function downgrade<Schema extends IJsonSchema = IJsonSchema, Operation extends IOperation<Schema> = IOperation<Schema>>(document: IDocument<Schema, Operation>, version: "2.0"): SwaggerV2.IDocument; | ||
/** | ||
* Downgrade to OpenAPI v3.0 document. | ||
* | ||
* Downgrade the given document (emeneded OpenAPI v3.1) into OpenAPI v3.0. | ||
* | ||
* @param document Emended OpenAPI v3.1 document to downgrade | ||
* @param version Version to downgrade | ||
* @returns OpenAPI v3.0 document | ||
*/ | ||
function downgrade<Schema extends IJsonSchema = IJsonSchema, Operation extends IOperation<Schema> = IOperation<Schema>>(document: IDocument<Schema, Operation>, version: "3.0"): OpenApiV3.IDocument; | ||
function migrate<Schema extends IJsonSchema = IJsonSchema, Operation extends IOperation<Schema> = IOperation<Schema>>(document: IDocument<Schema, Operation>): IMigrateDocument<Schema, Operation>; | ||
interface IDocument<Schema extends IJsonSchema = IJsonSchema, Operation extends IOperation<Schema> = IOperation<Schema>> { | ||
openapi: `3.1.${number}`; | ||
servers?: IServer[]; | ||
info?: IDocument.IInfo; | ||
components: IComponents; | ||
paths?: Record<string, IPath>; | ||
webhooks?: Record<string, IPath>; | ||
components: IComponents<Schema>; | ||
paths?: Record<string, IPath<Schema, Operation>>; | ||
webhooks?: Record<string, IPath<Schema, Operation>>; | ||
security?: Record<string, string[]>[]; | ||
@@ -109,12 +118,12 @@ tags?: IDocument.ITag[]; | ||
} | ||
type IPath = { | ||
type IPath<Schema extends IJsonSchema = IJsonSchema, Operation extends IOperation<Schema> = IOperation<Schema>> = { | ||
servers?: IServer[]; | ||
summary?: string; | ||
description?: string; | ||
} & Partial<Record<Method, IOperation>>; | ||
interface IOperation { | ||
} & Partial<Record<Method, Operation>>; | ||
interface IOperation<Schema extends IJsonSchema = IJsonSchema> { | ||
operationId?: string; | ||
parameters?: IOperation.IParameter[]; | ||
requestBody?: IOperation.IRequestBody; | ||
responses?: Record<string, IOperation.IResponse>; | ||
parameters?: IOperation.IParameter<Schema>[]; | ||
requestBody?: IOperation.IRequestBody<Schema>; | ||
responses?: Record<string, IOperation.IResponse<Schema>>; | ||
servers?: IServer[]; | ||
@@ -128,6 +137,6 @@ summary?: string; | ||
namespace IOperation { | ||
interface IParameter { | ||
interface IParameter<Schema extends IJsonSchema = IJsonSchema> { | ||
name?: string; | ||
in: "path" | "query" | "header" | "cookie"; | ||
schema: IJsonSchema; | ||
schema: Schema; | ||
required?: boolean; | ||
@@ -137,22 +146,22 @@ title?: string; | ||
} | ||
interface IRequestBody { | ||
interface IRequestBody<Schema extends IJsonSchema = IJsonSchema> { | ||
description?: string; | ||
required?: boolean; | ||
content?: IContent; | ||
content?: IContent<Schema>; | ||
"x-nestia-encrypted"?: boolean; | ||
} | ||
interface IResponse { | ||
content?: IContent; | ||
headers?: Record<string, IOperation.IParameter>; | ||
interface IResponse<Schema extends IJsonSchema = IJsonSchema> { | ||
content?: IContent<Schema>; | ||
headers?: Record<string, IOperation.IParameter<Schema>>; | ||
description?: string; | ||
"x-nestia-encrypted"?: boolean; | ||
} | ||
type IContent = Partial<Record<ContentType, IMediaType>>; | ||
interface IMediaType { | ||
schema?: IJsonSchema; | ||
type IContent<Schema extends IJsonSchema = IJsonSchema> = Partial<Record<ContentType, IMediaType<Schema>>>; | ||
interface IMediaType<Schema extends IJsonSchema = IJsonSchema> { | ||
schema?: Schema; | ||
} | ||
type ContentType = "text/plain" | "application/json" | "application/x-www-form-url-encoded" | "multipart/form-data" | "*/*" | (string & {}); | ||
} | ||
interface IComponents { | ||
schemas?: Record<string, IJsonSchema>; | ||
interface IComponents<Schema extends IJsonSchema = IJsonSchema> { | ||
schemas?: Record<string, Schema>; | ||
securitySchemes?: Record<string, ISecurityScheme>; | ||
@@ -196,4 +205,4 @@ } | ||
} | ||
interface IArray extends __ISignificant<"array"> { | ||
items: IJsonSchema; | ||
interface IArray<Schema extends IJsonSchema = IJsonSchema> extends __ISignificant<"array"> { | ||
items: Schema; | ||
uniqueItems?: boolean; | ||
@@ -203,5 +212,5 @@ /** @type uint64 */ minItems?: number; | ||
} | ||
interface ITuple extends __ISignificant<"array"> { | ||
prefixItems: IJsonSchema[]; | ||
additionalItems?: boolean | IJsonSchema; | ||
interface ITuple<Schema extends IJsonSchema = IJsonSchema> extends __ISignificant<"array"> { | ||
prefixItems: Schema[]; | ||
additionalItems?: boolean | Schema; | ||
uniqueItems?: boolean; | ||
@@ -211,5 +220,5 @@ /** @type uint64 */ minItems?: number; | ||
} | ||
interface IObject extends __ISignificant<"object"> { | ||
properties?: Record<string, IJsonSchema>; | ||
additionalProperties?: boolean | IJsonSchema; | ||
interface IObject<Schema extends IJsonSchema = IJsonSchema> extends __ISignificant<"object"> { | ||
properties?: Record<string, Schema>; | ||
additionalProperties?: boolean | Schema; | ||
required?: string[]; | ||
@@ -220,3 +229,3 @@ } | ||
} | ||
interface IOneOf extends __IAttribute { | ||
interface IOneOf<Schema extends IJsonSchema = IJsonSchema> extends __IAttribute { | ||
oneOf: Exclude<IJsonSchema, IJsonSchema.IOneOf>[]; | ||
@@ -223,0 +232,0 @@ } |
@@ -64,11 +64,2 @@ "use strict"; | ||
OpenApi.convert = convert; | ||
/** | ||
* Downgrade to OpenAPI v2.3 document. | ||
* | ||
* Downgrade the given document (emeneded OpenAPI v3.1) into OpenAPI v3.0. | ||
* | ||
* @param document Emended OpenAPI v3.1 document to downgrade | ||
* @param version Version to downgrade | ||
* @returns OpenAPI v3.0 document | ||
*/ | ||
function downgrade(document, version) { | ||
@@ -75,0 +66,0 @@ if (version === "2.0") |
{ | ||
"name": "@samchon/openapi", | ||
"version": "0.2.2", | ||
"version": "0.3.0-dev.20240703", | ||
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -17,7 +17,10 @@ import { IMigrateRoute } from "./IMigrateRoute"; | ||
*/ | ||
export interface IMigrateDocument { | ||
export interface IMigrateDocument< | ||
Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema> = OpenApi.IOperation<Schema>, | ||
> { | ||
/** | ||
* List of routes for migration. | ||
*/ | ||
routes: IMigrateRoute[]; | ||
routes: IMigrateRoute<Schema, Operation>[]; | ||
@@ -27,3 +30,3 @@ /** | ||
*/ | ||
errors: IMigrateDocument.IError[]; | ||
errors: IMigrateDocument.IError<Operation>[]; | ||
} | ||
@@ -34,7 +37,10 @@ export namespace IMigrateDocument { | ||
*/ | ||
export interface IError { | ||
export interface IError< | ||
Operation extends | ||
OpenApi.IOperation<any> = OpenApi.IOperation<OpenApi.IJsonSchema>, | ||
> { | ||
/** | ||
* Target operation causing the error. | ||
*/ | ||
operation: () => OpenApi.IOperation; | ||
operation: () => Operation; | ||
@@ -41,0 +47,0 @@ /** |
@@ -16,3 +16,6 @@ import { OpenApi } from "./OpenApi"; | ||
*/ | ||
export interface IMigrateRoute { | ||
export interface IMigrateRoute< | ||
Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema> = OpenApi.IOperation<Schema>, | ||
> { | ||
/** | ||
@@ -65,3 +68,3 @@ * Method of the route. | ||
* | ||
* - Exxample 1 | ||
* - Example 1 | ||
* - path: `POST /shopping/sellers/sales` | ||
@@ -78,3 +81,3 @@ * - accessor: `shopping.sellers.sales.post` | ||
*/ | ||
parameters: IMigrateRoute.IParameter[]; | ||
parameters: IMigrateRoute.IParameter<Schema>[]; | ||
@@ -96,3 +99,3 @@ /** | ||
*/ | ||
headers: IMigrateRoute.IHeaders | null; | ||
headers: IMigrateRoute.IHeaders<Schema> | null; | ||
@@ -114,3 +117,3 @@ /** | ||
*/ | ||
query: IMigrateRoute.IQuery | null; | ||
query: IMigrateRoute.IQuery<Schema> | null; | ||
@@ -126,3 +129,3 @@ /** | ||
*/ | ||
body: IMigrateRoute.IBody | null; | ||
body: IMigrateRoute.IBody<Schema> | null; | ||
@@ -138,3 +141,3 @@ /** | ||
*/ | ||
success: IMigrateRoute.IBody | null; | ||
success: IMigrateRoute.IBody<Schema> | null; | ||
@@ -152,3 +155,3 @@ /** | ||
*/ | ||
exceptions: Record<string, IMigrateRoute.IException>; | ||
exceptions: Record<string, IMigrateRoute.IException<Schema>>; | ||
@@ -177,22 +180,30 @@ /** | ||
*/ | ||
operation: () => OpenApi.IOperation; | ||
operation: () => Operation; | ||
} | ||
export namespace IMigrateRoute { | ||
export interface IParameter { | ||
export interface IParameter< | ||
Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, | ||
> { | ||
name: string; | ||
key: string; | ||
schema: OpenApi.IJsonSchema; | ||
schema: Schema; | ||
description?: string; | ||
} | ||
export interface IHeaders { | ||
export interface IHeaders< | ||
Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, | ||
> { | ||
name: string; // headers | ||
key: string; // headers | ||
schema: OpenApi.IJsonSchema.IObject | OpenApi.IJsonSchema.IReference; | ||
schema: Schema; | ||
} | ||
export interface IQuery { | ||
export interface IQuery< | ||
Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, | ||
> { | ||
name: string; | ||
key: string; | ||
schema: OpenApi.IJsonSchema.IObject | OpenApi.IJsonSchema.IReference; | ||
schema: Schema; | ||
} | ||
export interface IBody { | ||
export interface IBody< | ||
Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, | ||
> { | ||
name: string; | ||
@@ -205,9 +216,11 @@ key: string; | ||
| "multipart/form-data"; | ||
schema: OpenApi.IJsonSchema; | ||
schema: Schema; | ||
"x-nestia-encrypted"?: boolean; | ||
} | ||
export interface IException { | ||
export interface IException< | ||
Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema, | ||
> { | ||
description?: string; | ||
schema: OpenApi.IJsonSchema; | ||
schema: Schema; | ||
} | ||
} |
@@ -9,36 +9,42 @@ import { IMigrateRoute } from "../IMigrateRoute"; | ||
export namespace MigrateConverter { | ||
export const convert = (document: OpenApi.IDocument): IMigrateDocument => { | ||
const errors: IMigrateDocument.IError[] = []; | ||
const entire: Array<IMigrateRoute | null> = Object.entries({ | ||
...(document.paths ?? {}), | ||
...(document.webhooks ?? {}), | ||
}) | ||
.map(([path, collection]) => | ||
(["head", "get", "post", "put", "patch", "delete"] as const) | ||
.filter((method) => collection[method] !== undefined) | ||
.map((method) => { | ||
const operation: OpenApi.IOperation = collection[method]!; | ||
const migrated: IMigrateRoute | string[] = | ||
MigrateRouteConverter.convert({ | ||
document, | ||
method, | ||
path, | ||
emendedPath: StringUtil.reJoinWithDecimalParameters(path), | ||
operation, | ||
}); | ||
if (Array.isArray(migrated)) { | ||
errors.push({ | ||
method, | ||
path, | ||
operation: () => operation, | ||
messages: migrated, | ||
}); | ||
return null; | ||
} | ||
return migrated; | ||
}), | ||
) | ||
.flat(); | ||
const operations: IMigrateRoute[] = entire.filter( | ||
(o): o is IMigrateRoute => !!o, | ||
export const convert = < | ||
Schema extends OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema>, | ||
>( | ||
document: OpenApi.IDocument<Schema, Operation>, | ||
): IMigrateDocument<Schema, Operation> => { | ||
const errors: IMigrateDocument.IError<Operation>[] = []; | ||
const entire: Array<IMigrateRoute<Schema, Operation> | null> = | ||
Object.entries({ | ||
...(document.paths ?? {}), | ||
...(document.webhooks ?? {}), | ||
}) | ||
.map(([path, collection]) => | ||
(["head", "get", "post", "put", "patch", "delete"] as const) | ||
.filter((method) => collection[method] !== undefined) | ||
.map((method) => { | ||
const operation: Operation = collection[method]!; | ||
const migrated: IMigrateRoute<Schema, Operation> | string[] = | ||
MigrateRouteConverter.convert({ | ||
document, | ||
method, | ||
path, | ||
emendedPath: StringUtil.reJoinWithDecimalParameters(path), | ||
operation, | ||
}) as IMigrateRoute<Schema, Operation> | string[]; | ||
if (Array.isArray(migrated)) { | ||
errors.push({ | ||
method, | ||
path, | ||
operation: () => operation, | ||
messages: migrated, | ||
}); | ||
return null; | ||
} | ||
return migrated; | ||
}), | ||
) | ||
.flat(); | ||
const operations: IMigrateRoute<Schema, Operation>[] = entire.filter( | ||
(o): o is IMigrateRoute<Schema, Operation> => !!o, | ||
); | ||
@@ -45,0 +51,0 @@ MigrateRouteAccessor.overwrite(operations); |
import { IMigrateRoute } from "../IMigrateRoute"; | ||
import { OpenApi } from "../OpenApi"; | ||
import { Escaper } from "../utils/Escaper"; | ||
@@ -7,4 +8,9 @@ import { MapUtil } from "../utils/MapUtil"; | ||
export namespace MigrateRouteAccessor { | ||
export const overwrite = (routes: IMigrateRoute[]): void => { | ||
const dict: Map<string, IElement> = collect((op) => | ||
export const overwrite = < | ||
Schema extends OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema>, | ||
>( | ||
routes: IMigrateRoute<Schema, Operation>[], | ||
): void => { | ||
const dict: Map<string, IElement<Schema, Operation>> = collect((op) => | ||
op.emendedPath | ||
@@ -15,3 +21,3 @@ .split("/") | ||
.map((str) => (Escaper.variable(str) ? str : `_${str}`)), | ||
)(routes); | ||
)(routes) as Map<string, IElement<Schema, Operation>>; | ||
for (const props of dict.values()) | ||
@@ -45,8 +51,17 @@ props.entries.forEach((entry, i) => { | ||
const collect = | ||
(getter: (r: IMigrateRoute) => string[]) => | ||
(routes: IMigrateRoute[]): Map<string, IElement> => { | ||
const dict: Map<string, IElement> = new Map(); | ||
< | ||
Schema extends OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema>, | ||
>( | ||
getter: (r: IMigrateRoute<Schema, Operation>) => string[], | ||
) => | ||
( | ||
routes: IMigrateRoute<Schema, Operation>[], | ||
): Map<string, IElement<Schema, Operation>> => { | ||
const dict: Map<string, IElement<Schema, Operation>> = new Map(); | ||
for (const r of routes) { | ||
const namespace: string[] = getter(r); | ||
let last: IElement = MapUtil.take(dict)(namespace.join("."))(() => ({ | ||
let last: IElement<Schema, Operation> = MapUtil.take(dict)( | ||
namespace.join("."), | ||
)(() => ({ | ||
namespace, | ||
@@ -62,9 +77,9 @@ children: new Set(), | ||
const partial: string[] = namespace.slice(0, array.length - i); | ||
const element: IElement = MapUtil.take(dict)(partial.join("."))( | ||
() => ({ | ||
namespace: partial, | ||
children: new Set(), | ||
entries: [], | ||
}), | ||
); | ||
const element: IElement<Schema, Operation> = MapUtil.take(dict)( | ||
partial.join("."), | ||
)(() => ({ | ||
namespace: partial, | ||
children: new Set(), | ||
entries: [], | ||
})); | ||
element.children.add(last.namespace.at(-1)!); | ||
@@ -82,3 +97,8 @@ }); | ||
const getName = (op: IMigrateRoute): string => { | ||
const getName = < | ||
Schema extends OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema>, | ||
>( | ||
op: IMigrateRoute<Schema, Operation>, | ||
): string => { | ||
const method = op.method === "delete" ? "erase" : op.method; | ||
@@ -93,11 +113,17 @@ if (op.parameters.length === 0) return method; | ||
interface IElement { | ||
interface IElement< | ||
Schema extends OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema>, | ||
> { | ||
namespace: string[]; | ||
entries: IEntry[]; | ||
entries: IEntry<Schema, Operation>[]; | ||
children: Set<string>; | ||
} | ||
interface IEntry { | ||
route: IMigrateRoute; | ||
interface IEntry< | ||
Schema extends OpenApi.IJsonSchema, | ||
Operation extends OpenApi.IOperation<Schema>, | ||
> { | ||
route: IMigrateRoute<Schema, Operation>; | ||
alias: string; | ||
} | ||
} |
@@ -62,3 +62,6 @@ import { IMigrateDocument } from "./IMigrateDocument"; | ||
*/ | ||
export function convert( | ||
export function convert< | ||
Schema extends IJsonSchema = IJsonSchema, | ||
Operation extends IOperation<Schema> = IOperation<Schema>, | ||
>( | ||
input: | ||
@@ -68,7 +71,13 @@ | SwaggerV2.IDocument | ||
| OpenApiV3_1.IDocument | ||
| OpenApi.IDocument, | ||
): IDocument { | ||
if (OpenApiV3_1.is(input)) return OpenApiV3_1Converter.convert(input); | ||
else if (OpenApiV3.is(input)) return OpenApiV3Converter.convert(input); | ||
else if (SwaggerV2.is(input)) return SwaggerV2Converter.convert(input); | ||
| OpenApi.IDocument<Schema, Operation>, | ||
): IDocument<Schema, Operation> { | ||
if (OpenApiV3_1.is(input)) | ||
return OpenApiV3_1Converter.convert(input) as IDocument< | ||
Schema, | ||
Operation | ||
>; | ||
else if (OpenApiV3.is(input)) | ||
return OpenApiV3Converter.convert(input) as IDocument<Schema, Operation>; | ||
else if (SwaggerV2.is(input)) | ||
return SwaggerV2Converter.convert(input) as IDocument<Schema, Operation>; | ||
throw new TypeError("Unrecognized Swagger/OpenAPI version."); | ||
@@ -86,13 +95,12 @@ } | ||
*/ | ||
export function downgrade( | ||
document: IDocument, | ||
export function downgrade< | ||
Schema extends IJsonSchema = IJsonSchema, | ||
Operation extends IOperation<Schema> = IOperation<Schema>, | ||
>( | ||
document: IDocument<Schema, Operation>, | ||
version: "2.0", | ||
): SwaggerV2.IDocument; | ||
export function downgrade( | ||
document: IDocument, | ||
version: "3.0", | ||
): OpenApiV3.IDocument; | ||
/** | ||
* Downgrade to OpenAPI v2.3 document. | ||
* Downgrade to OpenAPI v3.0 document. | ||
* | ||
@@ -105,4 +113,15 @@ * Downgrade the given document (emeneded OpenAPI v3.1) into OpenAPI v3.0. | ||
*/ | ||
export function downgrade( | ||
document: IDocument, | ||
export function downgrade< | ||
Schema extends IJsonSchema = IJsonSchema, | ||
Operation extends IOperation<Schema> = IOperation<Schema>, | ||
>( | ||
document: IDocument<Schema, Operation>, | ||
version: "3.0", | ||
): OpenApiV3.IDocument; | ||
export function downgrade< | ||
Schema extends IJsonSchema = IJsonSchema, | ||
Operation extends IOperation<Schema> = IOperation<Schema>, | ||
>( | ||
document: IDocument<Schema, Operation>, | ||
version: string, | ||
@@ -115,3 +134,8 @@ ): SwaggerV2.IDocument | OpenApiV3.IDocument { | ||
export function migrate(document: IDocument): IMigrateDocument { | ||
export function migrate< | ||
Schema extends IJsonSchema = IJsonSchema, | ||
Operation extends IOperation<Schema> = IOperation<Schema>, | ||
>( | ||
document: IDocument<Schema, Operation>, | ||
): IMigrateDocument<Schema, Operation> { | ||
return MigrateConverter.convert(document); | ||
@@ -123,9 +147,12 @@ } | ||
----------------------------------------------------------- */ | ||
export interface IDocument { | ||
export interface IDocument< | ||
Schema extends IJsonSchema = IJsonSchema, | ||
Operation extends IOperation<Schema> = IOperation<Schema>, | ||
> { | ||
openapi: `3.1.${number}`; | ||
servers?: IServer[]; | ||
info?: IDocument.IInfo; | ||
components: IComponents; | ||
paths?: Record<string, IPath>; | ||
webhooks?: Record<string, IPath>; | ||
components: IComponents<Schema>; | ||
paths?: Record<string, IPath<Schema, Operation>>; | ||
webhooks?: Record<string, IPath<Schema, Operation>>; | ||
security?: Record<string, string[]>[]; | ||
@@ -177,13 +204,16 @@ tags?: IDocument.ITag[]; | ||
----------------------------------------------------------- */ | ||
export type IPath = { | ||
export type IPath< | ||
Schema extends IJsonSchema = IJsonSchema, | ||
Operation extends IOperation<Schema> = IOperation<Schema>, | ||
> = { | ||
servers?: IServer[]; | ||
summary?: string; | ||
description?: string; | ||
} & Partial<Record<Method, IOperation>>; | ||
} & Partial<Record<Method, Operation>>; | ||
export interface IOperation { | ||
export interface IOperation<Schema extends IJsonSchema = IJsonSchema> { | ||
operationId?: string; | ||
parameters?: IOperation.IParameter[]; | ||
requestBody?: IOperation.IRequestBody; | ||
responses?: Record<string, IOperation.IResponse>; | ||
parameters?: IOperation.IParameter<Schema>[]; | ||
requestBody?: IOperation.IRequestBody<Schema>; | ||
responses?: Record<string, IOperation.IResponse<Schema>>; | ||
servers?: IServer[]; | ||
@@ -197,6 +227,6 @@ summary?: string; | ||
export namespace IOperation { | ||
export interface IParameter { | ||
export interface IParameter<Schema extends IJsonSchema = IJsonSchema> { | ||
name?: string; | ||
in: "path" | "query" | "header" | "cookie"; | ||
schema: IJsonSchema; | ||
schema: Schema; | ||
required?: boolean; | ||
@@ -206,11 +236,11 @@ title?: string; | ||
} | ||
export interface IRequestBody { | ||
export interface IRequestBody<Schema extends IJsonSchema = IJsonSchema> { | ||
description?: string; | ||
required?: boolean; | ||
content?: IContent; | ||
content?: IContent<Schema>; | ||
"x-nestia-encrypted"?: boolean; | ||
} | ||
export interface IResponse { | ||
content?: IContent; | ||
headers?: Record<string, IOperation.IParameter>; | ||
export interface IResponse<Schema extends IJsonSchema = IJsonSchema> { | ||
content?: IContent<Schema>; | ||
headers?: Record<string, IOperation.IParameter<Schema>>; | ||
description?: string; | ||
@@ -220,5 +250,7 @@ "x-nestia-encrypted"?: boolean; | ||
export type IContent = Partial<Record<ContentType, IMediaType>>; | ||
export interface IMediaType { | ||
schema?: IJsonSchema; | ||
export type IContent<Schema extends IJsonSchema = IJsonSchema> = Partial< | ||
Record<ContentType, IMediaType<Schema>> | ||
>; | ||
export interface IMediaType<Schema extends IJsonSchema = IJsonSchema> { | ||
schema?: Schema; | ||
} | ||
@@ -237,4 +269,4 @@ export type ContentType = | ||
----------------------------------------------------------- */ | ||
export interface IComponents { | ||
schemas?: Record<string, IJsonSchema>; | ||
export interface IComponents<Schema extends IJsonSchema = IJsonSchema> { | ||
schemas?: Record<string, Schema>; | ||
securitySchemes?: Record<string, ISecurityScheme>; | ||
@@ -316,4 +348,5 @@ } | ||
export interface IArray extends __ISignificant<"array"> { | ||
items: IJsonSchema; | ||
export interface IArray<Schema extends IJsonSchema = IJsonSchema> | ||
extends __ISignificant<"array"> { | ||
items: Schema; | ||
uniqueItems?: boolean; | ||
@@ -323,5 +356,6 @@ /** @type uint64 */ minItems?: number; | ||
} | ||
export interface ITuple extends __ISignificant<"array"> { | ||
prefixItems: IJsonSchema[]; | ||
additionalItems?: boolean | IJsonSchema; | ||
export interface ITuple<Schema extends IJsonSchema = IJsonSchema> | ||
extends __ISignificant<"array"> { | ||
prefixItems: Schema[]; | ||
additionalItems?: boolean | Schema; | ||
uniqueItems?: boolean; | ||
@@ -331,5 +365,6 @@ /** @type uint64 */ minItems?: number; | ||
} | ||
export interface IObject extends __ISignificant<"object"> { | ||
properties?: Record<string, IJsonSchema>; | ||
additionalProperties?: boolean | IJsonSchema; | ||
export interface IObject<Schema extends IJsonSchema = IJsonSchema> | ||
extends __ISignificant<"object"> { | ||
properties?: Record<string, Schema>; | ||
additionalProperties?: boolean | Schema; | ||
required?: string[]; | ||
@@ -341,3 +376,4 @@ } | ||
export interface IOneOf extends __IAttribute { | ||
export interface IOneOf<Schema extends IJsonSchema = IJsonSchema> | ||
extends __IAttribute { | ||
oneOf: Exclude<IJsonSchema, IJsonSchema.IOneOf>[]; | ||
@@ -344,0 +380,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
316589
7765