abi
abi2y ago

Oak, `ctx.assert`, and middleware

I thought that ctx.assert would somehow "carry along" the information about the asserted condition to the next middleware in the chain, but I can't seem to get that to work. Am I misunderstanding something here? I was thinking if I do something like this, I wouldn't have to to a null check in the last middleware, but I apparently do?
import { Application, Context } from "https://deno.land/x/oak@v12.4.0/mod.ts";

interface MyState {
value: null | 3;
}

const app = new Application<MyState>({
state: {
value: 3,
},
});

app
.use((ctx: Context<MyState>) => {
ctx.assert(ctx.state.value !== null, 500);
})
.use((ctx) => {
// 'ctx.state.value' is possibly 'null'.deno-ts(18047)
console.log(ctx.state.value + 5);
});
import { Application, Context } from "https://deno.land/x/oak@v12.4.0/mod.ts";

interface MyState {
value: null | 3;
}

const app = new Application<MyState>({
state: {
value: 3,
},
});

app
.use((ctx: Context<MyState>) => {
ctx.assert(ctx.state.value !== null, 500);
})
.use((ctx) => {
// 'ctx.state.value' is possibly 'null'.deno-ts(18047)
console.log(ctx.state.value + 5);
});
0 Replies
No replies yetBe the first to reply to this messageJoin