Build a runner for decorated methods
A transport adapter consumes routes that CHO already understands. A package that introduces a new kind of entry point also needs to describe and discover the methods itself. This example implements a named job that runs on demand.
Declare and execute a job
Install @chojs/core, save the following file as main.ts and run it with your TypeScript runner. The expected output is Catalog refreshed.
import {
add_to_metadata_object, type ChoMethodDecorator, Controller,
decorated_method, Injector, Module,
} from "@chojs/core/di";
const JOB = Symbol.for("library.jobs.name");
function Job(name: string): ChoMethodDecorator {
if (!name.trim()) throw new Error("A job needs a name");
return (target, context, descriptor) => {
const method = decorated_method("@Job()", target, context, descriptor);
if (method.static || method.private || typeof method.name !== "string") {
throw new Error("@Job() requires a public instance method");
}
add_to_metadata_object(method.meta, { [JOB]: name });
};
}
@Controller()
class CatalogJobs {
@Job("refresh")
refresh() { console.log("Catalog refreshed"); }
}
@Module({ controllers: [CatalogJobs] })
class Library {}
const injector = new Injector(Library);
try {
const jobs = new Map<string, () => Promise<void>>();
for (const node of injector.controllers()) {
for (const method of node.meta.methods) {
const job = method.meta[JOB];
if (typeof job !== "string") continue;
if (jobs.has(job)) throw new Error("Duplicate job: " + job);
jobs.set(job, async () => {
const instance = await injector.resolve(node.token, node.owner);
await instance[method.name]();
});
}
}
const refresh = jobs.get("refresh");
if (!refresh) throw new Error("Missing refresh job");
await refresh();
} finally {
await injector.dispose();
}Follow the lookup
@Job("refresh")callsdecorated_methodto normalize standard and legacy decorator arguments. It writes a package-specific symbol to the method's metadata.injector.controllers()yields each controller with its declaring module. The runner reads only methods carrying the job symbol and rejects duplicate public names.- Calling the registered function resolves the controller in
node.ownerand invokes the method on the instance, preservingthis. Listing jobs can therefore stay independent of the resources a job needs. finallyreleases the injector on both success and failure.
Extend the contract
To support middleware, decide what the downstream call returns. Collect the declaring module, controller and method middleware in that order. Use resolve_handle for injectable middleware classes and compose from @chojs/core/utils for dispatch. Define whether errors propagate, are translated or are considered settled. Publish these semantics along with your decorator and context types.
Test the extension with standard and legacy decorators, inherited methods, duplicate names and a failed asynchronous job. Core helper APIs are internal. Check the extension against the core version you distribute. Core entry points