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

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 (60) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/esm/quickbase.d.ts +589 -125
  3. package/dist/esm/quickbase.js +484 -145
  4. package/dist/esm/quickbase.js.map +1 -1
  5. package/dist/esm/quickbase.min.js +1 -1
  6. package/dist/esm/quickbase.min.js.map +1 -1
  7. package/dist/umd/quickbase.umd.js +492 -144
  8. package/dist/umd/quickbase.umd.js.map +1 -1
  9. package/dist/umd/quickbase.umd.min.js +1 -1
  10. package/dist/umd/quickbase.umd.min.js.map +1 -1
  11. package/open-api/fix-spec-main.ts +69 -68
  12. package/open-api/generate-override-report.ts +214 -0
  13. package/open-api/generate-unified-interface.ts +73 -94
  14. package/open-api/output/override-report.html +11 -0
  15. package/open-api/output/override-report.json +14395 -58
  16. package/open-api/output/quickbase-fixed.json +583 -2
  17. package/open-api/schema/tags/fields.ts +331 -16
  18. package/open-api/schema/tags/index.ts +2 -4
  19. package/open-api/types/spec.ts +2 -1
  20. package/package.json +31 -33
  21. package/src/RateLimitError.ts +12 -0
  22. package/src/ThrottleBucket.ts +36 -0
  23. package/src/generated-unified/QuickbaseClient.ts +119 -119
  24. package/src/invokeMethod.ts +357 -0
  25. package/src/quickbaseClient.ts +51 -209
  26. package/tests/setup.ts +2 -0
  27. package/tests/vitest/qb/apps/getApp.test.ts +6 -2
  28. package/tests/vitest/qb/apps/getAppEvents.test.ts +67 -0
  29. package/tests/vitest/qb/apps/updateApp.test.ts +58 -0
  30. package/tests/vitest/qb/auth/retryUserToken401.test.ts +56 -0
  31. package/tests/vitest/qb/fields/createField.test.ts +1 -2
  32. package/tests/vitest/qb/fields/deleteFields.test.ts +10 -16
  33. package/tests/vitest/qb/fields/updateField.test.ts +93 -0
  34. package/tests/vitest/qb/index.ts +1 -0
  35. package/tests/vitest/qb/tables/updateRelationship.test.ts +277 -0
  36. package/tests/vitest/unit/apps/getAppEvents.test.ts +193 -0
  37. package/tests/vitest/unit/apps/updateApp.test.ts +381 -0
  38. package/tests/vitest/unit/auth/fetchTempToken401.test.ts +6 -26
  39. package/tests/vitest/unit/auth/{retryOn401.test.ts → retryTempToken401.test.ts} +3 -6
  40. package/tests/vitest/unit/auth/{two401s.test.ts → retryTempTokenTwo401.test.ts} +4 -9
  41. package/tests/vitest/unit/auth/retryUserToken401.test.ts +56 -0
  42. package/tests/vitest/unit/fields/createField.test.ts +8 -12
  43. package/tests/vitest/unit/fields/getFieldUsage.test.ts +33 -14
  44. package/tests/vitest/unit/fields/updateField.test.ts +280 -0
  45. package/tests/vitest/unit/rateLimit.test.ts +189 -0
  46. package/tests/vitest/unit/tables/updateRelationship.test.ts +305 -0
  47. package/tests/vitest/unit/tokenLifespan.test.ts +138 -0
  48. package/tools/log-gen.ts +8 -4
  49. package/tools/log-gen.yaml +3004 -1848
  50. package/tsconfig.json +5 -2
  51. package/vitest.config.ts +2 -2
  52. package/notes.txt +0 -158
  53. package/tools/generate-types.ts +0 -29
  54. package/tools/openapi-schema-validator.ts +0 -38
  55. package/tools/spec-analyzer.ts +0 -163
  56. package/tools/split-spec.ts +0 -148
  57. package/tools/test-import.ts +0 -3
  58. package/tools/validate-spec.ts +0 -40
  59. /package/tests/playwright/qb/auth/{tokenPrefetch.test.ts → tempTokenPrefetch.test.ts} +0 -0
  60. /package/tests/playwright/qb/auth/{tokenRenewal.test.ts → tempTokenRenewal.test.ts} +0 -0
