quickbase-js 0.1.0-beta.7 → 0.1.0-beta.9

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 (82) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +46 -130
  3. package/dist/esm/quickbase.d.ts +1292 -125
  4. package/dist/esm/quickbase.js +666 -190
  5. package/dist/esm/quickbase.js.map +1 -1
  6. package/dist/esm/quickbase.min.js +1 -1
  7. package/dist/esm/quickbase.min.js.map +1 -1
  8. package/dist/umd/quickbase.umd.js +674 -189
  9. package/dist/umd/quickbase.umd.js.map +1 -1
  10. package/dist/umd/quickbase.umd.min.js +1 -1
  11. package/dist/umd/quickbase.umd.min.js.map +1 -1
  12. package/open-api/fix-spec-main.ts +69 -68
  13. package/open-api/generate-override-report.ts +214 -0
  14. package/open-api/generate-unified-interface.ts +162 -103
  15. package/open-api/output/override-report.html +11 -0
  16. package/open-api/output/override-report.json +14395 -58
  17. package/open-api/output/quickbase-fixed.json +583 -2
  18. package/open-api/schema/tags/fields.ts +331 -16
  19. package/open-api/schema/tags/index.ts +2 -4
  20. package/open-api/types/spec.ts +2 -1
  21. package/open-api/utils/generateJsDoc.ts +96 -0
  22. package/package.json +33 -33
  23. package/src/RateLimitError.ts +12 -0
  24. package/src/ThrottleBucket.ts +41 -0
  25. package/src/authorizationStrategy.ts +246 -0
  26. package/src/generated-unified/QuickbaseClient.ts +809 -119
  27. package/src/invokeMethod.ts +242 -0
  28. package/src/quickbaseClient.ts +117 -248
  29. package/src/rateLimiter.ts +25 -0
  30. package/src/tokenCache.ts +10 -26
  31. package/tests/setup.ts +2 -0
  32. package/tests/vitest/qb/apps/getApp.test.ts +6 -2
  33. package/tests/vitest/qb/apps/getAppEvents.test.ts +67 -0
  34. package/tests/vitest/qb/apps/updateApp.test.ts +58 -0
  35. package/tests/vitest/qb/auth/mockQuickbaseServer.ts +60 -0
  36. package/tests/vitest/qb/auth/retryUserToken401.test.ts +73 -0
  37. package/tests/vitest/qb/auth/ssoTokenIntegrationWithServer.test.ts +65 -0
  38. package/tests/vitest/qb/fields/createField.test.ts +1 -2
  39. package/tests/vitest/qb/fields/deleteFields.test.ts +10 -16
  40. package/tests/vitest/qb/fields/getFieldUsage.test.ts +58 -150
  41. package/tests/vitest/qb/fields/updateField.test.ts +93 -0
  42. package/tests/vitest/qb/formulas/runFormula.test.ts +96 -0
  43. package/tests/vitest/qb/index.ts +1 -0
  44. package/tests/vitest/qb/tables/createRelationship.test.ts +102 -0
  45. package/tests/vitest/qb/tables/deleteRelationship.test.ts +143 -0
  46. package/tests/vitest/qb/tables/updateRelationship.test.ts +250 -0
  47. package/tests/vitest/unit/ThrottleBucket.test.ts +95 -0
  48. package/tests/vitest/unit/apps/copyApp.test.ts +133 -40
  49. package/tests/vitest/unit/apps/getAppEvents.test.ts +193 -0
  50. package/tests/vitest/unit/apps/updateApp.test.ts +412 -0
  51. package/tests/vitest/unit/auth/fetchTempToken401.test.ts +7 -26
  52. package/tests/vitest/unit/auth/getTempToken.test.ts +42 -18
  53. package/tests/vitest/unit/auth/retryTempToken401.test.ts +95 -0
  54. package/tests/vitest/unit/auth/retryTempTokenMaxRetries.test.ts +235 -0
  55. package/tests/vitest/unit/auth/retryUserToken401.test.ts +69 -0
  56. package/tests/vitest/unit/auth/ssoTokenRefresh.test.ts +325 -0
  57. package/tests/vitest/unit/fields/createField.test.ts +180 -120
  58. package/tests/vitest/unit/fields/getFieldUsage.test.ts +47 -14
  59. package/tests/vitest/unit/fields/updateField.test.ts +285 -0
  60. package/tests/vitest/unit/formulas/runFormula.test.ts +205 -0
  61. package/tests/vitest/unit/index.ts +2 -1
  62. package/tests/vitest/unit/inferHttpMethod.test.ts +131 -0
  63. package/tests/vitest/unit/rateLimit.test.ts +191 -0
  64. package/tests/vitest/unit/tables/createRelationship.test.ts +212 -0
  65. package/tests/vitest/unit/tables/deleteRelationship.test.ts +97 -0
  66. package/tests/vitest/unit/tables/updateRelationship.test.ts +307 -0
  67. package/tests/vitest/unit/tokenLifespan.test.ts +204 -0
  68. package/tools/log-gen.ts +20 -7
  69. package/tools/log-gen.yaml +3446 -1848
  70. package/tsconfig.json +5 -2
  71. package/vitest.config.ts +2 -2
  72. package/notes.txt +0 -158
  73. package/tests/vitest/unit/auth/retryOn401.test.ts +0 -116
  74. package/tests/vitest/unit/auth/two401s.test.ts +0 -86
  75. package/tools/generate-types.ts +0 -29
  76. package/tools/openapi-schema-validator.ts +0 -38
  77. package/tools/spec-analyzer.ts +0 -163
  78. package/tools/split-spec.ts +0 -148
  79. package/tools/test-import.ts +0 -3
  80. package/tools/validate-spec.ts +0 -40
  81. /package/tests/playwright/qb/auth/{tokenPrefetch.test.ts → tempTokenPrefetch.test.ts} +0 -0
  82. /package/tests/playwright/qb/auth/{tokenRenewal.test.ts → tempTokenRenewal.test.ts} +0 -0
