Skip to content

Commit

Permalink
feat(dredge-route, dredge-types): d contain d.req() and d.res() (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrjarun authored Dec 23, 2024
1 parent 6e777d8 commit 53ab51e
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 1 deletion.
67 changes: 67 additions & 0 deletions packages/route/source/d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -37,6 +39,71 @@ export class D {
}
}

req(reqUpdate: {
url?: string;
method?: string;
dataType?: string;
data?: any;
params?: Record<string, any>;
queries?: Record<string, any[]>;
headers?: Record<string, string | null>;
}) {
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<D extends any>(resUpdate: {
status?: number;
statusText?: string;
data?: D;
dataType?: string;
headers?: Record<string, string | null>;
}) {
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;
Expand Down
100 changes: 99 additions & 1 deletion packages/route/test/d.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {});

Expand Down
17 changes: 17 additions & 0 deletions packages/types/source/route/dredge-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@ import {
import { HTTPMethod } from "./http";

export type RouteD<DataType extends string, Context, Data> = {
res<D extends Data>(resUpdate: {
status?: number;
statusText?: string;
data?: D;
dataType?: DataType;
headers?: Record<string, string | null>;
}): RouteD<DataType, Context, Data>;
req(reqUpdate: {
url?: string;
method?: string;
dataType?: string;
data?: any;
params?: Record<string, any>;
queries?: Record<string, any[]>;
headers?: Record<string, string | null>;
}): RouteD<DataType, Context, Data>;
status(number: number, text?: string): RouteD<DataType, Context, Data>;
data<D extends Data>(data: D): RouteD<DataType, Context, D>;
dataType(type: DataType): RouteD<DataType, Context, Data>;
state<State extends Record<string, any>>(
state: State,
): RouteD<DataType, Overwrite<Context, State>, Data>;
Expand Down

0 comments on commit 53ab51e

Please sign in to comment.