-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from samchon/features/migrate
New feature, `OpenApi.migrate()` function.
- Loading branch information
Showing
14 changed files
with
830 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { IMigrateRoute } from "./IMigrateRoute"; | ||
import { OpenApi } from "./OpenApi"; | ||
|
||
export interface IMigrateDocument { | ||
routes: IMigrateRoute[]; | ||
errors: IMigrateDocument.IError[]; | ||
} | ||
export namespace IMigrateDocument { | ||
export interface IError { | ||
operation: () => OpenApi.IOperation; | ||
method: "head" | "get" | "post" | "put" | "patch" | "delete"; | ||
path: string; | ||
messages: string[]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { OpenApi } from "./OpenApi"; | ||
|
||
export interface IMigrateRoute { | ||
method: "head" | "get" | "post" | "put" | "patch" | "delete"; | ||
path: string; | ||
emendedPath: string; | ||
accessor: string[]; // function accessor | ||
parameters: IMigrateRoute.IParameter[]; // path parameters | ||
headers: IMigrateRoute.IHeaders | null; // as an object | ||
query: IMigrateRoute.IQuery | null; // as an object | ||
body: IMigrateRoute.IBody | null; // request body | ||
success: IMigrateRoute.IBody | null; // 200 or 201 status case | ||
exceptions: Record<string, IMigrateRoute.IException>; // other status cases | ||
comment: () => string; | ||
operation: () => OpenApi.IOperation; | ||
} | ||
export namespace IMigrateRoute { | ||
export interface IParameter { | ||
name: string; | ||
key: string; | ||
schema: OpenApi.IJsonSchema; | ||
description?: string; | ||
} | ||
export interface IHeaders { | ||
name: string; // headers | ||
key: string; // headers | ||
schema: OpenApi.IJsonSchema.IObject | OpenApi.IJsonSchema.IReference; | ||
} | ||
export interface IQuery { | ||
name: string; | ||
key: string; | ||
schema: OpenApi.IJsonSchema.IObject | OpenApi.IJsonSchema.IReference; | ||
} | ||
export interface IBody { | ||
name: string; | ||
key: string; | ||
type: | ||
| "text/plain" | ||
| "application/json" | ||
| "application/x-www-form-urlencoded" | ||
| "multipart/form-data"; | ||
schema: OpenApi.IJsonSchema; | ||
"x-nestia-encrypted"?: boolean; | ||
} | ||
export interface IException { | ||
description?: string; | ||
schema: OpenApi.IJsonSchema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { IMigrateRoute } from "../IMigrateRoute"; | ||
import { IMigrateDocument } from "../IMigrateDocument"; | ||
import { OpenApi } from "../OpenApi"; | ||
import { StringUtil } from "../utils/StringUtil"; | ||
import { MigrateOperationConverter } from "./MigrateOperationConverter"; | ||
import { MigrateRouteAccessor } from "./MigrateRouteAccessor"; | ||
|
||
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[] = | ||
MigrateOperationConverter.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, | ||
); | ||
MigrateRouteAccessor.overwrite(operations); | ||
return { | ||
routes: operations, | ||
errors, | ||
}; | ||
}; | ||
} |
Oops, something went wrong.