torotask
Version:
Task queue processing in NodeJS based on BullMQ and Redis
70 lines • 2.82 kB
JavaScript
import { z } from 'zod';
/**
* Defines a task group configuration.
* This function helps with type inference for task group definitions.
*
* @template TDefs The type of task definitions contained in this group.
* @param name The name of the task group.
* @param definitions The task definitions for this group.
* @returns The task group definition object, correctly typed.
*/
export function defineTaskGroup(config // config is { id?: 'groupX', tasks: TTaskMap }
) {
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;
}
/**
* 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);
}
/**
* Retrieves the step executor from the task handler context.
* This function is useful for accessing the step executor in a strongly typed manner.
*
* @template TAllTaskGroupsDefs The type of all task group definitions.
* @template TActualPayload The actual payload type for the task.
* @template TResult The result type for the task.
* @param context The task handler context.
* @returns The step executor, correctly typed.
*/
export function getTypedStep(context, _currentGroup) {
return context.step;
}
//# sourceMappingURL=functions.js.map