Skip to content

Commit

Permalink
build: npm publish
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Oct 7, 2023
1 parent a2fbadd commit 9e0d945
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"packageManager": "[email protected]",
"type": "module",
"scripts": {
"build": "rollup -c"
"build": "rollup -c",
"publish": "pnpm publish -r"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.0",
Expand All @@ -19,4 +20,4 @@
"rollup-plugin-typescript2": "^0.34.1",
"typescript": "5.0.3"
}
}
}
5 changes: 4 additions & 1 deletion packages/gopeed-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"scripts": {
"build": "tsc index.ts"
},
"keywords": [],
"keywords": [
"gopeed",
"gopeed-rest"
],
"author": "",
"license": "ISC",
"devDependencies": {
Expand Down
54 changes: 33 additions & 21 deletions packages/gopeed-rest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { CreateTaskWithRequest, CreateTaskWithResolveResult, ResolveResult, Task, TaskStatus } from '@gopeed/types';
import type {
CreateTaskWithRequest,
CreateTaskWithResolveResult,
ResolveResult,
Task,
TaskStatus,
} from '@gopeed/types';

interface ClientOptions {
host: string;
token: string;
}

interface Result<T> {
code: number;
msg: string;
data: T;
}

class ApiError extends Error {
code: number;
msg: string;
Expand All @@ -25,33 +37,33 @@ class Client {

/**
* Resolve the download request
* @param request
* @param request - The request to create a new download task
* @returns
*/
public async resole(request: Request): Promise<ResolveResult> {
return await this.doRequest<ResolveResult>('POST', '/api/v1/resolve', {
return this.doRequest<ResolveResult>('POST', '/api/v1/resolve', {
data: request,
});
}

/**
* Create a new download task
* @param request
* @param request - The request to create a new download task
* @returns
*/
public async createTask(request: CreateTaskWithResolveResult | CreateTaskWithRequest): Promise<string> {
return await this.doRequest<string>('POST', '/api/v1/tasks', {
return this.doRequest<string>('POST', '/api/v1/tasks', {
data: request,
});
}

/**
* Get task list
* @param statuses
* @param statuses - Filter by task status
* @returns
*/
public async getTasks(statuses: TaskStatus[] = []): Promise<Task[]> {
return await this.doRequest<Task[]>('GET', '/api/v1/tasks', {
return this.doRequest<Task[]>('GET', '/api/v1/tasks', {
query: {
status: statuses.join(','),
},
Expand All @@ -60,41 +72,41 @@ class Client {

/**
* Pause a task
* @param id
* @param id - Task id
*/
public async pauseTask(id: string): Promise<void> {
return await this.doRequest('PUT', `/api/v1/tasks/${id}/pause`);
await this.doRequest('PUT', `/api/v1/tasks/${id}/pause`);
}

/**
* Continue a task
* @param id
* @param id - Task id
*/
public async continueTask(id: string): Promise<void> {
return await this.doRequest('PUT', `/api/v1/tasks/${id}/continue`);
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');
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');
await this.doRequest('PUT', '/api/v1/tasks/continue');
}

/**
* Delete a task
* @param id
* @param force
* @param id - Task id
* @param force - Delete files
*/
public async deleteTask(id: string, force = false): Promise<void> {
return await this.doRequest('DELETE', `/api/v1/tasks/${id}?force=${force}`);
await this.doRequest('DELETE', `/api/v1/tasks/${id}?force=${force}`);
}

private async doRequest<T>(
Expand All @@ -107,7 +119,7 @@ class Client {
const queryParams = Object.entries(query)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value as string)}`)
.join('&');
url += '?' + queryParams;
url += `?${queryParams}`;
}
const headers: Record<string, string> = {
'Content-Type': 'application/json',
Expand All @@ -117,18 +129,18 @@ class Client {
}
try {
const resp = await fetch(url, {
method: method,
headers: headers,
method,
headers,
body: data ? JSON.stringify(data) : undefined,
});
if (resp.status !== 200) {
throw new ApiError(1000, await resp.text());
}
const result = await resp.json();
const result = (await resp.json()) as Result<T>;
if (result.code !== 0) {
throw new ApiError(result.code, result.msg);
}
return result.data as T;
return result.data;
} catch (error) {
throw new ApiError(1000, (error as Error).message);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/gopeed-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"scripts": {
"build": "tsc src/index.ts"
},
"keywords": [],
"keywords": [
"gopeed"
],
"author": "",
"license": "ISC"
}
}
7 changes: 5 additions & 2 deletions packages/gopeed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"keywords": [
"gopeed",
"gopeed-extension"
],
"author": "",
"license": "ISC",
"devDependencies": {
"@gopeed/types": "workspace:*"
}
}
}
11 changes: 11 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const packages = fs.readdirSync(packagesDir).filter((f) => {
return true;
});

// sort packages, gopeed-types should be first
packages.sort((a, b) => {
if (a === 'gopeed-types') {
return -1;
}
if (b === 'gopeed-types') {
return 1;
}
return a.localeCompare(b);
});

export default packages.map((pkgName) => {
const pkg = JSON.parse(fs.readFileSync(path.join(packagesDir, pkgName, 'package.json'), 'utf-8'));
return {
Expand Down

0 comments on commit 9e0d945

Please sign in to comment.