Skip to content

Commit

Permalink
🐛 fix(index.js): add missing semicolon after PreProcessTask function …
Browse files Browse the repository at this point in the history
…call in createTask method

✨ feat(index.js): add default value for appId parameter in createTask method

🔍 chore(index.js): improve code readability by adding line breaks and indentation
🐛 fix(index.js): fix errorId property name in solve method return object
✨ feat(index.js): add verbose message when task is solved or failed
  • Loading branch information
VeeBack committed Apr 19, 2023
1 parent 4a314e0 commit 418d13a
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ class CapSolver {

async verbose(log) {
if (this.#options?.verbose || false) {
const verboseIdentifier = this.#options?.verboseIdentifier ? `${this.#options?.verboseIdentifier} ` : "";
const verboseIdentifier = this.#options?.verboseIdentifier
? `${this.#options?.verboseIdentifier} `
: "";
console.log(`${verboseIdentifier}${log}`);
}
}
Expand All @@ -294,6 +296,17 @@ class CapSolver {
* @typedef {(RecognitionTask|TokenTask)} CapSolverTask
*/

/**
* @typedef {Object} CapSolverBalanceResponse
* @property {(0|1)} errorId Error message: 0 - no error, 1 - with error
* @property {String} errorCode https://docs.capsolver.com/guide/api-error.html
* @property {String} errorDescription Short description of the error
* @property {Number} balance Balance in USD
* @property {any[]} packages List of Monthly/Weekly Packages
*/
/**
* @returns {CapSolverBalanceResponse}
*/
async getBalance() {
const response = await this.#client.request({
method: "POST",
Expand All @@ -319,13 +332,13 @@ class CapSolver {
* @returns {CapSolverCreateTaskResponse}
*/
async createTask(task) {
PreProcessTask(task)
PreProcessTask(task);
const response = await this.#client.request({
method: "POST",
url: "/createTask",
data: JSON.stringify({
clientKey: this.#clientKey,
appId: this.#options.appId || null,
appId: this.#options.appId || "6B27D516-3A6F-4E13-9DED-F517295F5F89",
task,
}),
});
Expand Down Expand Up @@ -370,29 +383,31 @@ class CapSolver {
* @returns {Promise<CapSolverSolveTaskResult>}
*/
async solve(task) {
if (!task) return {
errorCode: 'ERROR_INVALID_TASK_DATA',
errorDescription: 'Missing task data.',
errorId: 1
}
if (!task)
return {
errorCode: "ERROR_INVALID_TASK_DATA",
errorDescription: "Missing task data.",
errorId: 1,
};

const response = await this.createTask(task);
if (response.status === "ready" || response.errorId === 1) return response;
const taskId = response.taskId;

this.verbose(`[${taskId}] Created [${task.type}].`)
this.verbose(`[${taskId}] Created [${task.type}].`);

while (true) {
const result = await this.getTaskResult(taskId);
if (result.status === "ready" || result.errorId === 1) {
const verboseMessage = result?.status === "ready" ? `Solved!` : `Failed!`;
const verboseMessage =
result?.status === "ready" ? `Solved!` : `Failed!`;
this.verbose(`[${taskId}] ${verboseMessage}`);

return result;
}

this.verbose(`[${taskId}] Waiting 2500ms...`);
await this.delay(2500)
await this.delay(2500);
}
}
}
Expand Down

0 comments on commit 418d13a

Please sign in to comment.