🔥
This commit is contained in:
6
src/core/fetch/fetch.client.js
Normal file
6
src/core/fetch/fetch.client.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import 'whatwg-fetch';
|
||||
|
||||
export default self.fetch.bind(self);
|
||||
export const Headers = self.Headers;
|
||||
export const Request = self.Request;
|
||||
export const Response = self.Response;
|
||||
24
src/core/fetch/fetch.server.js
Normal file
24
src/core/fetch/fetch.server.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import Promise from 'bluebird';
|
||||
import fetch, { Request, Headers, Response } from 'node-fetch';
|
||||
import { host } from '../../config';
|
||||
|
||||
fetch.Promise = Promise;
|
||||
Response.Promise = Promise;
|
||||
|
||||
function localUrl(url) {
|
||||
if (url.startsWith('//')) {
|
||||
return `https:${url}`;
|
||||
}
|
||||
|
||||
if (url.startsWith('http')) {
|
||||
return url;
|
||||
}
|
||||
|
||||
return `http://${host}${url}`;
|
||||
}
|
||||
|
||||
function localFetch(url, options) {
|
||||
return fetch(localUrl(url), options);
|
||||
}
|
||||
|
||||
export { localFetch as default, Request, Headers, Response };
|
||||
6
src/core/fetch/package.json
Normal file
6
src/core/fetch/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "fetch",
|
||||
"main": "./fetch.server.js",
|
||||
"browser": "./fetch.client.js"
|
||||
}
|
||||
Reference in New Issue
Block a user