forked from nativescript-community/https
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
50 lines (45 loc) · 2.13 KB
/
index.ts
File metadata and controls
50 lines (45 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ImageSource } from '@nativescript/core';
import { request } from './request';
import type { HttpsRequestOptions } from './request';
export * from './request';
/**
* Downloads the content from the specified URL as a string.
* @param arg either The URL to request from or HttpsRequestOptions options.
*/
export async function getString(arg: string | HttpsRequestOptions): Promise<string> {
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
return r.content.toString();
}
/**
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
* @param arg either The URL to request from or HttpsRequestOptions options.
*/
export async function getJSON<T>(arg: string | HttpsRequestOptions): Promise<T> {
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
return r.content.toJSON();
}
/**
* Downloads the content from the specified URL and attempts to decode it as an image.
* @param arg either The URL to request from or HttpsRequestOptions options.
*/
export async function getImage(arg: string | HttpsRequestOptions): Promise<ImageSource> {
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
return r.content.toImage();
}
/**
* Downloads the content from the specified URL and attempts to save it as file.
* @param arg either The URL to request from or HttpsRequestOptions options.
* @param destinationFilePath Optional. The downloaded file path.
*/
export async function getFile(arg: string | HttpsRequestOptions, destinationFilePath?: string): Promise<any> {
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
return r.content.toFile(destinationFilePath);
}
/**
* Downloads the content from the specified URL as binary and returns an ArrayBuffer.
* @param arg either The URL to request from or HttpsRequestOptions options.
*/
export async function getBinary(arg: string | HttpsRequestOptions): Promise<ArrayBuffer> {
const r = await request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg);
return r.content.toArrayBuffer();
}