Skip to content

Commit

Permalink
Merge pull request #995 from samchon/feature/input
Browse files Browse the repository at this point in the history
Fix #994: when `INestiaConfig.input` is not application, but controller path(s).
  • Loading branch information
samchon authored Aug 22, 2024
2 parents 5473c06 + 3a5c4d2 commit 4b75158
Show file tree
Hide file tree
Showing 21 changed files with 117 additions and 89 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"reflect-metadata": "^0.2.2",
"tgrid": "^1.0.3",
"tstl": "^3.0.0",
"typia": "^6.8.0"
"typia": "^6.9.0"
},
"devDependencies": {
"@types/autocannon": "^7.9.0",
Expand Down
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.11.1",
"version": "3.11.2",
"description": "Nestia station",
"scripts": {
"build": "node build/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ts-patch": "^3.2.1",
"typescript": "^5.5.4",
"typescript-transform-paths": "^3.4.7",
"typia": "^6.8.0",
"typia": "^6.9.0",
"uuid": "^10.0.0"
},
"dependencies": {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "3.11.1",
"version": "3.11.2",
"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": "^3.11.1",
"@nestia/fetcher": "^3.11.2",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@samchon/openapi": "^0.4.5",
"@samchon/openapi": "^0.4.6",
"detect-ts-node": "^1.0.5",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
Expand All @@ -49,16 +49,16 @@
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.3",
"tgrid": "^1.0.0",
"typia": "^6.8.0",
"typia": "^6.9.0",
"ws": "^7.5.3"
},
"peerDependencies": {
"@nestia/fetcher": ">=3.11.1",
"@nestia/fetcher": ">=3.11.2",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.3",
"typia": ">=6.8.0 <7.0.0"
"typia": ">=6.9.0 <7.0.0"
},
"devDependencies": {
"@fastify/multipart": "^8.1.0",
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/decorators/internal/load_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import is_ts_node from "detect-ts-node";
import { Creator } from "../../typings/Creator";
import { SourceFinder } from "../../utils/SourceFinder";

/**
* @internal
*/
export const load_controllers = async (
path: string | string[] | { include: string[]; exclude?: string[] },
isTsNode?: boolean,
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"ts-patch": "^3.2.1",
"typescript": "^5.5.3",
"typescript-transform-paths": "^3.4.7",
"typia": "^6.8.0"
"typia": "^6.9.0"
},
"files": [
"lib",
Expand Down
4 changes: 2 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.11.1",
"version": "3.11.2",
"description": "Fetcher library of Nestia SDK",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -43,6 +43,6 @@
"src"
],
"dependencies": {
"@samchon/openapi": "^0.4.5"
"@samchon/openapi": "^0.4.6"
}
}
4 changes: 2 additions & 2 deletions packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@
"typescript-transform-paths": "^3.4.6"
},
"dependencies": {
"@samchon/openapi": "^0.4.5",
"@samchon/openapi": "^0.4.6",
"commander": "10.0.0",
"inquirer": "8.2.5",
"prettier": "^3.2.5",
"tstl": "^3.0.0",
"typescript": "^5.5.4",
"typia": "^6.8.0"
"typia": "^6.9.0"
},
"files": [
"lib",
Expand Down
16 changes: 8 additions & 8 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/sdk",
"version": "3.11.1",
"version": "3.11.2",
"description": "Nestia SDK and Swagger generator",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -32,9 +32,9 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/core": "^3.11.1",
"@nestia/fetcher": "^3.11.1",
"@samchon/openapi": "^0.4.5",
"@nestia/core": "^3.11.2",
"@nestia/fetcher": "^3.11.2",
"@samchon/openapi": "^0.4.6",
"@wrtnio/openai-function-schema": "^0.2.3",
"cli": "^1.0.1",
"get-function-location": "^2.0.0",
Expand All @@ -45,16 +45,16 @@
"tsconfck": "^2.1.2",
"tsconfig-paths": "^4.1.1",
"tstl": "^3.0.0",
"typia": "^6.8.0"
"typia": "^6.9.0"
},
"peerDependencies": {
"@nestia/core": ">=3.11.1",
"@nestia/fetcher": ">=3.11.1",
"@nestia/core": ">=3.11.2",
"@nestia/fetcher": ">=3.11.2",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
"ts-node": ">=10.6.0",
"typia": ">=6.8.0 <7.0.0"
"typia": ">=6.9.0 <7.0.0"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
Expand Down
82 changes: 68 additions & 14 deletions packages/sdk/src/analyses/ConfigAnalyzer.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
/// <reference path="../typings/get-function-location.d.ts" />
import { DynamicModule } from "@nestia/core";
import { INestApplication, VersioningType } from "@nestjs/common";
import { MODULE_PATH } from "@nestjs/common/constants";
import { NestContainer, NestFactory } from "@nestjs/core";
import { NestContainer } from "@nestjs/core";
import { Module } from "@nestjs/core/injector/module";
import fs from "fs";
import getFunctionLocation from "get-function-location";
import { HashMap } from "tstl";
import path from "path";
import { HashMap, Pair, Singleton } from "tstl";

import { INestiaConfig } from "../INestiaConfig";
import { SdkGenerator } from "../generates/SdkGenerator";
import { INestiaSdkInput } from "../structures/INestiaSdkInput";
import { ArrayUtil } from "../utils/ArrayUtil";
import { MapUtil } from "../utils/MapUtil";
import { SourceFinder } from "../utils/SourceFinder";

export namespace ConfigAnalyzer {
export const input = async (
config: INestiaConfig,
): Promise<INestiaSdkInput> => {
return MapUtil.take(memory, config, async () => {
const app: INestApplication =
typeof config.input === "function"
? await config.input()
: await NestFactory.create(
await DynamicModule.mount(config.input, {}, true as any),
{
logger: false,
},
);
return analyze_application(app);
if (typeof config.input === "function")
return analyze_application(await config.input());

const sources: string[] = await SourceFinder.find({
include: Array.isArray(config.input)
? config.input
: typeof config.input === "object"
? config.input.include
: [config.input],
exclude:
typeof config.input === "object" && !Array.isArray(config.input)
? config.input.exclude ?? []
: [],
filter: filter(config),
});
const controllers: INestiaSdkInput.IController[] = [];
for (const file of sources) {
const external: any[] = await import(file);
for (const key in external) {
const instance: Function = external[key];
if (Reflect.getMetadata("path", instance) !== undefined)
controllers.push({
class: instance,
location: file,
prefixes: [],
});
}
}
return {
controllers,
};
});
};

Expand Down Expand Up @@ -66,7 +91,6 @@ export namespace ConfigAnalyzer {

const versioning = (app as any).config?.versioningOptions;
return {
application: app,
controllers,
globalPrefix:
typeof (app as any).config?.globalPrefix === "string"
Expand Down Expand Up @@ -99,3 +123,33 @@ const normalize_file = (str: string) =>
? 7
: 0,
);

const filter =
(config: INestiaConfig) =>
async (location: string): Promise<boolean> =>
location.endsWith(".ts") &&
!location.endsWith(".d.ts") &&
(config.output === undefined ||
(location.indexOf(path.join(config.output, "functional")) === -1 &&
(await (
await bundler.get(config.output)
)(location))) === false);

const bundler = new Singleton(async (output: string) => {
const assets: string[] = await fs.promises.readdir(SdkGenerator.BUNDLE_PATH);
const tuples: Pair<string, boolean>[] = await ArrayUtil.asyncMap(
assets,
async (file) => {
const relative: string = path.join(output, file);
const location: string = path.join(SdkGenerator.BUNDLE_PATH, file);
const stats: fs.Stats = await fs.promises.stat(location);
return new Pair(relative, stats.isDirectory());
},
);
return async (file: string): Promise<boolean> => {
for (const it of tuples)
if (it.second === false && file === it.first) return true;
else if (it.second === true && file.indexOf(it.first) === 0) return true;
return false;
};
});
7 changes: 1 addition & 6 deletions packages/sdk/src/structures/INestiaSdkInput.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {
INestApplication,
RouteInfo,
VersionValue,
} from "@nestjs/common/interfaces";
import { RouteInfo, VersionValue } from "@nestjs/common/interfaces";

export interface INestiaSdkInput {
application: INestApplication;
controllers: INestiaSdkInput.IController[];
globalPrefix?: {
prefix: string;
Expand Down
19 changes: 5 additions & 14 deletions test/features/all/src/api/functional/health/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,11 @@ import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher";
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function get(connection: IConnection): Promise<void> {
return PlainFetcher.fetch(
{
...connection,
headers: {
...connection.headers,
"Content-Type": "application/json",
},
},
{
...get.METADATA,
template: get.METADATA.path,
path: get.path(),
},
);
return PlainFetcher.fetch(connection, {
...get.METADATA,
template: get.METADATA.path,
path: get.path(),
});
}
export namespace get {
export const METADATA = {
Expand Down
19 changes: 5 additions & 14 deletions test/features/all/src/api/functional/performance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,11 @@ import type { IPerformance } from "../../structures/IPerformance";
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function get(connection: IConnection): Promise<get.Output> {
return PlainFetcher.fetch(
{
...connection,
headers: {
...connection.headers,
"Content-Type": "application/json",
},
},
{
...get.METADATA,
template: get.METADATA.path,
path: get.path(),
},
);
return PlainFetcher.fetch(connection, {
...get.METADATA,
template: get.METADATA.path,
path: get.path(),
});
}
export namespace get {
export type Output = Primitive<IPerformance>;
Expand Down
2 changes: 1 addition & 1 deletion test/features/all/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
],
"info": {
"version": "3.11.0",
"version": "3.11.2",
"title": "@samchon/nestia-test",
"description": "Test program of Nestia",
"license": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
},
"dependencies": {
"@nestia/fetcher": "^3.11.0",
"typia": "^6.8.0"
"typia": "^6.9.0"
}
}
2 changes: 1 addition & 1 deletion test/features/distribute-assert/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
},
"dependencies": {
"@nestia/fetcher": "^3.11.0",
"typia": "^6.8.0"
"typia": "^6.9.0"
}
}
2 changes: 1 addition & 1 deletion test/features/distribute-json/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
},
"dependencies": {
"@nestia/fetcher": "^3.11.0",
"typia": "^6.8.0"
"typia": "^6.9.0"
}
}
2 changes: 1 addition & 1 deletion test/features/distribute/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
},
"dependencies": {
"@nestia/fetcher": "^3.11.0",
"typia": "^6.8.0"
"typia": "^6.9.0"
}
}
Loading

0 comments on commit 4b75158

Please sign in to comment.