forked from NativeScript/nativescript-app-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTNSRequester.ts
53 lines (48 loc) · 1.67 KB
/
TNSRequester.ts
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
51
52
53
import { HttpResponse, Http as nsHttp } from "@nativescript/core";
import Requester = Http.Requester;
const packageJson = require("./package.json");
export class TNSRequester implements Requester {
request(verb: Http.Verb, url: string, callback: Callback<Http.Response>): void;
request(verb: Http.Verb, url: string, requestBody: string, callback: Callback<Http.Response>): void;
request(verb: Http.Verb, url: string, requestBody, callback?: Callback<Http.Response>): void {
if (typeof requestBody === "function") {
callback = requestBody;
requestBody = null;
}
if (requestBody && typeof requestBody === "object") {
requestBody = JSON.stringify(requestBody);
}
nsHttp.request({
method: TNSRequester.getHttpMethodName(verb),
url,
content: requestBody,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"X-NativeScript-AppSync-Plugin-Name": packageJson.name,
"X-NativeScript-AppSync-Plugin-Version": packageJson.version,
"X-NativeScript-AppSync-SDK-Version": packageJson.dependencies["nativescript-app-sync-cli"]
}
}).then((response: HttpResponse) => {
callback(null, {
statusCode: response.statusCode,
body: response.content ? response.content.toString() : null
});
});
}
private static getHttpMethodName(verb): string {
// This should stay in sync with the enum at
// https://github.com/Microsoft/code-push/blob/master/sdk/script/acquisition-sdk.ts#L6
return [
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"TRACE",
"OPTIONS",
"CONNECT",
"PATCH"
][verb];
}
}