@terreno/rtk 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/authSlice.test.js +68 -0
  2. package/dist/authSlice.test.js.map +1 -1
  3. package/dist/authSliceNative.test.d.ts +2 -0
  4. package/dist/authSliceNative.test.d.ts.map +1 -0
  5. package/dist/authSliceNative.test.js +167 -0
  6. package/dist/authSliceNative.test.js.map +1 -0
  7. package/dist/betterAuthClient.d.ts +16 -0
  8. package/dist/betterAuthClient.d.ts.map +1 -1
  9. package/dist/betterAuthClient.js +5 -2
  10. package/dist/betterAuthClient.js.map +1 -1
  11. package/dist/betterAuthClient.test.d.ts +2 -0
  12. package/dist/betterAuthClient.test.d.ts.map +1 -0
  13. package/dist/betterAuthClient.test.js +151 -0
  14. package/dist/betterAuthClient.test.js.map +1 -0
  15. package/dist/betterAuthSlice.test.js +54 -1
  16. package/dist/betterAuthSlice.test.js.map +1 -1
  17. package/dist/buildNumber.test.d.ts +2 -0
  18. package/dist/buildNumber.test.d.ts.map +1 -0
  19. package/dist/buildNumber.test.js +95 -0
  20. package/dist/buildNumber.test.js.map +1 -0
  21. package/dist/constants.d.ts +27 -3
  22. package/dist/constants.d.ts.map +1 -1
  23. package/dist/constants.js +45 -56
  24. package/dist/constants.js.map +1 -1
  25. package/dist/constants.test.js +174 -123
  26. package/dist/constants.test.js.map +1 -1
  27. package/dist/isolated/useUpgradeCheck.isolated.d.ts +2 -0
  28. package/dist/isolated/useUpgradeCheck.isolated.d.ts.map +1 -0
  29. package/dist/isolated/useUpgradeCheck.isolated.js +135 -0
  30. package/dist/isolated/useUpgradeCheck.isolated.js.map +1 -0
  31. package/dist/mongooseSlice.test.d.ts +2 -0
  32. package/dist/mongooseSlice.test.d.ts.map +1 -0
  33. package/dist/mongooseSlice.test.js +39 -0
  34. package/dist/mongooseSlice.test.js.map +1 -0
  35. package/dist/tagGenerator.test.d.ts +2 -0
  36. package/dist/tagGenerator.test.d.ts.map +1 -0
  37. package/dist/tagGenerator.test.js +96 -0
  38. package/dist/tagGenerator.test.js.map +1 -0
  39. package/dist/testPreload.test.d.ts +2 -0
  40. package/dist/testPreload.test.d.ts.map +1 -0
  41. package/dist/testPreload.test.js +27 -0
  42. package/dist/testPreload.test.js.map +1 -0
  43. package/dist/useFeatureFlags.d.ts +25 -1
  44. package/dist/useFeatureFlags.d.ts.map +1 -1
  45. package/dist/useFeatureFlags.js +18 -16
  46. package/dist/useFeatureFlags.js.map +1 -1
  47. package/dist/useFeatureFlags.test.d.ts +2 -0
  48. package/dist/useFeatureFlags.test.d.ts.map +1 -0
  49. package/dist/useFeatureFlags.test.js +162 -0
  50. package/dist/useFeatureFlags.test.js.map +1 -0
  51. package/dist/useUpgradeCheck.d.ts +2 -0
  52. package/dist/useUpgradeCheck.d.ts.map +1 -1
  53. package/dist/useUpgradeCheck.js +39 -46
  54. package/dist/useUpgradeCheck.js.map +1 -1
  55. package/dist/useUpgradeCheck.test.d.ts +2 -0
  56. package/dist/useUpgradeCheck.test.d.ts.map +1 -0
  57. package/dist/useUpgradeCheck.test.js +326 -0
  58. package/dist/useUpgradeCheck.test.js.map +1 -0
  59. package/package.json +6 -3
  60. package/src/authSlice.test.ts +79 -0
  61. package/src/authSliceNative.test.ts +187 -0
  62. package/src/betterAuthClient.test.ts +176 -0
  63. package/src/betterAuthClient.ts +6 -3
  64. package/src/betterAuthSlice.test.ts +67 -0
  65. package/src/buildNumber.test.ts +120 -0
  66. package/src/constants.test.ts +193 -154
  67. package/src/constants.ts +72 -70
  68. package/src/isolated/useUpgradeCheck.isolated.ts +175 -0
  69. package/src/mongooseSlice.test.ts +46 -0
  70. package/src/tagGenerator.test.ts +109 -0
  71. package/src/testPreload.test.ts +30 -0
  72. package/src/useFeatureFlags.test.ts +209 -0
  73. package/src/useFeatureFlags.ts +44 -5
  74. package/src/useUpgradeCheck.test.ts +408 -0
  75. package/src/useUpgradeCheck.ts +41 -48
  76. package/dist/test-preload.d.ts +0 -2
  77. package/dist/test-preload.d.ts.map +0 -1
  78. package/dist/test-preload.js +0 -24
  79. package/dist/test-preload.js.map +0 -1
  80. package/src/test-preload.ts +0 -28
