Skip to content

Commit

Permalink
Fix compiler part to support @TypedFormData()'s options
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Nov 27, 2024
1 parent 4b19546 commit 38316ad
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 723 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": "4.0.0-dev.20241027",
"version": "4.0.0-dev.20241027-2",
"description": "Nestia station",
"scripts": {
"build": "node build/index.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@samchon/openapi": "^2.0.0-dev.20241127-2",
"@types/multer": "^1.4.11",
"detect-ts-node": "^1.0.5",
"fastify-multer": "^2.0.3",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
"multer": "1.4.5-lts.1",
Expand All @@ -49,7 +51,7 @@
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.3",
"tgrid": "^1.0.0",
"typia": "^7.0.0-dev.20241027-2",
"typia": ">=7.0.0-dev.20241027-2 <8.0.0",
"ws": "^7.5.3"
},
"peerDependencies": {
Expand All @@ -66,16 +68,14 @@
"@types/express": "^4.17.15",
"@types/glob": "^7.2.0",
"@types/inquirer": "^9.0.3",
"@types/multer": "^1.4.11",
"@types/ts-expose-internals": "npm:[email protected]",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"eslint-plugin-deprecation": "^1.4.1",
"fastify": "^4.25.2",
"fastify-multer": "^2.0.3",
"fastify": "^4.28.1",
"git-last-commit": "^1.0.1",
"inquirer": "^8.2.5",
"rimraf": "^3.0.2",
Expand Down
40 changes: 28 additions & 12 deletions packages/core/src/decorators/TypedFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import type express from "express";
import type { FastifyReply, FastifyRequest } from "fastify";
import fastifyMulter from "fastify-multer";
import type * as FastifyMulter from "fastify-multer/lib/interfaces";
import "fastify-multer/typings/fastify/index.d.ts";
import expressMulter from "multer";
import ExpressMulter from "multer";

import type { IRequestFormDataProps } from "../options/IRequestFormDataProps";
import { Singleton } from "../utils/Singleton";
Expand Down Expand Up @@ -71,10 +70,18 @@ export namespace TypedFormData {
*
* Much easier and type safer than `@nest.UploadFile()` decorator.
*
* @param props Automatically filled by transformer
* @param options Options for the `multer` or `fastify-multer`
*/
export function Body(
options?: ExpressMulter.Options | FastifyMulter.Options,
): ParameterDecorator;

/**
* @internal
*/
export function Body<T extends object>(
props?: IRequestFormDataProps<T>,
options: ExpressMulter.Options | FastifyMulter.Options | undefined,
props?: IRequestFormDataProps<T> | undefined,
): ParameterDecorator {
if (typeof File === "undefined")
throw new Error(
Expand All @@ -83,7 +90,9 @@ export namespace TypedFormData {
const checker = validate_request_form_data(props);
const predicator = (type: "express" | "fastify") =>
new Singleton(() =>
type === "express" ? decodeExpress(props!) : decodeFastify(props!),
type === "express"
? decodeExpress(options as ExpressMulter.Options | undefined, props!)
: decodeFastify(options as FastifyMulter.Options | undefined, props!),
);
return createParamDecorator(async function TypedFormDataBody(
_unknown: any,
Expand Down Expand Up @@ -113,8 +122,11 @@ export namespace TypedFormData {
/**
* @internal
*/
const decodeExpress = <T>(props: IRequestFormDataProps<T>) => {
const upload = expressMulter(props.options as expressMulter.Options).fields(
const decodeExpress = <T>(
options: ExpressMulter.Options | undefined,
props: IRequestFormDataProps<T>,
) => {
const upload = ExpressMulter(options).fields(
props!.files.map((file) => ({
name: file.name,
...(file.limit === 1 ? { maxCount: 1 } : {}),
Expand Down Expand Up @@ -146,8 +158,11 @@ const decodeExpress = <T>(props: IRequestFormDataProps<T>) => {
/**
* @internal
*/
const decodeFastify = <T>(props: IRequestFormDataProps<T>) => {
const fastifyInstance = fastifyMulter(props.options as FastifyMulter.Options);
const decodeFastify = <T>(
options: FastifyMulter.Options | undefined,
props: IRequestFormDataProps<T>,
) => {
const fastifyInstance = fastifyMulter(options);
const upload = fastifyInstance.fields(
props!.files.map((file) => ({
name: file.name,
Expand All @@ -166,8 +181,8 @@ const decodeFastify = <T>(props: IRequestFormDataProps<T>) => {
response: FastifyReply;
}): Promise<FormData> => {
if (
socket.request.files === undefined ||
typeof socket.request.files !== "function"
(socket.request as any).files === undefined ||
(typeof socket.request as any).files !== "function"
)
throw new InternalServerErrorException(
"Have not configured the `fastify-multer` plugin yet. Inquiry to the backend developer.",
Expand All @@ -180,7 +195,8 @@ const decodeFastify = <T>(props: IRequestFormDataProps<T>) => {
if (Array.isArray(value))
for (const elem of value) data.append(key, String(elem));
else data.append(key, String(value));
if (socket.request.files) parseFiles(data)(socket.request.files);
if ((socket.request as any).files)
parseFiles(data)((socket.request as any).files);
return data;
};
};
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/options/IRequestFormDataProps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type fastifyMulter from "fastify-multer/lib/interfaces";
import type expressMulter from "multer";
import { IValidation } from "typia";

export interface IRequestFormDataProps<T> {
Expand All @@ -8,7 +6,6 @@ export interface IRequestFormDataProps<T> {
| IRequestFormDataProps.IAssert<T>
| IRequestFormDataProps.IIs<T>
| IRequestFormDataProps.IValidate<T>;
options?: expressMulter.Options | fastifyMulter.Options;
}
export namespace IRequestFormDataProps {
export interface IAssert<T> {
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/transformers/ParameterDecoratorTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,14 @@ const FUNCTORS: Record<string, Programmer> = {
? props.arguments
: [TypedQueryBodyProgrammer.generate(props)],
"TypedFormData.Body": (props) =>
props.arguments.length
? props.arguments
: [TypedFormDataBodyProgrammer.generate(props)],
props.arguments.length === 0
? [
ts.factory.createIdentifier("undefined"),
TypedFormDataBodyProgrammer.generate(props),
]
: props.arguments.length === 1
? [props.arguments[0], TypedFormDataBodyProgrammer.generate(props)]
: props.arguments,
PlainBody: (props) =>
props.arguments.length
? props.arguments
Expand Down
Loading

0 comments on commit 38316ad

Please sign in to comment.