-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
```typescript import core from "@nestia/core"; import { Controller } from "@nestjs/common"; import typia, { tags } from "typia"; import { v4 } from "uuid"; import { IBbsArticle } from "@api/lib/structures/IBbsArticle"; @controller("bbs/articles") export class BbsArticlesController { /** * Create an article. * * @param request Request object from express. Must be disappeared in SDK * @param input Content to store * @returns Newly archived article * * @author Samchon * @warning This is an fake API */ @core.SwaggerExample.Response(typia.random<IBbsArticle>()) @core.TypedRoute.Post() public async create( @core.SwaggerExample.Parameter(typia.random<IBbsArticle.ICreate>()) @core.SwaggerExample.Parameter("x", typia.random<IBbsArticle.ICreate>()) @core.SwaggerExample.Parameter("y", typia.random<IBbsArticle.ICreate>()) @core.SwaggerExample.Parameter("z", typia.random<IBbsArticle.ICreate>()) @core.TypedBody() input: IBbsArticle.ICreate, ): Promise<IBbsArticle> { const output: IBbsArticle = { ...typia.random<IBbsArticle>(), ...input, }; return output; } @core.SwaggerExample.Response(typia.random<IBbsArticle>()) @core.SwaggerExample.Response("a", typia.random<IBbsArticle>()) @core.SwaggerExample.Response("b", typia.random<IBbsArticle>()) @core.TypedRoute.Put(":id") public async update( @core.SwaggerExample.Parameter(v4()) @core.TypedParam("id") id: string & tags.Format<"uuid">, @core.SwaggerExample.Parameter(typia.random<IBbsArticle.IUpdate>()) @core.TypedBody() input: IBbsArticle.IUpdate, ): Promise<IBbsArticle> { return { ...typia.random<IBbsArticle>(), ...input, id, }; } } ```
- Loading branch information
Showing
30 changed files
with
1,013 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
export namespace SwaggerExample { | ||
export function Response<T>(value: T): MethodDecorator; | ||
export function Response<T>(key: string, value: T): MethodDecorator; | ||
export function Response(...args: any[]): MethodDecorator { | ||
return function SwaggerExampleResponse( | ||
_target: Object, | ||
_propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<any>, | ||
): TypedPropertyDescriptor<any> { | ||
emplaceValue(emplaceOfResponse(descriptor))(args); | ||
return descriptor; | ||
}; | ||
} | ||
|
||
export function Parameter<T>(value: T): ParameterDecorator; | ||
export function Parameter<T>(key: string, value: T): ParameterDecorator; | ||
export function Parameter(...args: any[]): ParameterDecorator { | ||
return function SwaggerExampleParameter( | ||
target: Object, | ||
propertyKey: string | symbol | undefined, | ||
index: number, | ||
): void { | ||
emplaceValue(emplaceOfParameter(target, propertyKey ?? "", index))(args); | ||
}; | ||
} | ||
|
||
export interface IData<T> { | ||
examples?: Record<string, T>; | ||
example?: T; | ||
index?: number; | ||
} | ||
} | ||
|
||
const emplaceValue = | ||
<T>(data: SwaggerExample.IData<T>) => | ||
(args: any[]) => { | ||
if (args.length === 1) data.example = args[0]; | ||
else { | ||
const key: string = args[0]; | ||
const value: T = args[1]; | ||
data.examples ??= {}; | ||
data.examples[key] = value; | ||
} | ||
}; | ||
|
||
const emplaceOfResponse = <T>( | ||
descriptor: TypedPropertyDescriptor<any>, | ||
): SwaggerExample.IData<T> => { | ||
const oldbie: SwaggerExample.IData<T> | undefined = Reflect.getMetadata( | ||
"nestia/SwaggerExample/Response", | ||
descriptor.value, | ||
); | ||
if (oldbie !== undefined) return oldbie; | ||
const newbie: SwaggerExample.IData<T> = {}; | ||
Reflect.defineMetadata( | ||
"nestia/SwaggerExample/Response", | ||
newbie, | ||
descriptor.value, | ||
); | ||
return newbie; | ||
}; | ||
|
||
const emplaceOfParameter = ( | ||
target: Object, | ||
propertyKey: string | symbol, | ||
index: number, | ||
): SwaggerExample.IData<any> => { | ||
const array: SwaggerExample.IData<any>[] = emplaceArrayOfParameters( | ||
target, | ||
propertyKey, | ||
); | ||
const oldibe: SwaggerExample.IData<any> | undefined = array.find( | ||
(e) => e.index === index, | ||
); | ||
if (oldibe !== undefined) return oldibe; | ||
|
||
const data: SwaggerExample.IData<any> = { index }; | ||
array.push(data); | ||
return data; | ||
}; | ||
|
||
const emplaceArrayOfParameters = ( | ||
target: Object, | ||
propertyKey: string | symbol, | ||
): SwaggerExample.IData<any>[] => { | ||
const array: SwaggerExample.IData<any>[] | undefined = Reflect.getMetadata( | ||
"nestia/SwaggerExample/Parameters", | ||
target, | ||
propertyKey, | ||
); | ||
if (array !== undefined) return array; | ||
const newbie: SwaggerExample.IData<any>[] = []; | ||
Reflect.defineMetadata( | ||
"nestia/SwaggerExample/Parameters", | ||
newbie, | ||
target, | ||
propertyKey, | ||
); | ||
return newbie; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
swagger: { | ||
output: "swagger.json", | ||
beautify: true, | ||
security: { | ||
bearer: { | ||
type: "apiKey", | ||
}, | ||
}, | ||
}, | ||
}; | ||
export default NESTIA_CONFIG; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { HttpError } from "@nestia/fetcher"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { IConnection } from "@nestia/fetcher"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { Primitive } from "@nestia/fetcher"; |
Oops, something went wrong.