Skip to content

Commit 4289d36

Browse files
Cancel workflow (#8)
1 parent f5b2261 commit 4289d36

File tree

7 files changed

+104
-51
lines changed

7 files changed

+104
-51
lines changed

examples/advanced-usage.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import LinkedApi, { LinkedApiError } from 'linkedapi-node';
2+
3+
async function advancedUsageExample(): Promise<void> {
4+
const linkedapi = new LinkedApi({
5+
linkedApiToken: process.env.LINKED_API_TOKEN!,
6+
identificationToken: process.env.IDENTIFICATION_TOKEN!,
7+
});
8+
9+
try {
10+
await customWorkflowExample(linkedapi);
11+
await cancelWorkflowExample(linkedapi);
12+
} catch (error) {
13+
if (error instanceof LinkedApiError) {
14+
console.error('🚨 Linked API Error:', error.message);
15+
console.error('📝 Details:', error.details);
16+
} else {
17+
console.error('💥 Unknown error:', error);
18+
}
19+
}
20+
}
21+
22+
async function customWorkflowExample(linkedapi: LinkedApi): Promise<void> {
23+
console.log('🚀 Linked API custom workflow example starting...');
24+
const workflowId = await linkedapi.customWorkflow.execute({
25+
actionType: 'st.searchPeople',
26+
limit: 3,
27+
filter: {
28+
locations: ["San Francisco"],
29+
},
30+
then: {
31+
actionType: 'st.doForPeople',
32+
then: {
33+
actionType: 'st.openPersonPage',
34+
basicInfo: true,
35+
then: {
36+
actionType: 'st.retrievePersonSkills',
37+
}
38+
}
39+
}
40+
});
41+
console.log('🔍 Workflow started: ', workflowId);
42+
const result = await linkedapi.customWorkflow.result(workflowId);
43+
44+
console.log('✅ Custom workflow executed successfully');
45+
console.log('🔍 Result: ', JSON.stringify(result.data, null, 2));
46+
}
47+
48+
async function cancelWorkflowExample(linkedapi: LinkedApi): Promise<void> {
49+
console.log('🚀 Linked API cancel workflow example starting...');
50+
const workflowId = await linkedapi.searchPeople.execute({
51+
limit: 3,
52+
filter: {
53+
locations: ["San Francisco"],
54+
},
55+
});
56+
console.log('🔍 Workflow started: ', workflowId);
57+
const result = await linkedapi.searchPeople.cancel(workflowId);
58+
console.log('✅ Workflow cancelled: ', result);
59+
}
60+
61+
62+
if (require.main === module) {
63+
advancedUsageExample();
64+
}

examples/custom-workflow.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linkedapi-node",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"description": "Official TypeScript SDK for Linked API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/core/linked-api-http-client.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import {
66
TLinkedApiResponse,
77
} from '../types';
88

9-
export function buildLinkedApiHttpClient(config: TLinkedApiConfig, client: string): HttpClient {
10-
return new LinkedApiHttpClient(config, client);
9+
export function buildLinkedApiHttpClient(
10+
config: TLinkedApiConfig,
11+
client: string,
12+
baseUrl: string = 'https://api.linkedapi.io',
13+
): HttpClient {
14+
return new LinkedApiHttpClient(config, client, baseUrl);
1115
}
1216

1317
class LinkedApiHttpClient extends HttpClient {
1418
private readonly baseUrl: string;
1519
private readonly headers: Record<string, string>;
1620

17-
constructor(config: TLinkedApiConfig, client: string) {
21+
constructor(config: TLinkedApiConfig, client: string, baseUrl: string) {
1822
super();
19-
this.baseUrl = 'https://api.linkedapi.io';
23+
this.baseUrl = baseUrl;
2024
this.headers = {
2125
'Content-Type': 'application/json',
2226
'linked-api-token': config.linkedApiToken,
@@ -89,4 +93,16 @@ class LinkedApiHttpClient extends HttpClient {
8993
this.handleError(error);
9094
}
9195
}
96+
97+
public async delete<T>(url: string): Promise<TLinkedApiResponse<T>> {
98+
try {
99+
const response = await fetch(`${this.baseUrl}${url}`, {
100+
method: 'DELETE',
101+
headers: this.headers,
102+
});
103+
return this.handleResponse<T>(response);
104+
} catch (error) {
105+
this.handleError(error);
106+
}
107+
}
92108
}

src/core/operation.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
LinkedApiError,
55
LinkedApiWorkflowTimeoutError,
66
TLinkedApiErrorType,
7+
TWorkflowCancelResponse,
78
TWorkflowCompletion,
89
TWorkflowResponse,
910
TWorkflowRunningStatus,
@@ -90,6 +91,19 @@ export abstract class Operation<TParams, TResult> {
9091
return this.mapper.mapResponse(result);
9192
}
9293

94+
public async cancel(workflowId: string): Promise<boolean> {
95+
const response = await this.httpClient.delete<TWorkflowCancelResponse>(
96+
`/workflows/${workflowId}`,
97+
);
98+
if (response.error) {
99+
throw new LinkedApiError(response.error.type as TLinkedApiErrorType, response.error.message);
100+
}
101+
if (!response.result) {
102+
throw LinkedApiError.unknownError();
103+
}
104+
return response.result.cancelled;
105+
}
106+
93107
private async getWorkflowResult(workflowId: string): Promise<TWorkflowResponse> {
94108
const response = await this.httpClient.get<TWorkflowResponse>(`/workflows/${workflowId}`);
95109
if (response.error) {

src/types/http-client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import type { TLinkedApiResponse } from '../types/responses';
33
export abstract class HttpClient {
44
public abstract get<T>(url: string): Promise<TLinkedApiResponse<T>>;
55
public abstract post<T>(url: string, data?: unknown): Promise<TLinkedApiResponse<T>>;
6+
public abstract delete<T>(url: string): Promise<TLinkedApiResponse<T>>;
67
}

src/types/workflows.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface TWorkflowFailure {
3030
message: string;
3131
}
3232

33+
export interface TWorkflowCancelResponse {
34+
cancelled: boolean;
35+
}
36+
3337
export interface TWorkflowStatusResponse {
3438
workflowId: string;
3539
workflowStatus: TWorkflowStatus;

0 commit comments

Comments
 (0)