From 4709fa973ec355dcaed44a65642c1c5c8fbff3d2 Mon Sep 17 00:00:00 2001 From: linyyyang Date: Mon, 15 Jan 2024 23:58:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20requestParam=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/params.ts | 33 ++++++++++---------------- src/router/payload.ts | 55 ++++++++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/router/params.ts b/src/router/params.ts index 6c8a390..f96da50 100644 --- a/src/router/params.ts +++ b/src/router/params.ts @@ -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): */ import { KoattyContext } from "koatty_core"; import { injectParam } from "./inject"; -import { PayloadOptions, BodyParser, QueryParser } from "./payload"; +import { PayloadOptions, bodyParser, queryParser } from "./payload"; /** * Get request header. @@ -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; @@ -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 ?? {}; @@ -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"); } @@ -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"); } diff --git a/src/router/payload.ts b/src/router/payload.ts index c0c6757..761ff9c 100644 --- a/src/router/payload.ts +++ b/src/router/payload.ts @@ -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): */ @@ -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); @@ -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 { - 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(); } } @@ -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; @@ -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 { + 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 *