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 EventManager from "../utils/event-manager";
import Session from "./session";
import { HOST } from "../utils/constants";
import Constants from "../utils/constants";
/**
* @type {EventSource}
@@ -117,7 +117,7 @@ class Database {
host(host) {
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
* And then it should continue.
*/
import { HOST, HEADERS } from "../../utils/constants";
import Constants from "../../utils/constants";
import Collector from "./collector";
import Merger from "./merger";
import { areAllEmpty } from "./utils";
@@ -46,8 +46,8 @@ export default class Sync {
}
async _fetch(lastSynced, token) {
let response = await fetch(`${HOST}/sync?lst=${lastSynced}`, {
headers: { ...HEADERS, Authorization: `Bearer ${token}` },
let response = await fetch(`${Constants.HOST}/sync?lst=${lastSynced}`, {
headers: { ...Constants.HEADERS, Authorization: `Bearer ${token}` },
});
return await response.json();
}
@@ -108,9 +108,9 @@ export default class Sync {
}
async _send(data, token) {
let response = await fetch(`${HOST}/sync`, {
let response = await fetch(`${Constants.HOST}/sync`, {
method: "POST",
headers: { ...HEADERS, Authorization: `Bearer ${token}` },
headers: { ...Constants.HEADERS, Authorization: `Bearer ${token}` },
body: JSON.stringify(data),
});
if (response.ok) {

View File

@@ -1,4 +1,4 @@
import { HOST, HEADERS } from "../utils/constants";
import Constants from "../utils/constants";
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",
headers: { ...HEADERS, ...headers },
headers: { ...Constants.HEADERS, ...headers },
body: get ? undefined : JSON.stringify(data),
});

View File

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