diff --git a/packages/route/source/d.ts b/packages/route/source/d.ts index 226363b..0e48535 100644 --- a/packages/route/source/d.ts +++ b/packages/route/source/d.ts @@ -4,11 +4,13 @@ import { RawContext } from "./context"; export class D { #context: RawContext; #response: RawContext["response"]; + #request: RawContext["request"]; #next: Function; constructor(context: RawContext, next: Function) { this.#context = context; this.#response = this.#context.response; + this.#request = this.#context.request; this.#next = next; for (const item of this.#context.dataTypes.keys()) { @@ -37,6 +39,71 @@ export class D { } } + req(reqUpdate: { + url?: string; + method?: string; + dataType?: string; + data?: any; + params?: Record; + queries?: Record; + headers?: Record; + }) { + const { + url = this.#request.url, + method = this.#request.method, + dataType = this.#request.dataType, + data = this.#request.data, + params = {}, + queries = {}, + headers = {}, + } = reqUpdate; + + this.#request.url = url; + this.#request.method = method; + this.#request.dataType = dataType; + this.#request.data = data; + this.#request.params = { + ...this.#request.params, + ...params, + }; + this.#request.queries = { + ...this.#request.queries, + ...queries, + }; + this.#request.headers = mergeDredgeHeaders(this.#request.headers, headers); + return this; + } + + res(resUpdate: { + status?: number; + statusText?: string; + data?: D; + dataType?: string; + headers?: Record; + }) { + const { + status = this.#response.status, + statusText = this.#response.statusText, + data = this.#response.data, + dataType = this.#response.dataType, + headers = {}, + } = resUpdate; + + this.#response.status = status; + this.#response.statusText = statusText; + this.#response.data = data; + this.#response.headers = mergeDredgeHeaders( + this.#response.headers, + headers, + ); + + if (dataType) { + this.dataType(dataType); + } + + return this; + } + status(status: number, statusText: string) { this.#response.status = status; this.#response.statusText = statusText; diff --git a/packages/route/test/d.test.ts b/packages/route/test/d.test.ts index 9c8526c..489e8e8 100644 --- a/packages/route/test/d.test.ts +++ b/packages/route/test/d.test.ts @@ -13,7 +13,105 @@ test("updates options.status", () => { expect(context.response.statusText).toBe("OK"); }); -test("updates options.data", () => { +test("update ctx.request", () => { + const context = createRawContext({}); + + const d = new D(context, () => {}); + + let update = { + url: "https://google.com", + method: "get", + dataType: "json", + data: { + hello: "world", + }, + params: { + a: "b", + }, + queries: { + c: ["d", "e"], + }, + headers: { + "content-type": "application/json", + "x-custom-header": "custom-value", + }, + }; + d.req(update); + + expect(context.request).toStrictEqual(update); + d.req({ + params: { + a: "c", + b: "d", + }, + queries: { + c: ["m"], + m: ["n"], + }, + headers: { + "x-custom-header": null, + "content-type": "text/plain", + }, + }); + + expect(context.request).toStrictEqual({ + ...update, + params: { + a: "c", + b: "d", + }, + queries: { + c: ["m"], + m: ["n"], + }, + headers: { + "content-type": "text/plain", + }, + }); +}); + +test("update ctx.response", () => { + const context = createRawContext({ + dataTypes: new DataTypes({ + text: "text/plain;charset=utf-8", + form: "multipart/form-data", + }), + }); + + const d = new D(context, () => {}); + + const update = { + status: 200, + statusText: "OK", + data: "hello", + dataType: "text", + headers: { + "x-custom-header": "custom-value", + }, + }; + d.res(update); + expect(context.response).toStrictEqual({ + ...update, + headers: { + ...update.headers, + "content-type": "text/plain;charset=utf-8", + }, + }); + + d.res({ + headers: { + "x-custom-header": null, + }, + }); + expect(context.response).toStrictEqual({ + ...update, + headers: { + "content-type": "text/plain;charset=utf-8", + }, + }); +}); + +test("updates response.data", () => { const context = createRawContext({}); const d = new D(context, () => {}); diff --git a/packages/types/source/route/dredge-route.ts b/packages/types/source/route/dredge-route.ts index 5ff6d13..53d037d 100644 --- a/packages/types/source/route/dredge-route.ts +++ b/packages/types/source/route/dredge-route.ts @@ -12,8 +12,25 @@ import { import { HTTPMethod } from "./http"; export type RouteD = { + res(resUpdate: { + status?: number; + statusText?: string; + data?: D; + dataType?: DataType; + headers?: Record; + }): RouteD; + req(reqUpdate: { + url?: string; + method?: string; + dataType?: string; + data?: any; + params?: Record; + queries?: Record; + headers?: Record; + }): RouteD; status(number: number, text?: string): RouteD; data(data: D): RouteD; + dataType(type: DataType): RouteD; state>( state: State, ): RouteD, Data>;