@@ -16,6 +16,8 @@ import {
1616 CursorPagePromise ,
1717 WaitpointTokenItem ,
1818 flattenAttributes ,
19+ WaitpointListTokenItem ,
20+ WaitpointTokenStatus ,
1921} from "@trigger.dev/core/v3" ;
2022import { tracer } from "./tracer.js" ;
2123import { conditionallyImportAndParsePacket } from "@trigger.dev/core/v3/utils/ioSerialization" ;
@@ -120,7 +122,7 @@ function createToken(
120122function listTokens (
121123 params ?: ListWaitpointTokensQueryParams ,
122124 requestOptions ?: ApiRequestOptions
123- ) : CursorPagePromise < typeof WaitpointTokenItem > {
125+ ) : CursorPagePromise < typeof WaitpointListTokenItem > {
124126 const apiClient = apiClientManager . clientOrThrow ( ) ;
125127
126128 const $requestOptions = mergeRequestOptions (
@@ -138,6 +140,26 @@ function listTokens(
138140 return apiClient . listWaitpointTokens ( params , $requestOptions ) ;
139141}
140142
143+ /**
144+ * A waitpoint token that has been retrieved.
145+ *
146+ * If the status is `WAITING`, this means the waitpoint is still pending.
147+ * For `COMPLETED` the `output` will be the data you passed in when completing the waitpoint.
148+ * For `TIMED_OUT` there will be an `error`.
149+ */
150+ export type WaitpointRetrievedToken < T > = {
151+ id : string ;
152+ status : WaitpointTokenStatus ;
153+ completedAt ?: Date ;
154+ timeoutAt ?: Date ;
155+ idempotencyKey ?: string ;
156+ idempotencyKeyExpiresAt ?: Date ;
157+ tags : string [ ] ;
158+ createdAt : Date ;
159+ output ?: T ;
160+ error ?: Error ;
161+ } ;
162+
141163/**
142164 * Retrieves a waitpoint token by its ID.
143165 *
@@ -151,12 +173,12 @@ function listTokens(
151173 * @param token - The token to retrieve.
152174 * This can be a string token ID or an object with an `id` property.
153175 * @param requestOptions - Optional API request options.
154- * @returns The waitpoint token details.
176+ * @returns The waitpoint token details, including the output or error if the waitpoint is completed or timed out .
155177 */
156- function retrieveToken (
178+ async function retrieveToken < T > (
157179 token : string | { id : string } ,
158180 requestOptions ?: ApiRequestOptions
159- ) : ApiPromise < WaitpointTokenItem > {
181+ ) : Promise < WaitpointRetrievedToken < T > > {
160182 const apiClient = apiClientManager . clientOrThrow ( ) ;
161183
162184 const $tokenId = typeof token === "string" ? token : token . id ;
@@ -182,7 +204,36 @@ function retrieveToken(
182204 requestOptions
183205 ) ;
184206
185- return apiClient . retrieveWaitpointToken ( $tokenId , $requestOptions ) ;
207+ const result = await apiClient . retrieveWaitpointToken ( $tokenId , $requestOptions ) ;
208+
209+ const data = result . output
210+ ? await conditionallyImportAndParsePacket (
211+ { data : result . output , dataType : result . outputType ?? "application/json" } ,
212+ apiClient
213+ )
214+ : undefined ;
215+
216+ let error : Error | undefined = undefined ;
217+ let output : T | undefined = undefined ;
218+
219+ if ( result . outputIsError ) {
220+ error = new WaitpointTimeoutError ( data . message ) ;
221+ } else {
222+ output = data as T ;
223+ }
224+
225+ return {
226+ id : result . id ,
227+ status : result . status ,
228+ completedAt : result . completedAt ,
229+ timeoutAt : result . timeoutAt ,
230+ idempotencyKey : result . idempotencyKey ,
231+ idempotencyKeyExpiresAt : result . idempotencyKeyExpiresAt ,
232+ tags : result . tags ,
233+ createdAt : result . createdAt ,
234+ output,
235+ error,
236+ } ;
186237}
187238
188239/**
0 commit comments