feat(download): Add download types

This commit is contained in:
Nico Jansen
2017-11-19 10:31:08 +01:00
parent 35272568c9
commit 0a4a4f3439
4 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
import * as fs from 'fs';
import download = require('download');
download('http://unicorn.com/foo.jpg', 'dist').then(() => {
console.log('done!');
});
download('http://unicorn.com/foo.jpg').then(data => {
fs.writeFileSync('dist/foo.jpg', data);
});
download('unicorn.com/foo.jpg').pipe(fs.createWriteStream('dist/foo.jpg'));
download('unicorn.com/foo.jpg', 'dest', {
body: '',
decompress: true,
encoding: 'utf8',
extract: true,
filename: 'filename',
followRedirect: true,
proxy: '',
query: '',
retries: (retry: number, error: any) => 4,
timeout: {
connect: 20,
request: 20,
socket: 20
},
useElectronNet: true
});

41
types/download/index.d.ts vendored Normal file
View File

@@ -0,0 +1,41 @@
// Type definitions for download 6.2
// Project: https://github.com/kevva/download
// Definitions by: Nico Jansen <https://github.com/nicojs>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
/// <reference types="node" />
interface TimeoutOptions {
connect?: number;
socket?: number;
request?: number;
}
type RetryFunction = (retry: number, error: any) => number;
interface DownloadOptions {
body?: string | Buffer | NodeJS.ReadableStream;
encoding?: string | null;
query?: string | object;
timeout?: number | TimeoutOptions;
retries?: number | RetryFunction;
followRedirect?: boolean;
decompress?: boolean;
useElectronNet?: boolean;
/**
* If set to true, try extracting the file using decompress.
*/
extract?: boolean;
/**
* Name of the saved file.
*/
filename?: string;
/**
* Proxy endpoint
*/
proxy?: string;
}
declare function download(url: string, destination?: string, options?: DownloadOptions): Promise<Buffer> & NodeJS.WritableStream & NodeJS.ReadableStream;
export = download;

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"download-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }