Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New decorator @HumanRoute(). #1164

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/core/src/decorators/HumanRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SwaggerCustomizer } from "./SwaggerCustomizer";

/**
* Human only API marking.
*
* This decorator marks the API for human only, so that LLM function calling
* schema composer (of [`@samchon/openapi`](https://github.com/samchon/openapi))
* excludes the API.
*
* In other words, if you adjust the `@HumanRoute()` decorator to the API,
* the API never participates in the LLM function calling. When calling the
* {@link HttpLlm.application} function, matched {@link IHttpLlmFunction} data
* never be composed.
*
* @returns Method decorator
* @author Jeongho Nam - https://github.com/samchon
*/
export function HumanRoute(): MethodDecorator {
return SwaggerCustomizer((props) => {
props.route["x-samchon-human"] = true;
});
}
1 change: 1 addition & 0 deletions packages/core/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./decorators/DynamicModule";
export * from "./utils/ExceptionManager";

export * from "./decorators/HumanRoute";
export * from "./decorators/TypedRoute";
export * from "./decorators/TypedBody";
export * from "./decorators/TypedQuery";
Expand Down
16 changes: 16 additions & 0 deletions test/features/route-human/nestia.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { INestiaConfig } from "@nestia/sdk";

export const NESTIA_CONFIG: INestiaConfig = {
input: ["src/controllers"],
output: "src/api",
e2e: "src/test",
swagger: {
output: "swagger.json",
security: {
bearer: {
type: "apiKey",
},
},
},
};
export default NESTIA_CONFIG;
27 changes: 27 additions & 0 deletions test/features/route-human/src/Backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import core from "@nestia/core";
import { INestApplication } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";

export class Backend {
private application_?: INestApplication;

public async open(): Promise<void> {
this.application_ = await NestFactory.create(
await core.EncryptedModule.dynamic(__dirname + "/controllers", {
key: "A".repeat(32),
iv: "B".repeat(16),
}),
{ logger: false },
);
await this.application_.listen(37_000);
}

public async close(): Promise<void> {
if (this.application_ === undefined) return;

const app = this.application_;
await app.close();

delete this.application_;
}
}
1 change: 1 addition & 0 deletions test/features/route-human/src/api/HttpError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HttpError } from "@nestia/fetcher";
1 change: 1 addition & 0 deletions test/features/route-human/src/api/IConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { IConnection } from "@nestia/fetcher";
1 change: 1 addition & 0 deletions test/features/route-human/src/api/Primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { Primitive } from "typia";
1 change: 1 addition & 0 deletions test/features/route-human/src/api/Resolved.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { Resolved } from "typia";
35 changes: 35 additions & 0 deletions test/features/route-human/src/api/functional/health/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @packageDocumentation
* @module api.functional.health
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
//================================================================
import type { IConnection } from "@nestia/fetcher";
import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher";

/**
* @controller HealthController.get
* @path GET /health
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function get(connection: IConnection): Promise<void> {
return PlainFetcher.fetch(connection, {
...get.METADATA,
template: get.METADATA.path,
path: get.path(),
});
}
export namespace get {
export const METADATA = {
method: "GET",
path: "/health",
request: null,
response: {
type: "application/json",
encrypted: false,
},
status: 200,
} as const;

export const path = () => "/health";
}
9 changes: 9 additions & 0 deletions test/features/route-human/src/api/functional/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @packageDocumentation
* @module api.functional
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
//================================================================
export * as health from "./health";
export * as performance from "./performance";
export * as route from "./route";
40 changes: 40 additions & 0 deletions test/features/route-human/src/api/functional/performance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @packageDocumentation
* @module api.functional.performance
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
//================================================================
import type { IConnection } from "@nestia/fetcher";
import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher";
import type { Primitive } from "typia";

import type { IPerformance } from "../../structures/IPerformance";

/**
* @controller PerformanceController.get
* @path GET /performance
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function get(connection: IConnection): Promise<get.Output> {
return PlainFetcher.fetch(connection, {
...get.METADATA,
template: get.METADATA.path,
path: get.path(),
});
}
export namespace get {
export type Output = Primitive<IPerformance>;

export const METADATA = {
method: "GET",
path: "/performance",
request: null,
response: {
type: "application/json",
encrypted: false,
},
status: 200,
} as const;

export const path = () => "/performance";
}
40 changes: 40 additions & 0 deletions test/features/route-human/src/api/functional/route/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @packageDocumentation
* @module api.functional.route
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
//================================================================
import type { IConnection } from "@nestia/fetcher";
import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher";
import type { Primitive } from "typia";

import type { IBbsArticle } from "../../structures/IBbsArticle";

/**
* @controller TypedRouteController.random
* @path GET /route/random
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function random(connection: IConnection): Promise<random.Output> {
return PlainFetcher.fetch(connection, {
...random.METADATA,
template: random.METADATA.path,
path: random.path(),
});
}
export namespace random {
export type Output = Primitive<IBbsArticle>;

export const METADATA = {
method: "GET",
path: "/route/random",
request: null,
response: {
type: "application/json",
encrypted: false,
},
status: 200,
} as const;

export const path = () => "/route/random";
}
4 changes: 4 additions & 0 deletions test/features/route-human/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as api from "./module";

export * from "./module";
export default api;
6 changes: 6 additions & 0 deletions test/features/route-human/src/api/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type * from "./IConnection";
export type * from "./Primitive";
export type * from "./Resolved";
export * from "./HttpError";

export * as functional from "./functional";
40 changes: 40 additions & 0 deletions test/features/route-human/src/api/structures/IBbsArticle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export interface IBbsArticle {
/**
* @format uuid
*/
id: string;

