Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/advanced-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import LinkedApi, { LinkedApiError } from 'linkedapi-node';

async function advancedUsageExample(): Promise<void> {
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<void> {
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<void> {
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();
}
46 changes: 0 additions & 46 deletions examples/custom-workflow.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 20 additions & 4 deletions src/core/linked-api-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;

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,
Expand Down Expand Up @@ -89,4 +93,16 @@ class LinkedApiHttpClient extends HttpClient {
this.handleError(error);
}
}

public async delete<T>(url: string): Promise<TLinkedApiResponse<T>> {
try {
const response = await fetch(`${this.baseUrl}${url}`, {
method: 'DELETE',
headers: this.headers,
});
return this.handleResponse<T>(response);
} catch (error) {
this.handleError(error);
}
}
}
14 changes: 14 additions & 0 deletions src/core/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
LinkedApiError,
LinkedApiWorkflowTimeoutError,
TLinkedApiErrorType,
TWorkflowCancelResponse,
TWorkflowCompletion,
TWorkflowResponse,
TWorkflowRunningStatus,
Expand Down Expand Up @@ -90,6 +91,19 @@ export abstract class Operation<TParams, TResult> {
return this.mapper.mapResponse(result);
}

public async cancel(workflowId: string): Promise<boolean> {
const response = await this.httpClient.delete<TWorkflowCancelResponse>(
`/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<TWorkflowResponse> {
const response = await this.httpClient.get<TWorkflowResponse>(`/workflows/${workflowId}`);
if (response.error) {
Expand Down
1 change: 1 addition & 0 deletions src/types/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import type { TLinkedApiResponse } from '../types/responses';
export abstract class HttpClient {
public abstract get<T>(url: string): Promise<TLinkedApiResponse<T>>;
public abstract post<T>(url: string, data?: unknown): Promise<TLinkedApiResponse<T>>;
public abstract delete<T>(url: string): Promise<TLinkedApiResponse<T>>;
}
4 changes: 4 additions & 0 deletions src/types/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface TWorkflowFailure {
message: string;
}

export interface TWorkflowCancelResponse {
cancelled: boolean;
}

export interface TWorkflowStatusResponse {
workflowId: string;
workflowStatus: TWorkflowStatus;
Expand Down