diff --git a/examples/advanced-usage.ts b/examples/advanced-usage.ts new file mode 100644 index 0000000..a0c77a0 --- /dev/null +++ b/examples/advanced-usage.ts @@ -0,0 +1,64 @@ +import LinkedApi, { LinkedApiError } from 'linkedapi-node'; + +async function advancedUsageExample(): Promise { + const linkedapi = new LinkedApi({ + linkedApiToken: process.env.LINKED_API_TOKEN!, + identificationToken: process.env.IDENTIFICATION_TOKEN!, + }); + + try { + await customWorkflowExample(linkedapi); + await cancelWorkflowExample(linkedapi); + } catch (error) { + if (error instanceof LinkedApiError) { + console.error('🚨 Linked API Error:', error.message); + console.error('📝 Details:', error.details); + } else { + console.error('💥 Unknown error:', error); + } + } +} + +async function customWorkflowExample(linkedapi: LinkedApi): Promise { + console.log('🚀 Linked API custom workflow example starting...'); + const workflowId = await linkedapi.customWorkflow.execute({ + actionType: 'st.searchPeople', + limit: 3, + filter: { + locations: ["San Francisco"], + }, + then: { + actionType: 'st.doForPeople', + then: { + actionType: 'st.openPersonPage', + basicInfo: true, + then: { + actionType: 'st.retrievePersonSkills', + } + } + } + }); + console.log('🔍 Workflow started: ', workflowId); + const result = await linkedapi.customWorkflow.result(workflowId); + + console.log('✅ Custom workflow executed successfully'); + console.log('🔍 Result: ', JSON.stringify(result.data, null, 2)); +} + +async function cancelWorkflowExample(linkedapi: LinkedApi): Promise { + console.log('🚀 Linked API cancel workflow example starting...'); + const workflowId = await linkedapi.searchPeople.execute({ + limit: 3, + filter: { + locations: ["San Francisco"], + }, + }); + console.log('🔍 Workflow started: ', workflowId); + const result = await linkedapi.searchPeople.cancel(workflowId); + console.log('✅ Workflow cancelled: ', result); +} + + +if (require.main === module) { + advancedUsageExample(); +} \ No newline at end of file diff --git a/examples/custom-workflow.ts b/examples/custom-workflow.ts deleted file mode 100644 index 862fb46..0000000 --- a/examples/custom-workflow.ts +++ /dev/null @@ -1,46 +0,0 @@ -import LinkedApi, { LinkedApiError } from 'linkedapi-node'; - -async function customWorkflowExample(): Promise { - - const linkedapi = new LinkedApi({ - linkedApiToken: process.env.LINKED_API_TOKEN!, - identificationToken: process.env.IDENTIFICATION_TOKEN!, - }); - - try { - console.log('🚀 Linked API custom workflow example starting...'); - const workflowId = await linkedapi.customWorkflow.execute({ - actionType: 'st.searchPeople', - limit: 3, - filter: { - locations: ["San Francisco"], - }, - then: { - actionType: 'st.doForPeople', - then: { - actionType: 'st.openPersonPage', - basicInfo: true, - then: { - actionType: 'st.retrievePersonSkills', - } - } - } - }); - console.log('🔍 Workflow started: ', workflowId); - const result = await linkedapi.customWorkflow.result(workflowId); - - console.log('✅ Custom workflow executed successfully'); - console.log('🔍 Result: ', JSON.stringify(result.data, null, 2)); - } catch (error) { - if (error instanceof LinkedApiError) { - console.error('🚨 Linked API Error:', error.message); - console.error('📝 Details:', error.details); - } else { - console.error('💥 Unknown error:', error); - } - } -} - -if (require.main === module) { - customWorkflowExample(); -} \ No newline at end of file diff --git a/package.json b/package.json index 2a3fc4f..3cf4847 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "linkedapi-node", - "version": "1.2.3", + "version": "1.2.4", "description": "Official TypeScript SDK for Linked API", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/core/linked-api-http-client.ts b/src/core/linked-api-http-client.ts index 222c633..dbe09ad 100644 --- a/src/core/linked-api-http-client.ts +++ b/src/core/linked-api-http-client.ts @@ -6,17 +6,21 @@ import { TLinkedApiResponse, } from '../types'; -export function buildLinkedApiHttpClient(config: TLinkedApiConfig, client: string): HttpClient { - return new LinkedApiHttpClient(config, client); +export function buildLinkedApiHttpClient( + config: TLinkedApiConfig, + client: string, + baseUrl: string = 'https://api.linkedapi.io', +): HttpClient { + return new LinkedApiHttpClient(config, client, baseUrl); } class LinkedApiHttpClient extends HttpClient { private readonly baseUrl: string; private readonly headers: Record; - constructor(config: TLinkedApiConfig, client: string) { + constructor(config: TLinkedApiConfig, client: string, baseUrl: string) { super(); - this.baseUrl = 'https://api.linkedapi.io'; + this.baseUrl = baseUrl; this.headers = { 'Content-Type': 'application/json', 'linked-api-token': config.linkedApiToken, @@ -89,4 +93,16 @@ class LinkedApiHttpClient extends HttpClient { this.handleError(error); } } + + public async delete(url: string): Promise> { + try { + const response = await fetch(`${this.baseUrl}${url}`, { + method: 'DELETE', + headers: this.headers, + }); + return this.handleResponse(response); + } catch (error) { + this.handleError(error); + } + } } diff --git a/src/core/operation.ts b/src/core/operation.ts index d2a248e..10f1368 100644 --- a/src/core/operation.ts +++ b/src/core/operation.ts @@ -4,6 +4,7 @@ import { LinkedApiError, LinkedApiWorkflowTimeoutError, TLinkedApiErrorType, + TWorkflowCancelResponse, TWorkflowCompletion, TWorkflowResponse, TWorkflowRunningStatus, @@ -90,6 +91,19 @@ export abstract class Operation { return this.mapper.mapResponse(result); } + public async cancel(workflowId: string): Promise { + const response = await this.httpClient.delete( + `/workflows/${workflowId}`, + ); + if (response.error) { + throw new LinkedApiError(response.error.type as TLinkedApiErrorType, response.error.message); + } + if (!response.result) { + throw LinkedApiError.unknownError(); + } + return response.result.cancelled; + } + private async getWorkflowResult(workflowId: string): Promise { const response = await this.httpClient.get(`/workflows/${workflowId}`); if (response.error) { diff --git a/src/types/http-client.ts b/src/types/http-client.ts index 97a2d8c..3481ca5 100644 --- a/src/types/http-client.ts +++ b/src/types/http-client.ts @@ -3,4 +3,5 @@ import type { TLinkedApiResponse } from '../types/responses'; export abstract class HttpClient { public abstract get(url: string): Promise>; public abstract post(url: string, data?: unknown): Promise>; + public abstract delete(url: string): Promise>; } diff --git a/src/types/workflows.ts b/src/types/workflows.ts index b62263b..dd81ee2 100644 --- a/src/types/workflows.ts +++ b/src/types/workflows.ts @@ -30,6 +30,10 @@ export interface TWorkflowFailure { message: string; } +export interface TWorkflowCancelResponse { + cancelled: boolean; +} + export interface TWorkflowStatusResponse { workflowId: string; workflowStatus: TWorkflowStatus;