diff --git a/packages/core/package.json b/packages/core/package.json index 327ed9d..2864702 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "make-traffic-integration-core", - "version": "0.2.7", + "version": "0.3.4", "description": "Core Library for Make Traffic task manager", "scripts": { "build": "webpack" @@ -8,7 +8,7 @@ "exports": { ".": "./dist/bundle.esm.js" }, - "types": "dist/index.d.ts", + "types": "dist/src/index.d.ts", "type": "module", "files": [ "dist/" diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 33880be..5f7b085 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,9 +1,8 @@ export * from "./types/index"; -import {Config, EventPayloadMap} from "./types"; -import {HttpClient, TaskFilters} from "./services/HttpClient"; +import { HttpClient, TaskFilters} from "./services/HttpClient"; import {EventRegister} from "./services/EventRegister"; import {TaskProcessor} from "./services/TaskProcessor"; -import {Task, TasksList, Events} from "./types"; +import {Config, EventPayloadMap, Task, TasksList, Events, ProcessMethodOptions, ClaimOptions} from "./types"; import {PluginsManager} from "./services/PluginsManager"; export class TaskManagerApp { @@ -27,12 +26,12 @@ export class TaskManagerApp { return this.httpClient.getTasks(userID, filters); } - goProcess = async (userID: string, task: Task) => { - return this.taskProcessor.goProcess(userID, task); + goProcess = async (userID: string, task: Task, options?: ProcessMethodOptions) => { + return this.taskProcessor.goProcess(userID, task, options); } - claimProcess = async (userID: string, task: Task) => { - return this.taskProcessor.claimProcess(userID, task); + claimProcess = async (userID: string, task: Task, options?: ClaimOptions) => { + return this.taskProcessor.claimProcess(userID, task, options); } subscribe = ( diff --git a/packages/core/src/services/HttpClient.ts b/packages/core/src/services/HttpClient.ts index 14fe414..b28d8e0 100644 --- a/packages/core/src/services/HttpClient.ts +++ b/packages/core/src/services/HttpClient.ts @@ -1,4 +1,12 @@ -import {Task, TasksList, Plugin, PaginationResponseType} from "../types"; +import { + Task, + TasksList, + Plugin, + PaginationResponseType, + ClaimOptions, + DefaultAuthProvider, + TrackOptions +} from "../types"; import { version as LIB_VERSION } from '../../package.json'; export class HttpError extends Error { @@ -17,6 +25,7 @@ export interface TaskFilters { categories?: string[]; // e.g., ['default','partners'] page?: number; pageSize?: number; + authProvider?: string; } export class HttpClient { @@ -71,6 +80,7 @@ export class HttpClient { } 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); @@ -91,11 +101,12 @@ export class HttpClient { return await response.json(); } - trackEvent = async (event: string, taskID: string, userID: string) => { + 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) { @@ -103,10 +114,11 @@ export class HttpClient { } } - claimProcess = async (appKey: string, userID: string, task: Task) => { + 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 && response.status === 409) { diff --git a/packages/core/src/services/PluginsManager.ts b/packages/core/src/services/PluginsManager.ts index 009a132..1181e38 100644 --- a/packages/core/src/services/PluginsManager.ts +++ b/packages/core/src/services/PluginsManager.ts @@ -1,6 +1,7 @@ -import {PluginProcessMethod, Plugin} from "../types"; +import {Plugin, PluginProcessMethod, PluginVerifyMethod} from "../types"; import {HttpClient} from "./HttpClient"; + export class PluginsManager { private isScriptsUploaded = false; private userPlugins: Plugin[] = []; @@ -122,7 +123,7 @@ export class PluginsManager { return !!plugin.scriptVerifyEndpoint; } - getVerifyEndpointByID = (id: number): PluginProcessMethod => { + getVerifyEndpointByID = (id: number): PluginVerifyMethod => { const plugin = this.userPlugins.find(one => one.id === id); if (!plugin) { throw new Error(`Plugin ${id} is not found`); diff --git a/packages/core/src/services/TaskProcessor.ts b/packages/core/src/services/TaskProcessor.ts index 4a0318e..821d8e3 100644 --- a/packages/core/src/services/TaskProcessor.ts +++ b/packages/core/src/services/TaskProcessor.ts @@ -1,7 +1,7 @@ -import {Task, Events} from "../types"; +import {Task, Events, ProcessMethodOptions, ClaimOptions} from "../types"; import {PluginsManager} from "./PluginsManager"; import {EventRegister} from "./EventRegister"; -import {HttpClient} from "./HttpClient"; +import { HttpClient} from "./HttpClient"; export class TaskProcessor { constructor( @@ -12,17 +12,17 @@ export class TaskProcessor { ) { } - goProcess = async (userID: string, task: Task) => { + goProcess = async (userID: string, task: Task, options?: ProcessMethodOptions) => { const processMethod = this.pluginsManager.getProcessEndpointByID(task.plugin.id); - return processMethod(this.appKey, userID, task) + return processMethod(this.appKey, userID, task, options) } - claimProcess = async (userID: string, task: Task) => { + claimProcess = async (userID: string, task: Task, options?: ClaimOptions) => { const run = this.pluginsManager.hasVerifyEndpointByID(task.plugin.id) ? this.pluginsManager.getVerifyEndpointByID(task.plugin.id) : this.httpClient.claimProcess.bind(this.httpClient); - return run(this.appKey, userID, task).then( + return run(this.appKey, userID, task, options).then( () => this.eventRegister.emit(Events.TaskClaimSucceed, task), ).catch((err) => { if (err.name === 'HttpError' && err.statusCode === 409) { diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts index 69aabfa..6081a63 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -5,4 +5,5 @@ export * from "./plugin"; export * from "./plugins/custom"; export * from "./plugins/redirect-timeout"; export * from "./plugins/action-based"; -export * from "./plugins/interfaces"; \ No newline at end of file +export * from "./plugins/interfaces"; +export * from "./options" \ No newline at end of file diff --git a/packages/core/src/types/options.ts b/packages/core/src/types/options.ts new file mode 100644 index 0000000..a091bef --- /dev/null +++ b/packages/core/src/types/options.ts @@ -0,0 +1,13 @@ +export const DefaultAuthProvider = 'telegram'; + +export type TrackOptions = { + authProvider?: string; +} + +export type ProcessMethodOptions = { + authProvider?: string; +} + +export interface ClaimOptions { + authProvider?: string; +} \ No newline at end of file diff --git a/packages/core/src/types/plugin.ts b/packages/core/src/types/plugin.ts index 8ee96b4..ed78675 100644 --- a/packages/core/src/types/plugin.ts +++ b/packages/core/src/types/plugin.ts @@ -1,4 +1,5 @@ import {Task} from "./task"; +import {ClaimOptions, ProcessMethodOptions} from './options'; export interface Plugin { id: number @@ -11,4 +12,5 @@ export interface Plugin { scriptVerifyEndpoint?: string } -export type PluginProcessMethod = (appKey: string, userID: string, task: Task) => Promise; +export type PluginProcessMethod = (appKey: string, userID: string, task: Task, options?: ProcessMethodOptions) => Promise; +export type PluginVerifyMethod = (appKey: string, userID: string, task: Task, options?: ClaimOptions) => Promise;