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,191 @@
1
+ // tests/vitest/unit/rateLimit.test.ts
2
+ import { describe, it, expect, beforeEach } from "vitest";
3
+ import {
4
+ createClient,
5
+ mockFetch,
6
+ QB_APP_ID,
7
+ QB_REALM,
8
+ QB_USER_TOKEN,
9
+ } from "@tests/setup.ts";
10
+ import { RateLimitError } from "@/RateLimitError";
11
+ import { GetApp200Response } from "@/generated/models";
12
+
13
+ describe("QuickbaseClient Unit - Rate Limit Handling", () => {
14
+ let client: ReturnType<typeof createClient>;
15
+
16
+ beforeEach(() => {
17
+ mockFetch.mockClear();
18
+ });
19
+
20
+ it("succeeds after retrying on 429 Too Many Requests", async () => {
21
+ client = createClient(mockFetch, {
22
+ realm: QB_REALM,
23
+ userToken: QB_USER_TOKEN,
24
+ debug: true,
25
+ maxRetries: 2,
26
+ retryDelay: 100,
27
+ throttle: { rate: 10, burst: 10 },
28
+ });
29
+
30
+ const appId = QB_APP_ID;
31
+
32
+ const mockResponse: GetApp200Response = {
33
+ id: appId,
34
+ name: "TestApp",
35
+ created: "2025-01-01T00:00:00Z",
36
+ updated: "2025-01-02T00:00:00Z",
37
+ description: "Test app",
38
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
39
+ dateFormat: "MM-DD-YYYY",
40
+ hasEveryoneOnTheInternet: false,
41
+ memoryInfo: { estMemory: 0, estMemoryInclDependentApps: 0 },
42
+ securityProperties: {
43
+ allowClone: false,
44
+ allowExport: true,
45
+ enableAppTokens: true,
46
+ hideFromPublic: false,
47
+ mustBeRealmApproved: false,
48
+ useIPFilter: false,
49
+ },
50
+ };
51
+
52
+ mockFetch
53
+ .mockResolvedValueOnce({
54
+ ok: false,
55
+ status: 429,
56
+ headers: new Headers({ "Retry-After": "1" }),
57
+ json: () => Promise.resolve({ message: "Too Many Requests" }),
58
+ })
59
+ .mockResolvedValueOnce({
60
+ ok: true,
61
+ status: 200,
62
+ json: () => Promise.resolve(mockResponse),
63
+ });
64
+
65
+ const result = await client.getApp({ appId });
66
+
67
+ expect(result).toEqual({
68
+ ...mockResponse,
69
+ created: new Date("2025-01-01T00:00:00Z"),
70
+ updated: new Date("2025-01-02T00:00:00Z"),
71
+ });
72
+ expect(mockFetch).toHaveBeenCalledTimes(2);
73
+ expect(mockFetch).toHaveBeenNthCalledWith(
74
+ 1,
75
+ `https://api.quickbase.com/v1/apps/${appId}`,
76
+ expect.objectContaining({
77
+ method: "GET",
78
+ headers: expect.objectContaining({
79
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
80
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
81
+ "Content-Type": "application/json",
82
+ }),
83
+ credentials: "omit",
84
+ })
85
+ );
86
+ expect(mockFetch).toHaveBeenNthCalledWith(
87
+ 2,
88
+ `https://api.quickbase.com/v1/apps/${appId}`,
89
+ expect.objectContaining({
90
+ method: "GET",
91
+ headers: expect.objectContaining({
92
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
93
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
94
+ "Content-Type": "application/json",
95
+ }),
96
+ credentials: "omit",
97
+ })
98
+ );
99
+ });
100
+
101
+ it("throws RateLimitError after max retries on 429", async () => {
102
+ client = createClient(mockFetch, {
103
+ realm: QB_REALM,
104
+ userToken: QB_USER_TOKEN,
105
+ debug: true,
106
+ maxRetries: 1,
107
+ retryDelay: 100,
108
+ throttle: { rate: 10, burst: 10 },
109
+ });
110
+
111
+ const appId = QB_APP_ID;
112
+
113
+ mockFetch
114
+ .mockResolvedValueOnce({
115
+ ok: false,
116
+ status: 429,
117
+ headers: new Headers({ "Retry-After": "1" }),
118
+ json: () => Promise.resolve({ message: "Too Many Requests" }),
119
+ })
120
+ .mockResolvedValueOnce({
121
+ ok: false,
122
+ status: 429,
123
+ headers: new Headers({ "Retry-After": "1" }),
124
+ json: () => Promise.resolve({ message: "Too Many Requests" }),
125
+ });
126
+
127
+ await expect(client.getApp({ appId })).rejects.toMatchObject({
128
+ name: "RateLimitError",
129
+ message: "API Error: Too Many Requests (Status: 429)",
130
+ status: 429,
131
+ retryAfter: 1,
132
+ });
133
+
134
+ expect(mockFetch).toHaveBeenCalledTimes(2);
135
+ });
136
+
137
+ it("respects throttle limits without exceeding rate", async () => {
138
+ client = createClient(mockFetch, {
139
+ realm: QB_REALM,
140
+ userToken: QB_USER_TOKEN,
141
+ debug: true,
142
+ throttle: { rate: 5, burst: 5 },
143
+ maxRetries: 0, // Prevent retries for this test
144
+ });
145
+
146
+ const appId = QB_APP_ID;
147
+
148
+ const mockResponse: GetApp200Response = {
149
+ id: appId,
150
+ name: "TestApp",
151
+ created: "2025-01-01T00:00:00Z",
152
+ updated: "2025-01-02T00:00:00Z",
153
+ description: "Test app",
154
+ timeZone: "(UTC-08:00) Pacific Time (US & Canada)",
155
+ dateFormat: "MM-DD-YYYY",
156
+ hasEveryoneOnTheInternet: false,
157
+ memoryInfo: { estMemory: 0, estMemoryInclDependentApps: 0 },
158
+ securityProperties: {
159
+ allowClone: false,
160
+ allowExport: true,
161
+ enableAppTokens: true,
162
+ hideFromPublic: false,
163
+ mustBeRealmApproved: false,
164
+ useIPFilter: false,
165
+ },
166
+ };
167
+
168
+ mockFetch.mockResolvedValue({
169
+ ok: true,
170
+ status: 200,
171
+ json: () => Promise.resolve(mockResponse),
172
+ });
173
+
174
+ const startTime = Date.now();
175
+ await Promise.all([
176
+ client.getApp({ appId }),
177
+ client.getApp({ appId }),
178
+ client.getApp({ appId }),
179
+ client.getApp({ appId }),
180
+ client.getApp({ appId }),
181
+ client.getApp({ appId }), // 6th call should be throttled
182
+ ]);
183
+ const endTime = Date.now();
184
+ const durationMs = endTime - startTime;
185
+
186
+ console.log(`[Test Debug] mockFetch calls: ${mockFetch.mock.calls.length}`);
187
+ expect(mockFetch).toHaveBeenCalledTimes(6);
188
+ expect(durationMs).toBeGreaterThanOrEqual(200); // 1/5 sec for 6th call
189
+ expect(durationMs).toBeLessThan(1000);
190
+ });
191
+ });
@@ -0,0 +1,112 @@
1
+ // tests/vitest/unit/records/deleteRecords.test.ts
2
+ import { describe, expect, test, beforeEach } from "vitest";
3
+ import {
4
+ createClient,
5
+ mockFetch,
6
+ QB_REALM,
7
+ QB_USER_TOKEN,
8
+ QB_TABLE_ID_1,
9
+ } from "@tests/setup.ts";
10
+
11
+ describe("QuickbaseClient Unit - deleteRecords", () => {
12
+ let qb: ReturnType<typeof createClient>;
13
+
14
+ beforeEach(() => {
15
+ mockFetch.mockClear();
16
+ qb = createClient(mockFetch, { debug: true }); // Use debug: true for consistency with other tests
17
+ });
18
+
19
+ test("sends correct DELETE request and handles success", async () => {
20
+ mockFetch.mockResolvedValueOnce({
21
+ ok: true,
22
+ status: 200,
23
+ json: () => Promise.resolve({ numberDeleted: 1 }),
24
+ });
25
+
26
+ const result = await qb.deleteRecords({
27
+ body: {
28
+ from: QB_TABLE_ID_1,
29
+ where: "{3.EX.'5'}", // Simulate deleting Record ID 5
30
+ },
31
+ });
32
+
33
+ expect(mockFetch).toHaveBeenCalledWith(
34
+ "https://api.quickbase.com/v1/records",
35
+ expect.objectContaining({
36
+ method: "DELETE",
37
+ headers: expect.objectContaining({
38
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
39
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
40
+ "Content-Type": "application/json",
41
+ }),
42
+ body: JSON.stringify({ from: QB_TABLE_ID_1, where: "{3.EX.'5'}" }),
43
+ })
44
+ );
45
+ expect(result).toEqual({ numberDeleted: 1 });
46
+ });
47
+
48
+ test("handles no records deleted", async () => {
49
+ mockFetch.mockResolvedValueOnce({
50
+ ok: true,
51
+ status: 200,
52
+ json: () => Promise.resolve({ numberDeleted: 0 }),
53
+ });
54
+
55
+ const result = await qb.deleteRecords({
56
+ body: {
57
+ from: QB_TABLE_ID_1,
58
+ where: "{3.EX.'999999'}", // Non-existent Record ID
59
+ },
60
+ });
61
+
62
+ expect(mockFetch).toHaveBeenCalledWith(
63
+ "https://api.quickbase.com/v1/records",
64
+ expect.objectContaining({
65
+ method: "DELETE",
66
+ headers: expect.objectContaining({
67
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
68
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
69
+ "Content-Type": "application/json",
70
+ }),
71
+ body: JSON.stringify({
72
+ from: QB_TABLE_ID_1,
73
+ where: "{3.EX.'999999'}",
74
+ }),
75
+ })
76
+ );
77
+ expect(result).toEqual({ numberDeleted: 0 });
78
+ });
79
+
80
+ test("throws error on API failure", async () => {
81
+ mockFetch.mockResolvedValueOnce({
82
+ ok: false,
83
+ status: 400,
84
+ json: () => Promise.resolve({ message: "Invalid query" }),
85
+ });
86
+
87
+ await expect(() =>
88
+ qb.deleteRecords({
89
+ body: {
90
+ from: QB_TABLE_ID_1,
91
+ where: "{3.EX.'invalid'}",
92
+ },
93
+ })
94
+ ).rejects.toThrow("API Error: Invalid query (Status: 400)");
95
+
96
+ expect(mockFetch).toHaveBeenCalledWith(
97
+ "https://api.quickbase.com/v1/records",
98
+ expect.objectContaining({
99
+ method: "DELETE",
100
+ headers: expect.objectContaining({
101
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
102
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
103
+ "Content-Type": "application/json",
104
+ }),
105
+ body: JSON.stringify({
106
+ from: QB_TABLE_ID_1,
107
+ where: "{3.EX.'invalid'}",
108
+ }),
109
+ })
110
+ );
111
+ });
112
+ });
@@ -0,0 +1,102 @@
1
+ // tests/vitest/unit/records/runQuery.test.ts
2
+ import { describe, test, expect, beforeEach } from "vitest";
3
+ import {
4
+ createClient,
5
+ mockFetch,
6
+ QB_REALM,
7
+ QB_USER_TOKEN,
8
+ QB_TABLE_ID_1,
9
+ } from "@tests/setup.ts";
10
+ import { RunQueryRequest, RunQueryResponse } from "@/generated";
11
+
12
+ describe("QuickbaseClient Unit - runQuery", () => {
13
+ let client: ReturnType<typeof createClient>;
14
+
15
+ beforeEach(() => {
16
+ mockFetch.mockClear();
17
+ client = createClient(mockFetch, { debug: true });
18
+ });
19
+
20
+ test("sends correct POST request and handles successful query response", async () => {
21
+ const queryRequest: RunQueryRequest = {
22
+ from: QB_TABLE_ID_1,
23
+ select: [3, 6, 7],
24
+ where: "{6.EX.'Task 1'}",
25
+ sortBy: [{ fieldId: 6, order: "ASC" }],
26
+ groupBy: [{ fieldId: 7, grouping: "equal-values" }],
27
+ options: { skip: 0, top: 100, compareWithAppLocalTime: false },
28
+ };
29
+
30
+ const mockResponse: RunQueryResponse = {
31
+ data: [
32
+ { "3": { value: 1 }, "6": { value: "Task 1" }, "7": { value: "High" } },
33
+ ],
34
+ fields: [
35
+ { id: 3, label: "Record ID#", type: "recordid" },
36
+ { id: 6, label: "Task Name", type: "text" },
37
+ { id: 7, label: "Priority", type: "text" },
38
+ ],
39
+ metadata: {
40
+ numFields: 3,
41
+ numRecords: 1,
42
+ skip: 0,
43
+ top: 100,
44
+ totalRecords: 1,
45
+ },
46
+ };
47
+
48
+ mockFetch.mockResolvedValueOnce({
49
+ ok: true,
50
+ status: 200,
51
+ json: () => Promise.resolve(mockResponse),
52
+ });
53
+
54
+ const result = await client.runQuery({ body: queryRequest });
55
+
56
+ expect(mockFetch).toHaveBeenCalledWith(
57
+ "https://api.quickbase.com/v1/records/query",
58
+ expect.objectContaining({
59
+ method: "POST",
60
+ headers: expect.objectContaining({
61
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
62
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
63
+ "Content-Type": "application/json",
64
+ }),
65
+ body: JSON.stringify(queryRequest),
66
+ })
67
+ );
68
+
69
+ expect(result).toEqual(mockResponse);
70
+ });
71
+
72
+ test("handles API error with invalid query", async () => {
73
+ const invalidQueryRequest: RunQueryRequest = {
74
+ from: QB_TABLE_ID_1,
75
+ select: [3],
76
+ where: "{999.EX.'Invalid'}",
77
+ };
78
+
79
+ mockFetch.mockResolvedValueOnce({
80
+ ok: false,
81
+ status: 400,
82
+ json: () => Promise.resolve({ message: "Invalid query" }),
83
+ });
84
+
85
+ await expect(
86
+ client.runQuery({ body: invalidQueryRequest })
87
+ ).rejects.toThrow("API Error: Invalid query (Status: 400)");
88
+
89
+ expect(mockFetch).toHaveBeenCalledWith(
90
+ "https://api.quickbase.com/v1/records/query",
91
+ expect.objectContaining({
92
+ method: "POST",
93
+ headers: expect.objectContaining({
94
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
95
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
96
+ "Content-Type": "application/json",
97
+ }),
98
+ body: JSON.stringify(invalidQueryRequest),
99
+ })
100
+ );
101
+ });
102
+ });
@@ -0,0 +1,234 @@
1
+ // tests/vitest/unit/records/upsert.test.ts
2
+ import { describe, expect, test, beforeEach } from "vitest";
3
+ import {
4
+ createClient,
5
+ mockFetch,
6
+ QB_REALM,
7
+ QB_USER_TOKEN,
8
+ QB_TABLE_ID_1,
9
+ } from "@tests/setup.ts";
10
+ import { vi } from "vitest";
11
+
12
+ // Note: Type augmentation moved to src/types.d.ts
13
+ describe("QuickbaseClient Unit - upsert", () => {
14
+ beforeEach(() => {
15
+ vi.resetModules(); // Reset module cache
16
+ mockFetch.mockClear();
17
+ });
18
+
19
+ test("sends correct POST request and handles successful upsert", async () => {
20
+ const qb = createClient(mockFetch, { debug: true });
21
+
22
+ mockFetch.mockResolvedValueOnce({
23
+ ok: true,
24
+ status: 200,
25
+ json: () =>
26
+ Promise.resolve({
27
+ data: [{ id: 1, "6": { value: "value1" } }],
28
+ metadata: {
29
+ createdRecordIds: [1],
30
+ updatedRecordIds: [],
31
+ unchangedRecordIds: [],
32
+ totalNumberOfRecordsProcessed: 1,
33
+ },
34
+ }),
35
+ });
36
+
37
+ console.log("[Test] Calling qb.upsert with:", {
38
+ body: { to: QB_TABLE_ID_1, data: [{ "6": { value: "value1" } }] },
39
+ });
40
+ const result = await qb.upsert({
41
+ body: { to: QB_TABLE_ID_1, data: [{ "6": { value: "value1" } }] },
42
+ });
43
+
44
+ console.log("[Test] mockFetch calls:", mockFetch.mock.calls);
45
+ expect(mockFetch).toHaveBeenCalledWith(
46
+ "https://api.quickbase.com/v1/records",
47
+ expect.objectContaining({
48
+ method: "POST",
49
+ headers: expect.objectContaining({
50
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
51
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
52
+ "Content-Type": "application/json",
53
+ }),
54
+ body: expect.any(String), // Allow any string, check content below
55
+ credentials: "omit",
56
+ })
57
+ );
58
+ const callArgs = mockFetch.mock.calls[0];
59
+ const receivedBody = JSON.parse(callArgs[1].body as string);
60
+ expect(receivedBody).toEqual({
61
+ to: QB_TABLE_ID_1,
62
+ data: [{ "6": { value: "value1" } }],
63
+ });
64
+
65
+ expect(result).toEqual({
66
+ data: [{ id: 1, "6": { value: "value1" } }],
67
+ metadata: {
68
+ createdRecordIds: [1],
69
+ updatedRecordIds: [],
70
+ unchangedRecordIds: [],
71
+ totalNumberOfRecordsProcessed: 1,
72
+ },
73
+ });
74
+ });
75
+
76
+ test("handles API error with invalid data", async () => {
77
+ const qb = createClient(mockFetch, { debug: true });
78
+
79
+ mockFetch.mockResolvedValueOnce({
80
+ ok: false,
81
+ status: 400,
82
+ json: () => Promise.resolve({ message: "Invalid data format" }),
83
+ });
84
+
85
+ await expect(
86
+ qb.upsert({
87
+ body: { to: QB_TABLE_ID_1, data: [{ "999": { value: "invalid" } }] },
88
+ })
89
+ ).rejects.toThrow("API Error: Invalid data format (Status: 400)");
90
+
91
+ expect(mockFetch).toHaveBeenCalledWith(
92
+ "https://api.quickbase.com/v1/records",
93
+ expect.objectContaining({
94
+ method: "POST",
95
+ headers: expect.objectContaining({
96
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
97
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
98
+ "Content-Type": "application/json",
99
+ }),
100
+ body: expect.any(String),
101
+ credentials: "omit",
102
+ })
103
+ );
104
+ const callArgs = mockFetch.mock.calls[0];
105
+ const receivedBody = JSON.parse(callArgs[1].body as string);
106
+ expect(receivedBody).toEqual({
107
+ to: QB_TABLE_ID_1,
108
+ data: [{ "999": { value: "invalid" } }],
109
+ });
110
+ });
111
+
112
+ test("handles upsert with temp token", async () => {
113
+ const qb = createClient(mockFetch, { useTempTokens: true, debug: true });
114
+
115
+ mockFetch
116
+ .mockResolvedValueOnce({
117
+ ok: true,
118
+ status: 200,
119
+ json: () => Promise.resolve({ temporaryAuthorization: "temp-token" }),
120
+ })
121
+ .mockResolvedValueOnce({
122
+ ok: true,
123
+ status: 200,
124
+ json: () =>
125
+ Promise.resolve({
126
+ data: [{ id: 2, "7": { value: "value2" } }],
127
+ metadata: {
128
+ createdRecordIds: [2],
129
+ updatedRecordIds: [],
130
+ unchangedRecordIds: [],
131
+ totalNumberOfRecordsProcessed: 1,
132
+ },
133
+ }),
134
+ });
135
+
136
+ const result = await qb.upsert({
137
+ body: { to: QB_TABLE_ID_1, data: [{ "7": { value: "value2" } }] },
138
+ dbid: QB_TABLE_ID_1, // Use QB_TABLE_ID_1 as dbid for temp token
139
+ } as any); // Temporary type assertion until types.d.ts is applied
140
+
141
+ expect(mockFetch).toHaveBeenCalledTimes(2);
142
+ expect(mockFetch).toHaveBeenNthCalledWith(
143
+ 1,
144
+ `https://api.quickbase.com/v1/auth/temporary/${QB_TABLE_ID_1}`,
145
+ expect.objectContaining({
146
+ method: "GET",
147
+ headers: expect.objectContaining({
148
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
149
+ "Content-Type": "application/json",
150
+ }),
151
+ credentials: "include",
152
+ })
153
+ );
154
+ expect(mockFetch).toHaveBeenNthCalledWith(
155
+ 2,
156
+ "https://api.quickbase.com/v1/records",
157
+ expect.objectContaining({
158
+ method: "POST",
159
+ headers: expect.objectContaining({
160
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
161
+ Authorization: "QB-TEMP-TOKEN temp-token",
162
+ "Content-Type": "application/json",
163
+ }),
164
+ body: expect.any(String),
165
+ credentials: "omit",
166
+ })
167
+ );
168
+ const callArgs = mockFetch.mock.calls[1];
169
+ const receivedBody = JSON.parse(callArgs[1].body as string);
170
+ expect(receivedBody).toEqual({
171
+ to: QB_TABLE_ID_1,
172
+ data: [{ "7": { value: "value2" } }],
173
+ });
174
+
175
+ expect(result).toEqual({
176
+ data: [{ id: 2, "7": { value: "value2" } }],
177
+ metadata: {
178
+ createdRecordIds: [2],
179
+ updatedRecordIds: [],
180
+ unchangedRecordIds: [],
181
+ totalNumberOfRecordsProcessed: 1,
182
+ },
183
+ });
184
+ });
185
+
186
+ test("sends correct POST request with data fields", async () => {
187
+ const qb = createClient(mockFetch, { debug: true });
188
+
189
+ mockFetch.mockResolvedValueOnce({
190
+ ok: true,
191
+ status: 200,
192
+ json: () =>
193
+ Promise.resolve({
194
+ data: [{ id: 1, "6": { value: "value1" } }],
195
+ metadata: {
196
+ createdRecordIds: [1],
197
+ updatedRecordIds: [],
198
+ unchangedRecordIds: [],
199
+ totalNumberOfRecordsProcessed: 1,
200
+ },
201
+ }),
202
+ });
203
+
204
+ const result = await qb.upsert({
205
+ body: { to: QB_TABLE_ID_1, data: [{ "6": { value: "value1" } }] },
206
+ });
207
+
208
+ expect(mockFetch).toHaveBeenCalledWith(
209
+ "https://api.quickbase.com/v1/records",
210
+ expect.objectContaining({
211
+ method: "POST",
212
+ headers: expect.objectContaining({
213
+ "QB-Realm-Hostname": `${QB_REALM}.quickbase.com`,
214
+ Authorization: `QB-USER-TOKEN ${QB_USER_TOKEN}`,
215
+ "Content-Type": "application/json",
216
+ }),
217
+ body: expect.any(String),
218
+ credentials: "omit",
219
+ })
220
+ );
221
+ const callArgs = mockFetch.mock.calls[0];
222
+ const receivedBody = JSON.parse(callArgs[1].body as string);
223
+ expect(receivedBody).toEqual({
224
+ to: QB_TABLE_ID_1,
225
+ data: [{ "6": { value: "value1" } }],
226
+ });
227
+
228
+ if (result.data && result.data.length > 0) {
229
+ expect((result.data[0]["6"] as { value: any }).value).toBe("value1");
230
+ } else {
231
+ throw new Error("Expected data array to have at least one element");
232
+ }
233
+ });
234
+ });