@trigger.dev/core 3.0.13 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/v3/apiClient/core.d.ts +1 -1
- package/dist/commonjs/v3/apiClient/core.js +9 -1
- package/dist/commonjs/v3/apiClient/core.js.map +1 -1
- package/dist/commonjs/v3/apiClient/index.d.ts +64 -36
- package/dist/commonjs/v3/apiClient/index.js +100 -4
- package/dist/commonjs/v3/apiClient/index.js.map +1 -1
- package/dist/commonjs/v3/apiClient/runStream.d.ts +50 -0
- package/dist/commonjs/v3/apiClient/runStream.js +159 -0
- package/dist/commonjs/v3/apiClient/runStream.js.map +1 -0
- package/dist/commonjs/v3/apiClient/stream.d.ts +9 -0
- package/dist/commonjs/v3/apiClient/stream.js +37 -0
- package/dist/commonjs/v3/apiClient/stream.js.map +1 -0
- package/dist/commonjs/v3/apiClient/types.d.ts +4 -0
- package/dist/commonjs/v3/apiClientManager/index.d.ts +2 -1
- package/dist/commonjs/v3/apiClientManager/index.js +18 -7
- package/dist/commonjs/v3/apiClientManager/index.js.map +1 -1
- package/dist/commonjs/v3/apiClientManager/types.d.ts +7 -0
- package/dist/commonjs/v3/apps/process.d.ts +2 -0
- package/dist/commonjs/v3/apps/process.js +2 -0
- package/dist/commonjs/v3/apps/process.js.map +1 -1
- package/dist/commonjs/v3/errors.d.ts +5 -0
- package/dist/commonjs/v3/errors.js +185 -30
- package/dist/commonjs/v3/errors.js.map +1 -1
- package/dist/commonjs/v3/index.d.ts +2 -1
- package/dist/commonjs/v3/index.js +4 -3
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/jwt.d.ts +10 -0
- package/dist/commonjs/v3/jwt.js +34 -0
- package/dist/commonjs/v3/jwt.js.map +1 -0
- package/dist/commonjs/v3/otel/index.d.ts +2 -3
- package/dist/commonjs/v3/otel/index.js +15 -21
- package/dist/commonjs/v3/otel/index.js.map +1 -1
- package/dist/commonjs/v3/otel/utils.d.ts +2 -0
- package/dist/commonjs/v3/otel/utils.js +24 -0
- package/dist/commonjs/v3/otel/utils.js.map +1 -0
- package/dist/commonjs/v3/runMetadata/index.d.ts +8 -6
- package/dist/commonjs/v3/runMetadata/index.js +18 -39
- package/dist/commonjs/v3/runMetadata/index.js.map +1 -1
- package/dist/commonjs/v3/runMetadata/manager.d.ts +17 -0
- package/dist/commonjs/v3/runMetadata/manager.js +116 -0
- package/dist/commonjs/v3/runMetadata/manager.js.map +1 -0
- package/dist/commonjs/v3/runMetadata/noopManager.d.ts +12 -0
- package/dist/commonjs/v3/runMetadata/noopManager.js +26 -0
- package/dist/commonjs/v3/runMetadata/noopManager.js.map +1 -0
- package/dist/commonjs/v3/runMetadata/types.d.ts +11 -0
- package/dist/commonjs/v3/runMetadata/types.js +3 -0
- package/dist/commonjs/v3/runMetadata/types.js.map +1 -0
- package/dist/commonjs/v3/runtime/noopRuntimeManager.js +5 -1
- package/dist/commonjs/v3/runtime/noopRuntimeManager.js.map +1 -1
- package/dist/commonjs/v3/schemas/api.d.ts +193 -64
- package/dist/commonjs/v3/schemas/api.js +34 -4
- package/dist/commonjs/v3/schemas/api.js.map +1 -1
- package/dist/commonjs/v3/schemas/build.d.ts +12 -12
- package/dist/commonjs/v3/schemas/common.d.ts +24 -24
- package/dist/commonjs/v3/schemas/common.js +2 -0
- package/dist/commonjs/v3/schemas/common.js.map +1 -1
- package/dist/commonjs/v3/schemas/messages.d.ts +124 -123
- package/dist/commonjs/v3/schemas/messages.js +1 -0
- package/dist/commonjs/v3/schemas/messages.js.map +1 -1
- package/dist/commonjs/v3/schemas/schemas.d.ts +2 -2
- package/dist/commonjs/v3/tracer.js +2 -2
- package/dist/commonjs/v3/types/idempotencyKeys.d.ts +7 -0
- package/dist/commonjs/v3/types/idempotencyKeys.js +3 -0
- package/dist/commonjs/v3/types/idempotencyKeys.js.map +1 -0
- package/dist/commonjs/v3/types/index.d.ts +9 -63
- package/dist/commonjs/v3/types/index.js +2 -0
- package/dist/commonjs/v3/types/index.js.map +1 -1
- package/dist/commonjs/v3/types/schemas.d.ts +44 -0
- package/dist/commonjs/v3/types/schemas.js +41 -0
- package/dist/commonjs/v3/types/schemas.js.map +1 -0
- package/dist/commonjs/v3/types/tasks.d.ts +473 -0
- package/dist/commonjs/v3/types/tasks.js +42 -0
- package/dist/commonjs/v3/types/tasks.js.map +1 -0
- package/dist/commonjs/v3/utils/flattenAttributes.d.ts +2 -1
- package/dist/commonjs/v3/utils/flattenAttributes.js +39 -16
- package/dist/commonjs/v3/utils/flattenAttributes.js.map +1 -1
- package/dist/commonjs/v3/utils/getEnv.d.ts +1 -0
- package/dist/commonjs/v3/utils/getEnv.js +12 -0
- package/dist/commonjs/v3/utils/getEnv.js.map +1 -1
- package/dist/commonjs/v3/utils/globals.d.ts +2 -2
- package/dist/commonjs/v3/utils/globals.js.map +1 -1
- package/dist/commonjs/v3/utils/ioSerialization.d.ts +1 -0
- package/dist/commonjs/v3/utils/ioSerialization.js +21 -3
- package/dist/commonjs/v3/utils/ioSerialization.js.map +1 -1
- package/dist/commonjs/v3/utils/safeAsyncLocalStorage.d.ts +1 -0
- package/dist/commonjs/v3/utils/safeAsyncLocalStorage.js +3 -0
- package/dist/commonjs/v3/utils/safeAsyncLocalStorage.js.map +1 -1
- package/dist/commonjs/v3/workers/index.d.ts +2 -1
- package/dist/commonjs/v3/workers/index.js +4 -1
- package/dist/commonjs/v3/workers/index.js.map +1 -1
- package/dist/commonjs/v3/workers/taskExecutor.js +39 -7
- package/dist/commonjs/v3/workers/taskExecutor.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/apiClient/core.d.ts +1 -1
- package/dist/esm/v3/apiClient/core.js +9 -1
- package/dist/esm/v3/apiClient/core.js.map +1 -1
- package/dist/esm/v3/apiClient/index.d.ts +64 -36
- package/dist/esm/v3/apiClient/index.js +100 -4
- package/dist/esm/v3/apiClient/index.js.map +1 -1
- package/dist/esm/v3/apiClient/runStream.d.ts +50 -0
- package/dist/esm/v3/apiClient/runStream.js +154 -0
- package/dist/esm/v3/apiClient/runStream.js.map +1 -0
- package/dist/esm/v3/apiClient/stream.d.ts +9 -0
- package/dist/esm/v3/apiClient/stream.js +33 -0
- package/dist/esm/v3/apiClient/stream.js.map +1 -0
- package/dist/esm/v3/apiClient/types.d.ts +4 -0
- package/dist/esm/v3/apiClientManager/index.d.ts +2 -1
- package/dist/esm/v3/apiClientManager/index.js +18 -7
- package/dist/esm/v3/apiClientManager/index.js.map +1 -1
- package/dist/esm/v3/apiClientManager/types.d.ts +7 -0
- package/dist/esm/v3/apps/process.d.ts +2 -0
- package/dist/esm/v3/apps/process.js +2 -0
- package/dist/esm/v3/apps/process.js.map +1 -1
- package/dist/esm/v3/errors.d.ts +5 -0
- package/dist/esm/v3/errors.js +182 -29
- package/dist/esm/v3/errors.js.map +1 -1
- package/dist/esm/v3/index.d.ts +2 -1
- package/dist/esm/v3/index.js +2 -1
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/jwt.d.ts +10 -0
- package/dist/esm/v3/jwt.js +29 -0
- package/dist/esm/v3/jwt.js.map +1 -0
- package/dist/esm/v3/otel/index.d.ts +2 -3
- package/dist/esm/v3/otel/index.js +1 -20
- package/dist/esm/v3/otel/index.js.map +1 -1
- package/dist/esm/v3/otel/utils.d.ts +2 -0
- package/dist/esm/v3/otel/utils.js +21 -0
- package/dist/esm/v3/otel/utils.js.map +1 -0
- package/dist/esm/v3/runMetadata/index.d.ts +8 -6
- package/dist/esm/v3/runMetadata/index.js +20 -41
- package/dist/esm/v3/runMetadata/index.js.map +1 -1
- package/dist/esm/v3/runMetadata/manager.d.ts +17 -0
- package/dist/esm/v3/runMetadata/manager.js +112 -0
- package/dist/esm/v3/runMetadata/manager.js.map +1 -0
- package/dist/esm/v3/runMetadata/noopManager.d.ts +12 -0
- package/dist/esm/v3/runMetadata/noopManager.js +22 -0
- package/dist/esm/v3/runMetadata/noopManager.js.map +1 -0
- package/dist/esm/v3/runMetadata/types.d.ts +11 -0
- package/dist/esm/v3/runMetadata/types.js +2 -0
- package/dist/esm/v3/runMetadata/types.js.map +1 -0
- package/dist/esm/v3/runtime/noopRuntimeManager.js +5 -1
- package/dist/esm/v3/runtime/noopRuntimeManager.js.map +1 -1
- package/dist/esm/v3/schemas/api.d.ts +193 -64
- package/dist/esm/v3/schemas/api.js +33 -3
- package/dist/esm/v3/schemas/api.js.map +1 -1
- package/dist/esm/v3/schemas/build.d.ts +12 -12
- package/dist/esm/v3/schemas/common.d.ts +24 -24
- package/dist/esm/v3/schemas/common.js +2 -0
- package/dist/esm/v3/schemas/common.js.map +1 -1
- package/dist/esm/v3/schemas/messages.d.ts +124 -123
- package/dist/esm/v3/schemas/messages.js +1 -0
- package/dist/esm/v3/schemas/messages.js.map +1 -1
- package/dist/esm/v3/schemas/schemas.d.ts +2 -2
- package/dist/esm/v3/tracer.js +1 -1
- package/dist/esm/v3/types/idempotencyKeys.d.ts +7 -0
- package/dist/esm/v3/types/idempotencyKeys.js +2 -0
- package/dist/esm/v3/types/idempotencyKeys.js.map +1 -0
- package/dist/esm/v3/types/index.d.ts +9 -63
- package/dist/esm/v3/types/index.js +2 -0
- package/dist/esm/v3/types/index.js.map +1 -1
- package/dist/esm/v3/types/schemas.d.ts +44 -0
- package/dist/esm/v3/types/schemas.js +38 -0
- package/dist/esm/v3/types/schemas.js.map +1 -0
- package/dist/esm/v3/types/tasks.d.ts +473 -0
- package/dist/esm/v3/types/tasks.js +37 -0
- package/dist/esm/v3/types/tasks.js.map +1 -0
- package/dist/esm/v3/utils/flattenAttributes.d.ts +2 -1
- package/dist/esm/v3/utils/flattenAttributes.js +38 -15
- package/dist/esm/v3/utils/flattenAttributes.js.map +1 -1
- package/dist/esm/v3/utils/getEnv.d.ts +1 -0
- package/dist/esm/v3/utils/getEnv.js +11 -0
- package/dist/esm/v3/utils/getEnv.js.map +1 -1
- package/dist/esm/v3/utils/globals.d.ts +2 -2
- package/dist/esm/v3/utils/globals.js.map +1 -1
- package/dist/esm/v3/utils/ioSerialization.d.ts +1 -0
- package/dist/esm/v3/utils/ioSerialization.js +20 -3
- package/dist/esm/v3/utils/ioSerialization.js.map +1 -1
- package/dist/esm/v3/utils/safeAsyncLocalStorage.d.ts +1 -0
- package/dist/esm/v3/utils/safeAsyncLocalStorage.js +3 -0
- package/dist/esm/v3/utils/safeAsyncLocalStorage.js.map +1 -1
- package/dist/esm/v3/workers/index.d.ts +2 -1
- package/dist/esm/v3/workers/index.js +2 -1
- package/dist/esm/v3/workers/index.js.map +1 -1
- package/dist/esm/v3/workers/taskExecutor.js +40 -8
- package/dist/esm/v3/workers/taskExecutor.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +35 -1
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import { SerializableJson } from "../../schemas/json.js";
|
|
2
|
+
import { RunTags } from "../schemas/api.js";
|
|
3
|
+
import { QueueOptions } from "../schemas/schemas.js";
|
|
4
|
+
import { IdempotencyKey } from "./idempotencyKeys.js";
|
|
5
|
+
import { MachineCpu, MachineMemory, RetryOptions, TaskMetadata, TaskRunContext } from "../schemas/index.js";
|
|
6
|
+
import { Prettify } from "./utils.js";
|
|
7
|
+
import { AnySchemaParseFn, inferSchemaOut, Schema } from "./schemas.js";
|
|
8
|
+
import { TriggerApiRequestOptions } from "../apiClient/index.js";
|
|
9
|
+
type RequireOne<T, K extends keyof T> = {
|
|
10
|
+
[X in Exclude<keyof T, K>]?: T[X];
|
|
11
|
+
} & {
|
|
12
|
+
[P in K]-?: T[P];
|
|
13
|
+
};
|
|
14
|
+
export type Queue = RequireOne<QueueOptions, "name">;
|
|
15
|
+
export type TaskSchema = Schema;
|
|
16
|
+
export type { inferSchemaIn } from "./schemas.js";
|
|
17
|
+
type TaskRunConcurrencyOptions = Queue;
|
|
18
|
+
export declare class SubtaskUnwrapError extends Error {
|
|
19
|
+
readonly taskId: string;
|
|
20
|
+
readonly runId: string;
|
|
21
|
+
readonly cause?: unknown;
|
|
22
|
+
constructor(taskId: string, runId: string, subtaskError: unknown);
|
|
23
|
+
}
|
|
24
|
+
export declare class TaskRunPromise<T> extends Promise<TaskRunResult<T>> {
|
|
25
|
+
private readonly taskId;
|
|
26
|
+
constructor(executor: (resolve: (value: TaskRunResult<T> | PromiseLike<TaskRunResult<T>>) => void, reject: (reason?: any) => void) => void, taskId: string);
|
|
27
|
+
unwrap(): Promise<T>;
|
|
28
|
+
}
|
|
29
|
+
export type InitOutput = Record<string, any> | void | undefined;
|
|
30
|
+
export type RunFnParams<TInitOutput extends InitOutput> = Prettify<{
|
|
31
|
+
/** Metadata about the task, run, attempt, queue, environment, organization, project and batch. */
|
|
32
|
+
ctx: Context;
|
|
33
|
+
/** If you use the `init` function, this will be whatever you returned. */
|
|
34
|
+
init?: TInitOutput;
|
|
35
|
+
/** Abort signal that is aborted when a task run exceeds it's maxDuration. Can be used to automatically cancel downstream requests */
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
}>;
|
|
38
|
+
export type MiddlewareFnParams = Prettify<{
|
|
39
|
+
ctx: Context;
|
|
40
|
+
next: () => Promise<void>;
|
|
41
|
+
/** Abort signal that is aborted when a task run exceeds it's maxDuration. Can be used to automatically cancel downstream requests */
|
|
42
|
+
signal?: AbortSignal;
|
|
43
|
+
}>;
|
|
44
|
+
export type InitFnParams = Prettify<{
|
|
45
|
+
ctx: Context;
|
|
46
|
+
/** Abort signal that is aborted when a task run exceeds it's maxDuration. Can be used to automatically cancel downstream requests */
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
}>;
|
|
49
|
+
export type StartFnParams = Prettify<{
|
|
50
|
+
ctx: Context;
|
|
51
|
+
/** Abort signal that is aborted when a task run exceeds it's maxDuration. Can be used to automatically cancel downstream requests */
|
|
52
|
+
signal?: AbortSignal;
|
|
53
|
+
}>;
|
|
54
|
+
export type Context = TaskRunContext;
|
|
55
|
+
export type SuccessFnParams<TInitOutput extends InitOutput> = RunFnParams<TInitOutput>;
|
|
56
|
+
export type FailureFnParams<TInitOutput extends InitOutput> = RunFnParams<TInitOutput>;
|
|
57
|
+
export type HandleErrorFnParams<TInitOutput extends InitOutput> = RunFnParams<TInitOutput> & Prettify<{
|
|
58
|
+
retry?: RetryOptions;
|
|
59
|
+
retryAt?: Date;
|
|
60
|
+
retryDelayInMs?: number;
|
|
61
|
+
}>;
|
|
62
|
+
export type HandleErrorModificationOptions = {
|
|
63
|
+
skipRetrying?: boolean | undefined;
|
|
64
|
+
retryAt?: Date | undefined;
|
|
65
|
+
retryDelayInMs?: number | undefined;
|
|
66
|
+
retry?: RetryOptions | undefined;
|
|
67
|
+
error?: unknown;
|
|
68
|
+
};
|
|
69
|
+
export type HandleErrorResult = undefined | void | HandleErrorModificationOptions | Promise<undefined | void | HandleErrorModificationOptions>;
|
|
70
|
+
export type HandleErrorArgs = {
|
|
71
|
+
ctx: Context;
|
|
72
|
+
retry?: RetryOptions;
|
|
73
|
+
retryAt?: Date;
|
|
74
|
+
retryDelayInMs?: number;
|
|
75
|
+
/** Abort signal that is aborted when a task run exceeds it's maxDuration. Can be used to automatically cancel downstream requests */
|
|
76
|
+
signal?: AbortSignal;
|
|
77
|
+
};
|
|
78
|
+
export type HandleErrorFunction = (payload: any, error: unknown, params: HandleErrorArgs) => HandleErrorResult;
|
|
79
|
+
type CommonTaskOptions<TIdentifier extends string, TPayload = void, TOutput = unknown, TInitOutput extends InitOutput = any> = {
|
|
80
|
+
/** An id for your task. This must be unique inside your project and not change between versions. */
|
|
81
|
+
id: TIdentifier;
|
|
82
|
+
/** The retry settings when an uncaught error is thrown.
|
|
83
|
+
*
|
|
84
|
+
* If omitted it will use the values in your `trigger.config.ts` file.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
*
|
|
88
|
+
* ```
|
|
89
|
+
* export const taskWithRetries = task({
|
|
90
|
+
id: "task-with-retries",
|
|
91
|
+
retry: {
|
|
92
|
+
maxAttempts: 10,
|
|
93
|
+
factor: 1.8,
|
|
94
|
+
minTimeoutInMs: 500,
|
|
95
|
+
maxTimeoutInMs: 30_000,
|
|
96
|
+
randomize: false,
|
|
97
|
+
},
|
|
98
|
+
run: async ({ payload, ctx }) => {
|
|
99
|
+
//...
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
* ```
|
|
103
|
+
* */
|
|
104
|
+
retry?: RetryOptions;
|
|
105
|
+
/** Used to configure what should happen when more than one run is triggered at the same time.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* one at a time execution
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* export const oneAtATime = task({
|
|
112
|
+
id: "one-at-a-time",
|
|
113
|
+
queue: {
|
|
114
|
+
concurrencyLimit: 1,
|
|
115
|
+
},
|
|
116
|
+
run: async ({ payload, ctx }) => {
|
|
117
|
+
//...
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
queue?: QueueOptions;
|
|
123
|
+
/** Configure the spec of the machine you want your task to run on.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* export const heavyTask = task({
|
|
129
|
+
id: "heavy-task",
|
|
130
|
+
machine: {
|
|
131
|
+
cpu: 2,
|
|
132
|
+
memory: 4,
|
|
133
|
+
},
|
|
134
|
+
run: async ({ payload, ctx }) => {
|
|
135
|
+
//...
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
machine?: {
|
|
141
|
+
/** vCPUs. The default is 0.5.
|
|
142
|
+
*
|
|
143
|
+
* Possible values:
|
|
144
|
+
* - 0.25
|
|
145
|
+
* - 0.5
|
|
146
|
+
* - 1
|
|
147
|
+
* - 2
|
|
148
|
+
* - 4
|
|
149
|
+
* @deprecated use preset instead
|
|
150
|
+
*/
|
|
151
|
+
cpu?: MachineCpu;
|
|
152
|
+
/** In GBs of RAM. The default is 1.
|
|
153
|
+
*
|
|
154
|
+
* Possible values:
|
|
155
|
+
* - 0.25
|
|
156
|
+
* - 0.5
|
|
157
|
+
* - 1
|
|
158
|
+
* - 2
|
|
159
|
+
* - 4
|
|
160
|
+
* - 8
|
|
161
|
+
* * @deprecated use preset instead
|
|
162
|
+
*/
|
|
163
|
+
memory?: MachineMemory;
|
|
164
|
+
/** Preset to use for the machine. Defaults to small-1x */
|
|
165
|
+
preset?: "micro" | "small-1x" | "small-2x" | "medium-1x" | "medium-2x" | "large-1x" | "large-2x";
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* The maximum duration in compute-time seconds that a task run is allowed to run. If the task run exceeds this duration, it will be stopped.
|
|
169
|
+
*
|
|
170
|
+
* Minimum value is 5 seconds
|
|
171
|
+
*/
|
|
172
|
+
maxDuration?: number;
|
|
173
|
+
/** This gets called when a task is triggered. It's where you put the code you want to execute.
|
|
174
|
+
*
|
|
175
|
+
* @param payload - The payload that is passed to your task when it's triggered. This must be JSON serializable.
|
|
176
|
+
* @param params - Metadata about the run.
|
|
177
|
+
*/
|
|
178
|
+
run: (payload: TPayload, params: RunFnParams<TInitOutput>) => Promise<TOutput>;
|
|
179
|
+
/**
|
|
180
|
+
* init is called before the run function is called. It's useful for setting up any global state.
|
|
181
|
+
*/
|
|
182
|
+
init?: (payload: TPayload, params: InitFnParams) => Promise<TInitOutput>;
|
|
183
|
+
/**
|
|
184
|
+
* cleanup is called after the run function has completed.
|
|
185
|
+
*/
|
|
186
|
+
cleanup?: (payload: TPayload, params: RunFnParams<TInitOutput>) => Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* handleError is called when the run function throws an error. It can be used to modify the error or return new retry options.
|
|
189
|
+
*/
|
|
190
|
+
handleError?: (payload: TPayload, error: unknown, params: HandleErrorFnParams<TInitOutput>) => HandleErrorResult;
|
|
191
|
+
/**
|
|
192
|
+
* middleware allows you to run code "around" the run function. This can be useful for logging, metrics, or other cross-cutting concerns.
|
|
193
|
+
*
|
|
194
|
+
* When writing middleware, you should always call `next()` to continue the execution of the task:
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* export const middlewareTask = task({
|
|
198
|
+
* id: "middleware-task",
|
|
199
|
+
* middleware: async (payload, { ctx, next }) => {
|
|
200
|
+
* console.log("Before run");
|
|
201
|
+
* await next();
|
|
202
|
+
* console.log("After run");
|
|
203
|
+
* },
|
|
204
|
+
* run: async (payload, { ctx }) => {}
|
|
205
|
+
* });
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
middleware?: (payload: TPayload, params: MiddlewareFnParams) => Promise<void>;
|
|
209
|
+
/**
|
|
210
|
+
* onStart is called the first time a task is executed in a run (not before every retry)
|
|
211
|
+
*/
|
|
212
|
+
onStart?: (payload: TPayload, params: StartFnParams) => Promise<void>;
|
|
213
|
+
/**
|
|
214
|
+
* onSuccess is called after the run function has successfully completed.
|
|
215
|
+
*/
|
|
216
|
+
onSuccess?: (payload: TPayload, output: TOutput, params: SuccessFnParams<TInitOutput>) => Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* onFailure is called after a task run has failed (meaning the run function threw an error and won't be retried anymore)
|
|
219
|
+
*/
|
|
220
|
+
onFailure?: (payload: TPayload, error: unknown, params: FailureFnParams<TInitOutput>) => Promise<void>;
|
|
221
|
+
};
|
|
222
|
+
export type TaskOptions<TIdentifier extends string, TPayload = void, TOutput = unknown, TInitOutput extends InitOutput = any> = CommonTaskOptions<TIdentifier, TPayload, TOutput, TInitOutput>;
|
|
223
|
+
export type TaskWithSchemaOptions<TIdentifier extends string, TSchema extends TaskSchema | undefined = undefined, TOutput = unknown, TInitOutput extends InitOutput = any> = CommonTaskOptions<TIdentifier, inferSchemaOut<TSchema>, TOutput, TInitOutput> & {
|
|
224
|
+
schema?: TSchema;
|
|
225
|
+
};
|
|
226
|
+
declare const __output: unique symbol;
|
|
227
|
+
declare const __payload: unique symbol;
|
|
228
|
+
type BrandRun<P, O> = {
|
|
229
|
+
[__output]: O;
|
|
230
|
+
[__payload]: P;
|
|
231
|
+
};
|
|
232
|
+
export type BrandedRun<T, P, O> = T & BrandRun<O, P>;
|
|
233
|
+
export type RunHandle<TTaskIdentifier extends string, TPayload, TOutput> = BrandedRun<{
|
|
234
|
+
id: string;
|
|
235
|
+
/**
|
|
236
|
+
* An auto-generated JWT that can be used to access the run
|
|
237
|
+
*/
|
|
238
|
+
publicAccessToken: string;
|
|
239
|
+
taskIdentifier: TTaskIdentifier;
|
|
240
|
+
}, TPayload, TOutput>;
|
|
241
|
+
export type AnyRunHandle = RunHandle<string, any, any>;
|
|
242
|
+
/**
|
|
243
|
+
* A BatchRunHandle can be used to retrieve the runs of a batch trigger in a typesafe manner.
|
|
244
|
+
*/
|
|
245
|
+
export type BatchRunHandle<TTaskIdentifier extends string, TPayload, TOutput> = BrandedRun<{
|
|
246
|
+
batchId: string;
|
|
247
|
+
runs: Array<RunHandle<TTaskIdentifier, TPayload, TOutput>>;
|
|
248
|
+
publicAccessToken: string;
|
|
249
|
+
taskIdentifier: TTaskIdentifier;
|
|
250
|
+
}, TOutput, TPayload>;
|
|
251
|
+
export type RunHandleOutput<TRunHandle> = TRunHandle extends RunHandle<string, any, infer TOutput> ? TOutput : never;
|
|
252
|
+
export type RunHandlePayload<TRunHandle> = TRunHandle extends RunHandle<string, infer TPayload, any> ? TPayload : never;
|
|
253
|
+
export type RunHandleTaskIdentifier<TRunHandle> = TRunHandle extends RunHandle<infer TTaskIdentifier, any, any> ? TTaskIdentifier : never;
|
|
254
|
+
export type TaskRunResult<TOutput = any> = {
|
|
255
|
+
ok: true;
|
|
256
|
+
id: string;
|
|
257
|
+
output: TOutput;
|
|
258
|
+
} | {
|
|
259
|
+
ok: false;
|
|
260
|
+
id: string;
|
|
261
|
+
error: unknown;
|
|
262
|
+
};
|
|
263
|
+
export type BatchResult<TOutput = any> = {
|
|
264
|
+
id: string;
|
|
265
|
+
runs: TaskRunResult<TOutput>[];
|
|
266
|
+
};
|
|
267
|
+
export type BatchItem<TInput> = TInput extends void ? {
|
|
268
|
+
payload?: TInput;
|
|
269
|
+
options?: TaskRunOptions;
|
|
270
|
+
} : {
|
|
271
|
+
payload: TInput;
|
|
272
|
+
options?: TaskRunOptions;
|
|
273
|
+
};
|
|
274
|
+
export interface Task<TIdentifier extends string, TInput = void, TOutput = any> {
|
|
275
|
+
/**
|
|
276
|
+
* The id of the task.
|
|
277
|
+
*/
|
|
278
|
+
id: TIdentifier;
|
|
279
|
+
/**
|
|
280
|
+
* Trigger a task with the given payload, and continue without waiting for the result. If you want to wait for the result, use `triggerAndWait`. Returns the id of the triggered task run.
|
|
281
|
+
* @param payload
|
|
282
|
+
* @param options
|
|
283
|
+
* @returns RunHandle
|
|
284
|
+
* - `id` - The id of the triggered task run.
|
|
285
|
+
*/
|
|
286
|
+
trigger: (payload: TInput, options?: TaskRunOptions, requestOptions?: TriggerApiRequestOptions) => Promise<RunHandle<TIdentifier, TInput, TOutput>>;
|
|
287
|
+
/**
|
|
288
|
+
* Batch trigger multiple task runs with the given payloads, and continue without waiting for the results. If you want to wait for the results, use `batchTriggerAndWait`. Returns the id of the triggered batch.
|
|
289
|
+
* @param items
|
|
290
|
+
* @returns InvokeBatchHandle
|
|
291
|
+
* - `batchId` - The id of the triggered batch.
|
|
292
|
+
* - `runs` - The ids of the triggered task runs.
|
|
293
|
+
*/
|
|
294
|
+
batchTrigger: (items: Array<BatchItem<TInput>>, requestOptions?: TriggerApiRequestOptions) => Promise<BatchRunHandle<TIdentifier, TInput, TOutput>>;
|
|
295
|
+
/**
|
|
296
|
+
* Trigger a task with the given payload, and wait for the result. Returns the result of the task run
|
|
297
|
+
* @param payload
|
|
298
|
+
* @param options - Options for the task run
|
|
299
|
+
* @returns TaskRunResult
|
|
300
|
+
* @example
|
|
301
|
+
* ```
|
|
302
|
+
* const result = await task.triggerAndWait({ foo: "bar" });
|
|
303
|
+
*
|
|
304
|
+
* if (result.ok) {
|
|
305
|
+
* console.log(result.output);
|
|
306
|
+
* } else {
|
|
307
|
+
* console.error(result.error);
|
|
308
|
+
* }
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
triggerAndWait: (payload: TInput, options?: TaskRunOptions) => TaskRunPromise<TOutput>;
|
|
312
|
+
/**
|
|
313
|
+
* Batch trigger multiple task runs with the given payloads, and wait for the results. Returns the results of the task runs.
|
|
314
|
+
* @param items
|
|
315
|
+
* @returns BatchResult
|
|
316
|
+
* @example
|
|
317
|
+
* ```
|
|
318
|
+
* const result = await task.batchTriggerAndWait([
|
|
319
|
+
* { payload: { foo: "bar" } },
|
|
320
|
+
* { payload: { foo: "baz" } },
|
|
321
|
+
* ]);
|
|
322
|
+
*
|
|
323
|
+
* for (const run of result.runs) {
|
|
324
|
+
* if (run.ok) {
|
|
325
|
+
* console.log(run.output);
|
|
326
|
+
* } else {
|
|
327
|
+
* console.error(run.error);
|
|
328
|
+
* }
|
|
329
|
+
* }
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
batchTriggerAndWait: (items: Array<BatchItem<TInput>>) => Promise<BatchResult<TOutput>>;
|
|
333
|
+
}
|
|
334
|
+
export type AnyTask = Task<string, any, any>;
|
|
335
|
+
export type TaskPayload<TTask extends AnyTask> = TTask extends Task<string, infer TInput, any> ? TInput : never;
|
|
336
|
+
export type TaskOutput<TTask extends AnyTask> = TTask extends Task<string, any, infer TOutput> ? TOutput : never;
|
|
337
|
+
export type TaskOutputHandle<TTask extends AnyTask> = TTask extends Task<infer TIdentifier, infer TInput, infer TOutput> ? RunHandle<TIdentifier, TOutput, TInput> : never;
|
|
338
|
+
export type TaskBatchOutputHandle<TTask extends AnyTask> = TTask extends Task<infer TIdentifier, infer TInput, infer TOutput> ? BatchRunHandle<TIdentifier, TOutput, TInput> : never;
|
|
339
|
+
export type TaskIdentifier<TTask extends AnyTask> = TTask extends Task<infer TIdentifier, any, any> ? TIdentifier : never;
|
|
340
|
+
export type TriggerJwtOptions = {
|
|
341
|
+
/**
|
|
342
|
+
* The expiration time of the JWT. This can be a string like "1h" or a Date object.
|
|
343
|
+
*
|
|
344
|
+
* Defaults to 1 hour.
|
|
345
|
+
*/
|
|
346
|
+
expirationTime?: number | Date | string;
|
|
347
|
+
};
|
|
348
|
+
export type TaskRunOptions = {
|
|
349
|
+
/**
|
|
350
|
+
* A unique key that can be used to ensure that a task is only triggered once per key.
|
|
351
|
+
*
|
|
352
|
+
* You can use `idempotencyKeys.create` to create an idempotency key first, and then pass it to the task options.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
*
|
|
356
|
+
* ```typescript
|
|
357
|
+
* import { idempotencyKeys, task } from "@trigger.dev/sdk/v3";
|
|
358
|
+
*
|
|
359
|
+
* export const myTask = task({
|
|
360
|
+
* id: "my-task",
|
|
361
|
+
* run: async (payload: any) => {
|
|
362
|
+
* // scoped to the task run by default
|
|
363
|
+
* const idempotencyKey = await idempotencyKeys.create("my-task-key");
|
|
364
|
+
*
|
|
365
|
+
* // Use the idempotency key when triggering child tasks
|
|
366
|
+
* await childTask.triggerAndWait(payload, { idempotencyKey });
|
|
367
|
+
*
|
|
368
|
+
* // scoped globally, does not include the task run ID
|
|
369
|
+
* const globalIdempotencyKey = await idempotencyKeys.create("my-task-key", { scope: "global" });
|
|
370
|
+
*
|
|
371
|
+
* await childTask.triggerAndWait(payload, { idempotencyKey: globalIdempotencyKey });
|
|
372
|
+
*
|
|
373
|
+
* // You can also pass a string directly, which is the same as a global idempotency key
|
|
374
|
+
* await childTask.triggerAndWait(payload, { idempotencyKey: "my-very-unique-key" });
|
|
375
|
+
* }
|
|
376
|
+
* });
|
|
377
|
+
* ```
|
|
378
|
+
*
|
|
379
|
+
* When triggering a task inside another task, we automatically inject the run ID into the key material.
|
|
380
|
+
*
|
|
381
|
+
* If you are triggering a task from your backend, ensure you include some sufficiently unique key material to prevent collisions.
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
*
|
|
385
|
+
* ```typescript
|
|
386
|
+
* import { idempotencyKeys, tasks } from "@trigger.dev/sdk/v3";
|
|
387
|
+
*
|
|
388
|
+
* // Somewhere in your backend
|
|
389
|
+
* const idempotencyKey = await idempotenceKeys.create(["my-task-trigger", "user-123"]);
|
|
390
|
+
* await tasks.trigger("my-task", { foo: "bar" }, { idempotencyKey });
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
393
|
+
*/
|
|
394
|
+
idempotencyKey?: IdempotencyKey | string | string[];
|
|
395
|
+
maxAttempts?: number;
|
|
396
|
+
queue?: TaskRunConcurrencyOptions;
|
|
397
|
+
concurrencyKey?: string;
|
|
398
|
+
/**
|
|
399
|
+
* The delay before the task is executed. This can be a string like "1h" or a Date object.
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* "1h" - 1 hour
|
|
403
|
+
* "30d" - 30 days
|
|
404
|
+
* "15m" - 15 minutes
|
|
405
|
+
* "2w" - 2 weeks
|
|
406
|
+
* "60s" - 60 seconds
|
|
407
|
+
* new Date("2025-01-01T00:00:00Z")
|
|
408
|
+
*/
|
|
409
|
+
delay?: string | Date;
|
|
410
|
+
/**
|
|
411
|
+
* Set a time-to-live for this run. If the run is not executed within this time, it will be removed from the queue and never execute.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
*
|
|
415
|
+
* ```ts
|
|
416
|
+
* await myTask.trigger({ foo: "bar" }, { ttl: "1h" });
|
|
417
|
+
* await myTask.trigger({ foo: "bar" }, { ttl: 60 * 60 }); // 1 hour
|
|
418
|
+
* ```
|
|
419
|
+
*
|
|
420
|
+
* The minimum value is 1 second. Setting the `ttl` to `0` will disable the TTL and the run will never expire.
|
|
421
|
+
*
|
|
422
|
+
* **Note:** Runs in development have a default `ttl` of 10 minutes. You can override this by setting the `ttl` option.
|
|
423
|
+
*/
|
|
424
|
+
ttl?: string | number;
|
|
425
|
+
/**
|
|
426
|
+
* Tags to attach to the run. Tags can be used to filter runs in the dashboard and using the SDK.
|
|
427
|
+
*
|
|
428
|
+
* You can set up to 10 tags per run, they must be less than 128 characters each.
|
|
429
|
+
*
|
|
430
|
+
* We recommend prefixing tags with a namespace using an underscore or colon, like `user_1234567` or `org:9876543`.
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
*
|
|
434
|
+
* ```ts
|
|
435
|
+
* await myTask.trigger({ foo: "bar" }, { tags: ["user:1234567", "org:9876543"] });
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
tags?: RunTags;
|
|
439
|
+
/**
|
|
440
|
+
* Metadata to attach to the run. Metadata can be used to store additional information about the run. Limited to 4KB.
|
|
441
|
+
*/
|
|
442
|
+
metadata?: Record<string, SerializableJson>;
|
|
443
|
+
/**
|
|
444
|
+
* The maximum duration in compute-time seconds that a task run is allowed to run. If the task run exceeds this duration, it will be stopped.
|
|
445
|
+
*
|
|
446
|
+
* This will override the task's maxDuration.
|
|
447
|
+
*
|
|
448
|
+
* Minimum value is 5 seconds
|
|
449
|
+
*/
|
|
450
|
+
maxDuration?: number;
|
|
451
|
+
};
|
|
452
|
+
export type TaskMetadataWithFunctions = TaskMetadata & {
|
|
453
|
+
fns: {
|
|
454
|
+
run: (payload: any, params: RunFnParams<any>) => Promise<any>;
|
|
455
|
+
init?: (payload: any, params: InitFnParams) => Promise<InitOutput>;
|
|
456
|
+
cleanup?: (payload: any, params: RunFnParams<any>) => Promise<void>;
|
|
457
|
+
middleware?: (payload: any, params: MiddlewareFnParams) => Promise<void>;
|
|
458
|
+
handleError?: (payload: any, error: unknown, params: HandleErrorFnParams<any>) => HandleErrorResult;
|
|
459
|
+
onSuccess?: (payload: any, output: any, params: SuccessFnParams<any>) => Promise<void>;
|
|
460
|
+
onFailure?: (payload: any, error: unknown, params: FailureFnParams<any>) => Promise<void>;
|
|
461
|
+
onStart?: (payload: any, params: StartFnParams) => Promise<void>;
|
|
462
|
+
parsePayload?: AnySchemaParseFn;
|
|
463
|
+
};
|
|
464
|
+
};
|
|
465
|
+
export type RunTypes<TTaskIdentifier extends string, TPayload, TOutput> = {
|
|
466
|
+
output: TOutput;
|
|
467
|
+
payload: TPayload;
|
|
468
|
+
taskIdentifier: TTaskIdentifier;
|
|
469
|
+
};
|
|
470
|
+
export type AnyRunTypes = RunTypes<string, any, any>;
|
|
471
|
+
export type InferRunTypes<T> = T extends RunHandle<infer TTaskIdentifier, infer TPayload, infer TOutput> ? RunTypes<TTaskIdentifier, TPayload, TOutput> : T extends Task<infer TTaskIdentifier, infer TPayload, infer TOutput> ? RunTypes<TTaskIdentifier, TPayload, TOutput> : AnyRunTypes;
|
|
472
|
+
export type RunHandleFromTypes<TRunTypes extends AnyRunTypes> = RunHandle<TRunTypes["taskIdentifier"], TRunTypes["payload"], TRunTypes["output"]>;
|
|
473
|
+
export type BatchRunHandleFromTypes<TRunTypes extends AnyRunTypes> = BatchRunHandle<TRunTypes["taskIdentifier"], TRunTypes["payload"], TRunTypes["output"]>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export class SubtaskUnwrapError extends Error {
|
|
2
|
+
taskId;
|
|
3
|
+
runId;
|
|
4
|
+
cause;
|
|
5
|
+
constructor(taskId, runId, subtaskError) {
|
|
6
|
+
if (subtaskError instanceof Error) {
|
|
7
|
+
super(`Error in ${taskId}: ${subtaskError.message}`);
|
|
8
|
+
this.cause = subtaskError;
|
|
9
|
+
this.name = "SubtaskUnwrapError";
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
super(`Error in ${taskId}`);
|
|
13
|
+
this.name = "SubtaskUnwrapError";
|
|
14
|
+
this.cause = subtaskError;
|
|
15
|
+
}
|
|
16
|
+
this.taskId = taskId;
|
|
17
|
+
this.runId = runId;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class TaskRunPromise extends Promise {
|
|
21
|
+
taskId;
|
|
22
|
+
constructor(executor, taskId) {
|
|
23
|
+
super(executor);
|
|
24
|
+
this.taskId = taskId;
|
|
25
|
+
}
|
|
26
|
+
unwrap() {
|
|
27
|
+
return this.then((result) => {
|
|
28
|
+
if (result.ok) {
|
|
29
|
+
return result.output;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
throw new SubtaskUnwrapError(this.taskId, result.id, result.error);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=tasks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tasks.js","sourceRoot":"","sources":["../../../../src/v3/types/tasks.ts"],"names":[],"mappings":"AA2BA,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3B,MAAM,CAAS;IACf,KAAK,CAAS;IACd,KAAK,CAAW;IAEhC,YAAY,MAAc,EAAE,KAAa,EAAE,YAAqB;QAC9D,IAAI,YAAY,YAAY,KAAK,EAAE,CAAC;YAClC,KAAK,CAAC,YAAY,MAAM,KAAK,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC;YACrD,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC;YAC1B,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACnC,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,YAAY,MAAM,EAAE,CAAC,CAAC;YAC5B,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;YACjC,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC;QAC5B,CAAC;QAED,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF;AAED,MAAM,OAAO,cAAkB,SAAQ,OAAyB;IAM3C;IALnB,YACE,QAGS,EACQ,MAAc;QAE/B,KAAK,CAAC,QAAQ,CAAC,CAAC;QAFC,WAAM,GAAN,MAAM,CAAQ;IAGjC,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;YAC1B,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;gBACd,OAAO,MAAM,CAAC,MAAM,CAAC;YACvB,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;YACrE,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Attributes } from "@opentelemetry/api";
|
|
2
2
|
export declare const NULL_SENTINEL = "$@null((";
|
|
3
|
-
export declare
|
|
3
|
+
export declare const CIRCULAR_REFERENCE_SENTINEL = "$@circular((";
|
|
4
|
+
export declare function flattenAttributes(obj: Record<string, unknown> | Array<unknown> | string | boolean | number | null | undefined, prefix?: string, seen?: WeakSet<object>): Attributes;
|
|
4
5
|
export declare function unflattenAttributes(obj: Attributes): Record<string, unknown> | string | number | boolean | null | undefined;
|
|
5
6
|
export declare function primitiveValueOrflattenedAttributes(obj: Record<string, unknown> | Array<unknown> | string | boolean | number | undefined, prefix: string | undefined): Attributes | string | number | boolean | undefined;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export const NULL_SENTINEL = "$@null((";
|
|
2
|
-
export
|
|
2
|
+
export const CIRCULAR_REFERENCE_SENTINEL = "$@circular((";
|
|
3
|
+
export function flattenAttributes(obj, prefix, seen = new WeakSet()) {
|
|
3
4
|
const result = {};
|
|
4
5
|
// Check if obj is null or undefined
|
|
5
6
|
if (obj === undefined) {
|
|
@@ -25,13 +26,22 @@ export function flattenAttributes(obj, prefix) {
|
|
|
25
26
|
result[prefix || ""] = obj.toISOString();
|
|
26
27
|
return result;
|
|
27
28
|
}
|
|
29
|
+
// Check for circular reference
|
|
30
|
+
if (obj !== null && typeof obj === "object" && seen.has(obj)) {
|
|
31
|
+
result[prefix || ""] = CIRCULAR_REFERENCE_SENTINEL;
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
// Add object to seen set
|
|
35
|
+
if (obj !== null && typeof obj === "object") {
|
|
36
|
+
seen.add(obj);
|
|
37
|
+
}
|
|
28
38
|
for (const [key, value] of Object.entries(obj)) {
|
|
29
39
|
const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`;
|
|
30
40
|
if (Array.isArray(value)) {
|
|
31
41
|
for (let i = 0; i < value.length; i++) {
|
|
32
42
|
if (typeof value[i] === "object" && value[i] !== null) {
|
|
33
43
|
// update null check here as well
|
|
34
|
-
Object.assign(result, flattenAttributes(value[i], `${newPrefix}.[${i}]
|
|
44
|
+
Object.assign(result, flattenAttributes(value[i], `${newPrefix}.[${i}]`, seen));
|
|
35
45
|
}
|
|
36
46
|
else {
|
|
37
47
|
if (value[i] === null) {
|
|
@@ -45,7 +55,7 @@ export function flattenAttributes(obj, prefix) {
|
|
|
45
55
|
}
|
|
46
56
|
else if (isRecord(value)) {
|
|
47
57
|
// update null check here
|
|
48
|
-
Object.assign(result, flattenAttributes(value, newPrefix));
|
|
58
|
+
Object.assign(result, flattenAttributes(value, newPrefix, seen));
|
|
49
59
|
}
|
|
50
60
|
else {
|
|
51
61
|
if (typeof value === "number" || typeof value === "string" || typeof value === "boolean") {
|
|
@@ -77,10 +87,16 @@ export function unflattenAttributes(obj) {
|
|
|
77
87
|
const result = {};
|
|
78
88
|
for (const [key, value] of Object.entries(obj)) {
|
|
79
89
|
const parts = key.split(".").reduce((acc, part) => {
|
|
80
|
-
if (part.
|
|
81
|
-
//
|
|
82
|
-
const
|
|
83
|
-
|
|
90
|
+
if (part.startsWith("[") && part.endsWith("]")) {
|
|
91
|
+
// Handle array indices more precisely
|
|
92
|
+
const match = part.match(/^\[(\d+)\]$/);
|
|
93
|
+
if (match && match[1]) {
|
|
94
|
+
acc.push(parseInt(match[1]));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// Remove brackets for non-numeric array keys
|
|
98
|
+
acc.push(part.slice(1, -1));
|
|
99
|
+
}
|
|
84
100
|
}
|
|
85
101
|
else {
|
|
86
102
|
acc.push(part);
|
|
@@ -90,22 +106,23 @@ export function unflattenAttributes(obj) {
|
|
|
90
106
|
let current = result;
|
|
91
107
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
92
108
|
const part = parts[i];
|
|
93
|
-
|
|
109
|
+
const nextPart = parts[i + 1];
|
|
110
|
+
if (!part && part !== 0) {
|
|
94
111
|
continue;
|
|
95
112
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
current[part] = [];
|
|
113
|
+
if (typeof nextPart === "number") {
|
|
114
|
+
// Ensure we create an array for numeric indices
|
|
115
|
+
current[part] = Array.isArray(current[part]) ? current[part] : [];
|
|
100
116
|
}
|
|
101
|
-
else if (
|
|
117
|
+
else if (current[part] === undefined) {
|
|
118
|
+
// Create an object for non-numeric paths
|
|
102
119
|
current[part] = {};
|
|
103
120
|
}
|
|
104
121
|
current = current[part];
|
|
105
122
|
}
|
|
106
123
|
const lastPart = parts[parts.length - 1];
|
|
107
|
-
if (lastPart) {
|
|
108
|
-
current[lastPart] = rehydrateNull(value);
|
|
124
|
+
if (lastPart !== undefined) {
|
|
125
|
+
current[lastPart] = rehydrateNull(rehydrateCircular(value));
|
|
109
126
|
}
|
|
110
127
|
}
|
|
111
128
|
// Convert the result to an array if all top-level keys are numeric indices
|
|
@@ -119,6 +136,12 @@ export function unflattenAttributes(obj) {
|
|
|
119
136
|
}
|
|
120
137
|
return result;
|
|
121
138
|
}
|
|
139
|
+
function rehydrateCircular(value) {
|
|
140
|
+
if (value === CIRCULAR_REFERENCE_SENTINEL) {
|
|
141
|
+
return "[Circular Reference]";
|
|
142
|
+
}
|
|
143
|
+
return value;
|
|
144
|
+
}
|
|
122
145
|
export function primitiveValueOrflattenedAttributes(obj, prefix) {
|
|
123
146
|
if (typeof obj === "string" ||
|
|
124
147
|
typeof obj === "number" ||
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flattenAttributes.js","sourceRoot":"","sources":["../../../../src/v3/utils/flattenAttributes.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"flattenAttributes.js","sourceRoot":"","sources":["../../../../src/v3/utils/flattenAttributes.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAC;AACxC,MAAM,CAAC,MAAM,2BAA2B,GAAG,cAAc,CAAC;AAE1D,MAAM,UAAU,iBAAiB,CAC/B,GAA4F,EAC5F,MAAe,EACf,OAAwB,IAAI,OAAO,EAAE;IAErC,MAAM,MAAM,GAAe,EAAE,CAAC;IAE9B,oCAAoC;IACpC,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QACjB,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,aAAa,CAAC;QACrC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC;QAC3B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC;QAC3B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,OAAO,GAAG,KAAK,SAAS,EAAE,CAAC;QAC7B,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC;QAC3B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,GAAG,YAAY,IAAI,EAAE,CAAC;QACxB,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC;QACzC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,+BAA+B;IAC/B,IAAI,GAAG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