Skip to content

Commit

Permalink
refactor: requestParam重新定义
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Jun 25, 2024
1 parent 179c93f commit 4709fa9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
33 changes: 12 additions & 21 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: 2024-01-14 15:52:57
* @LastEditTime: 2024-01-15 13:02:38
* @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 @@ -106,14 +106,14 @@ export function File(name?: string): ParameterDecorator {


/**
* Get request body (contains the values of @Post and @File).
* Get parsed body(form variable and file object).
*
* @export
* @returns
* @returns ex: {post: {...}, file: {...}}
*/
export function RequestBody(): ParameterDecorator {
return injectParam((ctx: KoattyContext, opt?: PayloadOptions) => {
return BodyParser(ctx, opt);
return bodyParser(ctx, opt);
}, "RequestBody");
}

Expand All @@ -125,24 +125,15 @@ export function RequestBody(): ParameterDecorator {
export const Body = RequestBody;

/**
* Get POST/GET parameters, POST priority
*
* Get parsed query-string and path variable(koa ctx.query and ctx.params),
* and set as an object.
*
* @export
* @param {string} [name]
* @returns {ParameterDecorator}
*/
export function RequestParam(name?: string): ParameterDecorator {
export function RequestParam(): ParameterDecorator {
return injectParam((ctx: KoattyContext, opt?: PayloadOptions) => {
return BodyParser(ctx, opt).then((body: {
post: Object
}) => {
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];
}
return { ...queryParams, ...postParams };
});
return queryParser(ctx, opt)
}, "RequestParam");
}

Expand Down
55 changes: 36 additions & 19 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: 2024-01-14 15:55:24
* @LastEditTime: 2024-01-15 13:30:33
* @License: BSD (3-Clause)
* @Copyright (c): <richenlin(at)gmail.com>
*/
Expand All @@ -16,7 +16,7 @@ import { parseStringPromise } from "xml2js";
import { IncomingForm, BufferEncoding } from "formidable";
import { DefaultLogger as Logger } from "koatty_logger";
import onFinished from "on-finished";
import { KoattyContext } from "koatty_core";
import { KoattyContext, KoattyNext } from "koatty_core";
import { Helper } from "koatty_lib";
import { DefaultLogger as logger } from "koatty_logger";
const fsUnlink = util.promisify(fs.unlink);
Expand Down Expand Up @@ -57,25 +57,19 @@ const defaultOptions: PayloadOptions = {
};

/**
* @description:
* @param {KoattyContext} ctx
* @description: payload middleware
* @param {PayloadOptions} options
* @return {*}
*/
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);
return body;
} catch (err) {
logger.Error(err);
return {};
export function payload(options?: PayloadOptions) {
return (ctx: KoattyContext, next: KoattyNext) => {
Helper.define(ctx, "requestParam", () => {
return queryParser(ctx, options);
});
Helper.define(ctx, "requestBody", () => {
return bodyParser(ctx, options);
});
return next();
}
}

Expand All @@ -85,7 +79,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 All @@ -95,6 +89,29 @@ export function QueryParser(ctx: KoattyContext, options?: PayloadOptions): any {
return query;
}

/**
* @description:
* @param {KoattyContext} ctx
* @param {PayloadOptions} options
* @return {*}
*/
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);
return body;
} catch (err) {
logger.Error(err);
return {};
}
}

/**
* parseBody
*
Expand Down

0 comments on commit 4709fa9

Please sign in to comment.