mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-06 06:19:58 +08:00
[form-data] align typing to v2.2, add linting and fix found issues (#18312)
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
/// <reference types="node-fetch" />
|
||||
|
||||
import FormData = require('form-data');
|
||||
import fs = require('fs');
|
||||
import http = require('http');
|
||||
@@ -8,43 +6,45 @@ import fetch from 'node-fetch';
|
||||
|
||||
import * as ImportUsingES6Syntax from 'form-data';
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
form.append('my_field', 'my value');
|
||||
form.append('my_buffer', new Buffer(10));
|
||||
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
|
||||
}
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
const num: number = form.getLengthSync();
|
||||
const bool: boolean = form.hasKnownLength();
|
||||
})();
|
||||
|
||||
http.request({ path: 'http://nodejs.org/images/logo.png' }, function (response) {
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
|
||||
http.request({ path: 'http://nodejs.org/images/logo.png' }, response => {
|
||||
form.append('my_field', 'my value');
|
||||
form.append('my_buffer', new Buffer(10));
|
||||
form.append('my_logo', response);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
|
||||
form.append('my_field', 'my value');
|
||||
form.append('my_buffer', new Buffer(10));
|
||||
form.append('my_logo', request('http://nodejs.org/images/logo.png'));
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
form.submit('http://example.org/', function (err, res) {
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
form.submit('http://example.org/', (err, res) => {
|
||||
// res – response object (http.IncomingMessage) //
|
||||
res.resume();
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
var request = http.request({
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
const request = http.request({
|
||||
method: 'post',
|
||||
host: 'example.org',
|
||||
path: '/upload',
|
||||
@@ -53,89 +53,86 @@ import * as ImportUsingES6Syntax from 'form-data';
|
||||
|
||||
form.pipe(request);
|
||||
|
||||
request.on('response', function (res: any) {
|
||||
request.on('response', res => {
|
||||
console.log(res.statusCode);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
form.submit('example.org/upload', function (err, res) {
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
form.submit('example.org/upload', (err, res) => {
|
||||
const r: http.IncomingMessage = res;
|
||||
console.log(res.statusCode);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var CRLF = '\r\n';
|
||||
var form = new FormData();
|
||||
var buffer = new Buffer('');
|
||||
(() => {
|
||||
const CRLF = '\r\n';
|
||||
const form = new FormData();
|
||||
const buffer = new Buffer('');
|
||||
|
||||
var options = {
|
||||
const options = {
|
||||
header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
|
||||
knownLength: 1
|
||||
};
|
||||
|
||||
form.append('my_buffer', buffer, options);
|
||||
|
||||
form.submit('http://example.com/', function (err, res) {
|
||||
form.submit('http://example.com/', (err, res) => {
|
||||
if (err) throw err;
|
||||
console.log('Done');
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
form.submit({
|
||||
host: 'example.com',
|
||||
path: '/probably.php?extra=params',
|
||||
auth: 'username:password'
|
||||
}, function (err, res) {
|
||||
}, (err, res) => {
|
||||
console.log(res.statusCode);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
form.submit({
|
||||
host: 'example.com',
|
||||
path: '/surelynot.php',
|
||||
headers: { 'x-test-header': 'test-header-value' }
|
||||
}, function (err, res) {
|
||||
}, (err, res) => {
|
||||
console.log(res.statusCode);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var formData = {
|
||||
(() => {
|
||||
const formData = {
|
||||
my_field: 'my_value',
|
||||
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
|
||||
};
|
||||
|
||||
request.post({ url: 'http://service.com/upload', formData: formData }, function (err, httpResponse, body) {
|
||||
request.post({ url: 'http://service.com/upload', formData }, (err, httpResponse, body) => {
|
||||
if (err) {
|
||||
return console.error('upload failed:', err);
|
||||
}
|
||||
console.log('Upload successful! Server responded with:', body);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
|
||||
form.append('a', 1);
|
||||
|
||||
fetch('http://example.com', { method: 'POST', body: form })
|
||||
.then(function (res) {
|
||||
return res.json();
|
||||
}).then(function (json) {
|
||||
.then(res => res.json())
|
||||
.then(json => {
|
||||
console.log(json);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
() => {
|
||||
var form = new FormData();
|
||||
form.getLength((err: Error, length: number): void => {
|
||||
// nothing
|
||||
});
|
||||
}
|
||||
(() => {
|
||||
const form = new FormData();
|
||||
form.getLength((err: Error, length: number): void => {});
|
||||
})();
|
||||
|
||||
38
types/form-data/index.d.ts
vendored
38
types/form-data/index.d.ts
vendored
@@ -1,26 +1,42 @@
|
||||
// Type definitions for form-data
|
||||
// Project: https://github.com/felixge/node-form-data
|
||||
// Definitions by: Carlos Ballesteros Velasco <https://github.com/soywiz>, Leon Yu <https://github.com/leonyu>
|
||||
// Type definitions for form-data 2.2
|
||||
// Project: https://github.com/form-data/form-data
|
||||
// Definitions by: Carlos Ballesteros Velasco <https://github.com/soywiz>
|
||||
// Leon Yu <https://github.com/leonyu>
|
||||
// BendingBender <https://github.com/BendingBender>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
// Imported from: https://github.com/soywiz/typescript-node-definitions/form-data.d.ts
|
||||
|
||||
/// <reference types="node" />
|
||||
import * as stream from 'stream';
|
||||
import * as http from 'http';
|
||||
|
||||
export = FormData;
|
||||
|
||||
import * as stream from "stream";
|
||||
|
||||
declare class FormData extends stream.Readable {
|
||||
append(key: string, value: any, options?: any): void;
|
||||
getHeaders(): FormData.Dictionary<string>;
|
||||
submit(params: string | Object, callback: (error: any, response: any) => void): any;
|
||||
append(key: string, value: any, options?: FormData.AppendOptions | string): void;
|
||||
getHeaders(): FormData.Headers;
|
||||
submit(params: string | FormData.SubmitOptions, callback?: (error: Error | undefined, response: http.IncomingMessage) => void): http.ClientRequest;
|
||||
getBoundary(): string;
|
||||
getLength(callback: (err: Error, length: number) => void): void;
|
||||
getLength(callback: (err: Error | undefined, length: number) => void): void;
|
||||
getLengthSync(): number;
|
||||
hasKnownLength(): boolean;
|
||||
}
|
||||
|
||||
declare namespace FormData {
|
||||
interface Dictionary<T> {
|
||||
[key: string]: T;
|
||||
interface Headers {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
interface AppendOptions {
|
||||
header?: string | Headers;
|
||||
knownLength?: number;
|
||||
filename?: string;
|
||||
filepath?: string;
|
||||
contentType?: string;
|
||||
}
|
||||
|
||||
interface SubmitOptions extends http.RequestOptions {
|
||||
protocol?: 'https:' | 'http:';
|
||||
}
|
||||
}
|
||||
|
||||
1
types/form-data/tslint.json
Normal file
1
types/form-data/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user