UNPKG

@samchon/openapi

Version:

OpenAPI definitions and converters for 'typia' and 'nestia'.

1 lines 7.24 kB
{"version":3,"file":"HttpMigration.mjs","sources":["../src/HttpMigration.ts"],"sourcesContent":["import { OpenApi } from \"./OpenApi\";\nimport { OpenApiV3 } from \"./OpenApiV3\";\nimport { OpenApiV3_1 } from \"./OpenApiV3_1\";\nimport { SwaggerV2 } from \"./SwaggerV2\";\nimport { HttpMigrateApplicationComposer } from \"./composers/migrate/HttpMigrateApplicationComposer\";\nimport { HttpMigrateRouteFetcher } from \"./http/HttpMigrateRouteFetcher\";\nimport { IHttpConnection } from \"./structures/IHttpConnection\";\nimport { IHttpMigrateApplication } from \"./structures/IHttpMigrateApplication\";\nimport { IHttpMigrateRoute } from \"./structures/IHttpMigrateRoute\";\nimport { IHttpResponse } from \"./structures/IHttpResponse\";\n\n/**\n * HTTP migration application composer from OpenAPI document.\n *\n * `HttpMigration` is a module for composing HTTP migration application from the\n * {@link OpenApi.IDocument OpenAPI document}. It is designed for helping the OpenAPI\n * generator libraries, which converts {@link OpenApi.IOperation OpenAPI operations} to\n * an RPC (Remote Procedure Call) function.\n *\n * The key feature of the `HttpModule` is the {@link HttpMigration.application} function.\n * It converts the {@link OpenApi.IOperation OpenAPI operations} to the\n * {@link IHttpMigrateRoute HTTP migration route}, and it normalizes the OpenAPI operations\n * to the RPC function calling suitable route structure.\n *\n * The other functions, {@link HttpMigration.execute} and {@link HttpMigration.propagate},\n * are for executing the HTTP request to the HTTP server. The {@link HttpMigration.execute}\n * function returns the response body from the API endpoint when the status code is `200`\n * or `201`. Otherwise, it throws an {@link HttpError} when the status code is not `200`\n * or `201`. The {@link HttpMigration.propagate} function returns the response information\n * from the API endpoint, including the status code, headers, and response body.\n *\n * The {@link HttpLlm} module is a good example utilizing this `HttpMigration` module\n * for composing RPC function calling application. The {@link HttpLlm} module composes\n * LLM (Large Language Model) function calling application from the OpenAPI document\n * bypassing through the {@link IHttpLlmApplication} type.\n *\n * @author Jeongho Nam - https://github.com/samchon\n */\nexport namespace HttpMigration {\n /* -----------------------------------------------------------\n COMPOSER\n ----------------------------------------------------------- */\n /**\n * Convert HTTP migration application from OpenAPI document.\n *\n * `HttpMigration.application()` is a function converting the\n * {@link OpenApi.IDocument OpenAPI document} and its {@link OpenApi.IOperation operations}\n * to the {@link IHttpMigrateApplication HTTP migration application}.\n *\n * The HTTP migration application is designed for helping the OpenAPI generator\n * libraries, which converts OpenAPI operations to an RPC (Remote Procedure Call)\n * function. To support the OpenAPI generator libraries, {@link IHttpMigrateRoute}\n * takes below normalization rules:\n *\n * - Path parameters are separated to atomic level.\n * - Query parameters are binded into one object.\n * - Header parameters are binded into one object.\n * - Allow only below HTTP methods\n * - `head`\n * - `get`\n * - `post`\n * - `put`\n * - `patch`\n * - `delete`\n * - Allow only below content media types\n * - `application/json`\n * - `application/x-www-form-urlencoded`\n * - `multipart/form-data`\n * - `text/plain`\n *\n * If there're some {@link OpenApi.IOperation API operations} which canont adjust\n * the above rules or there're some logically insensible, these operation would be\n * failed to migrate and registered into the {@link IHttpMigrateApplication.errors}.\n *\n * @param document OpenAPI document to migrate.\n * @returns Migrated application.\n */\n export const application = (\n document:\n | OpenApi.IDocument\n | SwaggerV2.IDocument\n | OpenApiV3.IDocument\n | OpenApiV3_1.IDocument,\n ): IHttpMigrateApplication =>\n HttpMigrateApplicationComposer.compose(OpenApi.convert(document));\n\n /**\n * Properties for the request to the HTTP server.\n */\n export interface IFetchProps {\n /**\n * Connection info to the HTTP server.\n */\n connection: IHttpConnection;\n\n /**\n * Route information for the migration.\n */\n route: IHttpMigrateRoute;\n\n /**\n * Path parameters.\n *\n * Path parameters with sequenced array or key-value paired object.\n */\n parameters:\n | Array<string | number | boolean | bigint | null>\n | Record<string, string | number | boolean | bigint | null>;\n\n /**\n * Query parameters as a key-value paired object.\n */\n query?: object | undefined;\n\n /**\n * Request body data.\n */\n body?: object | undefined;\n }\n\n /* -----------------------------------------------------------\n FETCHERS\n ----------------------------------------------------------- */\n /**\n * Execute the HTTP request.\n *\n * `HttpMigration.execute()` is a function executing the HTTP request to the HTTP server.\n *\n * It returns the response body from the API endpoint when the status code is `200`\n * or `201`. Otherwise, it throws an {@link HttpError} when the status code is not\n * `200` or `201`.\n *\n * If you want to get more information than the response body, or get the detailed\n * response information even when the status code is `200` or `201`, use the\n * {@link HttpMigration.propagate} function instead.\n *\n * @param props Properties for the request.\n * @returns Return value (response body) from the API endpoint.\n * @throws HttpError when the API endpoint responds none 200/201 status.\n */\n export const execute = (props: IFetchProps): Promise<unknown> =>\n HttpMigrateRouteFetcher.execute(props);\n\n /**\n * Propagate the HTTP request.\n *\n * `HttpMigration.propagate()` is a function propagating the request to the HTTP server.\n *\n * It returns the response information from the API endpoint, including the status code,\n * headers, and response body.\n *\n * Even if the status code is not `200` or `201`, this function\n * would return the response information. By the way, if the connection to the HTTP server\n * is failed, this function would throw an {@link Error}.\n *\n * @param props Properties for the request.\n * @returns Response from the API endpoint.\n * @throws Error when the connection is failed.\n */\n export const propagate = (props: IFetchProps): Promise<IHttpResponse> =>\n HttpMigrateRouteFetcher.propagate(props);\n}\n"],"names":["HttpMigration","application","document","HttpMigrateApplicationComposer","compose","OpenApi","convert","execute","props","HttpMigrateRouteFetcher","propagate"],"mappings":";;;;;;AAsCM,IAAWA;;CAAjB,SAAiBA;IAuCFA,cAAAC,cACXC,YAMAC,+BAA+BC,QAAQC,QAAQC,QAAQJ;IAwD5CF,cAAAO,UAAWC,SACtBC,wBAAwBF,QAAQC;IAkBrBR,cAAAU,YAAaF,SACxBC,wBAAwBC,UAAUF;AACrC,EA3HD,CAAiBR,kBAAAA,gBA2HhB,CAAA;;"}