Skip to main content

Context

One major feature in Pashmak is context. It allows for separating processes safely without sharing sensitive data between them. It also leaves flexibility to share resources among different processes.

contextualized processes:

  • http requests
  • cli command
  • cron jobs
  • queue jobs

creating your own context

import { ctx } from "@devbro/pashmak/context";

ctx().set("context_key", my_object);

ctx().get < MyObject > get("context_key");

It is suggested that you add a wrapper around your context.

function getMyObject(): MyObject {
return ctx().getOrThrow < MyObject > get("context_key");
}

Unit testing in a contexualized env

during testing you may want to have your own contextualized test or mini process.

import { context_provider } from "@devbro/pashmak/context";

test("context test", async () => {
await context_provider.run(async (): Promise<void> => {
ctx().get("????");
});
});

If you ever get an error that Context has not started it means you are trying to access context outside of a context provider run block. Just wrap your code in context_provider.run(async () => { /* YOUR CODE */ });

detect context availability

// to detect if the code is running inside a context or not
if (ctx.isActive()) {
ctx().get(KEY);
}