From 11c26c1ace6b7bafd0dff504207d5b1597dc2eab Mon Sep 17 00:00:00 2001 From: Sascha Zarhuber Date: Wed, 2 Oct 2019 20:46:31 +0200 Subject: [PATCH] [node-openload] add type definitions (#38799) * feat(node-openload): added base files * feat(node-openload): added types * feat(node-upload): added tests, fixed interfaces * fix(node-openload): fix test errors * fix(node-openload): fix test errors * fix(node-openload): fix test errors * fix(node-openload): fix default export * fix(node-openload): fixed default export --- types/node-openload/index.d.ts | 204 +++++++++++++++++++++ types/node-openload/node-openload-tests.ts | 56 ++++++ types/node-openload/tsconfig.json | 23 +++ types/node-openload/tslint.json | 1 + 4 files changed, 284 insertions(+) create mode 100644 types/node-openload/index.d.ts create mode 100644 types/node-openload/node-openload-tests.ts create mode 100644 types/node-openload/tsconfig.json create mode 100644 types/node-openload/tslint.json diff --git a/types/node-openload/index.d.ts b/types/node-openload/index.d.ts new file mode 100644 index 0000000000..f350391573 --- /dev/null +++ b/types/node-openload/index.d.ts @@ -0,0 +1,204 @@ +// Type definitions for node-openload 2.2 +// Project: https://github.com/saschazar21/node-openload#readme +// Definitions by: Sascha Zarhuber +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * The model for the base config object needed for the Openload constructor + */ +interface OpenloadConfig { + /* the api_key, available directly from the WebUI after successful login */ + api_key: string; + /* the api_login, a string available from the WebUI, NOT the user's e-mail */ + api_login: string; + /* the api_version to target, needed for forming the URL, by default 1 */ + api_version?: number; +} + +/** + * The account info response + */ +interface AccountInfo { + extid: string; + email: string; + signup_at: string; + storage_left: number; + storage_used: string; + traffic: { + left: number; + used_24h: number; + }; + balance: number; +} + +interface DownloadTicket { + ticket: string; + captcha_url: string; + captcha_w: number; + captcha_h: number; + wait_time: number; + valid_until: string; +} + +interface DownloadLink { + name: string; + size: number; + sha1: string; + content_type: string; + upload_at: string; + url: string; + token: string; +} + +interface DownloadLinkParam { + file: string; + ticket: string; + captcha_response: string; +} + +interface FileInfo { + [key: string]: { + id: string; + status: number; + name: string; + size: number; + sha1: string; + content_type: string; + }; +} + +interface Upload { + url: string; + valid_until: string; +} + +interface UploadParam { + file: string | ArrayBuffer; + folder?: string; + filename?: string; + contentType?: string; +} + +interface RemoteUpload { + id: string; + folderid: string; +} + +interface RemoteUploadParam { + url: string; + folder?: string; + headers?: string; +} + +interface RemoteUploadStatus { + [key: number]: { + id: number; + remoteurl: string; + status: string; + bytes_loaded: string; + bytes_total: string; + folderid: string; + added: string; + last_update: string; + extid: string | boolean; + url: string | boolean; + }; +} + +interface RemoteUploadStatusParam { + limit?: number; + id?: string; +} + +interface ListFolder { + folders: [ + { + id: string; + name: string; + }, + ]; + files: [ + { + name: string; + sha1: string; + folderid: string; + upload_at: string; + status: string; + size: string; + content_type: string; + download_count: string; + cstatus: string; + link: string; + linkextid: string; + }, + ]; +} + +interface RunningFileConverts { + name: string; + id: string; + status: string; + last_update: string; + progress: number; + retries: string; + link: string; + linkextid: string; +} + +interface UploadProgress { + percent: number; + transferred: number; + total: number; +} + +/** + * The Openload base class, contains all the supported endpoints as member functions + */ +declare class Openload { + private _version: number; + private _locationPrefix: string; + private _config: OpenloadConfig; + + constructor(config: OpenloadConfig); + + // TypeScript Version: 3.6 + get config(): OpenloadConfig; + + // TypeScript Version: 3.6 + set config(object: OpenloadConfig); + + // TypeScript Version: 3.6 + get locationPrefix(): string; + + getAccountInfo(): Promise; + + getDownloadTicket(file: string): Promise; + + getDownloadLink(obj: DownloadLinkParam): Promise; + + getDownload(file: string): Promise; + + getFileInfo(file: string): Promise; + + deleteFile(file: string | string[]): Promise<[boolean]>; + + listFolder(folder: string): Promise; + + getFolder(folder: string): Promise; + + remoteUpload(obj: RemoteUploadParam): Promise; + + remoteUploadStatus(obj: RemoteUploadStatusParam): Promise; + + upload(obj: UploadParam, cb: (progress: UploadProgress) => void): Promise; + + getSplashImage(file: string): Promise; +} + +/** + * Exports a Singleton of the Openload class by default + * @param config The base config containing the user credentials + * @returns An Openload singleton + */ +declare function openload(config: OpenloadConfig): Openload; +export = openload; diff --git a/types/node-openload/node-openload-tests.ts b/types/node-openload/node-openload-tests.ts new file mode 100644 index 0000000000..c0928ca14d --- /dev/null +++ b/types/node-openload/node-openload-tests.ts @@ -0,0 +1,56 @@ +import openload = require('node-openload'); + +const config = { + api_key: 'test-1234', + api_login: 'testlogin', +}; + +const ol = openload(config); + +ol.config; +ol.config = config; +ol.locationPrefix; + +ol.getAccountInfo().then(info => info.email); +ol.getDownloadTicket('testfile-id') + .then(result => { + const { ticket } = result; + + return ol.getDownloadLink({ + captcha_response: 'arbitrary captcha response', + file: 'testfile-id', + ticket, + }); + }) + .then(result => result.url); + +ol.getDownload('testfile-id').then(result => result.url); + +ol.getFileInfo('testfile-id').then(result => result.content_type); + +ol.deleteFile('testfile-id, testfile_id2').then(result => result.length); + +ol.listFolder('testfolder').then(result => result.files); + +ol.getFolder('testfolder').then(result => result.folders); + +// Needed because remoteUploadStatus returns an object with integer keys +// https://stackoverflow.com/a/52856805 +declare global { + interface ObjectConstructor { + numberKeys(o: T): Array; + } +} +Object.numberKeys = Object.keys as any; +ol.remoteUpload({ url: 'https://someurl.com/to/image.jpg' }) + .then(result => { + const { id } = result; + + return ol.remoteUploadStatus({ id }); + }) + .then(result => Object.numberKeys(result).map(key => result[key].remoteurl)); + +const cb = (progress: { percent: number; transferred: number; total: number }) => progress.percent; +ol.upload({ file: './file.txt/' }, cb).then(result => result.url); + +ol.getSplashImage('testfile-id').then(imgUrl => imgUrl); diff --git a/types/node-openload/tsconfig.json b/types/node-openload/tsconfig.json new file mode 100644 index 0000000000..6cae918a4a --- /dev/null +++ b/types/node-openload/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-openload-tests.ts" + ] +} diff --git a/types/node-openload/tslint.json b/types/node-openload/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/node-openload/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }