fix: properly change host via db.host method

This commit is contained in:
thecodrr
2020-09-19 11:46:36 +05:00
parent 1058ae9909
commit bf29a0ab16
5 changed files with 32 additions and 19 deletions

View File

@@ -0,0 +1,9 @@
import DB from "../api";
import Constants from "../utils/constants";
import StorageInterface from "../__mocks__/storage.mock";
test("db.host should change HOST", () => {
const db = new DB(StorageInterface);
db.host("hello world");
expect(Constants.HOST).toBe("hello world");
});

View File

@@ -12,7 +12,7 @@ import Backup from "../database/backup";
import Conflicts from "./sync/conflicts"; import Conflicts from "./sync/conflicts";
import EventManager from "../utils/event-manager"; import EventManager from "../utils/event-manager";
import Session from "./session"; import Session from "./session";
import { HOST } from "../utils/constants"; import Constants from "../utils/constants";
/** /**
* @type {EventSource} * @type {EventSource}
@@ -117,7 +117,7 @@ class Database {
host(host) { host(host) {
if (process.env.NODE_ENV !== "production") { if (process.env.NODE_ENV !== "production") {
HOST = host || HOST; Constants.HOST = host || HOST;
} }
} }
} }

View File

@@ -25,7 +25,7 @@
* Syncing should pause until all the conflicts have been resolved * Syncing should pause until all the conflicts have been resolved
* And then it should continue. * And then it should continue.
*/ */
import { HOST, HEADERS } from "../../utils/constants"; import Constants from "../../utils/constants";
import Collector from "./collector"; import Collector from "./collector";
import Merger from "./merger"; import Merger from "./merger";
import { areAllEmpty } from "./utils"; import { areAllEmpty } from "./utils";
@@ -46,8 +46,8 @@ export default class Sync {
} }
async _fetch(lastSynced, token) { async _fetch(lastSynced, token) {
let response = await fetch(`${HOST}/sync?lst=${lastSynced}`, { let response = await fetch(`${Constants.HOST}/sync?lst=${lastSynced}`, {
headers: { ...HEADERS, Authorization: `Bearer ${token}` }, headers: { ...Constants.HEADERS, Authorization: `Bearer ${token}` },
}); });
return await response.json(); return await response.json();
} }
@@ -108,9 +108,9 @@ export default class Sync {
} }
async _send(data, token) { async _send(data, token) {
let response = await fetch(`${HOST}/sync`, { let response = await fetch(`${Constants.HOST}/sync`, {
method: "POST", method: "POST",
headers: { ...HEADERS, Authorization: `Bearer ${token}` }, headers: { ...Constants.HEADERS, Authorization: `Bearer ${token}` },
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (response.ok) { if (response.ok) {

View File

@@ -1,4 +1,4 @@
import { HOST, HEADERS } from "../utils/constants"; import Constants from "../utils/constants";
export default class User { export default class User {
/** /**
@@ -138,9 +138,9 @@ async function authRequest(endpoint, data, auth = false, get = false) {
}; };
} }
let response = await fetch(`${HOST}/${endpoint}`, { let response = await fetch(`${Constants.HOST}/${endpoint}`, {
method: get ? "GET" : "POST", method: get ? "GET" : "POST",
headers: { ...HEADERS, ...headers }, headers: { ...Constants.HEADERS, ...headers },
body: get ? undefined : JSON.stringify(data), body: get ? undefined : JSON.stringify(data),
}); });

View File

@@ -1,10 +1,14 @@
export var HOST = const module = {
process.env.NODE_ENV === "production" HOST:
? "https://api.notesnook.com" process.env.NODE_ENV === "production"
: "http://0.0.0.0:8000"; ? "https://api.notesnook.com"
export const HEADERS = { : "http://0.0.0.0:8000",
agent: "nn/1.0.0", HEADERS: {
origin: "notesnook.com", agent: "nn/1.0.0",
"Content-Type": "application/json", origin: "notesnook.com",
Accept: "application/json", "Content-Type": "application/json",
Accept: "application/json",
},
}; };
export default module;