@@ -1,189 +1,228 @@
1
1
  import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
2
2
 
3
- type ConstantsModule = typeof import("./constants");
4
-
5
- type ExpoConstantsMock = {
6
- experienceUrl?: string;
7
- expoConfig?: {
8
- extra?: Record<string, string>;
9
- hostUri?: string;
10
- };
11
- expoGoConfig?: {
12
- debuggerHost?: string;
13
- };
14
- };
15
-
16
- const loadConstantsModule = async ({
17
- apiUrl,
18
- constantsMock,
19
- isDev = false,
20
- }: {
21
- apiUrl?: string;
22
- constantsMock: ExpoConstantsMock;
23
- isDev?: boolean;
24
- }): Promise<ConstantsModule> => {
25
- const hasApiUrl = "EXPO_PUBLIC_API_URL" in process.env;
26
- const previousApiUrl = process.env.EXPO_PUBLIC_API_URL;
27
- const previousDev = (globalThis as {__DEV__?: boolean}).__DEV__;
28
-
29
- if (apiUrl) {
30
- process.env.EXPO_PUBLIC_API_URL = apiUrl;
31
- } else {
32
- delete process.env.EXPO_PUBLIC_API_URL;
33
- }
34
-
35
- (globalThis as {__DEV__?: boolean}).__DEV__ = isDev;
36
-
37
- mock.module("expo-constants", () => ({
38
- default: constantsMock,
39
- }));
40
-
41
- const testCaseId = `${Date.now()}-${Math.random()}`;
42
- const module = (await import(`./constants?testCase=${testCaseId}`)) as ConstantsModule;
43
-
44
- if (hasApiUrl) {
45
- process.env.EXPO_PUBLIC_API_URL = previousApiUrl;
46
- } else {
47
- delete process.env.EXPO_PUBLIC_API_URL;
48
- }
49
-
50
- if (typeof previousDev === "undefined") {
51
- delete (globalThis as {__DEV__?: boolean}).__DEV__;
52
- } else {
53
- (globalThis as {__DEV__?: boolean}).__DEV__ = previousDev;
54
- }
55
-
56
- return module;
57
- };
58
-
59
- describe("constants", () => {
60
- const originalDebug = console.debug;
61
- const originalError = console.error;
62
-
63
- beforeEach(() => {
64
- console.debug = mock(() => {});
65
- console.error = mock(() => {});
3
+ import {
4
+ AUTH_DEBUG,
5
+ baseTasksUrl,
6
+ baseUrl,
7
+ baseWebsocketsUrl,
8
+ logAuth,
9
+ logSocket,
10
+ resolveBaseUrls,
11
+ } from "./constants";
12
+
13
+ describe("resolveBaseUrls", () => {
14
+ it("treats an empty envApiUrl as unset and falls back to localhost", () => {
15
+ const urls = resolveBaseUrls({
16
+ envApiUrl: "",
17
+ expoConstants: {expoConfig: {extra: {}}},
18
+ isDev: false,
19
+ });
20
+ expect(urls.baseUrl).toBe("http://localhost:4000");
21
+ expect(urls.baseWebsocketsUrl).toBe("ws://localhost:4000/");
22
+ expect(urls.baseTasksUrl).toBe("http://localhost:4000/tasks");
66
23
  });
67
24
 
68
- afterEach(() => {
69
- console.debug = originalDebug;
70
- console.error = originalError;
71
- });
72
-
73
- it("uses EXPO_PUBLIC_API_URL override and enables debug logs", async () => {
74
- const constants = await loadConstantsModule({
75
- apiUrl: "https://api.example.com",
76
- constantsMock: {
77
- expoConfig: {
78
- extra: {
79
- APP_ENV: "production",
80
- AUTH_DEBUG: "true",
81
- WEBSOCKETS_DEBUG: "true",
82
- },
83
- },
84
- },
25
+ it("treats an empty BASE_URL extra as unset and falls back to localhost in non-dev", () => {
26
+ const urls = resolveBaseUrls({
27
+ expoConstants: {expoConfig: {extra: {BASE_URL: ""}}},
85
28
  isDev: false,
86
29
  });
30
+ expect(urls.baseUrl).toBe("http://localhost:4000");
31
+ });
87
32
 
88
- expect(constants.baseUrl).toBe("https://api.example.com");
89
- expect(constants.baseWebsocketsUrl).toBe("https://ws.example.com/");
90
- expect(constants.baseTasksUrl).toBe("https://tasks.example.com/tasks");
91
- expect(constants.AUTH_DEBUG).toBe(true);
92
-
93
- constants.logAuth("auth message");
94
- constants.logSocket(undefined, "socket message");
95
-
96
- expect(console.debug).toHaveBeenCalledWith("auth message");
97
- expect(console.debug).toHaveBeenCalledWith("[websocket]", "socket message");
33
+ it("uses env override when provided", () => {
34
+ const urls = resolveBaseUrls({
35
+ envApiUrl: "https://api.example.com",
36
+ expoConstants: {expoConfig: {extra: {}}},
37
+ isDev: false,
38
+ });
39
+ expect(urls.baseUrl).toBe("https://api.example.com");
40
+ expect(urls.baseWebsocketsUrl).toBe("https://ws.example.com/");
41
+ expect(urls.baseTasksUrl).toBe("https://tasks.example.com/tasks");
98
42
  });
99
43
 
100
- it("resolves dev hostUri URLs when __DEV__ is true", async () => {
101
- const constants = await loadConstantsModule({
102
- constantsMock: {
103
- expoConfig: {
104
- extra: {},
105
- hostUri: "10.0.0.12:8081",
106
- },
107
- },
44
+ it("uses hostUri in dev mode", () => {
45
+ const urls = resolveBaseUrls({
46
+ expoConstants: {expoConfig: {extra: {}, hostUri: "10.0.0.12:8081"}},
108
47
  isDev: true,
109
48
  });
110
-
111
- expect(constants.baseUrl).toBe("http://10.0.0.12:4000");
112
- expect(constants.baseWebsocketsUrl).toBe("ws://10.0.0.12:4000/");
113
- expect(constants.baseTasksUrl).toBe("http://10.0.0.12:4000/tasks");
49
+ expect(urls.baseUrl).toBe("http://10.0.0.12:4000");
50
+ expect(urls.baseWebsocketsUrl).toBe("ws://10.0.0.12:4000/");
51
+ expect(urls.baseTasksUrl).toBe("http://10.0.0.12:4000/tasks");
114
52
  });
115
53
 
116
- it("resolves dev experienceUrl URLs when hostUri is unavailable", async () => {
117
- const constants = await loadConstantsModule({
118
- constantsMock: {
54
+ it("falls back to experienceUrl in dev mode when hostUri missing", () => {
55
+ const urls = resolveBaseUrls({
56
+ expoConstants: {
119
57
  experienceUrl: "exp://192.168.1.20:19000",
120
- expoConfig: {
121
- extra: {},
122
- },
58
+ expoConfig: {extra: {}},
123
59
  },
124
60
  isDev: true,
125
61
  });
62
+ expect(urls.baseUrl).toBe("http://192.168.1.20:4000");
63
+ expect(urls.baseWebsocketsUrl).toBe("ws://192.168.1.20:4000/");
64
+ expect(urls.baseTasksUrl).toBe("http://192.168.1.20:4000/tasks");
65
+ });
126
66
 
127
- expect(constants.baseUrl).toBe("http://192.168.1.20:4000");
128
- expect(constants.baseWebsocketsUrl).toBe("ws://192.168.1.20:4000/");
129
- expect(constants.baseTasksUrl).toBe("http://192.168.1.20:4000/tasks");
67
+ it("falls back to localhost in dev mode when nothing else is available", () => {
68
+ const urls = resolveBaseUrls({
69
+ expoConstants: {expoConfig: {extra: {}}},
70
+ isDev: true,
71
+ });
72
+ expect(urls.baseUrl).toBe("http://localhost:4000");
73
+ expect(urls.baseWebsocketsUrl).toBe("ws://localhost:4000/");
74
+ expect(urls.baseTasksUrl).toBe("http://localhost:4000/tasks");
130
75
  });
131
76
 
132
- it("uses BASE_URL outside dev mode and allows user websocket debug flag", async () => {
133
- const constants = await loadConstantsModule({
134
- constantsMock: {
135
- expoConfig: {
136
- extra: {
137
- BASE_URL: "https://api.prod.com",
138
- WEBSOCKETS_DEBUG: "false",
139
- },
140
- },
141
- },
77
+ it("uses BASE_URL from extra when not in dev mode", () => {
78
+ const urls = resolveBaseUrls({
79
+ expoConstants: {expoConfig: {extra: {BASE_URL: "https://api.prod.com"}}},
142
80
  isDev: false,
143
81
  });
82
+ expect(urls.baseUrl).toBe("https://api.prod.com");
83
+ expect(urls.baseWebsocketsUrl).toBe("https://ws.prod.com/");
84
+ expect(urls.baseTasksUrl).toBe("https://tasks.prod.com/tasks");
85
+ });
144
86
 
145
- expect(constants.baseUrl).toBe("https://api.prod.com");
146
- expect(constants.baseWebsocketsUrl).toBe("https://ws.prod.com/");
147
- expect(constants.baseTasksUrl).toBe("https://tasks.prod.com/tasks");
148
-
149
- constants.logSocket({featureFlags: {debugWebsockets: {enabled: true}}}, "enabled");
150
- expect(console.debug).toHaveBeenCalledWith("[websocket]", "enabled");
87
+ it("falls back to hostUri when BASE_URL absent in non-dev", () => {
88
+ const urls = resolveBaseUrls({
89
+ expoConstants: {expoConfig: {extra: {}, hostUri: "172.16.0.3:8081"}},
90
+ isDev: false,
91
+ });
92
+ expect(urls.baseUrl).toBe("http://172.16.0.3:4000");
93
+ expect(urls.baseWebsocketsUrl).toBe("ws://172.16.0.3:4000/");
94
+ expect(urls.baseTasksUrl).toBe("http://172.16.0.3:4000/tasks");
151
95
  });
152
96
 
153
- it("falls back to non-dev hostUri when BASE_URL is missing", async () => {
154
- const constants = await loadConstantsModule({
155
- constantsMock: {
156
- expoConfig: {
157
- extra: {},
158
- hostUri: "172.16.0.3:8081",
159
- },
97
+ it("falls back to experienceUrl in non-dev when hostUri absent", () => {
98
+ const urls = resolveBaseUrls({
99
+ expoConstants: {
100
+ experienceUrl: "exp://10.1.2.3:19000",
101
+ expoConfig: {extra: {}},
160
102
  },
161
103
  isDev: false,
162
104
  });
163
-
164
- expect(constants.baseUrl).toBe("http://172.16.0.3:4000");
165
- expect(constants.baseWebsocketsUrl).toBe("ws://172.16.0.3:4000/");
166
- expect(constants.baseTasksUrl).toBe("http://172.16.0.3:4000/tasks");
105
+ expect(urls.baseUrl).toBe("http://10.1.2.3:4000");
167
106
  });
168
107
 
169
- it("warns for exp.direct tunnels and falls back to localhost", async () => {
170
- const constants = await loadConstantsModule({
171
- constantsMock: {
172
- expoConfig: {
173
- extra: {},
174
- },
175
- expoGoConfig: {
176
- debuggerHost: "abc.exp.direct:80",
177
- },
178
- },
108
+ it("defaults to localhost in non-dev when nothing is configured", () => {
109
+ const urls = resolveBaseUrls({
110
+ expoConstants: {expoConfig: {extra: {}}},
179
111
  isDev: false,
180
112
  });
113
+ expect(urls.baseUrl).toBe("http://localhost:4000");
114
+ });
115
+ });
116
+
117
+ describe("module-level exports", () => {
118
+ it("exports baseUrl / websockets / tasks URLs that resolve to localhost with default mocks", () => {
119
+ expect(baseUrl).toBe("http://localhost:4000");
120
+ expect(baseWebsocketsUrl).toBe("ws://localhost:4000/");
121
+ expect(baseTasksUrl).toBe("http://localhost:4000/tasks");
122
+ });
123
+
124
+ it("AUTH_DEBUG is false by default with test-preload mocks", () => {
125
+ expect(AUTH_DEBUG).toBe(false);
126
+ });
127
+ });
128
+
129
+ describe("logAuth / logSocket", () => {
130
+ const originalDebug = console.debug;
131
+ const calls: unknown[][] = [];
132
+
133
+ beforeEach(() => {
134
+ calls.length = 0;
135
+ console.debug = (...args: unknown[]): void => {
136
+ calls.push(args);
137
+ };
138
+ });
139
+
140
+ afterEach(() => {
141
+ console.debug = originalDebug;
142
+ });
143
+
144
+ it("logAuth is a no-op when AUTH_DEBUG is disabled", () => {
145
+ logAuth("auth message");
146
+ expect(calls).toEqual([]);
147
+ });
148
+
149
+ it("logSocket logs when passed boolean true", () => {
150
+ logSocket(true, "socket message");
151
+ expect(calls).toEqual([["[websocket]", "socket message"]]);
152
+ });
153
+
154
+ it("logSocket does not log when passed boolean false", () => {
155
+ logSocket(false, "suppressed");
156
+ expect(calls).toEqual([]);
157
+ });
158
+
159
+ it("logSocket logs when user has debugWebsockets feature flag enabled", () => {
160
+ logSocket({featureFlags: {debugWebsockets: {enabled: true}}}, "enabled");
161
+ expect(calls).toEqual([["[websocket]", "enabled"]]);
162
+ });
163
+
164
+ it("logSocket does not log when user has debugWebsockets disabled", () => {
165
+ logSocket({featureFlags: {debugWebsockets: {enabled: false}}}, "disabled");
166
+ expect(calls).toEqual([]);
167
+ });
168
+
169
+ it("logSocket does not log with undefined user", () => {
170
+ logSocket(undefined, "no user");
171
+ expect(calls).toEqual([]);
172
+ });
173
+ });
174
+
175
+ describe("expo tunnel warning", () => {
176
+ it("warns when expoGoConfig.debuggerHost contains exp.direct", async () => {
177
+ const errorCalls: unknown[][] = [];
178
+ const originalError = console.error;
179
+ console.error = (...args: unknown[]): void => {
180
+ errorCalls.push(args);
181
+ };
182
+ mock.module("expo-constants", () => ({
183
+ default: {
184
+ expoConfig: {extra: {}},
185
+ expoGoConfig: {debuggerHost: "abc.exp.direct"},
186
+ },
187
+ }));
188
+ try {
189
+ const testId = `${Date.now()}-${Math.random()}`;
190
+ await import(`./constants?case=${testId}`);
191
+ const warning = errorCalls.find((args) =>
192
+ args.some((v) => typeof v === "string" && v.includes("Expo Tunnel is not currently"))
193
+ );
194
+ expect(warning).toBeDefined();
195
+ } finally {
196
+ console.error = originalError;
197
+ mock.module("expo-constants", () => ({default: {expoConfig: {extra: {}}}}));
198
+ }
199
+ });
200
+ });
181
201
 
182
- expect(console.error).toHaveBeenCalledWith(
183
- "Expo Tunnel is not currently supported for connecting to the API, please use LAN or Local mode."
184
- );
185
- expect(constants.baseUrl).toBe("http://localhost:4000");
186
- expect(constants.baseWebsocketsUrl).toBe("ws://localhost:4000/");
187
- expect(constants.baseTasksUrl).toBe("http://localhost:4000/tasks");
202
+ describe("AUTH_DEBUG enabled path", () => {
203
+ it("logs debug messages from logAuth when AUTH_DEBUG is true on module load", async () => {
204
+ const debugCalls: unknown[][] = [];
205
+ const originalDebug = console.debug;
206
+ console.debug = (...args: unknown[]): void => {
207
+ debugCalls.push(args);
208
+ };
209
+ mock.module("expo-constants", () => ({
210
+ default: {expoConfig: {extra: {AUTH_DEBUG: "true", WEBSOCKETS_DEBUG: "true"}}},
211
+ }));
212
+ try {
213
+ const testId = `${Date.now()}-${Math.random()}`;
214
+ const loaded = (await import(`./constants?case=${testId}`)) as typeof import("./constants");
215
+ expect(loaded.AUTH_DEBUG).toBe(true);
216
+ const preLength = debugCalls.length;
217
+ loaded.logAuth("hello");
218
+ expect(debugCalls.length).toBe(preLength + 1);
219
+ loaded.logSocket(undefined, "ws on");
220
+ expect(debugCalls.some((args) => args[0] === "[websocket]" && args[1] === "ws on")).toBe(
221
+ true
222
+ );
223
+ } finally {
224
+ console.debug = originalDebug;
225
+ mock.module("expo-constants", () => ({default: {expoConfig: {extra: {}}}}));
226
+ }
188
227
  });
189
228
  });
package/src/constants.ts CHANGED
@@ -37,6 +37,65 @@ export const logSocket = (
37
37
  }
38
38
  };
39
39
 
40
+ export interface ExpoConstantsShape {
41
+ experienceUrl?: string;
42
+ expoConfig?: {
43
+ extra?: Record<string, string | undefined>;
44
+ hostUri?: string;
45
+ };
46
+ expoGoConfig?: {
47
+ debuggerHost?: string;
48
+ };
49
+ }
50
+
51
+ export interface BaseUrls {
52
+ baseUrl: string;
53
+ baseWebsocketsUrl: string;
54
+ baseTasksUrl: string;
55
+ }
56
+
57
+ const LOCALHOST: BaseUrls = {
58
+ baseTasksUrl: "http://localhost:4000/tasks",
59
+ baseUrl: "http://localhost:4000",
60
+ baseWebsocketsUrl: "ws://localhost:4000/",
61
+ };
62
+
63
+ /**
64
+ * Pure resolver for the base URLs used throughout the RTK package.
65
+ * Decoupled from the Expo-constants module so it can be unit tested.
66
+ */
67
+ export const resolveBaseUrls = (args: {
68
+ envApiUrl?: string;
69
+ expoConstants: ExpoConstantsShape;
70
+ isDev: boolean;
71
+ }): BaseUrls => {
72
+ const hostUriPrefix = args.expoConstants.expoConfig?.hostUri?.split(":").shift();
73
+ const experiencePrefix = args.expoConstants.experienceUrl?.split(":")[1];
74
+ const baseFromExtra = args.expoConstants.expoConfig?.extra?.BASE_URL;
75
+ const base = args.envApiUrl || (!args.isDev ? baseFromExtra : undefined);
76
+ const host = args.isDev ? hostUriPrefix : !base ? hostUriPrefix : undefined;
77
+ const experience = !base && !host ? experiencePrefix : undefined;
78
+ if (base)
79
+ return {
80
+ baseTasksUrl: `${base.replace("api.", "tasks.")}/tasks`,
81
+ baseUrl: base,
82
+ baseWebsocketsUrl: `${base.replace("api.", "ws.")}/`,
83
+ };
84
+ if (host)
85
+ return {
86
+ baseTasksUrl: `http://${host}:4000/tasks`,
87
+ baseUrl: `http://${host}:4000`,
88
+ baseWebsocketsUrl: `ws://${host}:4000/`,
89
+ };
90
+ if (experience)
91
+ return {
92
+ baseTasksUrl: `http:${experience}:4000/tasks`,
93
+ baseUrl: `http:${experience}:4000`,
94
+ baseWebsocketsUrl: `ws:${experience}:4000/`,
95
+ };
96
+ return LOCALHOST;
97
+ };
98
+
40
99
  // When we use "expo publish", we want to point the API at the prod API. In the future,
41
100
  // we'll want to point at the staging API, and probably have a development release channel.
42
101
  if (Constants.expoGoConfig?.debuggerHost?.includes("exp.direct")) {
@@ -45,77 +104,20 @@ if (Constants.expoGoConfig?.debuggerHost?.includes("exp.direct")) {
45
104
  );
46
105
  }
47
106
 
48
- export let baseUrl: string;
49
- export let baseWebsocketsUrl: string;
50
- export let baseTasksUrl: string;
51
-
52
107
  const isDev = typeof __DEV__ !== "undefined" && __DEV__;
53
108
 
54
- if (process.env.EXPO_PUBLIC_API_URL) {
55
- // Explicit override (e.g. .env)
56
- baseUrl = process.env.EXPO_PUBLIC_API_URL;
57
- baseWebsocketsUrl = `${baseUrl.replace("api.", "ws.")}/`;
58
- baseTasksUrl = `${baseUrl.replace("api.", "tasks.")}/tasks`;
109
+ const resolved = resolveBaseUrls({
110
+ envApiUrl: process.env.EXPO_PUBLIC_API_URL,
111
+ expoConstants: Constants as ExpoConstantsShape,
112
+ isDev,
113
+ });
59
114
 
60
- console.debug(
61
- `Base URL set to apiUrl ${baseUrl} for env ${
62
- Constants.expoConfig?.extra?.APP_ENV ?? "unknown"
63
- }, websocket to ${baseWebsocketsUrl}, tasks to ${baseTasksUrl}`
64
- );
65
- } else if (isDev && Constants.expoConfig?.hostUri) {
66
- // Dev simulator/device
67
- baseUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}`;
68
- baseWebsocketsUrl = `ws://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/`;
69
- baseTasksUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/tasks`;
70
- console.debug(
71
- `Base URL set to hostUri ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
72
- Constants.expoConfig?.hostUri
73
- );
74
- } else if (isDev && Constants.experienceUrl) {
75
- // Dev web (experienceUrl)
76
- baseUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}`;
77
- baseWebsocketsUrl = `ws:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/`;
78
- baseTasksUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/tasks`;
79
- console.debug(
80
- `Base URL set to experienceUrl ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
81
- Constants.expoConfig?.hostUri
82
- );
83
- } else if (isDev) {
84
- // Dev web fallback
85
- baseUrl = `http://localhost:4000`;
86
- baseWebsocketsUrl = `ws://localhost:4000/`;
87
- baseTasksUrl = `http://localhost:4000/tasks`;
88
- console.debug(`Base URL set to localhost ${baseUrl}, websocket to ${baseWebsocketsUrl}`);
89
- } else if (Constants.expoConfig?.extra?.BASE_URL) {
90
- // Prod/staging
91
- baseUrl = Constants.expoConfig?.extra?.BASE_URL;
92
- baseWebsocketsUrl = `${baseUrl.replace("api.", "ws.")}/`;
93
- baseTasksUrl = `${baseUrl.replace("api.", "tasks.")}/tasks`;
115
+ export const baseUrl = resolved.baseUrl;
116
+ export const baseWebsocketsUrl = resolved.baseWebsocketsUrl;
117
+ export const baseTasksUrl = resolved.baseTasksUrl;
94
118
 
95
- console.debug(
96
- `Base URL set to apiUrl ${baseUrl} for env ${
97
- Constants.expoConfig?.extra?.APP_ENV ?? "unknown"
98
- }, websocket to ${baseWebsocketsUrl}, tasks to ${baseTasksUrl}`
99
- );
100
- } else if (Constants.expoConfig?.hostUri) {
101
- baseUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}`;
102
- baseWebsocketsUrl = `ws://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/`;
103
- baseTasksUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/tasks`;
104
- console.debug(
105
- `Base URL set to hostUri ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
106
- Constants.expoConfig?.hostUri
107
- );
108
- } else if (Constants.experienceUrl) {
109
- baseUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}`;
110
- baseWebsocketsUrl = `ws:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/`;
111
- baseTasksUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/tasks`;
112
- console.debug(
113
- `Base URL set to experienceUrl ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
114
- Constants.expoConfig?.hostUri
115
- );
116
- } else {
117
- baseUrl = `http://localhost:4000`;
118
- baseWebsocketsUrl = `ws://localhost:4000/`;
119
- baseTasksUrl = `http://localhost:4000/tasks`;
120
- console.debug(`Base URL set to localhost ${baseUrl}, websocket to ${baseWebsocketsUrl}`);
121
- }
119
+ console.debug(
120
+ `Base URL set to ${baseUrl} for env ${
121
+ Constants.expoConfig?.extra?.APP_ENV ?? "unknown"
122
+ }, websocket to ${baseWebsocketsUrl}, tasks to ${baseTasksUrl}`
123
+ );