-
-
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.
- `@TypedQuery.Get()` - `@TypedQuery.Post()` - `@TypedQuery.Put()` - `@TypedQuery.Patch()` - `@TypedQuery.Delete()`
- Loading branch information
Showing
21 changed files
with
718 additions
and
87 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
104 changes: 104 additions & 0 deletions
104
packages/core/src/decorators/internal/get_path_and_querify.ts
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,104 @@ | ||
import { InternalServerErrorException } from "@nestjs/common"; | ||
|
||
import typia, { IValidation, TypeGuardError } from "typia"; | ||
|
||
import { IResponseBodyQuerifier } from "../../options/IResponseBodyQuerifier"; | ||
import { NoTransformConfigureError } from "./NoTransformConfigureError"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const get_path_and_querify = | ||
(method: string) => | ||
( | ||
...args: any[] | ||
): [string | string[] | undefined, (input: any) => URLSearchParams] => { | ||
const path: string | string[] | null | undefined = | ||
args[0] === undefined || | ||
typeof args[0] === "string" || | ||
Array.isArray(args[0]) | ||
? args[0] | ||
: null; | ||
const functor: IResponseBodyQuerifier<any> | undefined = | ||
path === null ? args[0] : args[1]; | ||
return [path ?? undefined, take(method)(functor)]; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const take = | ||
(method: string) => | ||
<T>(functor?: IResponseBodyQuerifier<T> | null) => { | ||
if (functor === undefined) throw NoTransformConfigureError(method); | ||
else if (functor === null) return querify; | ||
else if (functor.type === "stringify") return functor.stringify; | ||
else if (functor.type === "assert") return assert(functor.assert); | ||
else if (functor.type === "is") return is(functor.is); | ||
else if (functor.type === "validate") return validate(functor.validate); | ||
throw new Error( | ||
`Error on nestia.core.${method}(): invalid typed stringify function.`, | ||
); | ||
}; | ||
|
||
const querify = (input: Record<string, any>): URLSearchParams => { | ||
const output: URLSearchParams = new URLSearchParams(); | ||
for (const [key, value] of Object.entries(input)) | ||
if (key === undefined) continue; | ||
else if (Array.isArray(value)) | ||
for (const elem of value) output.append(key, String(elem)); | ||
else output.append(key, String(value)); | ||
return output; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const assert = | ||
<T>(closure: (data: T) => URLSearchParams) => | ||
(data: T) => { | ||
try { | ||
return closure(data); | ||
} catch (exp) { | ||
if (typia.is<TypeGuardError>(exp)) | ||
throw new InternalServerErrorException({ | ||
path: exp.path, | ||
reason: exp.message, | ||
expected: exp.expected, | ||
value: exp.value, | ||
message: MESSAGE, | ||
}); | ||
throw exp; | ||
} | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const is = | ||
<T>(closure: (data: T) => URLSearchParams | null) => | ||
(data: T) => { | ||
const result: URLSearchParams | null = closure(data); | ||
if (result === null) throw new InternalServerErrorException(MESSAGE); | ||
return result; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const validate = | ||
<T>(closure: (data: T) => IValidation<URLSearchParams>) => | ||
(data: T) => { | ||
const result: IValidation<URLSearchParams> = closure(data); | ||
if (result.success === false) | ||
throw new InternalServerErrorException({ | ||
errors: result.errors, | ||
message: MESSAGE, | ||
}); | ||
return result.data; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const MESSAGE = "Response body data is not following the promised type."; |
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,25 @@ | ||
import { IValidation } from "typia"; | ||
|
||
export type IResponseBodyQuerifier<T> = | ||
| IResponseBodyquerifier.IStringify<T> | ||
| IResponseBodyquerifier.IIs<T> | ||
| IResponseBodyquerifier.IAssert<T> | ||
| IResponseBodyquerifier.IValidate<T>; | ||
export namespace IResponseBodyquerifier { | ||
export interface IStringify<T> { | ||
type: "stringify"; | ||
stringify: (input: T) => URLSearchParams; | ||
} | ||
export interface IIs<T> { | ||
type: "is"; | ||
is: (input: T) => URLSearchParams | null; | ||
} | ||
export interface IAssert<T> { | ||
type: "assert"; | ||
assert: (input: T) => URLSearchParams; | ||
} | ||
export interface IValidate<T> { | ||
type: "validate"; | ||
validate: (input: T) => IValidation<URLSearchParams>; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/core/src/programmers/TypedQueryRouteProgrammer.ts
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,55 @@ | ||
import ts from "typescript"; | ||
|
||
import { IProject } from "typia/lib/transformers/IProject"; | ||
|
||
import { INestiaTransformProject } from "../options/INestiaTransformProject"; | ||
import { HttpAssertQuerifyProgrammer } from "./http/HttpAssertQuerifyProgrammer"; | ||
import { HttpIsQuerifyProgrammer } from "./http/HttpIsQuerifyProgrammer"; | ||
import { HttpQuerifyProgrammer } from "./http/HttpQuerifyProgrammer"; | ||
import { HttpValidateQuerifyProgrammer } from "./http/HttpValidateQuerifyProgrammer"; | ||
|
||
export namespace TypedQueryRouteProgrammer { | ||
export const generate = | ||
(project: INestiaTransformProject) => | ||
(modulo: ts.LeftHandSideExpression) => | ||
(type: ts.Type): ts.Expression => { | ||
// GENERATE STRINGIFY PLAN | ||
const parameter = ( | ||
key: string, | ||
programmer: ( | ||
project: IProject, | ||
) => ( | ||
modulo: ts.LeftHandSideExpression, | ||
) => (type: ts.Type) => ts.ArrowFunction, | ||
) => | ||
ts.factory.createObjectLiteralExpression([ | ||
ts.factory.createPropertyAssignment( | ||
ts.factory.createIdentifier("type"), | ||
ts.factory.createStringLiteral(key), | ||
), | ||
ts.factory.createPropertyAssignment( | ||
ts.factory.createIdentifier(key), | ||
programmer({ | ||
...project, | ||
options: {}, // use default option | ||
})(modulo)(type), | ||
), | ||
]); | ||
|
||
// RETURNS | ||
if (project.options.stringify === "is") | ||
return parameter("is", HttpIsQuerifyProgrammer.write); | ||
else if (project.options.stringify === "validate") | ||
return parameter( | ||
"validate", | ||
HttpValidateQuerifyProgrammer.write, | ||
); | ||
else if (project.options.stringify === "stringify") | ||
return parameter("stringify", HttpQuerifyProgrammer.write); | ||
else if (project.options.stringify === null) | ||
return ts.factory.createNull(); | ||
|
||
// ASSERT IS DEFAULT | ||
return parameter("assert", HttpAssertQuerifyProgrammer.write); | ||
}; | ||
} |
Oops, something went wrong.