@@ -0,0 +1,246 @@
1
+ // src/authorizationStrategy.ts
2
+
3
+ import { TokenCache } from "./tokenCache";
4
+
5
+ export interface AuthorizationStrategy {
6
+ getToken(dbid: string): Promise<string | undefined>;
7
+ applyHeaders(headers: Record<string, string>, token: string): void;
8
+ handleError(
9
+ status: number,
10
+ params: any,
11
+ fetchTempToken: (dbid: string) => Promise<string>,
12
+ attempt: number,
13
+ maxAttempts: number,
14
+ debug?: boolean,
15
+ methodName?: string
16
+ ): Promise<string | null>;
17
+ }
18
+
19
+ export class TempTokenStrategy implements AuthorizationStrategy {
20
+ constructor(
21
+ private tokenCache: TokenCache,
22
+ private initialTempToken?: string
23
+ ) {}
24
+
25
+ async getToken(dbid: string): Promise<string | undefined> {
26
+ return this.tokenCache.get(dbid) || this.initialTempToken;
27
+ }
28
+
29
+ applyHeaders(headers: Record<string, string>, token: string): void {
30
+ headers["Authorization"] = `QB-TEMP-TOKEN ${token}`;
31
+ }
32
+
33
+ async handleError(
34
+ status: number,
35
+ params: any,
36
+ fetchTempToken: (dbid: string) => Promise<string>,
37
+ attempt: number,
38
+ maxAttempts: number,
39
+ debug?: boolean,
40
+ methodName?: string
41
+ ): Promise<string | null> {
42
+ if (status !== 401 || attempt >= maxAttempts - 1) return null;
43
+ if (debug)
44
+ console.log(
45
+ `Authorization error for ${
46
+ methodName || "method"
47
+ } (temp token), refreshing token:`
48
+ );
49
+ const dbid = extractDbid(params);
50
+ if (!dbid) {
51
+ if (debug)
52
+ console.log(
53
+ `No dbid available for ${
54
+ methodName || "method"
55
+ }, skipping token refresh`
56
+ );
57
+ return null;
58
+ }
59
+ if (debug) console.log(`Refreshing temp token for dbid: ${dbid}`);
60
+ const newToken = await fetchTempToken(dbid);
61
+ this.tokenCache.set(dbid, newToken);
62
+ return newToken;
63
+ }
64
+ }
65
+
66
+ export class UserTokenStrategy implements AuthorizationStrategy {
67
+ constructor(private userToken: string) {}
68
+
69
+ async getToken(_dbid: string): Promise<string> {
70
+ return this.userToken;
71
+ }
72
+
73
+ applyHeaders(headers: Record<string, string>, token: string): void {
74
+ headers["Authorization"] = `QB-USER-TOKEN ${token}`;
75
+ }
76
+
77
+ async handleError(
78
+ status: number,
79
+ _params: any,
80
+ _fetchTempToken: (dbid: string) => Promise<string>,
81
+ attempt: number,
82
+ maxAttempts: number,
83
+ debug?: boolean,
84
+ methodName?: string
85
+ ): Promise<string | null> {
86
+ if (status !== 401 || attempt >= maxAttempts - 1) return null;
87
+ if (debug)
88
+ console.log(
89
+ `Retrying ${
90
+ methodName || "method"
91
+ } with existing user token: ${this.userToken.substring(0, 10)}...`
92
+ );
93
+ return this.userToken;
94
+ }
95
+ }
96
+
97
+ export class SsoTokenStrategy implements AuthorizationStrategy {
98
+ private currentToken: string | undefined;
99
+
100
+ constructor(
101
+ private samlToken: string,
102
+ private realm: string,
103
+ private fetchApi: typeof fetch,
104
+ private debug: boolean = false
105
+ ) {}
106
+
107
+ async getToken(_dbid: string): Promise<string | undefined> {
108
+ if (!this.currentToken) {
109
+ this.currentToken = await this.fetchSsoToken({
110
+ grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
111
+ requested_token_type:
112
+ "urn:quickbase:params:oauth:token-type:temp_token",
113
+ subject_token: this.samlToken,
114
+ subject_token_type: "urn:ietf:params:oauth:token-type:saml2",
115
+ });
116
+ }
117
+ return this.currentToken;
118
+ }
119
+
120
+ applyHeaders(headers: Record<string, string>, token: string): void {
121
+ headers["Authorization"] = `QB-TEMP-TOKEN ${token}`;
122
+ }
123
+
124
+ async handleError(
125
+ status: number,
126
+ _params: any,
127
+ _fetchTempToken: (dbid: string) => Promise<string>,
128
+ attempt: number,
129
+ maxAttempts: number,
130
+ debug?: boolean,
131
+ methodName?: string
132
+ ): Promise<string | null> {
133
+ if (status !== 401 || attempt >= maxAttempts - 1) return null;
134
+
135
+ if (debug || this.debug) {
136
+ console.log(
137
+ `Authorization error for ${
138
+ methodName || "method"
139
+ } (SSO), refreshing token`
140
+ );
141
+ }
142
+
143
+ const newToken = await this.refreshSsoToken(debug || this.debug);
144
+ if (newToken) {
145
+ this.currentToken = newToken;
146
+ return newToken;
147
+ }
148
+ return null;
149
+ }
150
+
151
+ private async refreshSsoToken(debug: boolean = false): Promise<string> {
152
+ const payload = {
153
+ grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
154
+ requested_token_type: "urn:quickbase:params:oauth:token-type:temp_token",
155
+ subject_token: this.samlToken,
156
+ subject_token_type: "urn:ietf:params:oauth:token-type:saml2",
157
+ };
158
+
159
+ const response = await this.fetchApi(
160
+ `https://api.quickbase.com/v1/auth/oauth/token`,
161
+ {
162
+ method: "POST",
163
+ headers: {
164
+ "QB-Realm-Hostname": `${this.realm}.quickbase.com`,
165
+ "Content-Type": "application/json",
166
+ },
167
+ body: JSON.stringify(payload),
168
+ credentials: "omit",
169
+ }
170
+ );
171
+
172
+ if (!response.ok) {
173
+ const errorBody = await response.json().catch(() => ({}));
174
+ if (debug) {
175
+ console.log(`[SSO Refresh] Failed: ${response.status}`, errorBody);
176
+ }
177
+ throw new Error(
178
+ `SSO token refresh failed: ${errorBody.message || "Unknown error"}`
179
+ );
180
+ }
181
+
182
+ const result = await response.json();
183
+ const newToken = result.access_token;
184
+ if (!newToken) {
185
+ throw new Error("No access token returned from SSO token exchange");
186
+ }
187
+
188
+ if (debug) {
189
+ console.log(`[SSO Refresh] New token: ${newToken.substring(0, 10)}...`);
190
+ }
191
+ return newToken;
192
+ }
193
+
194
+ private async fetchSsoToken(params: {
195
+ grant_type: string;
196
+ requested_token_type: string;
197
+ subject_token: string;
198
+ subject_token_type: string;
199
+ }): Promise<string> {
200
+ const response = await this.fetchApi(
201
+ `https://api.quickbase.com/v1/auth/oauth/token`,
202
+ {
203
+ method: "POST",
204
+ headers: {
205
+ "QB-Realm-Hostname": `${this.realm}.quickbase.com`,
206
+ "Content-Type": "application/json",
207
+ },
208
+ body: JSON.stringify(params),
209
+ credentials: "omit",
210
+ }
211
+ );
212
+
213
+ if (!response.ok) {
214
+ const errorBody = await response.json().catch(() => ({}));
215
+ if (this.debug) {
216
+ console.log(`[fetchSsoToken] Failed: ${response.status}`, errorBody);
217
+ }
218
+ throw new Error(
219
+ `SSO token fetch failed: ${errorBody.message || "Unknown error"}`
220
+ );
221
+ }
222
+
223
+ const result = await response.json();
224
+ const newToken = result.access_token;
225
+ if (!newToken) {
226
+ throw new Error("No access token returned from SSO token exchange");
227
+ }
228
+
229
+ if (this.debug) {
230
+ console.log(
231
+ `[fetchSsoToken] Fetched token: ${newToken.substring(0, 10)}...`
232
+ );
233
+ }
234
+ return newToken;
235
+ }
236
+ }
237
+
238
+ export function extractDbid(params: any): string | undefined {
239
+ return (
240
+ params.dbid ||
241
+ params.tableId ||
242
+ params.appId ||
243
+ params.body?.from ||
244
+ params.body?.to
245
+ );
246
+ }