quickbase-js 0.1.0-beta.10

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 (165) hide show
  1. package/CHANGELOG.md +211 -0
  2. package/LICENSE +21 -0
  3. package/README.md +179 -0
  4. package/api-examples.md +49 -0
  5. package/build-common.js +131 -0
  6. package/build-esm.js +71 -0
  7. package/build-umd.js +69 -0
  8. package/dist/esm/quickbase.d.ts +8081 -0
  9. package/dist/esm/quickbase.js +7928 -0
  10. package/dist/esm/quickbase.js.map +1 -0
  11. package/dist/esm/quickbase.min.js +2 -0
  12. package/dist/esm/quickbase.min.js.map +1 -0
  13. package/dist/umd/quickbase.umd.js +8504 -0
  14. package/dist/umd/quickbase.umd.js.map +1 -0
  15. package/dist/umd/quickbase.umd.min.js +2 -0
  16. package/dist/umd/quickbase.umd.min.js.map +1 -0
  17. package/docs/.vitepress/cache/deps/@theme_index.js +275 -0
  18. package/docs/.vitepress/cache/deps/@theme_index.js.map +7 -0
  19. package/docs/.vitepress/cache/deps/_metadata.json +40 -0
  20. package/docs/.vitepress/cache/deps/chunk-5A67QEFC.js +12542 -0
  21. package/docs/.vitepress/cache/deps/chunk-5A67QEFC.js.map +7 -0
  22. package/docs/.vitepress/cache/deps/chunk-7D4P4MMP.js +9719 -0
  23. package/docs/.vitepress/cache/deps/chunk-7D4P4MMP.js.map +7 -0
  24. package/docs/.vitepress/cache/deps/package.json +3 -0
  25. package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js +4504 -0
  26. package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js.map +7 -0
  27. package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js +583 -0
  28. package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js.map +7 -0
  29. package/docs/.vitepress/cache/deps/vue.js +343 -0
  30. package/docs/.vitepress/cache/deps/vue.js.map +7 -0
  31. package/docs/.vitepress/config.mts +26 -0
  32. package/docs/api-examples.md +49 -0
  33. package/docs/index.md +59 -0
  34. package/docs/markdown-examples.md +85 -0
  35. package/docs-data/api-docs.json +4143 -0
  36. package/examples/quicbase-jsCDN.html +36 -0
  37. package/examples/react.js +48 -0
  38. package/examples/vanillajs.html +47 -0
  39. package/index.md +25 -0
  40. package/markdown-examples.md +85 -0
  41. package/open-api/fix-spec-main.ts +133 -0
  42. package/open-api/generate-unified-interface.ts +104 -0
  43. package/open-api/generateDocsJson.ts +24 -0
  44. package/open-api/output/quickbase-fixed.json +11191 -0
  45. package/open-api/post-process-generated.ts +226 -0
  46. package/open-api/regenerate-client.ts +117 -0
  47. package/open-api/schema/enhance-general.ts +111 -0
  48. package/open-api/schema/fix-arrays.ts +68 -0
  49. package/open-api/schema/tags/apps.ts +90 -0
  50. package/open-api/schema/tags/fields.ts +471 -0
  51. package/open-api/schema/tags/groups.ts +51 -0
  52. package/open-api/schema/tags/index.ts +66 -0
  53. package/open-api/schema/tags/records.ts +115 -0
  54. package/open-api/schema/tags/tables.ts +211 -0
  55. package/open-api/schema/tags/users.ts +47 -0
  56. package/open-api/types/spec.ts +42 -0
  57. package/open-api/utils/common.ts +29 -0
  58. package/open-api/utils/generateJsDoc.ts +74 -0
  59. package/open-api/utils/infer-schema.ts +40 -0
  60. package/open-api/utils/naming.ts +10 -0
  61. package/open-api/utils/sharedUtils.ts +352 -0
  62. package/package.json +96 -0
  63. package/playwright.config.ts +37 -0
  64. package/rollup.config.js +114 -0
  65. package/specs/QuickBase_RESTful_API_2025-03-04T06_22_39.725Z.json +46878 -0
  66. package/specs/split/apps.json +1315 -0
  67. package/specs/split/auth.json +191 -0
  68. package/specs/split/fields.json +3869 -0
  69. package/specs/split/records.json +754 -0
  70. package/specs/split/reports.json +619 -0
  71. package/specs/split/tables.json +1605 -0
  72. package/src/RateLimitError.ts +12 -0
  73. package/src/ThrottleBucket.ts +41 -0
  74. package/src/authorizationStrategy.ts +283 -0
  75. package/src/generated-unified/QuickbaseClient.ts +1135 -0
  76. package/src/generated-unified/missing-types-report.json +3 -0
  77. package/src/index.ts +1 -0
  78. package/src/invokeMethod.ts +245 -0
  79. package/src/quickbaseClient.ts +194 -0
  80. package/src/rateLimiter.ts +25 -0
  81. package/src/tokenCache.ts +35 -0
  82. package/src/utils.ts +50 -0
  83. package/tests/playwright/qb/auth/cacheSwitching.test.ts +159 -0
  84. package/tests/playwright/qb/auth/fetchTempToken401.test.ts +219 -0
  85. package/tests/playwright/qb/auth/getAppWithTempToken.test.ts +162 -0
  86. package/tests/playwright/qb/auth/getAppWithUserToken.test.ts +153 -0
  87. package/tests/playwright/qb/auth/getTempToken.test.ts +152 -0
  88. package/tests/playwright/qb/auth/tempTokenPrefetch.test.ts +179 -0
  89. package/tests/playwright/qb/auth/tempTokenRenewal.test.ts +133 -0
  90. package/tests/setup.ts +53 -0
  91. package/tests/vitest/qb/apps/copyApp.test.ts +76 -0
  92. package/tests/vitest/qb/apps/createApp.test.ts +64 -0
  93. package/tests/vitest/qb/apps/deleteApp.test.ts +76 -0
  94. package/tests/vitest/qb/apps/getApp.test.ts +44 -0
  95. package/tests/vitest/qb/apps/getAppEvents.test.ts +67 -0
  96. package/tests/vitest/qb/apps/updateApp.test.ts +58 -0
  97. package/tests/vitest/qb/auth/mockQuickbaseServer.ts +60 -0
  98. package/tests/vitest/qb/auth/retryUserToken401.test.ts +73 -0
  99. package/tests/vitest/qb/auth/ssoTokenIntegrationWithServer.test.ts +65 -0
  100. package/tests/vitest/qb/fields/createField.test.ts +77 -0
  101. package/tests/vitest/qb/fields/deleteFields.test.ts +83 -0
  102. package/tests/vitest/qb/fields/getField.test.ts +85 -0
  103. package/tests/vitest/qb/fields/getFieldUsage.test.ts +82 -0
  104. package/tests/vitest/qb/fields/getFields.test.ts +232 -0
  105. package/tests/vitest/qb/fields/getFieldsUsage.test.ts +77 -0
  106. package/tests/vitest/qb/fields/updateField.test.ts +93 -0
  107. package/tests/vitest/qb/formulas/runFormula.test.ts +96 -0
  108. package/tests/vitest/qb/index.ts +29 -0
  109. package/tests/vitest/qb/records/deleteRecords.test.ts +110 -0
  110. package/tests/vitest/qb/records/runQuery.test.ts +146 -0
  111. package/tests/vitest/qb/records/upsert.test.ts +102 -0
  112. package/tests/vitest/qb/tables/createRelationship.test.ts +102 -0
  113. package/tests/vitest/qb/tables/createTable.test.ts +55 -0
  114. package/tests/vitest/qb/tables/deleteRelationship.test.ts +143 -0
  115. package/tests/vitest/qb/tables/deleteTable.test.ts +50 -0
  116. package/tests/vitest/qb/tables/getAppTables.test.ts +46 -0
  117. package/tests/vitest/qb/tables/getRelationships.test.ts +80 -0
  118. package/tests/vitest/qb/tables/getTable.test.ts +26 -0
  119. package/tests/vitest/qb/tables/updateRelationship.test.ts +250 -0
  120. package/tests/vitest/qb/tables/updateTable.test.ts +48 -0
  121. package/tests/vitest/unit/ThrottleBucket.test.ts +95 -0
  122. package/tests/vitest/unit/apps/copyApp.test.ts +581 -0
  123. package/tests/vitest/unit/apps/createApp.test.ts +186 -0
  124. package/tests/vitest/unit/apps/deleteApp.test.ts +216 -0
  125. package/tests/vitest/unit/apps/getApp.test.ts +128 -0
  126. package/tests/vitest/unit/apps/getAppEvents.test.ts +193 -0
  127. package/tests/vitest/unit/apps/updateApp.test.ts +412 -0
  128. package/tests/vitest/unit/auth/fetchTempToken401.test.ts +63 -0
  129. package/tests/vitest/unit/auth/getTempToken.test.ts +119 -0
  130. package/tests/vitest/unit/auth/retryTempToken401.test.ts +95 -0
  131. package/tests/vitest/unit/auth/retryTempTokenMaxRetries.test.ts +206 -0
  132. package/tests/vitest/unit/auth/retryUserToken401.test.ts +69 -0
  133. package/tests/vitest/unit/auth/ssoTokenRefresh.test.ts +325 -0
  134. package/tests/vitest/unit/fields/createField.test.ts +451 -0
  135. package/tests/vitest/unit/fields/deleteFields.test.ts +141 -0
  136. package/tests/vitest/unit/fields/getField.test.ts +189 -0
  137. package/tests/vitest/unit/fields/getFieldUsage.test.ts +180 -0
  138. package/tests/vitest/unit/fields/getFields.test.ts +122 -0
  139. package/tests/vitest/unit/fields/getFieldsUsage.test.ts +160 -0
  140. package/tests/vitest/unit/fields/updateField.test.ts +285 -0
  141. package/tests/vitest/unit/formulas/runFormula.test.ts +205 -0
  142. package/tests/vitest/unit/index.ts +34 -0
  143. package/tests/vitest/unit/quickbaseClientHttpMethods.test.ts +139 -0
  144. package/tests/vitest/unit/rateLimit.test.ts +191 -0
  145. package/tests/vitest/unit/records/deleteRecords.test.ts +112 -0
  146. package/tests/vitest/unit/records/runQuery.test.ts +102 -0
  147. package/tests/vitest/unit/records/upsert.test.ts +234 -0
  148. package/tests/vitest/unit/tables/createRelationship.test.ts +212 -0
  149. package/tests/vitest/unit/tables/createTable.test.ts +70 -0
  150. package/tests/vitest/unit/tables/deleteRelationship.test.ts +97 -0
  151. package/tests/vitest/unit/tables/deleteTable.test.ts +77 -0
  152. package/tests/vitest/unit/tables/getAppTables.test.ts +85 -0
  153. package/tests/vitest/unit/tables/getRelationships.test.ts +259 -0
  154. package/tests/vitest/unit/tables/getTable.test.ts +233 -0
  155. package/tests/vitest/unit/tables/updateRelationship.test.ts +307 -0
  156. package/tests/vitest/unit/tables/updateTable.test.ts +80 -0
  157. package/tests/vitest/unit/tokenLifespan.test.ts +204 -0
  158. package/tests/vitest/unit/umd.test.ts +8 -0
  159. package/tools/log-gen.ts +251 -0
  160. package/tools/log-gen.yaml +4777 -0
  161. package/tools/openapi-jars/openapi-generator-cli-7.12.0.jar +0 -0
  162. package/tsconfig.build.json +17 -0
  163. package/tsconfig.json +32 -0
  164. package/versionrc.json +9 -0
  165. package/vitest.config.ts +27 -0