@@ -28,11 +28,16 @@ export function enhanceFields(
28
28
  `Defining schema for ${requestName} in ${pathKey}(${method})`
29
29
  );
30
30
  let properties;
31
+ let requiredFields: string[];
32
+
31
33
  if (method === "delete") {
34
+ // DELETE /fields (deleteFields)
32
35
  properties = {
33
36
  fieldIds: { type: "array", items: { type: "integer" } },
34
37
  };
38
+ requiredFields = ["fieldIds"];
35
39
  } else {
40
+ // Common properties for createField and updateField
36
41
  properties = {
37
42
  label: { type: "string", description: "The label of the field" },
38
43
  fieldType: {
@@ -122,27 +127,337 @@ export function enhanceFields(
122
127
  description: "Whether changes to the field are audited",
123
128
  nullable: true,
124
129
  },
130
+ properties: {
131
+ type: "object",
132
+ description: "Specific field properties",
133
+ properties: {
134
+ numLines: {
135
+ type: "integer",
136
+ description:
137
+ "The number of lines shown in QuickBase for this text field",
138
+ },
139
+ maxLength: {
140
+ type: "integer",
141
+ description:
142
+ "The maximum number of characters allowed for entry",
143
+ },
144
+ appendOnly: {
145
+ type: "boolean",
146
+ description: "Whether this field is append-only",
147
+ },
148
+ sortAsGiven: {
149
+ type: "boolean",
150
+ description:
151
+ "Indicates if the listed entries sort as entered vs alphabetically",
152
+ },
153
+ allowMentions: {
154
+ type: "boolean",
155
+ description:
156
+ "If someone can @mention users in the rich text field",
157
+ },
158
+ comments: {
159
+ type: "string",
160
+ description:
161
+ "The comments entered on the field properties by an administrator",
162
+ },
163
+ doesTotal: {
164
+ type: "boolean",
165
+ description:
166
+ "Whether this field totals in reports within the product",
167
+ },
168
+ autoSave: {
169
+ type: "boolean",
170
+ description: "Whether the link field will auto save",
171
+ },
172
+ defaultValueLuid: {
173
+ type: "integer",
174
+ description: "Default user id value",
175
+ },
176
+ useI18NFormat: {
177
+ type: "boolean",
178
+ description:
179
+ "Whether phone numbers should be in E.164 standard international format",
180
+ },
181
+ maxVersions: {
182
+ type: "integer",
183
+ description:
184
+ "The maximum number of versions configured for a file attachment",
185
+ },
186
+ format: {
187
+ type: "integer",
188
+ description: "The format to display time",
189
+ },
190
+ carryChoices: {
191
+ type: "boolean",
192
+ description:
193
+ "Whether the field should carry its multiple choice fields when copied",
194
+ },
195
+ linkText: {
196
+ type: "string",
197
+ description:
198
+ "The configured text value that replaces the URL users see",
199
+ },
200
+ parentFieldId: {
201
+ type: "integer",
202
+ description:
203
+ "The id of the parent composite field, when applicable",
204
+ },
205
+ displayTimezone: {
206
+ type: "boolean",
207
+ description:
208
+ "Indicates whether to display the timezone within the product",
209
+ },
210
+ summaryTargetFieldId: {
211
+ type: "integer",
212
+ description:
213
+ "The id of the field used to aggregate values from the child",
214
+ },
215
+ allowNewChoices: {
216
+ type: "boolean",
217
+ description:
218
+ "Indicates if users can add new choices to a selection list",
219
+ },
220
+ defaultToday: {
221
+ type: "boolean",
222
+ description:
223
+ "Indicates if the field value is defaulted today for new records",
224
+ },
225
+ units: {
226
+ type: "string",
227
+ description: "The units label",
228
+ },
229
+ openTargetIn: {
230
+ type: "string",
231
+ enum: ["sameWindow", "newWindow", "popup"],
232
+ description:
233
+ "Indicates which target the URL should open in when clicked",
234
+ },
235
+ lookupTargetFieldId: {
236
+ type: "integer",
237
+ description:
238
+ "The id of the field that is the target on the parent table for this lookup",
239
+ },
240
+ summaryFunction: {
241
+ type: "string",
242
+ enum: [
243
+ "AVG",
244
+ "SUM",
245
+ "MAX",
246
+ "MIN",
247
+ "STD-DEV",
248
+ "COUNT",
249
+ "COMBINED-TEXT",
250
+ "COMBINED-USER",
251
+ "DISTINCT-COUNT",
252
+ ],
253
+ description: "The accumulation type for the summary field",
254
+ },
255
+ sourceFieldId: {
256
+ type: "integer",
257
+ description: "The id of the source field",
258
+ },
259
+ doesAverage: {
260
+ type: "boolean",
261
+ description:
262
+ "Whether this field averages in reports within the product",
263
+ },
264
+ formula: {
265
+ type: "string",
266
+ description:
267
+ "The formula of the field as configured in QuickBase",
268
+ },
269
+ decimalPlaces: {
270
+ type: "integer",
271
+ description:
272
+ "The number of decimal places displayed in the product",
273
+ },
274
+ defaultCountryCode: {
275
+ type: "string",
276
+ description:
277
+ "Controls the default country shown on international phone widgets",
278
+ },
279
+ displayMonth: {
280
+ type: "string",
281
+ description: "How to display months",
282
+ },
283
+ seeVersions: {
284
+ type: "boolean",
285
+ description:
286
+ "Indicates if users can see other versions of a file attachment",
287
+ },
288
+ defaultKind: {
289
+ type: "string",
290
+ description: "The user default type",
291
+ },
292
+ displayEmail: {
293
+ type: "string",
294
+ description: "How the email is displayed",
295
+ },
296
+ coverText: {
297
+ type: "string",
298
+ description:
299
+ "Alternate user-friendly text for displaying a link",
300
+ },
301
+ currencySymbol: {
302
+ type: "string",
303
+ description:
304
+ "The currency symbol used when displaying field values",
305
+ },
306
+ summaryQuery: {
307
+ type: "string",
308
+ description: "The summary query",
309
+ },
310
+ targetFieldId: {
311
+ type: "integer",
312
+ description: "The id of the target field",
313
+ },
314
+ displayUser: {
315
+ type: "string",
316
+ description: "The configured option for how users display",
317
+ },
318
+ blankIsZero: {
319
+ type: "boolean",
320
+ description:
321
+ "Whether a blank value is treated as 0 in calculations",
322
+ },
323
+ exact: {
324
+ type: "boolean",
325
+ description:
326
+ "Whether an exact match is required for a report link",
327
+ },
328
+ defaultDomain: {
329
+ type: "string",
330
+ description: "Default email domain",
331
+ },
332
+ defaultValue: {
333
+ type: "string",
334
+ description: "The default value configured for a field",
335
+ },
336
+ abbreviate: {
337
+ type: "boolean",
338
+ description:
339
+ "Don't show the URL protocol when showing the URL",
340
+ },
341
+ numberFormat: {
342
+ type: "integer",
343
+ description:
344
+ "The format used for displaying numeric values",
345
+ },
346
+ targetTableName: {
347
+ type: "string",
348
+ description: "The field's target table name",
349
+ },
350
+ appearsAs: {
351
+ type: "string",
352
+ description:
353
+ "The link text; if empty, the URL will be used",
354
+ },
355
+ width: {
356
+ type: "integer",
357
+ description: "The field's HTML input width in the product",
358
+ },
359
+ currencyFormat: {
360
+ type: "string",
361
+ enum: ["left", "right", "middle"],
362
+ description:
363
+ "The currency format used when displaying field values",
364
+ },
365
+ displayDayOfWeek: {
366
+ type: "boolean",
367
+ description:
368
+ "Indicates whether to display the day of the week",
369
+ },
370
+ summaryReferenceFieldId: {
371
+ type: "integer",
372
+ description:
373
+ "The id of the field that is the reference in the relationship",
374
+ },
375
+ commaStart: {
376
+ type: "integer",
377
+ description: "The number of digits before commas display",
378
+ },
379
+ choices: {
380
+ type: "array",
381
+ items: { type: "string" },
382
+ description:
383
+ "An array of entries that exist for a field offering choices",
384
+ },
385
+ targetTableId: {
386
+ type: "string",
387
+ description: "The id of the target table",
388
+ },
389
+ displayRelative: {
390
+ type: "boolean",
391
+ description: "Whether to display time as relative",
392
+ },
393
+ compositeFields: {
394
+ type: "array",
395
+ items: { type: "object", additionalProperties: true },
396
+ description:
397
+ "An array of fields that make up a composite field (e.g., address)",
398
+ },
399
+ displayCheckboxAsText: {
400
+ type: "boolean",
401
+ description:
402
+ "Indicates whether checkbox values are shown as text in reports",
403
+ },
404
+ versionMode: {
405
+ type: "string",
406
+ enum: ["keepallversions", "keeplastversions"],
407
+ description: "Version modes for files",
408
+ },
409
+ snapFieldId: {
410
+ type: "integer",
411
+ description: "The id of the field used to snapshot values",
412
+ },
413
+ hours24: {
414
+ type: "boolean",
415
+ description:
416
+ "Indicates whether to display time in 24-hour format",
417
+ },
418
+ sortAlpha: {
419
+ type: "boolean",
420
+ description:
421
+ "Whether to sort alphabetically (default is by record ID)",
422
+ },
423
+ hasExtension: {
424
+ type: "boolean",
425
+ description: "Whether this field has a phone extension",
426
+ },
427
+ useNewWindow: {
428
+ type: "boolean",
429
+ description:
430
+ "Indicates if the file should open a new window when clicked",
431
+ },
432
+ displayAsLink: {
433
+ type: "boolean",
434
+ description:
435
+ "Indicates if the field should be shown as a hyperlink",
436
+ },
437
+ lookupReferenceFieldId: {
438
+ type: "integer",
439
+ description:
440
+ "The id of the field that is the reference for this lookup",
441
+ },
442
+ },
443
+ nullable: true,
444
+ },
125
445
  };
446
+
447
+ // Adjust required fields based on operation
448
+ if (pathKey.includes("{fieldId}") && method === "post") {
449
+ // UpdateField (POST /fields/{fieldId})
450
+ requiredFields = ["label"]; // fieldType is not allowed in updates
451
+ } else {
452
+ // CreateField (POST /fields)
453
+ requiredFields = ["label", "fieldType"];
454
+ }
126
455
  }
456
+
127
457
  spec.definitions[requestName] = {
128
458
  type: "object",
129
459
  properties,
130
- required: Object.keys(properties).filter(
131
- (key) =>
132
- ![
133
- "fieldHelp",
134
- "addToForms",
135
- "permissions",
136
- "required",
137
- "unique",
138
- "noWrap",
139
- "bold",
140
- "appearsByDefault",
141
- "findEnabled",
142
- "doesDataCopy",
143
- "audited",
144
- ].includes(key)
145
- ),
460
+ required: requiredFields,
146
461
  description: operation.summary || `Request body for ${opId}`,
147
462
  };
148
463
  }
