torotask
Version:
Task queue processing in NodeJS based on BullMQ and Redis
96 lines • 3.67 kB
JavaScript
import { z } from 'zod';
/**
* Defines a task group configuration.
* This function helps with type inference for task group definitions.
*
* @template TTaskMap The type of the task definition registry (a map of task definitions).
* @param config The task group definition, including an optional `id` and a `tasks` object.
* @returns The task group definition object, correctly typed.
*/
export function defineTaskGroup(config) {
return config;
}
/**
* Defines a task group registry.
* This function helps with type inference for task group definitions.
*
* @template T The specific type of the task group definitions registry.
* @param groups The task group definitions.
* @returns The task group definition registry, preserving its specific type.
*/
export function defineTaskGroupRegistry(groups) {
return groups;
}
/**
* Defines a task configuration.
* This function helps with type inference for the task definition.
*
* @template PayloadExplicit The type of the payload for the task. Defaults to `unknown`.
* If a `schema` is provided, this type is typically overridden by the schema's inferred type.
* @template SchemaInput The type of the schema or schema function.
* @template ResultExplicit The explicit type of the result returned by the task handler.
* If not provided (or set to `unknown`), the result type will be inferred from the handler.
* @template InferredHandlerResult (Internal) The actual inferred result type from the handler.
* @param config The task definition object.
* @returns The task definition object, correctly typed.
*/
export function defineTask(config) {
// The config object, as provided, has a handler that returns InferredHandlerResult.
// The returned TaskDefinition needs its handler to be typed with the final resolved result type.
// The underlying handler function is the same.
return config;
}
/**
* Defines a task configuration with an explicit result type.
* Use this when you want to specify the result type explicitly while still
* inferring the payload from the schema.
*
* @example
* ```ts
* const myTask = defineTaskWithResult<MyResultType>()({
* schema: createSchema(z => z.object({ id: z.string() })),
* handler: async (opts, ctx) => { return { success: true }; }
* })
* ```
*
* @template ResultExplicit The explicit result type for the task handler.
*/
export function defineTaskWithResult() {
return (config) => {
return config;
};
}
/**
* Creates a schema directly from a function that receives the Zod builder.
* This avoids needing to import `zod` in the consumer's file just for the type.
*
* @example
* ```ts
* const mySchema = createSchema(z => z.object({ id: z.string() }));
* ```
*/
export function createSchema(schemaFn) {
return schemaFn(z);
}
/**
* Returns a fully typed context for a given task group.
* This includes strongly-typed `step`, `group`, `client`, `task`, and `job` properties.
*
* @template TAllTaskGroupsDefs The type of all task group definitions.
* @template TCurrentTaskGroup The key of the current task group.
* @template TContext The original task handler context, used to infer payload and result types.
* @param context The generic task handler context.
* @param _currentGroup The identifier for the current task group, used for type inference.
* @returns A strongly-typed context object.
*/
export function getTypedContext(context, _currentGroup) {
return {
...context,
step: context.step,
group: context.group,
client: context.client,
task: context.task,
job: context.job,
};
}
//# sourceMappingURL=functions.js.map