Skip to content

Commit

Permalink
Complete versioning module
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Oct 17, 2023
1 parent e4f83ff commit fb6d06c
Show file tree
Hide file tree
Showing 78 changed files with 2,686 additions and 926 deletions.
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "2.3.0-dev.20231017",
"version": "2.3.0-dev.20231017-3",
"description": "Super-fast validation decorators of NestJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "^2.3.0-dev.20231017",
"@nestia/fetcher": "^2.3.0-dev.20231017-3",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@nestjs/platform-express": ">=7.0.1",
Expand All @@ -47,7 +47,7 @@
"typia": ">=5.2.2 <6.0.0"
},
"peerDependencies": {
"@nestia/fetcher": ">=2.3.0-dev.20231017",
"@nestia/fetcher": ">=2.3.0-dev.20231017-3",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@nestjs/platform-express": ">=7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/fetcher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/fetcher",
"version": "2.3.0-dev.20231017",
"version": "2.3.0-dev.20231017-3",
"description": "Fetcher library of Nestia SDK",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
40 changes: 28 additions & 12 deletions packages/sdk/assets/config/nestia.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import { INestiaConfig } from "@nestia/sdk";
import { Module } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";

export const NESTIA_CONFIG: INestiaConfig = {
/**
* Accessor of controller classes.
*
* You can specify it within two ways
*
* - Asynchronous function returning `INestApplication` instance
* - Specify the path or directory of controller class files
*/
input: async () => {
// change this to your own module
@Module({
controllers: [],
})
class MyModule {}
const app = await NestFactory.create(MyModule);
return app;
},
// input: "src/controllers",

/**
* Output directory that SDK would be placed in.
*
* If not configured, you can't build the SDK library.
*/
output: "src/api",

/**
* Building `swagger.json` is also possible.
*
Expand All @@ -17,18 +45,6 @@ export const NESTIA_CONFIG: INestiaConfig = {
output: "dist/swagger.json",
},

/**
* List of files or directories containing the NestJS controller classes.
*/
input: "src/controllers",

/**
* Output directory that SDK would be placed in.
*
* If not configured, you can't build the SDK library.
*/
output: "src/api",

/**
* Target directory that SDK distribution files would be placed in.
*
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/sdk",
"version": "2.3.0-dev.20231017",
"version": "2.3.0-dev.20231017-3",
"description": "Nestia SDK and Swagger generator",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,7 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "^2.3.0-dev.20231017",
"@nestia/fetcher": "^2.3.0-dev.20231017-3",
"cli": "^1.0.1",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
Expand All @@ -48,7 +48,7 @@
"typia": ">=5.2.2 <6.0.0"
},
"peerDependencies": {
"@nestia/fetcher": ">=2.3.0-dev.20231017",
"@nestia/fetcher": ">=2.3.0-dev.20231017-3",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/INestiaConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface INestiaConfig {
*
* You can specify it within two ways
*
* - Assign `INestApplication` instance or its factory function
* - Asynchronous function returning `INestApplication` instance
* - Specify the path or directory of controller class files
*/
input:
Expand Down
49 changes: 31 additions & 18 deletions packages/sdk/src/analyses/ControllerAnalyzer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VERSION_NEUTRAL, VersionValue } from "@nestjs/common/interfaces";
import path from "path";
import { HashMap } from "tstl/container/HashMap";
import ts from "typescript";
Expand Down Expand Up @@ -234,30 +235,31 @@ export namespace ControllerAnalyzer {
const input: INestiaConfig.IInput =
config.input as INestiaConfig.IInput;
const pathList: Set<string> = new Set();
const versions: Set<string | null> = new Set();

const versions: Array<string | null> = _Analyze_versions(
func.versions ??
controller.versions ??
(input.versioning?.defaultVersion !== undefined
? Array.isArray(input.versioning?.defaultVersion)
? input.versioning?.defaultVersion
: [input.versioning?.defaultVersion]
: undefined) ??
undefined,
);
for (const cPath of controller.paths)
for (const filePath of func.paths)
pathList.add(PathAnalyzer.join(cPath, filePath));
if (input.versioning?.prefix.length) {
if (input.versioning.defaultVersion !== undefined) {
if (typeof input.versioning.defaultVersion === "symbol")
versions.add(null);
else if (Array.isArray(input.versioning.defaultVersion))
for (const v of input.versioning.defaultVersion)
if (typeof v === "symbol") versions.add(null);
else versions.add(String(v));
else versions.add(String(input.versioning.defaultVersion));
}
for (const cv of input.versioning.prefix) {
versions.add(cv);
for (const fv of func.versions) versions.add(fv);
}
} else versions.add(null);

return [...pathList]
.map((individual) =>
PathAnalyzer.combinate(input.globalPrefix)([...versions])({
PathAnalyzer.combinate(input.globalPrefix)(
[...versions].map((v) =>
v === null
? null
: input.versioning?.prefix?.length
? `${input.versioning.prefix}${v}`
: v,
),
)({
method: func.method,
path: individual,
}),
Expand Down Expand Up @@ -338,4 +340,15 @@ export namespace ControllerAnalyzer {
typeName: tuple.typeName,
};
}

function _Analyze_versions(
value:
| Array<
Exclude<VersionValue, Array<string | typeof VERSION_NEUTRAL>>
>
| undefined,
): Array<string | null> {
if (value === undefined) return [null];
return value.map((v) => (typeof v === "symbol" ? null : v));
}
}
17 changes: 7 additions & 10 deletions packages/sdk/src/analyses/ReflectAnalyzer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Constants from "@nestjs/common/constants";
import { VersionValue } from "@nestjs/common/interfaces";
import { VERSION_NEUTRAL, VersionValue } from "@nestjs/common/interfaces";
import "reflect-metadata";
import { equal } from "tstl/ranges/module";

Expand Down Expand Up @@ -132,19 +132,16 @@ export namespace ReflectAnalyzer {
else return value;
}

function _Get_versions(target: any): Array<string | null> {
function _Get_versions(
target: any,
):
| Array<Exclude<VersionValue, Array<string | typeof VERSION_NEUTRAL>>>
| undefined {
const value: VersionValue | undefined = Reflect.getMetadata(
Constants.VERSION_METADATA,
target,
);
if (value === undefined || typeof value === "symbol") return [null];
else if (Array.isArray(value))
if (value.length === 0) return [null];
else
return value.map((v) =>
typeof value === "symbol" ? null : v,
) as Array<string | null>;
return [String(value)];
return value === undefined || Array.isArray(value) ? value : [value];
}

function _Get_securities(value: any): Record<string, string[]>[] {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/executable/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ async function main() {
else halt(USAGE);
}
main().catch((exp) => {
console.log(exp.message);
console.log(exp);
process.exit(-1);
});
14 changes: 11 additions & 3 deletions packages/sdk/src/structures/IController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ParamCategory } from "./ParamCategory";
import type { VERSION_NEUTRAL, VersionValue } from "@nestjs/common/interfaces";

import type { ParamCategory } from "./ParamCategory";

export interface IController {
file: string;
name: string;
paths: string[];
versions: Array<string | null>;
versions:
| Array<Exclude<VersionValue, Array<string | typeof VERSION_NEUTRAL>>>
| undefined;
functions: IController.IFunction[];
security: Record<string, string[]>[];
swaggerTgas: string[];
Expand All @@ -15,7 +19,11 @@ export namespace IController {
name: string;
method: string;
paths: string[];
versions: Array<string | null>;
versions:
| Array<
Exclude<VersionValue, Array<string | typeof VERSION_NEUTRAL>>
>
| undefined;
encrypted: boolean;
parameters: IParameter[];
status?: number;
Expand Down
18 changes: 18 additions & 0 deletions test/features/app-globalPrefix-version-uri/nestia.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { INestiaConfig } from "@nestia/sdk";

import { Backend } from "./src/Backend";

export const NESTIA_CONFIG: INestiaConfig = {
input: () => new Backend().application.get(),
output: "src/api",
e2e: "src/test",
swagger: {
output: "swagger.json",
security: {
bearer: {
type: "apiKey",
},
},
},
};
export default NESTIA_CONFIG;
26 changes: 26 additions & 0 deletions test/features/app-globalPrefix-version-uri/src/Backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { INestApplication, VersioningType } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { Singleton } from "tstl";

import { ApplicationModule } from "./modules/ApplicationModule";

export class Backend {
public readonly application: Singleton<Promise<INestApplication>> =
new Singleton(async () => {
const app = await NestFactory.create(ApplicationModule);
app.setGlobalPrefix("api/internal");
app.enableVersioning({
type: VersioningType.URI,
prefix: "v",
});
return app;
});

public async open(): Promise<void> {
return (await this.application.get()).listen(37_000);
}

public async close(): Promise<void> {
return (await this.application.get()).close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HttpError } from "@nestia/fetcher";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { IConnection } from "@nestia/fetcher";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { Primitive } from "@nestia/fetcher";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @packageDocumentation
* @module api.functional.api
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
//================================================================
export * as internal from "./internal";
Loading

0 comments on commit fb6d06c

Please sign in to comment.