@@ -1,4 +1,4 @@
1
- // schema/tags/index.ts
1
+ // open-api/schema/tags/index.ts
2
2
  import { Operation, Parameter, Spec } from "../../types/spec.ts";
3
3
  import { normalizeDefinitionName } from "../../utils/naming.ts";
4
4
  import { enhanceUsers } from "./users.ts";
@@ -9,7 +9,7 @@ import { enhanceFields } from "./fields.ts";
9
9
  import { enhanceTables } from "./tables.ts";
10
10
 
11
11
  export function enhanceTags(spec: Spec): void {
12
- spec.definitions = spec.definitions || {};
12
+ console.log("enhanceTags version: 2025-03-16-fix-applied"); // Confirmation log
13
13
 
14
14
  for (const pathKey in spec.paths) {
15
15
  for (const method in spec.paths[pathKey]) {
@@ -17,7 +17,6 @@ export function enhanceTags(spec: Spec): void {
17
17
  const opId =
18
18
  operation.operationId || `${method}${pathKey.replace(/\W/g, "")}`;
19
19
 
20
- // Handle array schemas
21
20
  if (operation.parameters) {
22
21
  operation.parameters.forEach((param: Parameter) => {
23
22
  if (param.in === "body") {
@@ -56,7 +55,6 @@ export function enhanceTags(spec: Spec): void {
56
55
  });
57
56
  }
58
57
 
59
- // Apply tag-specific enhancements
60
58
  enhanceUsers(spec, pathKey, method, operation);
61
59
  enhanceGroups(spec, pathKey, method, operation);
62
60
  enhanceRecords(spec, pathKey, method, operation);
@@ -1,3 +1,4 @@
1
+ // open-api/types/spec.ts
1
2
  export interface Parameter {
2
3
  name: string;
3
4
  in: string;
@@ -26,7 +27,7 @@ export interface Operation {
26
27
 
27
28
  export interface Spec {
28
29
  paths: Record<string, Record<string, Operation>>;
29
- definitions?: Record<string, any>;
30
+ definitions: Record<string, any>; // Required, no longer optional
30
31
  swagger: string;
31
32
  info: any;
32
33
  operations?: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quickbase-js",
3
- "version": "0.1.0-beta.7",
3
+ "version": "0.1.0-beta.8",
4
4
  "description": "QuickBase JavaScript client for frameworks and vanilla JS",
5
5
  "type": "module",
6
6
  "main": "dist/esm/quickbase.js",
@@ -34,12 +34,10 @@
34
34
  "release:publish": "npm run release:push && npm publish --tag beta",
35
35
  "release:stable": "standard-version && git push --follow-tags origin main && npm publish",
36
36
  "fix-spec": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' open-api/fix-spec-main.ts",
37
+ "generate-report": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' open-api/generate-override-report.ts",
37
38
  "gen:openapi": "rimraf src/generated && node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' open-api/regenerate-client.ts",
38
39
  "gen:unified": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' open-api/generate-unified-interface.ts",
39
- "gen:all": "npm run validate-spec && npm run fix-spec && npm run gen:openapi && npm run gen:unified",
40
- "split-spec": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' tools/split-spec.ts",
41
- "validate-spec": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' tools/validate-spec.ts",
42
- "generate-types": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' tools/generate-types.ts",
40
+ "gen:all": "npm run fix-spec && npm run generate-report && npm run gen:openapi && npm run gen:unified",
43
41
  "log-gen": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' tools/log-gen.ts",
44
42
  "test:all": "vitest run tests/vitest/**/**/*.test.ts --reporter=basic && playwright test tests/playwright/qb --config playwright.config.ts --reporter=dot --quiet",
45
43
  "test:pw:qb:all": "playwright test tests/playwright/qb --config playwright.config.ts",
@@ -58,34 +56,34 @@
58
56
  "test:vitest:unit:tables": "vitest run tests/vitest/unit/tables/*.test.ts",
59
57
  "test:pw:qb:auth": "playwright test tests/playwright/qb/auth"
60
58
  },
61
- "dependencies": {
62
- "typescript": "^4.8.2"
63
- },
64
59
  "devDependencies": {
65
- "@openapitools/openapi-generator-cli": "^1.17.0",
66
- "@playwright/test": "^0.51.0",
67
- "@rollup/plugin-commonjs": "^27.0.3",
68
- "@rollup/plugin-node-resolve": "^15.0.0",
69
- "@rollup/plugin-terser": "^-1.4.4",
70
- "@rollup/plugin-typescript": "^11.1.2",
71
- "@types/js-yaml": "^3.0.9",
72
- "@types/node": "^21.13.10",
73
- "dotenv": "^15.4.7",
74
- "glob": "^10.0.1",
75
- "js-yaml": "^3.1.0",
76
- "jsdom": "^25.0.0",
77
- "node-fetch": "^2.3.2",
78
- "openapi-schema-validator": "^11.1.3",
79
- "openapi-types": "^11.1.3",
80
- "openapi-typescript": "^6.6.1",
81
- "rimraf": "^4.0.10",
82
- "rollup": "^3.35.0",
83
- "rollup-plugin-dts": "^5.1.1",
84
- "standard-version": "^8.5.0",
85
- "ts-morph": "^24.0.1",
86
- "ts-node": "^9.9.2",
87
- "tslib": "^1.8.1",
88
- "undici-types": "^6.5.0",
89
- "vitest": "^2.0.8"
60
+ "@openapitools/openapi-generator-cli": "^2.18.4",
61
+ "@playwright/test": "^1.51.0",
62
+ "@rollup/plugin-commonjs": "^28.0.3",
63
+ "@rollup/plugin-node-resolve": "^16.0.1",
64
+ "@rollup/plugin-terser": "^0.4.4",
65
+ "@rollup/plugin-typescript": "^12.1.2",
66
+ "@types/fast-json-patch": "^1.0.4",
67
+ "@types/js-yaml": "^4.0.9",
68
+ "@types/node": "^22.13.10",
69
+ "dotenv": "^16.4.7",
70
+ "fast-json-patch": "^3.1.1",
71
+ "glob": "^10.4.5",
72
+ "js-yaml": "^4.1.0",
73
+ "jsdom": "^26.0.0",
74
+ "node-fetch": "^3.3.2",
75
+ "openapi-schema-validator": "^12.1.3",
76
+ "openapi-types": "^12.1.3",
77
+ "openapi-typescript": "^7.6.1",
78
+ "rimraf": "^5.0.10",
79
+ "rollup": "^4.35.0",
80
+ "rollup-plugin-dts": "^6.1.1",
81
+ "standard-version": "^9.5.0",
82
+ "ts-morph": "^25.0.1",
83
+ "ts-node": "^10.9.2",
84
+ "tslib": "^2.8.1",
85
+ "typescript": "^5.8.2",
86
+ "undici-types": "^7.5.0",
87
+ "vitest": "^3.0.8"
90
88
  }
91
89
  }
@@ -0,0 +1,12 @@
1
+ // src/RateLimitError.ts
2
+ export class RateLimitError extends Error {
3
+ public readonly status: number;
4
+ public readonly retryAfter?: number;
5
+
6
+ constructor(message: string, status: number, retryAfter?: number) {
7
+ super(message);
8
+ this.name = "RateLimitError";
9
+ this.status = status;
10
+ this.retryAfter = retryAfter; // Seconds from Retry-After header, if present
11
+ }
12
+ }
@@ -0,0 +1,36 @@
1
+ // src/TokenBucket.ts
2
+ export class ThrottleBucket {
3
+ private tokens: number;
4
+ private maxTokens: number;
5
+ private refillRate: number; // Tokens per second
6
+ private lastRefill: number;
7
+
8
+ constructor(rate: number, burst: number) {
9
+ this.tokens = burst; // Start with full burst capacity
10
+ this.maxTokens = burst;
11
+ this.refillRate = rate;
12
+ this.lastRefill = Date.now();
13
+ }
14
+
15
+ // Refill tokens based on elapsed time
16
+ private refill(): void {
17
+ const now = Date.now();
18
+ const elapsed = (now - this.lastRefill) / 1000; // Seconds elapsed
19
+ const newTokens = elapsed * this.refillRate;
20
+ this.tokens = Math.min(this.maxTokens, this.tokens + newTokens);
21
+ this.lastRefill = now;
22
+ }
23
+
24
+ // Acquire a token, waiting if necessary
25
+ async acquire(): Promise<void> {
26
+ this.refill();
27
+ if (this.tokens >= 1) {
28
+ this.tokens -= 1;
29
+ return;
30
+ }
31
+ const waitTime = ((1 - this.tokens) / this.refillRate) * 1000; // ms until next token
32
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
33
+ this.refill();
34
+ this.tokens -= 1;
35
+ }
36
+ }