This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
sprequestexecutorclient.ts
94 lines (80 loc) · 3.32 KB
/
sprequestexecutorclient.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { HttpClientImpl } from "./httpclient";
import { Util } from "../utils/util";
import { SPRequestExecutorUndefinedException } from "../utils/exceptions";
/**
* Makes requests using the SP.RequestExecutor library.
*/
export class SPRequestExecutorClient implements HttpClientImpl {
/**
* Fetches a URL using the SP.RequestExecutor library.
*/
public fetch(url: string, options: any): Promise<Response> {
if (typeof SP === "undefined" || typeof SP.RequestExecutor === "undefined") {
throw new SPRequestExecutorUndefinedException();
}
const addinWebUrl = url.substring(0, url.indexOf("/_api")),
executor = new SP.RequestExecutor(addinWebUrl);
let headers: { [key: string]: string; } = {},
iterator: IterableIterator<[string, string]>,
temp: IteratorResult<[string, string]>;
if (options.headers && options.headers instanceof Headers) {
iterator = <IterableIterator<[string, string]>>options.headers.entries();
temp = iterator.next();
while (!temp.done) {
headers[temp.value[0]] = temp.value[1];
temp = iterator.next();
}
} else {
headers = <any>options.headers;
}
// this is a way to determine if we need to set the binaryStringRequestBody by testing what method we are calling
// and if they would normally have a binary body. This addresses issue #565.
const paths = [
"files\/add",
"files\/addTemplateFile",
"file\/startUpload",
"file\/continueUpload",
"file\/finishUpload",
"attachmentfiles\/add",
];
const isBinaryRequest = (new RegExp(paths.join("|"), "i")).test(url);
return new Promise((resolve, reject) => {
let requestOptions = {
error: (error: SP.ResponseInfo) => {
reject(this.convertToResponse(error));
},
headers: headers,
method: options.method,
success: (response: SP.ResponseInfo) => {
resolve(this.convertToResponse(response));
},
url: url,
};
if (options.body) {
requestOptions = Util.extend(requestOptions, { body: options.body });
if (isBinaryRequest) {
requestOptions = Util.extend(requestOptions, { binaryStringRequestBody: true });
}
}
executor.executeAsync(requestOptions);
});
}
/**
* Converts a SharePoint REST API response to a fetch API response.
*/
private convertToResponse = (spResponse: SP.ResponseInfo): Response => {
const responseHeaders = new Headers();
for (const h in spResponse.headers) {
if (spResponse.headers[h]) {
responseHeaders.append(h, spResponse.headers[h]);
}
}
// issue #256, Cannot have an empty string body when creating a Response with status 204
const body = spResponse.statusCode === 204 ? null : spResponse.body;
return new Response(body, {
headers: responseHeaders,
status: spResponse.statusCode,
statusText: spResponse.statusText,
});
}
}