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,119 @@
1
+ // tests/vitest/unit/auth/getTempToken.test.ts
2
+ import { describe, it, expect, beforeEach, vi } from "vitest";
3
+ import {
4
+ createClient,
5
+ mockFetch,
6
+ QB_REALM,
7
+ QB_TABLE_ID_1,
8
+ } from "@tests/setup.ts";
9
+ import { TokenCache } from "../../../../src/tokenCache"; // Corrected path
10
+ import { AuthApi } from "../../../../src/generated/apis/AuthApi";
11
+
12
+ describe("QuickbaseClient Unit - getTempTokenDBID", () => {
13
+ let client: ReturnType<typeof createClient>;
14
+ let tokenCache: TokenCache;
15
+ let authApi: AuthApi;
16
+
17
+ beforeEach(() => {
18
+ mockFetch.mockClear();
19
+ tokenCache = new TokenCache();
20
+ client = createClient(mockFetch, {
21
+ useTempTokens: true,
22
+ debug: true,
23
+ tokenCache,
24
+ });
25
+ authApi = new AuthApi(client["configuration"]);
26
+ vi.spyOn(authApi, "getTempTokenDBIDRaw").mockImplementation(
27
+ async (params) => {
28
+ const mockToken = "b123xyz_temp_token";
29
+ if (params.dbid === QB_TABLE_ID_1) {
30
+ return {
31
+ value: () => Promise.resolve({ temporaryAuthorization: mockToken }),
32
+ } as any;
33
+ }
34
+ throw new Error("Unexpected dbid");
35
+ }
36
+ );
37
+ });
38
+
39
+ it("initializes without errors", () => {
40
+ expect(client).toBeDefined();
41
+ });
42
+
43
+ it("has getTempTokenDBID method", () => {
44
+ expect(typeof client.getTempTokenDBID).toBe("function");
45
+ });
46
+
47
+ it("fetches and caches temp token on first call", async () => {
48
+ const mockToken = "b123xyz_temp_token";
49
+
50
+ mockFetch.mockResolvedValueOnce(
51
+ new Response(JSON.stringify({ temporaryAuthorization: mockToken }), {
52
+ status: 200,
53
+ headers: { "Content-Type": "application/json" },
54
+ })
55
+ );
56
+
57
+ const result = await client.getTempTokenDBID({ dbid: QB_TABLE_ID_1 });
58
+ expect(result).toEqual({ temporaryAuthorization: mockToken });
59
+ expect(mockFetch).toHaveBeenCalledWith(
60
+ `https://api.quickbase.com/v1/auth/temporary/${QB_TABLE_ID_1}`,
61
+ expect.objectContaining({
62
+ method: "GET",
63
+ headers: expect.objectContaining({
64
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
65
+ "Content-Type": "application/json",
66
+ }),
67
+ credentials: "include",
68
+ })
69
+ );
70
+ });
71
+
72
+ it("reuses cached temp token on second call", async () => {
73
+ const mockToken = "b123xyz_temp_token";
74
+
75
+ mockFetch.mockResolvedValueOnce(
76
+ new Response(JSON.stringify({ temporaryAuthorization: mockToken }), {
77
+ status: 200,
78
+ headers: { "Content-Type": "application/json" },
79
+ })
80
+ );
81
+
82
+ const firstResult = await client.getTempTokenDBID({ dbid: QB_TABLE_ID_1 });
83
+ expect(firstResult).toEqual({ temporaryAuthorization: mockToken });
84
+ expect(mockFetch).toHaveBeenCalledTimes(1);
85
+
86
+ mockFetch.mockClear();
87
+ const secondResult = await client.getTempTokenDBID({ dbid: QB_TABLE_ID_1 });
88
+ expect(secondResult).toEqual({ temporaryAuthorization: mockToken });
89
+ expect(mockFetch).not.toHaveBeenCalled();
90
+ });
91
+
92
+ it("handles API error", async () => {
93
+ mockFetch.mockResolvedValueOnce(
94
+ new Response(JSON.stringify({ message: "Unauthorized" }), {
95
+ status: 401,
96
+ headers: { "Content-Type": "application/json" },
97
+ })
98
+ );
99
+
100
+ await expect(
101
+ client.getTempTokenDBID({ dbid: QB_TABLE_ID_1 })
102
+ ).rejects.toSatisfy((error: Error) => {
103
+ expect(error).toBeInstanceOf(Error);
104
+ expect(error.message).toBe("API Error: Unauthorized (Status: 401)");
105
+ return true;
106
+ });
107
+ expect(mockFetch).toHaveBeenCalledWith(
108
+ `https://api.quickbase.com/v1/auth/temporary/${QB_TABLE_ID_1}`,
109
+ expect.objectContaining({
110
+ method: "GET",
111
+ headers: expect.objectContaining({
112
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
113
+ "Content-Type": "application/json",
114
+ }),
115
+ credentials: "include",
116
+ })
117
+ );
118
+ });
119
+ });
@@ -0,0 +1,95 @@
1
+ // tests/vitest/unit/auth/retryTempToken401.test.ts
2
+
3
+ import { describe, it, expect, vi } from "vitest";
4
+ import {
5
+ createClient,
6
+ mockFetch,
7
+ QB_REALM,
8
+ QB_TABLE_ID_1,
9
+ } from "@tests/setup.ts";
10
+
11
+ describe("QuickbaseClient Unit - Temp Token Retry on 401", () => {
12
+ it("creates a new token on 401 and retries successfully", async () => {
13
+ mockFetch
14
+ .mockResolvedValueOnce(
15
+ new Response(
16
+ JSON.stringify({ temporaryAuthorization: "initial_token" }),
17
+ { status: 200 }
18
+ )
19
+ )
20
+ .mockResolvedValueOnce(
21
+ new Response(JSON.stringify({ message: "Unauthorized" }), {
22
+ status: 401,
23
+ })
24
+ )
25
+ .mockResolvedValueOnce(
26
+ new Response(
27
+ JSON.stringify({ temporaryAuthorization: "new_token_456" }),
28
+ { status: 200 }
29
+ )
30
+ )
31
+ .mockResolvedValueOnce(
32
+ new Response(JSON.stringify([{ id: 1, label: "Field1" }]), {
33
+ status: 200,
34
+ })
35
+ );
36
+
37
+ const consoleSpy = vi.spyOn(console, "log");
38
+ const client = createClient(mockFetch, {
39
+ realm: QB_REALM,
40
+ useTempTokens: true,
41
+ debug: true,
42
+ });
43
+
44
+ const result = await client.getFields({ tableId: QB_TABLE_ID_1 });
45
+
46
+ expect(result).toEqual([{ id: 1, label: "Field1" }]);
47
+ expect(mockFetch).toHaveBeenCalledTimes(4);
48
+ expect(mockFetch).toHaveBeenNthCalledWith(
49
+ 1,
50
+ `https://api.quickbase.com/v1/auth/temporary/${QB_TABLE_ID_1}`,
51
+ expect.objectContaining({
52
+ credentials: "include",
53
+ method: "GET",
54
+ })
55
+ );
56
+ expect(mockFetch).toHaveBeenNthCalledWith(
57
+ 2,
58
+ `https://api.quickbase.com/v1/fields?tableId=${QB_TABLE_ID_1}`,
59
+ expect.objectContaining({
60
+ method: "GET",
61
+ headers: expect.objectContaining({
62
+ Authorization: "QB-TEMP-TOKEN initial_token",
63
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
64
+ "Content-Type": "application/json",
65
+ }),
66
+ })
67
+ );
68
+ expect(mockFetch).toHaveBeenNthCalledWith(
69
+ 3,
70
+ `https://api.quickbase.com/v1/auth/temporary/${QB_TABLE_ID_1}`,
71
+ expect.objectContaining({
72
+ credentials: "include",
73
+ method: "GET",
74
+ })
75
+ );
76
+ expect(mockFetch).toHaveBeenNthCalledWith(
77
+ 4,
78
+ `https://api.quickbase.com/v1/fields?tableId=${QB_TABLE_ID_1}`,
79
+ expect.objectContaining({
80
+ method: "GET",
81
+ headers: expect.objectContaining({
82
+ Authorization: "QB-TEMP-TOKEN new_token_456",
83
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
84
+ "Content-Type": "application/json",
85
+ }),
86
+ })
87
+ );
88
+ expect(consoleSpy).toHaveBeenCalledWith(
89
+ "Authorization error for getFields (temp token), refreshing token:"
90
+ );
91
+ expect(consoleSpy).toHaveBeenCalledWith(
92
+ "[getFields] Retrying with token: new_token_..."
93
+ );
94
+ });
95
+ });
@@ -0,0 +1,206 @@
1
+ import { describe, it, expect, beforeEach, vi } from "vitest";
2
+ import { createClient, mockFetch } from "@tests/setup.ts";
3
+
4
+ describe("QuickbaseClient - Temp Token Retry Limit", () => {
5
+ let client: ReturnType<typeof createClient>;
6
+
7
+ beforeEach(() => {
8
+ mockFetch.mockClear();
9
+ });
10
+
11
+ it("exhausts retries after maxRetries (3) and throws an error", async () => {
12
+ client = createClient(mockFetch, {
13
+ useTempTokens: true,
14
+ debug: true,
15
+ maxRetries: 3,
16
+ });
17
+
18
+ const mockDbid = "mockDbid123";
19
+ let callCount = 0;
20
+
21
+ mockFetch.mockImplementation((url) => {
22
+ callCount++;
23
+ if (url.includes("auth/temporary") && callCount === 1) {
24
+ return Promise.resolve({
25
+ ok: true,
26
+ status: 200,
27
+ headers: new Headers({ "Content-Type": "application/json" }),
28
+ json: () => Promise.resolve({ temporaryAuthorization: "token_1" }),
29
+ });
30
+ }
31
+ if (url.includes("fields") && callCount === 2) {
32
+ return Promise.resolve({
33
+ ok: false,
34
+ status: 401,
35
+ headers: new Headers({ "Content-Type": "application/json" }),
36
+ json: () => Promise.resolve({ message: "Unauthorized 1" }),
37
+ });
38
+ }
39
+ if (url.includes("auth/temporary") && callCount === 3) {
40
+ return Promise.resolve({
41
+ ok: true,
42
+ status: 200,
43
+ headers: new Headers({ "Content-Type": "application/json" }),
44
+ json: () => Promise.resolve({ temporaryAuthorization: "token_2" }),
45
+ });
46
+ }
47
+ if (url.includes("fields") && callCount === 4) {
48
+ return Promise.resolve({
49
+ ok: false,
50
+ status: 401,
51
+ headers: new Headers({ "Content-Type": "application/json" }),
52
+ json: () => Promise.resolve({ message: "Unauthorized 2" }),
53
+ });
54
+ }
55
+ if (url.includes("auth/temporary") && callCount === 5) {
56
+ return Promise.resolve({
57
+ ok: true,
58
+ status: 200,
59
+ headers: new Headers({ "Content-Type": "application/json" }),
60
+ json: () => Promise.resolve({ temporaryAuthorization: "token_3" }),
61
+ });
62
+ }
63
+ if (url.includes("fields") && callCount === 6) {
64
+ return Promise.resolve({
65
+ ok: false,
66
+ status: 401,
67
+ headers: new Headers({ "Content-Type": "application/json" }),
68
+ json: () => Promise.resolve({ message: "Unauthorized 3" }),
69
+ });
70
+ }
71
+ if (url.includes("auth/temporary") && callCount === 7) {
72
+ return Promise.resolve({
73
+ ok: true,
74
+ status: 200,
75
+ headers: new Headers({ "Content-Type": "application/json" }),
76
+ json: () => Promise.resolve({ temporaryAuthorization: "token_4" }),
77
+ });
78
+ }
79
+ if (url.includes("fields") && callCount === 8) {
80
+ return Promise.resolve({
81
+ ok: false,
82
+ status: 401,
83
+ headers: new Headers({ "Content-Type": "application/json" }),
84
+ json: () => Promise.resolve({ message: "Unauthorized 4" }),
85
+ });
86
+ }
87
+ console.log(
88
+ `[mockFetch] Unexpected call: ${url}, callCount: ${callCount}`
89
+ );
90
+ return Promise.reject(new Error(`Unexpected fetch call: ${url}`));
91
+ });
92
+
93
+ const consoleSpy = vi.spyOn(console, "log");
94
+
95
+ await expect(client.getFields({ tableId: mockDbid })).rejects.toThrow(
96
+ "API Error: Unauthorized 4 (Status: 401)"
97
+ );
98
+
99
+ expect(mockFetch).toHaveBeenCalledTimes(8);
100
+ // Rest of assertions...
101
+ });
102
+
103
+ it("exhausts retries after maxRetries (2) and throws an error", async () => {
104
+ client = createClient(mockFetch, {
105
+ useTempTokens: true,
106
+ debug: true,
107
+ maxRetries: 2,
108
+ });
109
+
110
+ const mockDbid = "mockDbid123";
111
+ let callCount = 0;
112
+
113
+ mockFetch.mockImplementation((url) => {
114
+ callCount++;
115
+ if (url.includes("auth/temporary") && callCount === 1) {
116
+ return Promise.resolve({
117
+ ok: true,
118
+ status: 200,
119
+ headers: new Headers({ "Content-Type": "application/json" }),
120
+ json: () => Promise.resolve({ temporaryAuthorization: "token_1" }),
121
+ });
122
+ }
123
+ if (url.includes("fields") && callCount === 2) {
124
+ return Promise.resolve({
125
+ ok: false,
126
+ status: 401,
127
+ headers: new Headers({ "Content-Type": "application/json" }),
128
+ json: () => Promise.resolve({ message: "Unauthorized 1" }),
129
+ });
130
+ }
131
+ if (url.includes("auth/temporary") && callCount === 3) {
132
+ return Promise.resolve({
133
+ ok: true,
134
+ status: 200,
135
+ headers: new Headers({ "Content-Type": "application/json" }),
136
+ json: () => Promise.resolve({ temporaryAuthorization: "token_2" }),
137
+ });
138
+ }
139
+ if (url.includes("fields") && callCount === 4) {
140
+ return Promise.resolve({
141
+ ok: false,
142
+ status: 401,
143
+ headers: new Headers({ "Content-Type": "application/json" }),
144
+ json: () => Promise.resolve({ message: "Unauthorized 2" }),
145
+ });
146
+ }
147
+ if (url.includes("auth/temporary") && callCount === 5) {
148
+ return Promise.resolve({
149
+ ok: true,
150
+ status: 200,
151
+ headers: new Headers({ "Content-Type": "application/json" }),
152
+ json: () => Promise.resolve({ temporaryAuthorization: "token_3" }),
153
+ });
154
+ }
155
+ if (url.includes("fields") && callCount === 6) {
156
+ return Promise.resolve({
157
+ ok: false,
158
+ status: 401,
159
+ headers: new Headers({ "Content-Type": "application/json" }),
160
+ json: () => Promise.resolve({ message: "Unauthorized 3" }),
161
+ });
162
+ }
163
+ console.log(
164
+ `[mockFetch] Unexpected call: ${url}, callCount: ${callCount}`
165
+ );
166
+ return Promise.reject(new Error(`Unexpected fetch call: ${url}`));
167
+ });
168
+
169
+ const consoleSpy = vi.spyOn(console, "log");
170
+
171
+ await expect(client.getFields({ tableId: mockDbid })).rejects.toThrow(
172
+ "API Error: Unauthorized 3 (Status: 401)"
173
+ );
174
+
175
+ expect(mockFetch).toHaveBeenCalledTimes(6);
176
+
177
+ const tokenLogs = consoleSpy.mock.calls.filter((call) =>
178
+ call[0].includes("Fetched and cached new token")
179
+ );
180
+ expect(tokenLogs).toHaveLength(3);
181
+ expect(tokenLogs).toContainEqual([
182
+ "Fetched and cached new token for dbid: mockDbid123",
183
+ "token_1",
184
+ ]);
185
+ expect(tokenLogs).toContainEqual([
186
+ "Fetched and cached new token for dbid: mockDbid123",
187
+ "token_2",
188
+ ]);
189
+ expect(tokenLogs).toContainEqual([
190
+ "Fetched and cached new token for dbid: mockDbid123",
191
+ "token_3",
192
+ ]);
193
+
194
+ expect(consoleSpy).toHaveBeenCalledWith(
195
+ "Authorization error for getFields (temp token), refreshing token:"
196
+ );
197
+ expect(consoleSpy).toHaveBeenCalledWith(
198
+ "[getFields] Retrying with token: token_2..."
199
+ );
200
+ expect(consoleSpy).toHaveBeenCalledWith(
201
+ "[getFields] Retrying with token: token_3..."
202
+ );
203
+
204
+ consoleSpy.mockRestore();
205
+ });
206
+ });
@@ -0,0 +1,69 @@
1
+ // tests/vitest/unit/auth/retryUserToken401.test.ts
2
+
3
+ import { describe, it, expect, vi } from "vitest";
4
+ import {
5
+ createClient,
6
+ mockFetch,
7
+ QB_REALM,
8
+ QB_TABLE_ID_1,
9
+ } from "@tests/setup.ts";
10
+
11
+ describe("QuickbaseClient Unit - User Token Retry on 401", () => {
12
+ it("retries with the same user token on 401 and succeeds", async () => {
13
+ const userToken = "b9f3pk_q4jd_0_b4qu5eebyvuix3xs57ysd7zn3";
14
+ mockFetch
15
+ .mockResolvedValueOnce(
16
+ new Response(JSON.stringify({ message: "Unauthorized" }), {
17
+ status: 401,
18
+ })
19
+ )
20
+ .mockResolvedValueOnce(
21
+ new Response(JSON.stringify([{ id: 1, label: "Field1" }]), {
22
+ status: 200,
23
+ })
24
+ );
25
+
26
+ const consoleSpy = vi.spyOn(console, "log");
27
+ const client = createClient(mockFetch, {
28
+ realm: QB_REALM,
29
+ userToken,
30
+ useTempTokens: false,
31
+ debug: true,
32
+ });
33
+
34
+ const result = await client.getFields({ tableId: QB_TABLE_ID_1 });
35
+
36
+ expect(result).toEqual([{ id: 1, label: "Field1" }]);
37
+ expect(mockFetch).toHaveBeenCalledTimes(2);
38
+ expect(mockFetch).toHaveBeenNthCalledWith(
39
+ 1,
40
+ `https://api.quickbase.com/v1/fields?tableId=${QB_TABLE_ID_1}`,
41
+ expect.objectContaining({
42
+ method: "GET",
43
+ headers: expect.objectContaining({
44
+ Authorization: `QB-USER-TOKEN ${userToken}`,
45
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
46
+ "Content-Type": "application/json",
47
+ }),
48
+ })
49
+ );
50
+ expect(mockFetch).toHaveBeenNthCalledWith(
51
+ 2,
52
+ `https://api.quickbase.com/v1/fields?tableId=${QB_TABLE_ID_1}`,
53
+ expect.objectContaining({
54
+ method: "GET",
55
+ headers: expect.objectContaining({
56
+ Authorization: `QB-USER-TOKEN ${userToken}`,
57
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
58
+ "Content-Type": "application/json",
59
+ }),
60
+ })
61
+ );
62
+ expect(consoleSpy).toHaveBeenCalledWith(
63
+ `Retrying getFields with existing user token: b9f3pk_q4j...`
64
+ );
65
+ expect(consoleSpy).toHaveBeenCalledWith(
66
+ "[getFields] Retrying with token: b9f3pk_q4j..."
67
+ );
68
+ });
69
+ });