Skip to content

Commit dc4bb4f

Browse files
committed
fix: broken context inference
Signed-off-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent 8e167d4 commit dc4bb4f

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src-v4/ss-zod.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,36 @@ console.log(
9696
proc4("Carol", { user: "carol", role: "admin", paid: true }),
9797
);
9898

99-
const ping = zagora()
99+
const pingProc = zagora()
100+
.context<{ user: string }>({ user: "default" })
100101
.input(z.number())
101-
.output(z.object({ pong: z.number() }))
102+
.output(z.object({ pong: z.number(), userId: z.string() }).strict())
102103
.errors({
103104
NOT_FOUND: z.object({ type: z.literal("NOT_FOUND"), userId: z.string() }),
104105
AUTH_ERR: z.object({ type: z.literal("AUTH_ERR"), retryAfter: z.number() }),
105106
})
106-
.handler(({ errors }, id) => {
107+
.handler(({ errors, context }, id) => {
107108
if (id === 42) {
108109
throw errors.AUTH_ERR({ retryAfter: Date.now() + 1000 });
109110
}
110111
if (id <= 20) {
111112
throw errors.NOT_FOUND({ userId: "123" });
112113
}
113114

114-
return { pong: id + 1 };
115-
})
116-
.callable();
115+
return { pong: id + 1, userId: context.user + "-foo" };
116+
});
117+
118+
// const router = {
119+
// users: {
120+
// ping: pingProc,
121+
// },
122+
// };
123+
124+
// router.users.ping.callable({ user: "sasa" });
117125

118126
// Call synchronously, no try-catch needed
119-
const resPing = ping(42);
127+
const ping = pingProc.callable();
128+
const resPing = ping(30);
120129
console.log("Test 5 ping-pong:", resPing);
121130

122131
if (

src-v4/sscore.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export interface ProcedureOptions<
253253
TContext,
254254
TErrorsMap extends Record<string, StandardSchemaV1> | undefined,
255255
> {
256-
context: TContext | undefined;
256+
context: TContext;
257257
errors: TErrorsMap extends Record<string, StandardSchemaV1>
258258
? ErrorHelpers<TErrorsMap>
259259
: undefined;
@@ -362,7 +362,7 @@ class Builder<
362362
}
363363

364364
callable<
365-
TContext,
365+
TNewContext extends TContext,
366366
TSpread extends SpreadTuple<
367367
InferSchemaInput<TInputSchema>,
368368
ZagoraResult<
@@ -378,7 +378,7 @@ class Builder<
378378
InferSchemaOutput<TOutputSchema>,
379379
ErrorsMapResolved<TErrorsMap>
380380
>,
381-
>(context?: TContext): TProcReturn {
381+
>(context?: TNewContext): TProcReturn {
382382
const { initialContext, errorsMap } = this.def;
383383
const handlerFn = this.def.handler as any; // todo: fix, return internal error if not defined (thru createResult)
384384
const inputSchema = this.def.inputSchema as TInputSchema;
@@ -392,7 +392,7 @@ class Builder<
392392
const errors = errorsMap ? createErrorHelpers(errorsMap as any) : undefined;
393393
const options = {
394394
errors: errors as ErrorHelpers<TErrorsMap>,
395-
context: mergedContext,
395+
context: mergedContext as TNewContext,
396396
};
397397

398398
const wrapped = (...args: unknown[]) => {

0 commit comments

Comments
 (0)