feat(http): better binary support & XHR support (#7707)

* feat(http): binary upload support

* feat(http): better binary support & XHR support

* fix: linting issue

* chore: moved files from old place to the new one

* chore: Updated NativeScript.api.md

* feat(http): support both ByteBuffer and String

Co-authored-by: Vasil Trifonov <v.trifonov@gmail.com>
This commit is contained in:
Stefan Andres Charsley
2020-01-29 02:22:32 +13:00
committed by Vasil Trifonov
parent a311a922b5
commit e293367dfc
23 changed files with 1249 additions and 486 deletions

View File

@@ -92,6 +92,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
callbacks.resolveCallback({
content: {
raw: result.raw,
toArrayBuffer: () => Uint8Array.from(result.raw.toByteArray()).buffer,
toString: (encoding?: HttpResponseEncoding) => {
let str: string;
if (encoding) {
@@ -180,7 +181,14 @@ function buildJavaOptions(options: httpModule.HttpRequestOptions) {
javaOptions.method = options.method;
}
if (typeof options.content === "string" || options.content instanceof FormData) {
javaOptions.content = options.content.toString();
const nativeString = new java.lang.String(options.content.toString());
const nativeBytes = nativeString.getBytes("UTF-8");
const nativeBuffer = java.nio.ByteBuffer.wrap(nativeBytes);
javaOptions.content = nativeBuffer;
} else if (options.content instanceof ArrayBuffer) {
const typedArray = new Uint8Array(options.content as ArrayBuffer);
const nativeBuffer = java.nio.ByteBuffer.wrap(Array.from(typedArray));
javaOptions.content = nativeBuffer;
}
if (typeof options.timeout === "number") {
javaOptions.timeout = options.timeout;

View File

@@ -95,6 +95,9 @@ export function request(options: httpModule.HttpRequestOptions): Promise<httpMod
if (types.isString(options.content) || options.content instanceof FormData) {
urlRequest.HTTPBody = NSString.stringWithString(options.content.toString()).dataUsingEncoding(4);
} else if (options.content instanceof ArrayBuffer) {
const buffer = options.content as ArrayBuffer;
urlRequest.HTTPBody = NSData.dataWithData(buffer as any);
}
if (types.isNumber(options.timeout)) {
@@ -142,7 +145,15 @@ export function request(options: httpModule.HttpRequestOptions): Promise<httpMod
resolve({
content: {
raw: data,
toString: (encoding?: any) => NSDataToString(data, encoding),
toArrayBuffer: () => interop.bufferFromData(data),
toString: (encoding?: any) => {
const str = NSDataToString(data, encoding);
if (typeof str === "string") {
return str;
} else {
throw new Error("Response content may not be converted to string");
}
},
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
toImage: () => {
ensureImageSource();

View File

@@ -56,6 +56,18 @@ export function getFile(url: string, destinationFilePath?: string): Promise<File
*/
export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<File>;
/**
* Downloads the content from the specified URL as binary and returns an ArrayBuffer.
* @param url The URL to request from.
*/
export function getBinary(url: string): Promise<ArrayBuffer>;
/**
* Downloads the content from the specified URL as binary and returns an ArrayBuffer.
* @param options An object that specifies various request options.
*/
export function getBinary(options: HttpRequestOptions): Promise<ArrayBuffer>;
/**
* Makes a generic http request using the provided options and returns a HttpResponse Object.
* @param options An object that specifies various request options.
@@ -84,7 +96,7 @@ export interface HttpRequestOptions {
/**
* Gets or sets the request body.
*/
content?: string | FormData;
content?: string | FormData | ArrayBuffer;
/**
* Gets or sets the request timeout in milliseconds.
@@ -132,6 +144,11 @@ export interface HttpContent {
*/
raw: any;
/**
* Gets the response body as ArrayBuffer
*/
toArrayBuffer: () => ArrayBuffer;
/**
* Gets the response body as string.
*/

View File

@@ -58,3 +58,17 @@ export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
}, e => reject(e));
});
}
export function getBinary(arg: any): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => {
try {
const arrayBuffer = r.content.toArrayBuffer();
resolve(arrayBuffer);
} catch (e) {
reject(e);
}
}, e => reject(e));
});
}