-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
402 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Gopeed Javascript Monerepo | ||
|
||
Gopeed development kit for javascript. | ||
|
||
## Build | ||
|
||
```bash | ||
pnpm install | ||
pnpm run build | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { CreateTaskWithRequest, CreateTaskWithResolveResult, ResolveResult, Task, TaskStatus } from '@gopeed/types'; | ||
|
||
interface ClientOptions { | ||
host: string; | ||
token: string; | ||
} | ||
|
||
class ApiError extends Error { | ||
code: number; | ||
msg: string; | ||
|
||
constructor(code: number, msg: string) { | ||
super(msg); | ||
this.code = code; | ||
this.msg = msg; | ||
} | ||
} | ||
|
||
class Client { | ||
private options: ClientOptions; | ||
|
||
constructor(options: ClientOptions = { host: 'http://127.0.0.1:9999', token: '' }) { | ||
this.options = options; | ||
} | ||
|
||
/** | ||
* Resolve the download request | ||
* @param request | ||
* @returns | ||
*/ | ||
public async resole(request: Request): Promise<ResolveResult> { | ||
return await this.doRequest<ResolveResult>('POST', '/api/v1/resolve', { | ||
data: request, | ||
}); | ||
} | ||
|
||
/** | ||
* Create a new download task | ||
* @param request | ||
* @returns | ||
*/ | ||
public async createTask(request: CreateTaskWithResolveResult | CreateTaskWithRequest): Promise<string> { | ||
return await this.doRequest<string>('POST', '/api/v1/tasks', { | ||
data: request, | ||
}); | ||
} | ||
|
||
/** | ||
* Get task list | ||
* @param statuses | ||
* @returns | ||
*/ | ||
public async getTasks(statuses: TaskStatus[] = []): Promise<Task[]> { | ||
return await this.doRequest<Task[]>('GET', '/api/v1/tasks', { | ||
query: { | ||
status: statuses.join(','), | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Pause a task | ||
* @param id | ||
*/ | ||
public async pauseTask(id: string): Promise<void> { | ||
return await this.doRequest('PUT', `/api/v1/tasks/${id}/pause`); | ||
} | ||
|
||
/** | ||
* Continue a task | ||
* @param id | ||
*/ | ||
public async continueTask(id: string): Promise<void> { | ||
return await this.doRequest('PUT', `/api/v1/tasks/${id}/continue`); | ||
} | ||
|
||
/** | ||
* Pause all tasks | ||
*/ | ||
public async pauseAllTasks(): Promise<void> { | ||
return await this.doRequest('PUT', '/api/v1/tasks/pause'); | ||
} | ||
|
||
/** | ||
* Continue all tasks | ||
*/ | ||
public async continueAllTasks(): Promise<void> { | ||
return await this.doRequest('PUT', '/api/v1/tasks/continue'); | ||
} | ||
|
||
/** | ||
* Delete a task | ||
* @param id | ||
* @param force | ||
*/ | ||
public async deleteTask(id: string, force = false): Promise<void> { | ||
return await this.doRequest('DELETE', `/api/v1/tasks/${id}?force=${force}`); | ||
} | ||
|
||
private async doRequest<T>( | ||
method: string, | ||
path: string, | ||
{ query, data }: { query?: object; data?: object } = {} | ||
): Promise<T> { | ||
let url = this.options.host + path; | ||
if (query) { | ||
const queryParams = Object.entries(query) | ||
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value as string)}`) | ||
.join('&'); | ||
url += '?' + queryParams; | ||
} | ||
const headers: Record<string, string> = { | ||
'Content-Type': 'application/json', | ||
}; | ||
if (this.options.token) { | ||
headers['X-Api-Token'] = this.options.token; | ||
} | ||
try { | ||
const resp = await fetch(url, { | ||
method: method, | ||
headers: headers, | ||
body: data ? JSON.stringify(data) : undefined, | ||
}); | ||
if (resp.status !== 200) { | ||
throw new ApiError(1000, await resp.text()); | ||
} | ||
const result = await resp.json(); | ||
if (result.code !== 0) { | ||
throw new ApiError(result.code, result.msg); | ||
} | ||
return result.data as T; | ||
} catch (error) { | ||
throw new ApiError(1000, (error as Error).message); | ||
} | ||
} | ||
} | ||
|
||
export { Client }; | ||
export default Client; |
12 changes: 9 additions & 3 deletions
12
packages/model/package.json → packages/gopeed-types/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.