Skip to content

Commit 0a4e9ad

Browse files
authored
Merge pull request #14 from yepcode/feature/enrich-execute-response
Enrich executeProcessSync response with executionId
2 parents a8a9de4 + a1e3383 commit 0a4e9ad

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

src/api/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export interface Execution {
8282
export interface ExecutionId {
8383
executionId: string;
8484
}
85+
export interface ExecuteProcessSyncResponse {
86+
executionId: string;
87+
data: any;
88+
}
8589
export interface ExecutionLogsPaginatedResult {
8690
hasNextPage?: boolean;
8791
page?: number;

src/api/yepcodeApi.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ExecutionsPaginatedResult,
99
Execution,
1010
ExecutionLogsPaginatedResult,
11+
ExecuteProcessSyncResponse,
1112
Schedule,
1213
ScheduledProcessInput,
1314
SchedulesPaginatedResult,
@@ -232,11 +233,16 @@ export class YepCodeApi {
232233
return date as string;
233234
}
234235

235-
private async request<T>(
236+
private async request(
236237
method: string,
237238
endpoint: string,
238-
options: RequestOptions = {}
239-
): Promise<T> {
239+
options: RequestOptions = {},
240+
settings: {
241+
includeHeaders?: boolean;
242+
} = {
243+
includeHeaders: false,
244+
}
245+
): Promise<any> {
240246
if (!this.accessToken || this.isAccessTokenExpired(this.accessToken)) {
241247
await this.getAccessToken();
242248
}
@@ -297,17 +303,24 @@ export class YepCodeApi {
297303
if (options.responseType === "stream") {
298304
if (typeof response.body?.getReader === "function") {
299305
// @ts-ignore
300-
return Readable.fromWeb(response.body) as T;
306+
return Readable.fromWeb(response.body);
301307
}
302308
// @ts-ignore
303-
return response.body as T;
309+
return response.body;
304310
}
305311

306312
const responseText = await response.text();
307313
try {
308-
return JSON.parse(responseText);
314+
const data = JSON.parse(responseText);
315+
if (settings.includeHeaders) {
316+
return {
317+
data,
318+
headers: response.headers,
319+
};
320+
}
321+
return data;
309322
} catch (e) {
310-
return responseText as T;
323+
return responseText;
311324
}
312325
}
313326

@@ -452,21 +465,31 @@ export class YepCodeApi {
452465
comment?: string;
453466
settings?: any;
454467
} = {}
455-
): Promise<any> {
468+
): Promise<ExecuteProcessSyncResponse> {
456469
const headers: Record<string, string> = {};
457470
if (options.initiatedBy) {
458471
headers["Yep-Initiated-By"] = options.initiatedBy;
459472
}
460473

461-
return this.request("POST", `/processes/${processIdOrSlug}/execute-sync`, {
462-
data: {
463-
parameters: JSON.stringify(parameters),
464-
tag: options.tag,
465-
comment: options.comment,
466-
settings: options.settings,
474+
const { data, headers: responseHeaders } = await this.request(
475+
"POST",
476+
`/processes/${processIdOrSlug}/execute-sync`,
477+
{
478+
data: {
479+
parameters: JSON.stringify(parameters),
480+
tag: options.tag,
481+
comment: options.comment,
482+
settings: options.settings,
483+
},
484+
headers,
467485
},
468-
headers,
469-
});
486+
{ includeHeaders: true }
487+
);
488+
489+
return {
490+
executionId: responseHeaders.get("yep-execution-id"),
491+
data,
492+
};
470493
}
471494

472495
async createSchedule(
@@ -518,11 +541,7 @@ export class YepCodeApi {
518541
}
519542

520543
async rerunExecution(id: string): Promise<string> {
521-
const response = await this.request<{ executionId: string }>(
522-
"POST",
523-
`/executions/${id}/rerun`
524-
);
525-
return response.executionId;
544+
return this.request("POST", `/executions/${id}/rerun`);
526545
}
527546

528547
async killExecution(id: string): Promise<void> {

0 commit comments

Comments
 (0)