Skip to content

Commit

Permalink
Merge pull request #940 from samchon/features/migrate
Browse files Browse the repository at this point in the history
`MigrateFetcher()` on `@nestia/fetcher`
  • Loading branch information
samchon authored Jul 1, 2024
2 parents 922d85b + 2323cf6 commit 1fa202c
Show file tree
Hide file tree
Showing 47 changed files with 1,582 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@nestia/station",
"version": "3.3.2",
"version": "3.4.0",
"description": "Nestia station",
"scripts": {
"build": "node build/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestia",
"version": "5.3.0",
"version": "5.3.1",
"description": "Nestia CLI tool",
"main": "bin/index.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/NestiaSetupWizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export namespace NestiaSetupWizard {
// INSTALL TYPESCRIPT COMPILERS
pack.install({ dev: true, modulo: "ts-patch", version: "latest" });
pack.install({ dev: true, modulo: "ts-node", version: "latest" });
pack.install({ dev: true, modulo: "typescript", version: "5.4.2" });
pack.install({ dev: true, modulo: "typescript", version: "5.5.2" });
args.project ??= (() => {
const runner: string = pack.manager === "npm" ? "npx" : pack.manager;
CommandExecutor.run(`${runner} tsc --init`);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "3.3.2",
"version": "3.4.0",
"description": "Super-fast validation decorators of NestJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -36,10 +36,10 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "../fetcher/nestia-fetcher-3.3.2.tgz",
"@nestia/fetcher": "^3.4.0",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@samchon/openapi": "^0.2.1",
"@samchon/openapi": "^0.2.2",
"detect-ts-node": "^1.0.5",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
Expand All @@ -53,7 +53,7 @@
"ws": "^7.5.3"
},
"peerDependencies": {
"@nestia/fetcher": ">=3.3.2",
"@nestia/fetcher": ">=3.4.0",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
Expand Down
7 changes: 5 additions & 2 deletions packages/fetcher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/fetcher",
"version": "3.3.2",
"version": "3.4.0",
"description": "Fetcher library of Nestia SDK",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -41,5 +41,8 @@
"package.json",
"lib",
"src"
]
],
"dependencies": {
"@samchon/openapi": "^0.2.2"
}
}
70 changes: 70 additions & 0 deletions packages/fetcher/src/MigrateFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { IMigrateRoute } from "@samchon/openapi";

import { IConnection } from "./IConnection";
import { PlainFetcher } from "./PlainFetcher";

export namespace MigrateFetcher {
export interface IProps {
route: IMigrateRoute;
connection: IConnection;
arguments: any[];
}

export async function request(props: IProps): Promise<any> {
const length: number =
props.route.parameters.length +
(props.route.query ? 1 : 0) +
(props.route.body ? 1 : 0);
if (props.arguments.length !== length)
throw new Error(
`Error on MigrateFetcher.request(): arguments length is not matched with the route (expected: ${length}, actual: ${props.arguments.length}).`,
);
else if (
props.route.body?.["x-nestia-encrypted"] === true ||
props.route.success?.["x-nestia-encrypted"] === true
)
throw new Error(
`Error on MigrateFetcher.request(): encrypted API is not supported yet.`,
);
return PlainFetcher.fetch(
props.connection,
{
method: props.route.method.toUpperCase() as "POST",
path: getPath(props),
template: props.route.path,
status: null,
request: props.route.body
? {
encrypted: false,
type: props.route.body.type,
}
: null,
response: {
encrypted: false,
type: props.route.success?.type ?? "application/json",
},
},
props.route.body ? props.arguments.at(-1) : undefined,
);
}

function getPath(props: Pick<IProps, "arguments" | "route">): string {
let path: string = props.route.emendedPath;
props.route.parameters.forEach((p, i) => {
path = path.replace(`:${p.key}`, props.arguments[i]);
});
if (props.route.query)
path += getQueryPath(props.arguments[props.route.parameters.length]);
return path;
}

function getQueryPath(query: Record<string, any>): string {
const variables = new URLSearchParams();
for (const [key, value] of Object.entries(query))
if (undefined === value) continue;
else if (Array.isArray(value))
value.forEach((elem: any) => variables.append(key, String(elem)));
else variables.set(key, String(value));
return 0 === variables.size ? "" : `?${variables.toString()}`;
}
}
2 changes: 1 addition & 1 deletion packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"typescript-transform-paths": "^3.4.6"
},
"dependencies": {
"@samchon/openapi": "^0.2.1",
"@samchon/openapi": "^0.2.2",
"commander": "10.0.0",
"inquirer": "8.2.5",
"prettier": "^3.2.5",
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/sdk",
"version": "3.3.2",
"version": "3.4.0",
"description": "Nestia SDK and Swagger generator",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -32,8 +32,8 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "../fetcher/nestia-fetcher-3.3.2.tgz",
"@samchon/openapi": "^0.2.1",
"@nestia/fetcher": "^3.4.0",
"@samchon/openapi": "^0.2.2",
"cli": "^1.0.1",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
Expand All @@ -46,7 +46,7 @@
"typia": "^6.3.1"
},
"peerDependencies": {
"@nestia/fetcher": ">=3.3.2",
"@nestia/fetcher": ">=3.4.0",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
Expand Down
Loading

0 comments on commit 1fa202c

Please sign in to comment.