@@ -0,0 +1,581 @@
1
+ import { describe, it, expect, beforeEach } from "vitest";
2
+ import {
3
+ createClient,
4
+ mockFetch,
5
+ QB_APP_ID,
6
+ QB_REALM,
7
+ QB_USER_TOKEN,
8
+ } from "@tests/setup.ts";
9
+ import { CopyAppRequest, CopyApp200Response } from "@/generated/models";
10
+
11
+ describe("QuickbaseClient Unit - copyApp", () => {
12
+ let client: ReturnType<typeof createClient>;
13
+
14
+ beforeEach(() => {
15
+ mockFetch.mockClear();
16
+ });
17
+
18
+ it("initializes without errors", () => {
19
+ client = createClient(mockFetch, { debug: true });
20
+ expect(client).toBeDefined();
21
+ });
22
+
23
+ it("has copyApp method", () => {
24
+ client = createClient(mockFetch, { debug: true });
25
+ expect(typeof client.copyApp).toBe("function");
26
+ });
27
+
28
+ it("calls copyApp successfully with user token and dates as Date objects", async () => {
29
+ client = createClient(mockFetch, { debug: true }); // convertDates defaults to true
30
+
31
+ const request: CopyAppRequest = {
32
+ name: "Copied App",
33
+ description: "A copy of the original app",
34
+ properties: {
35
+ keepData: false,
36
+ excludeFiles: true,
37
+ usersAndRoles: false,
38
+ assignUserToken: true,
39
+ },
40
+ };
41
+
42
+ const mockResponse: CopyApp200Response = {
43
+ id: "bpqe82s1",
44
+ name: "Copied App",
45
+ description: "A copy of the original app",
46
+ created: new Date("2025-03-11T10:00:00Z"),
47
+ updated: new Date("2025-03-11T10:00:00Z"),
48
+ dateFormat: "MM-DD-YYYY",
49
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
50
+ hasEveryoneOnTheInternet: false,
51
+ ancestorId: "buwai2zpe",
52
+ dataClassification: "None",
53
+ variables: [],
54
+ };
55
+
56
+ mockFetch.mockResolvedValueOnce({
57
+ ok: true,
58
+ status: 200,
59
+ json: () => Promise.resolve(mockResponse),
60
+ });
61
+
62
+ const result = await client.copyApp({ appId: QB_APP_ID, body: request });
63
+
64
+ expect(result).toEqual(mockResponse);
65
+ expect(mockFetch).toHaveBeenCalledWith(
66
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
67
+ expect.objectContaining({
68
+ method: "POST",
69
+ headers: expect.objectContaining({
70
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
71
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
72
+ "Content-Type": "application/json",
73
+ }),
74
+ credentials: "omit",
75
+ body: JSON.stringify(request),
76
+ })
77
+ );
78
+ });
79
+
80
+ it("calls copyApp successfully with user token and dates as strings", async () => {
81
+ client = createClient(mockFetch, { debug: true, convertDates: false });
82
+
83
+ const request: CopyAppRequest = {
84
+ name: "Copied App",
85
+ description: "A copy of the original app",
86
+ properties: {
87
+ keepData: false,
88
+ excludeFiles: true,
89
+ usersAndRoles: false,
90
+ assignUserToken: true,
91
+ },
92
+ };
93
+
94
+ const mockResponse = {
95
+ id: "bpqe82s1",
96
+ name: "Copied App",
97
+ description: "A copy of the original app",
98
+ created: "2025-03-11T10:00:00Z",
99
+ updated: "2025-03-11T10:00:00Z",
100
+ dateFormat: "MM-DD-YYYY",
101
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
102
+ hasEveryoneOnTheInternet: false,
103
+ ancestorId: "buwai2zpe",
104
+ dataClassification: "None",
105
+ variables: [],
106
+ };
107
+
108
+ mockFetch.mockResolvedValueOnce({
109
+ ok: true,
110
+ status: 200,
111
+ json: () => Promise.resolve(mockResponse),
112
+ });
113
+
114
+ const result = await client.copyApp({ appId: QB_APP_ID, body: request });
115
+
116
+ expect(result).toEqual(mockResponse);
117
+ expect(mockFetch).toHaveBeenCalledWith(
118
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
119
+ expect.objectContaining({
120
+ method: "POST",
121
+ headers: expect.objectContaining({
122
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
123
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
124
+ "Content-Type": "application/json",
125
+ }),
126
+ credentials: "omit",
127
+ body: JSON.stringify(request),
128
+ })
129
+ );
130
+ });
131
+
132
+ it("calls copyApp successfully with temp token and dates as Date objects", async () => {
133
+ client = createClient(mockFetch, { useTempTokens: true, debug: true });
134
+
135
+ const request: CopyAppRequest = {
136
+ name: "Copied App with Temp Token",
137
+ description: "A copy using temp token",
138
+ properties: {
139
+ keepData: true,
140
+ excludeFiles: false,
141
+ usersAndRoles: true,
142
+ assignUserToken: false,
143
+ },
144
+ };
145
+
146
+ const mockResponse: CopyApp200Response = {
147
+ id: "bpqe82s2",
148
+ name: "Copied App with Temp Token",
149
+ description: "A copy using temp token",
150
+ created: new Date("2025-03-11T11:00:00Z"),
151
+ updated: new Date("2025-03-11T11:00:00Z"),
152
+ dateFormat: "MM-DD-YYYY",
153
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
154
+ hasEveryoneOnTheInternet: true,
155
+ ancestorId: QB_APP_ID,
156
+ dataClassification: "None",
157
+ variables: [],
158
+ };
159
+
160
+ mockFetch
161
+ .mockResolvedValueOnce({
162
+ ok: true,
163
+ status: 200,
164
+ json: () => Promise.resolve({ temporaryAuthorization: "temp_token" }),
165
+ })
166
+ .mockResolvedValueOnce({
167
+ ok: true,
168
+ status: 200,
169
+ json: () => Promise.resolve(mockResponse),
170
+ });
171
+
172
+ const result = await client.copyApp({ appId: QB_APP_ID, body: request });
173
+
174
+ expect(result).toEqual(mockResponse);
175
+ expect(mockFetch).toHaveBeenCalledTimes(2);
176
+ expect(mockFetch).toHaveBeenNthCalledWith(
177
+ 1,
178
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
179
+ expect.any(Object)
180
+ );
181
+ expect(mockFetch).toHaveBeenNthCalledWith(
182
+ 2,
183
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
184
+ expect.objectContaining({
185
+ headers: expect.objectContaining({
186
+ Authorization: "QB-TEMP-TOKEN temp_token",
187
+ }),
188
+ })
189
+ );
190
+ });
191
+
192
+ it("calls copyApp successfully with temp token and dates as strings", async () => {
193
+ client = createClient(mockFetch, {
194
+ useTempTokens: true,
195
+ debug: true,
196
+ convertDates: false,
197
+ });
198
+
199
+ const request: CopyAppRequest = {
200
+ name: "Copied App with Temp Token",
201
+ description: "A copy using temp token",
202
+ properties: {
203
+ keepData: true,
204
+ excludeFiles: false,
205
+ usersAndRoles: true,
206
+ assignUserToken: false,
207
+ },
208
+ };
209
+
210
+ const mockResponse = {
211
+ id: "bpqe82s2",
212
+ name: "Copied App with Temp Token",
213
+ description: "A copy using temp token",
214
+ created: "2025-03-11T11:00:00Z",
215
+ updated: "2025-03-11T11:00:00Z",
216
+ dateFormat: "MM-DD-YYYY",
217
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
218
+ hasEveryoneOnTheInternet: true,
219
+ ancestorId: QB_APP_ID,
220
+ dataClassification: "None",
221
+ variables: [],
222
+ };
223
+
224
+ mockFetch
225
+ .mockResolvedValueOnce({
226
+ ok: true,
227
+ status: 200,
228
+ json: () => Promise.resolve({ temporaryAuthorization: "temp_token" }),
229
+ })
230
+ .mockResolvedValueOnce({
231
+ ok: true,
232
+ status: 200,
233
+ json: () => Promise.resolve(mockResponse),
234
+ });
235
+
236
+ const result = await client.copyApp({ appId: QB_APP_ID, body: request });
237
+
238
+ expect(result).toEqual(mockResponse);
239
+ expect(mockFetch).toHaveBeenCalledTimes(2);
240
+ expect(mockFetch).toHaveBeenNthCalledWith(
241
+ 1,
242
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
243
+ expect.any(Object)
244
+ );
245
+ expect(mockFetch).toHaveBeenNthCalledWith(
246
+ 2,
247
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
248
+ expect.objectContaining({
249
+ headers: expect.objectContaining({
250
+ Authorization: "QB-TEMP-TOKEN temp_token",
251
+ }),
252
+ })
253
+ );
254
+ });
255
+
256
+ it("retries successfully after 401 with temp token and dates as Date objects", async () => {
257
+ client = createClient(mockFetch, { useTempTokens: true, debug: true });
258
+
259
+ const request: CopyAppRequest = {
260
+ name: "Retry Copied App",
261
+ description: "Retry after 401",
262
+ properties: {
263
+ keepData: false,
264
+ excludeFiles: true,
265
+ usersAndRoles: false,
266
+ assignUserToken: true,
267
+ },
268
+ };
269
+
270
+ const mockResponse: CopyApp200Response = {
271
+ id: "bpqe82s3",
272
+ name: "Retry Copied App",
273
+ description: "Retry after 401",
274
+ created: new Date("2025-03-11T12:00:00Z"),
275
+ updated: new Date("2025-03-11T12:00:00Z"),
276
+ dateFormat: "MM-DD-YYYY",
277
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
278
+ hasEveryoneOnTheInternet: false,
279
+ ancestorId: QB_APP_ID,
280
+ dataClassification: "None",
281
+ variables: [],
282
+ };
283
+
284
+ mockFetch
285
+ .mockResolvedValueOnce({
286
+ ok: true,
287
+ status: 200,
288
+ json: () =>
289
+ Promise.resolve({ temporaryAuthorization: "initial_token" }),
290
+ })
291
+ .mockResolvedValueOnce({
292
+ ok: false,
293
+ status: 401,
294
+ json: () => Promise.resolve({ message: "Unauthorized" }),
295
+ })
296
+ .mockResolvedValueOnce({
297
+ ok: true,
298
+ status: 200,
299
+ json: () => Promise.resolve({ temporaryAuthorization: "new_token" }),
300
+ })
301
+ .mockResolvedValueOnce({
302
+ ok: true,
303
+ status: 200,
304
+ json: () => Promise.resolve(mockResponse),
305
+ });
306
+
307
+ const result = await client.copyApp({ appId: QB_APP_ID, body: request });
308
+
309
+ expect(result).toEqual(mockResponse);
310
+ expect(mockFetch).toHaveBeenCalledTimes(4);
311
+ expect(mockFetch).toHaveBeenNthCalledWith(
312
+ 1,
313
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
314
+ expect.any(Object)
315
+ );
316
+ expect(mockFetch).toHaveBeenNthCalledWith(
317
+ 2,
318
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
319
+ expect.objectContaining({
320
+ headers: expect.objectContaining({
321
+ Authorization: "QB-TEMP-TOKEN initial_token",
322
+ }),
323
+ })
324
+ );
325
+ expect(mockFetch).toHaveBeenNthCalledWith(
326
+ 3,
327
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
328
+ expect.any(Object)
329
+ );
330
+ expect(mockFetch).toHaveBeenNthCalledWith(
331
+ 4,
332
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
333
+ expect.objectContaining({
334
+ headers: expect.objectContaining({
335
+ Authorization: "QB-TEMP-TOKEN new_token",
336
+ }),
337
+ })
338
+ );
339
+ });
340
+
341
+ it("retries successfully after 401 with temp token and dates as strings", async () => {
342
+ client = createClient(mockFetch, {
343
+ useTempTokens: true,
344
+ debug: true,
345
+ convertDates: false,
346
+ });
347
+
348
+ const request: CopyAppRequest = {
349
+ name: "Retry Copied App",
350
+ description: "Retry after 401",
351
+ properties: {
352
+ keepData: false,
353
+ excludeFiles: true,
354
+ usersAndRoles: false,
355
+ assignUserToken: true,
356
+ },
357
+ };
358
+
359
+ const mockResponse = {
360
+ id: "bpqe82s3",
361
+ name: "Retry Copied App",
362
+ description: "Retry after 401",
363
+ created: "2025-03-11T12:00:00Z",
364
+ updated: "2025-03-11T12:00:00Z",
365
+ dateFormat: "MM-DD-YYYY",
366
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
367
+ hasEveryoneOnTheInternet: false,
368
+ ancestorId: QB_APP_ID,
369
+ dataClassification: "None",
370
+ variables: [],
371
+ };
372
+
373
+ mockFetch
374
+ .mockResolvedValueOnce({
375
+ ok: true,
376
+ status: 200,
377
+ json: () =>
378
+ Promise.resolve({ temporaryAuthorization: "initial_token" }),
379
+ })
380
+ .mockResolvedValueOnce({
381
+ ok: false,
382
+ status: 401,
383
+ json: () => Promise.resolve({ message: "Unauthorized" }),
384
+ })
385
+ .mockResolvedValueOnce({
386
+ ok: true,
387
+ status: 200,
388
+ json: () => Promise.resolve({ temporaryAuthorization: "new_token" }),
389
+ })
390
+ .mockResolvedValueOnce({
391
+ ok: true,
392
+ status: 200,
393
+ json: () => Promise.resolve(mockResponse),
394
+ });
395
+
396
+ const result = await client.copyApp({ appId: QB_APP_ID, body: request });
397
+
398
+ expect(result).toEqual(mockResponse);
399
+ expect(mockFetch).toHaveBeenCalledTimes(4);
400
+ expect(mockFetch).toHaveBeenNthCalledWith(
401
+ 1,
402
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
403
+ expect.any(Object)
404
+ );
405
+ expect(mockFetch).toHaveBeenNthCalledWith(
406
+ 2,
407
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
408
+ expect.objectContaining({
409
+ headers: expect.objectContaining({
410
+ Authorization: "QB-TEMP-TOKEN initial_token",
411
+ }),
412
+ })
413
+ );
414
+ expect(mockFetch).toHaveBeenNthCalledWith(
415
+ 3,
416
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
417
+ expect.any(Object)
418
+ );
419
+ expect(mockFetch).toHaveBeenNthCalledWith(
420
+ 4,
421
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
422
+ expect.objectContaining({
423
+ headers: expect.objectContaining({
424
+ Authorization: "QB-TEMP-TOKEN new_token",
425
+ }),
426
+ })
427
+ );
428
+ });
429
+
430
+ it("handles 400 error for invalid request", async () => {
431
+ client = createClient(mockFetch, { debug: true });
432
+
433
+ const request: CopyAppRequest = {
434
+ name: "",
435
+ description: "Invalid request",
436
+ };
437
+
438
+ mockFetch.mockResolvedValueOnce({
439
+ ok: false,
440
+ status: 400,
441
+ json: () => Promise.resolve({ message: "Name is required" }),
442
+ });
443
+
444
+ await expect(
445
+ client.copyApp({ appId: QB_APP_ID, body: request })
446
+ ).rejects.toThrow("API Error: Name is required (Status: 400)");
447
+
448
+ expect(mockFetch).toHaveBeenCalledWith(
449
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
450
+ expect.objectContaining({
451
+ method: "POST",
452
+ headers: expect.objectContaining({
453
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
454
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
455
+ "Content-Type": "application/json",
456
+ }),
457
+ credentials: "omit",
458
+ body: JSON.stringify(request),
459
+ })
460
+ );
461
+ });
462
+
463
+ it("exhausts default maxRetries (3) with temp token and throws error", async () => {
464
+ client = createClient(mockFetch, { useTempTokens: true, debug: true }); // No explicit maxRetries, uses default 3
465
+
466
+ const request: CopyAppRequest = {
467
+ name: "Fail App",
468
+ description: "Test retry limit",
469
+ properties: {
470
+ keepData: false,
471
+ excludeFiles: true,
472
+ usersAndRoles: false,
473
+ assignUserToken: true,
474
+ },
475
+ };
476
+
477
+ mockFetch
478
+ .mockResolvedValueOnce({
479
+ ok: true,
480
+ status: 200,
481
+ json: () => Promise.resolve({ temporaryAuthorization: "token_1" }),
482
+ })
483
+ .mockResolvedValueOnce({
484
+ ok: false,
485
+ status: 401,
486
+ json: () => Promise.resolve({ message: "Unauthorized 1" }),
487
+ })
488
+ .mockResolvedValueOnce({
489
+ ok: true,
490
+ status: 200,
491
+ json: () => Promise.resolve({ temporaryAuthorization: "token_2" }),
492
+ })
493
+ .mockResolvedValueOnce({
494
+ ok: false,
495
+ status: 401,
496
+ json: () => Promise.resolve({ message: "Unauthorized 2" }),
497
+ })
498
+ .mockResolvedValueOnce({
499
+ ok: true,
500
+ status: 200,
501
+ json: () => Promise.resolve({ temporaryAuthorization: "token_3" }),
502
+ })
503
+ .mockResolvedValueOnce({
504
+ ok: false,
505
+ status: 401,
506
+ json: () => Promise.resolve({ message: "Unauthorized 3" }),
507
+ })
508
+ .mockResolvedValueOnce({
509
+ ok: true,
510
+ status: 200,
511
+ json: () => Promise.resolve({ temporaryAuthorization: "token_4" }),
512
+ })
513
+ .mockResolvedValueOnce({
514
+ ok: false,
515
+ status: 401,
516
+ json: () => Promise.resolve({ message: "Unauthorized 4" }),
517
+ });
518
+
519
+ await expect(
520
+ client.copyApp({ appId: QB_APP_ID, body: request })
521
+ ).rejects.toThrow("API Error: Unauthorized 4 (Status: 401)");
522
+
523
+ expect(mockFetch).toHaveBeenCalledTimes(8); // 4 token fetches + 4 API attempts
524
+ expect(mockFetch).toHaveBeenNthCalledWith(
525
+ 1,
526
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
527
+ expect.any(Object)
528
+ );
529
+ expect(mockFetch).toHaveBeenNthCalledWith(
530
+ 2,
531
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
532
+ expect.objectContaining({
533
+ headers: expect.objectContaining({
534
+ Authorization: "QB-TEMP-TOKEN token_1",
535
+ }),
536
+ })
537
+ );
538
+ expect(mockFetch).toHaveBeenNthCalledWith(
539
+ 3,
540
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
541
+ expect.any(Object)
542
+ );
543
+ expect(mockFetch).toHaveBeenNthCalledWith(
544
+ 4,
545
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
546
+ expect.objectContaining({
547
+ headers: expect.objectContaining({
548
+ Authorization: "QB-TEMP-TOKEN token_2",
549
+ }),
550
+ })
551
+ );
552
+ expect(mockFetch).toHaveBeenNthCalledWith(
553
+ 5,
554
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
555
+ expect.any(Object)
556
+ );
557
+ expect(mockFetch).toHaveBeenNthCalledWith(
558
+ 6,
559
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
560
+ expect.objectContaining({
561
+ headers: expect.objectContaining({
562
+ Authorization: "QB-TEMP-TOKEN token_3",
563
+ }),
564
+ })
565
+ );
566
+ expect(mockFetch).toHaveBeenNthCalledWith(
567
+ 7,
568
+ `https://api.quickbase.com/v1/auth/temporary/${QB_APP_ID}`,
569
+ expect.any(Object)
570
+ );
571
+ expect(mockFetch).toHaveBeenNthCalledWith(
572
+ 8,
573
+ `https://api.quickbase.com/v1/apps/${QB_APP_ID}/copy`,
574
+ expect.objectContaining({
575
+ headers: expect.objectContaining({
576
+ Authorization: "QB-TEMP-TOKEN token_4",
577
+ }),
578
+ })
579
+ );
580
+ });
581
+ });