[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
This commit is contained in:
Sascha Zarhuber
2019-10-02 11:46:31 -07:00
committed by Ryan Cavanaugh
parent f12108dd3b
commit 11c26c1ace
4 changed files with 284 additions and 0 deletions
+204
View File
@@ -0,0 +1,204 @@
// Type definitions for node-openload 2.2
// Project: https://github.com/saschazar21/node-openload#readme
// Definitions by: Sascha Zarhuber <https://github.com/saschazar21>
// 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<AccountInfo>;
getDownloadTicket(file: string): Promise<DownloadTicket>;
getDownloadLink(obj: DownloadLinkParam): Promise<DownloadLink>;
getDownload(file: string): Promise<DownloadLink>;
getFileInfo(file: string): Promise<FileInfo>;
deleteFile(file: string | string[]): Promise<[boolean]>;
listFolder(folder: string): Promise<ListFolder>;
getFolder(folder: string): Promise<ListFolder>;
remoteUpload(obj: RemoteUploadParam): Promise<RemoteUpload>;
remoteUploadStatus(obj: RemoteUploadStatusParam): Promise<RemoteUploadStatus>;
upload(obj: UploadParam, cb: (progress: UploadProgress) => void): Promise<Upload>;
getSplashImage(file: string): Promise<string>;
}
/**
* 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;
@@ -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<T>(o: T): Array<keyof T>;
}
}
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);
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }