mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
FormData support added
This commit is contained in:
@ -123,6 +123,7 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) =
|
|||||||
// ```
|
// ```
|
||||||
// </snippet>
|
// </snippet>
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
|
export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
|
||||||
var result;
|
var result;
|
||||||
@ -145,7 +146,7 @@ export var test_fetch_formData = function (done: (err: Error, res?: string) => v
|
|||||||
// ```
|
// ```
|
||||||
// </snippet>
|
// </snippet>
|
||||||
};
|
};
|
||||||
*/
|
|
||||||
export var test_fetch_fail_invalid_url = function (done) {
|
export var test_fetch_fail_invalid_url = function (done) {
|
||||||
var completed: boolean;
|
var completed: boolean;
|
||||||
var isReady = function () { return completed; }
|
var isReady = function () { return completed; }
|
||||||
@ -231,16 +232,19 @@ export var test_fetch_headers_sent = function (done) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export var test_fetch_post_form_data = function (done) {
|
export var test_fetch_post_form_data = function (done) {
|
||||||
|
var data = new FormData();
|
||||||
|
data.append("MyVariableOne", "ValueOne");
|
||||||
|
data.append("MyVariableTwo", "ValueTwo");
|
||||||
|
|
||||||
fetchModule.fetch("https://httpbin.org/post", {
|
fetchModule.fetch("https://httpbin.org/post", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||||
body: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
|
body: data
|
||||||
}).then(r => {
|
}).then(r => {
|
||||||
// return r.formData(); Uncomment this when FormData is available!
|
return r.formData();
|
||||||
return r.json();
|
|
||||||
}).then(function (r) {
|
}).then(function (r) {
|
||||||
try {
|
try {
|
||||||
TKUnit.assert(r.form["MyVariableOne"] === "ValueOne" && r.form["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Actual result is: " + r.form);
|
TKUnit.assert(r instanceof FormData, "Content not sent/received properly! Actual result is: " + r);
|
||||||
done(null);
|
done(null);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
@ -368,6 +368,32 @@ export var test_request_contentSentAndReceivedProperly = function (done) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export var test_request_FormDataContentSentAndReceivedProperly = function (done) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
var data = new FormData();
|
||||||
|
data.append("MyVariableOne", "ValueOne");
|
||||||
|
data.append("MyVariableTwo", "ValueTwo");
|
||||||
|
|
||||||
|
http.request({
|
||||||
|
url: "https://httpbin.org/post",
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||||
|
content: data
|
||||||
|
}).then(function (response) {
|
||||||
|
result = response.content.toJSON();
|
||||||
|
try {
|
||||||
|
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
}, function (e) {
|
||||||
|
done(e);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export var test_request_NonStringHeadersSentAndReceivedProperly = function (done) {
|
export var test_request_NonStringHeadersSentAndReceivedProperly = function (done) {
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
|
@ -113,6 +113,30 @@ export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done)
|
|||||||
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
|
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function (done) {
|
||||||
|
xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("POST", "https://httpbin.org/post");
|
||||||
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
xhr.onreadystatechange = function () {
|
||||||
|
if (xhr.readyState > 3) {
|
||||||
|
var result = JSON.parse(xhr.responseText);
|
||||||
|
try {
|
||||||
|
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Result is: " + xhr.responseText);
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var data = new FormData();
|
||||||
|
data.append("MyVariableOne", "ValueOne");
|
||||||
|
data.append("MyVariableTwo", "ValueTwo");
|
||||||
|
|
||||||
|
xhr.send(<any>data);
|
||||||
|
};
|
||||||
|
|
||||||
export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) {
|
export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) {
|
||||||
var flag = false;
|
var flag = false;
|
||||||
|
|
||||||
@ -173,11 +197,12 @@ export function test_responseType(done) {
|
|||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
xhr.responseType = "";
|
xhr.responseType = "";
|
||||||
xhr.responseType = "text";
|
xhr.responseType = "text";
|
||||||
|
xhr.responseType = "json";
|
||||||
|
|
||||||
TKUnit.assertThrows(
|
TKUnit.assertThrows(
|
||||||
() => xhr.responseType = "json",
|
() => xhr.responseType = "arraybuffer",
|
||||||
"Didn't raise on unsupported type.",
|
"Didn't raise on unsupported type.",
|
||||||
"Response type of 'json' not supported."
|
"Response type of 'arraybuffer' not supported."
|
||||||
);
|
);
|
||||||
done(null);
|
done(null);
|
||||||
}
|
}
|
||||||
|
2
fetch/fetch.d.ts
vendored
2
fetch/fetch.d.ts
vendored
@ -54,8 +54,8 @@ declare module "fetch" {
|
|||||||
/*
|
/*
|
||||||
arrayBuffer(): Promise<ArrayBuffer>;
|
arrayBuffer(): Promise<ArrayBuffer>;
|
||||||
blob(): Promise<Blob>;
|
blob(): Promise<Blob>;
|
||||||
formData(): Promise<FormData>;
|
|
||||||
*/
|
*/
|
||||||
|
formData(): Promise<FormData>;
|
||||||
json(): Promise<any>;
|
json(): Promise<any>;
|
||||||
text(): Promise<string>;
|
text(): Promise<string>;
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
var support = {
|
var support = {
|
||||||
blob: 'FileReader' in self && 'Blob' in self && (function () {
|
blob: 'FileReader' in global && 'Blob' in global && (function () {
|
||||||
try {
|
try {
|
||||||
new Blob();
|
new Blob();
|
||||||
return true
|
return true
|
||||||
@ -118,7 +118,7 @@
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
formData: 'FormData' in self
|
formData: 'FormData' in global
|
||||||
}
|
}
|
||||||
|
|
||||||
function Body() {
|
function Body() {
|
||||||
|
@ -15,6 +15,7 @@ if (types.isUndefined(global.NSObject)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
global.XMLHttpRequest = xhr.XMLHttpRequest;
|
global.XMLHttpRequest = xhr.XMLHttpRequest;
|
||||||
|
global.FormData = xhr.FormData;
|
||||||
global.alert = dialogs.alert;
|
global.alert = dialogs.alert;
|
||||||
|
|
||||||
export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) {
|
export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) {
|
||||||
|
@ -73,8 +73,8 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
|
|||||||
if (types.isString(options.method)) {
|
if (types.isString(options.method)) {
|
||||||
javaOptions.method = options.method;
|
javaOptions.method = options.method;
|
||||||
}
|
}
|
||||||
if (options.content) {
|
if (types.isString(options.content) || options.content instanceof FormData) {
|
||||||
javaOptions.content = options.content;
|
javaOptions.content = options.content.toString();
|
||||||
}
|
}
|
||||||
if (types.isNumber(options.timeout)) {
|
if (types.isNumber(options.timeout)) {
|
||||||
javaOptions.timeout = options.timeout;
|
javaOptions.timeout = options.timeout;
|
||||||
|
@ -32,8 +32,8 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (types.isString(options.content)) {
|
if (types.isString(options.content) || options.content instanceof FormData) {
|
||||||
urlRequest.HTTPBody = NSString.alloc().initWithString(options.content).dataUsingEncoding(4);
|
urlRequest.HTTPBody = NSString.alloc().initWithString(options.content.toString()).dataUsingEncoding(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (types.isNumber(options.timeout)) {
|
if (types.isNumber(options.timeout)) {
|
||||||
|
2
http/http.d.ts
vendored
2
http/http.d.ts
vendored
@ -69,7 +69,7 @@ declare module "http" {
|
|||||||
/**
|
/**
|
||||||
* Gets or sets the request body.
|
* Gets or sets the request body.
|
||||||
*/
|
*/
|
||||||
content?: string;
|
content?: string | FormData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets or sets the request timeout in milliseconds.
|
* Gets or sets the request timeout in milliseconds.
|
||||||
|
49
xhr/xhr.ts
49
xhr/xhr.ts
@ -1,6 +1,12 @@
|
|||||||
import http = require("http");
|
import http = require("http");
|
||||||
import types = require("utils/types");
|
import types = require("utils/types");
|
||||||
|
|
||||||
|
module XMLHttpRequestResponseType {
|
||||||
|
export var empty = "";
|
||||||
|
export var text = "text";
|
||||||
|
export var json = "json";
|
||||||
|
}
|
||||||
|
|
||||||
export class XMLHttpRequest {
|
export class XMLHttpRequest {
|
||||||
public UNSENT = 0;
|
public UNSENT = 0;
|
||||||
public OPENED = 1;
|
public OPENED = 1;
|
||||||
@ -15,7 +21,7 @@ export class XMLHttpRequest {
|
|||||||
private _readyState: number;
|
private _readyState: number;
|
||||||
private _status: number;
|
private _status: number;
|
||||||
private _response: any;
|
private _response: any;
|
||||||
private _responseText: string = "";
|
private _responseText: Function;
|
||||||
private _headers: any;
|
private _headers: any;
|
||||||
private _errorFlag: boolean;
|
private _errorFlag: boolean;
|
||||||
private _responseType: string = "";
|
private _responseType: string = "";
|
||||||
@ -58,7 +64,7 @@ export class XMLHttpRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public send(data?: string) {
|
public send(data?: any) {
|
||||||
this._errorFlag = false;
|
this._errorFlag = false;
|
||||||
this._response = null;
|
this._response = null;
|
||||||
this._responseText = null;
|
this._responseText = null;
|
||||||
@ -68,6 +74,8 @@ export class XMLHttpRequest {
|
|||||||
if (types.isDefined(this._options)) {
|
if (types.isDefined(this._options)) {
|
||||||
if (types.isString(data)) {
|
if (types.isString(data)) {
|
||||||
this._options.content = data;
|
this._options.content = data;
|
||||||
|
} else if (data instanceof FormData) {
|
||||||
|
this._options.content = (<FormData>data).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
http.request(this._options).then(r=> {
|
http.request(this._options).then(r=> {
|
||||||
@ -80,7 +88,12 @@ export class XMLHttpRequest {
|
|||||||
|
|
||||||
this._setReadyState(this.LOADING);
|
this._setReadyState(this.LOADING);
|
||||||
|
|
||||||
this._responseText = r.content.toString();
|
if (this.responseType === XMLHttpRequestResponseType.empty ||
|
||||||
|
this.responseType === XMLHttpRequestResponseType.text ||
|
||||||
|
this.responseType === XMLHttpRequestResponseType.json) {
|
||||||
|
this._responseText = r.content.toString;
|
||||||
|
}
|
||||||
|
|
||||||
this._setReadyState(this.DONE);
|
this._setReadyState(this.DONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +151,7 @@ export class XMLHttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public set responseType(value: string) {
|
public set responseType(value: string) {
|
||||||
if (value === "" || value === "text") {
|
if (value === XMLHttpRequestResponseType.empty || value in XMLHttpRequestResponseType) {
|
||||||
this._responseType = value;
|
this._responseType = value;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Response type of '${value}' not supported.`);
|
throw new Error(`Response type of '${value}' not supported.`);
|
||||||
@ -165,7 +178,11 @@ export class XMLHttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get responseText(): string {
|
get responseText(): string {
|
||||||
return this._responseText;
|
if (types.isFunction(this._responseText)) {
|
||||||
|
return this._responseText();
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
get response(): any {
|
get response(): any {
|
||||||
@ -226,3 +243,25 @@ var statuses = {
|
|||||||
504: "Gateway Timeout",
|
504: "Gateway Timeout",
|
||||||
505: "HTTP Version Not Supported"
|
505: "HTTP Version Not Supported"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export class FormData {
|
||||||
|
private _data: Map<string, any>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this._data = new Map<string, any>();
|
||||||
|
}
|
||||||
|
|
||||||
|
append(name: string, value: any) {
|
||||||
|
this._data.set(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
toString(): string {
|
||||||
|
var arr = new Array<string>();
|
||||||
|
|
||||||
|
this._data.forEach(function (value, name, map) {
|
||||||
|
arr.push(`${encodeURIComponent(name) }=${encodeURIComponent(value) }`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return arr.join("&");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user