/**
* @minLength 3
* @maxLength 50
*/
title: string;

body: string;

files: IAttachmentFile[];

/**
* @format date-time
*/
created_at: string;
}

export interface IAttachmentFile {
/**
* @minLengt 1
* @maxLength 255
*/
name: string | null;

/**
* @minLength 1
* @maxLength 8
*/
extension: string | null;

/**
* @format uri
*/
url: string;
}
10 changes: 10 additions & 0 deletions test/features/route-human/src/api/structures/IPerformance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Performance info.
*
* @author Samchon
*/
export interface IPerformance {
cpu: NodeJS.CpuUsage;
memory: NodeJS.MemoryUsage;
resource: NodeJS.ResourceUsage;
}
81 changes: 81 additions & 0 deletions test/features/route-human/src/api/structures/ISystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* System Information.
*
* @author Jeongho Nam
*/
export interface ISystem {
/**
* Random Unique ID.
*/
uid: number;

/**
* `process.argv`
*/
arguments: string[];

/**
* Git commit info.
*/
commit: ISystem.ICommit;

/**
* `package.json`
*/
package: ISystem.IPackage;

/**
* Creation time of this server.
*/
created_at: string;
}

export namespace ISystem {
/**
* Git commit info.
*/
export interface ICommit {
shortHash: string;
branch: string;
hash: string;
subject: string;
sanitizedSubject: string;
body: string;
author: ICommit.IUser;
committer: ICommit.IUser;
authored_at: string;
commited_at: string;
notes?: string;
tags: string[];
}
export namespace ICommit {
/**
* Git user account info.
*/
export interface IUser {
name: string;
email: string;
}
}

/**
* NPM package info.
*/
export interface IPackage {
name: string;
version: string;
description: string;
main?: string;
typings?: string;
scripts: Record<string, string>;
repository: { type: "git"; url: string };
author: string;
license: string;
bugs: { url: string };
homepage: string;
devDependencies?: Record<string, string>;
dependencies: Record<string, string>;
publishConfig?: { registry: string };
files?: string[];
}
}
9 changes: 9 additions & 0 deletions test/features/route-human/src/controllers/HealthController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import core from "@nestia/core";
import { Controller } from "@nestjs/common";

@Controller("health")
export class HealthController {
@core.HumanRoute()
@core.TypedRoute.Get()
public get(): void {}
}
17 changes: 17 additions & 0 deletions test/features/route-human/src/controllers/PerformanceController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import core from "@nestia/core";
import { Controller } from "@nestjs/common";

import { IPerformance } from "@api/lib/structures/IPerformance";

@Controller("performance")
export class PerformanceController {
@core.HumanRoute()
@core.TypedRoute.Get()
public async get(): Promise<IPerformance> {
return {
cpu: process.cpuUsage(),
memory: process.memoryUsage(),
resource: process.resourceUsage(),
};
}
}
Loading
Loading