Skip to content

Commit 4e51f10

Browse files
authored
feat: support context in params (#100)
1 parent 9e02818 commit 4e51f10

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

src/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export type InputContext<
169169
returnStatus?: boolean;
170170
use?: Middleware[];
171171
path?: string;
172+
context?: Record<string, any>;
172173
};
173174

174175
export const createInternalContext = async (

src/endpoint.test.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,121 @@ describe("creator", () => {
912912
});
913913
});
914914

915+
describe("context parameter", () => {
916+
it("should pass context from input parameter to endpoint", async () => {
917+
const endpoint = createEndpoint(
918+
"/test",
919+
{
920+
method: "POST",
921+
body: z.object({
922+
name: z.string(),
923+
}),
924+
},
925+
async (ctx) => {
926+
// Context should be accessible in the endpoint handler
927+
return {
928+
body: ctx.body,
929+
context: ctx.context,
930+
};
931+
},
932+
);
933+
934+
const response = await endpoint({
935+
body: { name: "test" },
936+
context: {
937+
userId: "123",
938+
requestId: "req-456",
939+
},
940+
});
941+
942+
expect(response.body.name).toBe("test");
943+
expect(response.context).toMatchObject({
944+
userId: "123",
945+
requestId: "req-456",
946+
});
947+
});
948+
949+
it("should merge context with middleware context", async () => {
950+
const endpoint = createEndpoint(
951+
"/test",
952+
{
953+
method: "POST",
954+
use: [
955+
createMiddleware(async () => {
956+
return {
957+
fromMiddleware: "middleware-value",
958+
};
959+
}),
960+
],
961+
},
962+
async (ctx) => {
963+
return ctx.context;
964+
},
965+
);
966+
967+
const response = await endpoint({
968+
context: {
969+
fromInput: "input-value",
970+
},
971+
});
972+
973+
// Both middleware context and input context should be available
974+
expect(response).toMatchObject({
975+
fromMiddleware: "middleware-value",
976+
fromInput: "input-value",
977+
});
978+
});
979+
980+
it("should handle empty context parameter", async () => {
981+
const endpoint = createEndpoint(
982+
"/test",
983+
{
984+
method: "GET",
985+
},
986+
async (ctx) => {
987+
return ctx.context;
988+
},
989+
);
990+
991+
const response = await endpoint();
992+
993+
// Should have empty context object
994+
expect(response).toEqual({});
995+
});
996+
997+
it("should allow context with complex nested objects", async () => {
998+
const endpoint = createEndpoint(
999+
"/test",
1000+
{
1001+
method: "POST",
1002+
},
1003+
async (ctx) => {
1004+
return ctx.context;
1005+
},
1006+
);
1007+
1008+
const complexContext = {
1009+
user: {
1010+
id: "123",
1011+
profile: {
1012+
name: "Test User",
1013+
email: "test@example.com",
1014+
},
1015+
},
1016+
metadata: {
1017+
traceId: "trace-123",
1018+
tags: ["tag1", "tag2"],
1019+
},
1020+
};
1021+
1022+
const response = await endpoint({
1023+
context: complexContext,
1024+
});
1025+
1026+
expect(response).toEqual(complexContext);
1027+
});
1028+
});
1029+
9151030
describe("onAPIError", () => {
9161031
it("should call onAPIError", async () => {
9171032
let error: APIError | undefined;

0 commit comments

Comments
 (0)