DenoDDeno
Powered by
abiA
Denoβ€’3y ago
abi

Oak, `ctx.assert`, and middleware

I thought that
ctx.assert
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);
  });
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Oak – ctx.assert with custom response headers
abiAabi / help
3y ago
CSP Middleware for OAK?
elekramEelekram / help
3y ago
GetIP + Ctx from middleware to a route not passed
foobarFfoobar / help
3y ago