forked from capricorn86/happy-dom
/
FetchHandler.ts
73 lines (67 loc) · 2.16 KB
/
FetchHandler.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
import URL from '../location/URL';
import RelativeURL from '../location/RelativeURL';
import { RequestInfo } from './IRequest';
import IRequestInit from './IRequestInit';
import IDocument from '../nodes/document/IDocument';
import IResponse from './IResponse';
import Response from './Response';
import NodeFetch from 'node-fetch';
import { Request } from 'node-fetch';
/**
* Helper class for performing fetch.
*/
export default class FetchHandler {
/**
* Returns resource data asynchonously.
*
* @param document Document.
* @param url URL to resource.
* @param [init] Init.
* @returns Response.
*/
public static fetch(
document: IDocument,
url: RequestInfo,
init?: IRequestInit
): Promise<IResponse> {
// We want to only load NodeFetch when it is needed to improve performance and not have direct dependencies to server side packages.
const taskManager = document.defaultView.happyDOM.asyncTaskManager;
return new Promise((resolve, reject) => {
const taskID = taskManager.startTask();
let request;
if (typeof url === 'string') {
request = new Request(RelativeURL.getAbsoluteURL(document.defaultView.location, url));
} else if (url instanceof URL) {
// URLs are always absolute, no need for getAbsoluteURL.
request = new Request(url);
} else {
request = new Request(RelativeURL.getAbsoluteURL(document.defaultView.location, url.url), {
...url
});
}
NodeFetch(request, init)
.then((response) => {
if (taskManager.getTaskCount() === 0) {
reject(new Error('Failed to complete fetch request. Task was canceled.'));
} else {
response.constructor['_ownerDocument'] = document;
for (const key of Object.keys(Response.prototype)) {
if (Response.prototype.hasOwnProperty(key) && key !== 'constructor') {
if (typeof Response.prototype[key] === 'function') {
response[key] = Response.prototype[key].bind(response);
} else {
response[key] = Response.prototype[key];
}
}
}
taskManager.endTask(taskID);
resolve(response);
}
})
.catch((error) => {
reject(error);
taskManager.cancelAll(error);
});
});
}
}