-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpClient.ts
More file actions
124 lines (110 loc) · 4.64 KB
/
HttpClient.ts
File metadata and controls
124 lines (110 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {
Task,
TasksList,
Plugin,
PaginationResponseType,
ClaimOptions,
DefaultAuthProvider,
TrackOptions,
HttpError,
TaskFilters,
} from "../types";
import { version as LIB_VERSION } from '../../package.json';
export class HttpClient {
constructor(private apiUrl: string, private appKey: string) {
}
private buildUrl(path: string, extraParams?: Record<string, string>): string {
const params = new URLSearchParams();
params.set("app_key", this.appKey);
if (extraParams) {
for (const [key, value] of Object.entries(extraParams)) {
if (value !== undefined && value !== null) {
params.set(key, value);
}
}
}
const query = params.toString();
return `${this.apiUrl}${path}${query ? `?${query}` : ""}`;
}
private async request(url: string, options: RequestInit = {}) {
const headers = new Headers(options.headers || {});
headers.set("X-App-JsLib-Version", LIB_VERSION);
return fetch(url, {
...options,
headers,
});
}
private async extractErrorMessage(response: Response, fallback: string): Promise<string> {
try {
const json = await response.json();
return json?.error?.message || json?.message || fallback;
} catch {
return fallback;
}
}
getTasks = async (userID: string, filters?: TaskFilters): Promise<TasksList> => {
const params = new URLSearchParams();
params.set("user_id", userID);
params.set("app_key", this.appKey);
// ---- New filters (optional) ----
// Legacy behavior: default to active-only when filters are not provided.
const isActive = filters?.isActive ?? true;
if (isActive !== "all") {
params.set("is_active", isActive ? "1" : "0");
}
if (filters?.entityType && filters.entityType !== "all") {
params.set("entity_type", filters.entityType); // 'task' | 'deal'
}
if (filters?.pluginIds?.length) {
// If your backend expects repeated params use: for (id of pluginIds) params.append('plugin_ids', String(id))
params.set("plugin_ids", filters.pluginIds.join(","));
}
if (filters?.categories?.length) {
params.set("categories", filters.categories.join(","));
}
params.set("pageSize", String(filters?.pageSize ?? 100));
params.set("currentPage", String(filters?.page ?? 1));
params.set("auth_provider", filters?.authProvider || DefaultAuthProvider);
const url = `${this.apiUrl}/v1/tasks?${params.toString()}`;
const response = await this.request(url);
if (!response.ok) {
const message = await this.extractErrorMessage(response, `Failed to get list of tasks: ${response.statusText}`);
return Promise.reject(new HttpError(message, response.status));
}
return await response.json();
}
getPlugins = async (): Promise<PaginationResponseType<Plugin>> => {
const url = this.buildUrl("/v1/plugins");
const response = await this.request(url);
if (!response.ok) {
const message = await this.extractErrorMessage(response, `Failed to get list of plugins: ${response.statusText}`);
return Promise.reject(new HttpError(message, response.status));
}
return await response.json();
}
trackEvent = async (event: string, taskID: string, userID: string, options?: TrackOptions) => {
const url = this.buildUrl("/v1/track-task-action", {
action: event,
task_id: taskID,
user_id: userID,
auth_provider: options?.authProvider || DefaultAuthProvider,
});
const response = await this.request(url);
if (!response.ok) {
const message = await this.extractErrorMessage(response, `Failed to track event: ${response.statusText}`);
return Promise.reject(new HttpError(message, response.status));
}
}
claimProcess = async (appKey: string, userID: string, task: Task, options?: ClaimOptions) => {
const url = this.buildUrl("/v1/action/claim", {
task_id: task.id,
user_id: userID,
auth_provider: options?.authProvider || DefaultAuthProvider,
});
const response = await this.request(url);
if (!response.ok) {
const message = await this.extractErrorMessage(response, `Failed to process claim: ${response.statusText}`);
return Promise.reject(new HttpError(message, response.status));
}
}
}