diff --git a/packages/core/__tests__/db.test.js b/packages/core/__tests__/db.test.js new file mode 100644 index 000000000..44de8f638 --- /dev/null +++ b/packages/core/__tests__/db.test.js @@ -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"); +}); diff --git a/packages/core/api/index.js b/packages/core/api/index.js index 53f7c2ccc..33909e377 100644 --- a/packages/core/api/index.js +++ b/packages/core/api/index.js @@ -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; } } } diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js index 44ef5871c..317758844 100644 --- a/packages/core/api/sync/index.js +++ b/packages/core/api/sync/index.js @@ -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) { diff --git a/packages/core/models/user.js b/packages/core/models/user.js index cdc220a55..5679d1e99 100644 --- a/packages/core/models/user.js +++ b/packages/core/models/user.js @@ -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), }); diff --git a/packages/core/utils/constants.js b/packages/core/utils/constants.js index 21b916ce4..997dcc080 100644 --- a/packages/core/utils/constants.js +++ b/packages/core/utils/constants.js @@ -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;