Skip to content

Commit

Permalink
fix: export parser
Browse files Browse the repository at this point in the history
  • Loading branch information
richenlin authored and linyyyang committed Jun 25, 2024
1 parent 632a901 commit e001b9a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "./server/terminus";
export * from "./router/router";
export * from "./router/mapping";
export * from "./router/params";
export * from "./router/payload";
16 changes: 8 additions & 8 deletions src/router/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* @Usage:
* @Author: richen
* @Date: 2023-12-09 12:02:29
* @LastEditTime: 2023-12-09 23:12:15
* @LastEditTime: 2024-01-14 15:52:57
* @License: BSD (3-Clause)
* @Copyright (c): <richenlin(at)gmail.com>
*/

import { KoattyContext } from "koatty_core";
import { injectParam } from "./inject";
import { PayloadOptions, bodyParser, queryParser } from "./payload";
import { PayloadOptions, BodyParser, QueryParser } from "./payload";

/**
* Get request header.
Expand Down Expand Up @@ -71,7 +71,7 @@ export function Get(name?: string): ParameterDecorator {
*/
export function Post(name?: string): ParameterDecorator {
return injectParam((ctx: KoattyContext, opt?: PayloadOptions) => {
return bodyParser(ctx, opt).then((body: {
return BodyParser(ctx, opt).then((body: {
post: Object
}) => {
const params: any = body.post ? body.post : body;
Expand All @@ -92,7 +92,7 @@ export function Post(name?: string): ParameterDecorator {
*/
export function File(name?: string): ParameterDecorator {
return injectParam((ctx: KoattyContext, opt?: PayloadOptions) => {
return bodyParser(ctx, opt).then((body: {
return BodyParser(ctx, opt).then((body: {
file: Object
}) => {
const params: any = body.file ?? {};
Expand All @@ -113,7 +113,7 @@ export function File(name?: string): ParameterDecorator {
*/
export function RequestBody(): ParameterDecorator {
return injectParam((ctx: KoattyContext, opt?: PayloadOptions) => {
return bodyParser(ctx, opt);
return BodyParser(ctx, opt);
}, "RequestBody");
}

Expand All @@ -133,10 +133,10 @@ export const Body = RequestBody;
*/
export function RequestParam(name?: string): ParameterDecorator {
return injectParam((ctx: KoattyContext, opt?: PayloadOptions) => {
return bodyParser(ctx, opt).then((body: {
return BodyParser(ctx, opt).then((body: {
post: Object
}) => {
const queryParams: any = queryParser(ctx, opt) ?? {};
const queryParams: any = QueryParser(ctx, opt) ?? {};
const postParams: any = (body.post ? body.post : body) ?? {};
if (name !== undefined) {
return postParams[name] === undefined ? queryParams[name] : postParams[name];
Expand All @@ -151,4 +151,4 @@ export function RequestParam(name?: string): ParameterDecorator {
* @param {*}
* @return {*}
*/
export const Param = RequestParam;
export const Param = RequestParam;
8 changes: 4 additions & 4 deletions src/router/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Usage:
* @Author: richen
* @Date: 2023-12-09 12:30:20
* @LastEditTime: 2023-12-14 21:29:46
* @LastEditTime: 2024-01-14 15:55:24
* @License: BSD (3-Clause)
* @Copyright (c): <richenlin(at)gmail.com>
*/
Expand Down Expand Up @@ -62,13 +62,13 @@ const defaultOptions: PayloadOptions = {
* @param {PayloadOptions} options
* @return {*}
*/
export async function bodyParser(ctx: KoattyContext, options?: PayloadOptions): Promise<any> {
options = { ...defaultOptions, ...options };
export async function BodyParser(ctx: KoattyContext, options?: PayloadOptions): Promise<any> {
let body = ctx.getMetaData("_body")[0];
if (!Helper.isEmpty(body)) {
return body;
}
try {
options = { ...defaultOptions, ...options };
const res = await parseBody(ctx, options);
body = res || {};
ctx.setMetaData("_body", body);
Expand All @@ -85,7 +85,7 @@ export async function bodyParser(ctx: KoattyContext, options?: PayloadOptions):
* @param {PayloadOptions} options
* @return {*}
*/
export function queryParser(ctx: KoattyContext, options?: PayloadOptions): any {
export function QueryParser(ctx: KoattyContext, options?: PayloadOptions): any {
let query = ctx.getMetaData("_query")[0];
if (!Helper.isEmpty(query)) {
return query;
Expand Down

0 comments on commit e001b9a

Please sign in to comment.