|
| 1 | +import fs from 'node:fs'; |
| 2 | +import FormData from 'form-data'; |
| 3 | +import { BaseService } from './base'; |
| 4 | + |
| 5 | +import { BrowserExtension, UploadExtensionResponse, ExtensionDetail, ExtensionListItem } from '../types'; |
| 6 | + |
| 7 | +export class ExtensionService extends BaseService implements BrowserExtension { |
| 8 | + constructor(apiKey: string, baseUrl: string, timeout: number) { |
| 9 | + super(apiKey, baseUrl, timeout); |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * Validates the file path and extracts the file name. |
| 14 | + * @param filePath The path to the file. |
| 15 | + * @returns The extracted file name. |
| 16 | + * @throws Error if the file suffix is not valid. |
| 17 | + */ |
| 18 | + private getFileName(filePath: string): string { |
| 19 | + const validSuffixes = ['.zip']; |
| 20 | + const fileSuffix = filePath.slice(filePath.lastIndexOf('.')).toLowerCase(); |
| 21 | + if (!validSuffixes.includes(fileSuffix)) { |
| 22 | + throw new Error(`Invalid file suffix: ${fileSuffix}. Supported suffixes: ${validSuffixes.join(', ')}`); |
| 23 | + } |
| 24 | + |
| 25 | + return filePath.slice(filePath.lastIndexOf('/') + 1); |
| 26 | + } |
| 27 | + |
| 28 | + async upload(filePath: string, name: string): Promise<UploadExtensionResponse> { |
| 29 | + const fileName = this.getFileName(filePath); |
| 30 | + const fileStream = fs.createReadStream(filePath); |
| 31 | + |
| 32 | + const formData = new FormData(); |
| 33 | + formData.append('file', fileStream, fileName); |
| 34 | + formData.append('name', name); |
| 35 | + |
| 36 | + const res = await this.request<UploadExtensionResponse, true>( |
| 37 | + '/browser/extensions/upload', |
| 38 | + 'POST', |
| 39 | + formData, |
| 40 | + formData.getHeaders(), |
| 41 | + true |
| 42 | + ); |
| 43 | + |
| 44 | + return res.data; |
| 45 | + } |
| 46 | + |
| 47 | + async update(extensionId: string, filePath: string, name?: string): Promise<{ success: boolean }> { |
| 48 | + const fileName = this.getFileName(filePath); |
| 49 | + const fileStream = fs.createReadStream(filePath); |
| 50 | + |
| 51 | + const formData = new FormData(); |
| 52 | + formData.append('file', fileStream, fileName); |
| 53 | + if (name) { |
| 54 | + formData.append('name', name); |
| 55 | + } |
| 56 | + |
| 57 | + const res = await this.request<{ success: boolean }, true>( |
| 58 | + `/browser/extensions/${extensionId}`, |
| 59 | + 'PUT', |
| 60 | + formData, |
| 61 | + formData.getHeaders(), |
| 62 | + true |
| 63 | + ); |
| 64 | + |
| 65 | + return res.data; |
| 66 | + } |
| 67 | + |
| 68 | + async get(extensionId: string): Promise<ExtensionDetail> { |
| 69 | + const res = await this.request<ExtensionDetail, true>( |
| 70 | + `/browser/extensions/${extensionId}`, |
| 71 | + 'GET', |
| 72 | + undefined, |
| 73 | + {}, |
| 74 | + true |
| 75 | + ); |
| 76 | + |
| 77 | + return res.data; |
| 78 | + } |
| 79 | + |
| 80 | + async list(): Promise<ExtensionListItem[]> { |
| 81 | + const res = await this.request<ExtensionListItem[], true>('/browser/extensions/list', 'GET', undefined, {}, true); |
| 82 | + return res.data; |
| 83 | + } |
| 84 | + |
| 85 | + async delete(extensionId: string): Promise<{ success: boolean }> { |
| 86 | + const res = await this.request<{ success: boolean }, true>( |
| 87 | + `/browser/extensions/${extensionId}`, |
| 88 | + 'DELETE', |
| 89 | + undefined, |
| 90 | + {}, |
| 91 | + true |
| 92 | + ); |
| 93 | + |
| 94 | + return res.data; |
| 95 | + } |
| 96 | +} |
0 commit comments