@uniformdev/context 18.8.1-alpha.1 → 18.9.0

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.
package/dist/api/api.d.ts CHANGED
@@ -47,6 +47,7 @@ declare class ApiClient<TOptions extends ClientOptions = ClientOptions> {
47
47
  private ensureApiHost;
48
48
  private static getRequestId;
49
49
  }
50
+ declare function handleRateLimits(callApi: () => Promise<Response>): Promise<Response>;
50
51
 
51
52
  declare class AggregateClient extends ApiClient {
52
53
  #private;
@@ -187,4 +188,4 @@ declare class CachedContextClient extends ContextClient {
187
188
  constructor(options: Omit<ClientOptions, 'bypassCache'>);
188
189
  }
189
190
 
190
- export { AggregateClient, ApiClient, ApiClientError, CachedAggregateClient, CachedContextClient, CachedDimensionClient, CachedEnrichmentClient, CachedManifestClient, CachedQuirkClient, CachedSignalClient, ClientOptions, ContextClient, DimensionClient, DimensionDisplayData, EnrichmentClient, ExceptProject, LimitPolicy, ManifestClient, QuirkClient, SignalClient, UncachedAggregateClient, UncachedContextClient, UncachedDimensionClient, UncachedEnrichmentClient, UncachedManifestClient, UncachedQuirkClient, UncachedSignalClient, computeDimensionDefinitionDisplayData, computeDimensionDisplayData, computeDimensionDisplayName, nullLimitPolicy };
191
+ export { AggregateClient, ApiClient, ApiClientError, CachedAggregateClient, CachedContextClient, CachedDimensionClient, CachedEnrichmentClient, CachedManifestClient, CachedQuirkClient, CachedSignalClient, ClientOptions, ContextClient, DimensionClient, DimensionDisplayData, EnrichmentClient, ExceptProject, LimitPolicy, ManifestClient, QuirkClient, SignalClient, UncachedAggregateClient, UncachedContextClient, UncachedDimensionClient, UncachedEnrichmentClient, UncachedManifestClient, UncachedQuirkClient, UncachedSignalClient, computeDimensionDefinitionDisplayData, computeDimensionDisplayData, computeDimensionDisplayName, handleRateLimits, nullLimitPolicy };
package/dist/api/api.js CHANGED
@@ -64,6 +64,7 @@ __export(api_exports, {
64
64
  computeDimensionDefinitionDisplayData: () => computeDimensionDefinitionDisplayData,
65
65
  computeDimensionDisplayData: () => computeDimensionDisplayData,
66
66
  computeDimensionDisplayName: () => computeDimensionDisplayName,
67
+ handleRateLimits: () => handleRateLimits,
67
68
  nullLimitPolicy: () => nullLimitPolicy
68
69
  });
69
70
  module.exports = __toCommonJS(api_exports);
@@ -126,13 +127,14 @@ var ApiClient = class {
126
127
  if (this.options.bypassCache) {
127
128
  coreHeaders["x-bypass-cache"] = "true";
128
129
  }
129
- const apiResponse = await this.options.fetch(fetchUri.toString(), {
130
+ const callApi = () => this.options.fetch(fetchUri.toString(), {
130
131
  ...options,
131
132
  headers: {
132
133
  ...options == null ? void 0 : options.headers,
133
134
  ...coreHeaders
134
135
  }
135
136
  });
137
+ const apiResponse = await handleRateLimits(callApi);
136
138
  if (!apiResponse.ok) {
137
139
  let message = "";
138
140
  try {
@@ -178,8 +180,8 @@ var ApiClient = class {
178
180
  ensureApiHost(apiHost) {
179
181
  if (!apiHost)
180
182
  return "https://uniform.app";
181
- if (!(apiHost == null ? void 0 : apiHost.startsWith("https://"))) {
182
- throw new Error('Your apiHost must start with "https://"');
183
+ if (!(apiHost == null ? void 0 : apiHost.startsWith("http"))) {
184
+ throw new Error('Your apiHost must start with "http"');
183
185
  }
184
186
  if (apiHost.indexOf("/", 8) > -1) {
185
187
  throw new Error("Your apiHost must not contain a path element after the domain");
@@ -200,6 +202,34 @@ var ApiClient = class {
200
202
  return void 0;
201
203
  }
202
204
  };
205
+ async function handleRateLimits(callApi) {
206
+ var _a;
207
+ const backoffRetries = 5;
208
+ let backoffRetriesLeft = backoffRetries;
209
+ let response;
210
+ while (backoffRetriesLeft > 0) {
211
+ response = await callApi();
212
+ if (response.status !== 429) {
213
+ break;
214
+ }
215
+ let resetWait = 0;
216
+ try {
217
+ const dateHeader = response.headers.get("date");
218
+ const serverTime = dateHeader ? new Date(dateHeader).getTime() : void 0;
219
+ const body = await response.json();
220
+ const resetTime = (_a = body == null ? void 0 : body.info) == null ? void 0 : _a.reset;
221
+ if (typeof serverTime === "number" && typeof resetTime === "number") {
222
+ resetWait = Math.max(0, Math.min(Math.round(1.1 * (resetTime - serverTime)), 1e4));
223
+ }
224
+ } catch (err) {
225
+ }
226
+ const base = Math.pow(2, backoffRetries - backoffRetriesLeft) * 333;
227
+ const backoffWait = base + Math.round(Math.random() * (base / 2)) * (Math.random() > 0.5 ? 1 : -1);
228
+ await new Promise((resolve) => setTimeout(resolve, resetWait + backoffWait));
229
+ backoffRetriesLeft -= 1;
230
+ }
231
+ return response;
232
+ }
203
233
 
204
234
  // src/api/AggregateClient.ts
205
235
  var _url;
@@ -571,5 +601,6 @@ var CachedContextClient = class extends ContextClient {
571
601
  computeDimensionDefinitionDisplayData,
572
602
  computeDimensionDisplayData,
573
603
  computeDimensionDisplayName,
604
+ handleRateLimits,
574
605
  nullLimitPolicy
575
606
  });
package/dist/api/api.mjs CHANGED
@@ -76,13 +76,14 @@ var ApiClient = class {
76
76
  if (this.options.bypassCache) {
77
77
  coreHeaders["x-bypass-cache"] = "true";
78
78
  }
79
- const apiResponse = await this.options.fetch(fetchUri.toString(), {
79
+ const callApi = () => this.options.fetch(fetchUri.toString(), {
80
80
  ...options,
81
81
  headers: {
82
82
  ...options == null ? void 0 : options.headers,
83
83
  ...coreHeaders
84
84
  }
85
85
  });
86
+ const apiResponse = await handleRateLimits(callApi);
86
87
  if (!apiResponse.ok) {
87
88
  let message = "";
88
89
  try {
@@ -128,8 +129,8 @@ var ApiClient = class {
128
129
  ensureApiHost(apiHost) {
129
130
  if (!apiHost)
130
131
  return "https://uniform.app";
131
- if (!(apiHost == null ? void 0 : apiHost.startsWith("https://"))) {
132
- throw new Error('Your apiHost must start with "https://"');
132
+ if (!(apiHost == null ? void 0 : apiHost.startsWith("http"))) {
133
+ throw new Error('Your apiHost must start with "http"');
133
134
  }
134
135
  if (apiHost.indexOf("/", 8) > -1) {
135
136
  throw new Error("Your apiHost must not contain a path element after the domain");
@@ -150,6 +151,34 @@ var ApiClient = class {
150
151
  return void 0;
151
152
  }
152
153
  };
154
+ async function handleRateLimits(callApi) {
155
+ var _a;
156
+ const backoffRetries = 5;
157
+ let backoffRetriesLeft = backoffRetries;
158
+ let response;
159
+ while (backoffRetriesLeft > 0) {
160
+ response = await callApi();
161
+ if (response.status !== 429) {
162
+ break;
163
+ }
164
+ let resetWait = 0;
165
+ try {
166
+ const dateHeader = response.headers.get("date");
167
+ const serverTime = dateHeader ? new Date(dateHeader).getTime() : void 0;
168
+ const body = await response.json();
169
+ const resetTime = (_a = body == null ? void 0 : body.info) == null ? void 0 : _a.reset;
170
+ if (typeof serverTime === "number" && typeof resetTime === "number") {
171
+ resetWait = Math.max(0, Math.min(Math.round(1.1 * (resetTime - serverTime)), 1e4));
172
+ }
173
+ } catch (err) {
174
+ }
175
+ const base = Math.pow(2, backoffRetries - backoffRetriesLeft) * 333;
176
+ const backoffWait = base + Math.round(Math.random() * (base / 2)) * (Math.random() > 0.5 ? 1 : -1);
177
+ await new Promise((resolve) => setTimeout(resolve, resetWait + backoffWait));
178
+ backoffRetriesLeft -= 1;
179
+ }
180
+ return response;
181
+ }
153
182
 
154
183
  // src/api/AggregateClient.ts
155
184
  var _url;
@@ -520,5 +549,6 @@ export {
520
549
  computeDimensionDefinitionDisplayData,
521
550
  computeDimensionDisplayData,
522
551
  computeDimensionDisplayName,
552
+ handleRateLimits,
523
553
  nullLimitPolicy
524
554
  };
package/dist/cli/cli.js CHANGED
@@ -19655,13 +19655,14 @@ var ApiClient = class {
19655
19655
  if (this.options.bypassCache) {
19656
19656
  coreHeaders["x-bypass-cache"] = "true";
19657
19657
  }
19658
- const apiResponse = await this.options.fetch(fetchUri.toString(), {
19658
+ const callApi = () => this.options.fetch(fetchUri.toString(), {
19659
19659
  ...options,
19660
19660
  headers: {
19661
19661
  ...options == null ? void 0 : options.headers,
19662
19662
  ...coreHeaders
19663
19663
  }
19664
19664
  });
19665
+ const apiResponse = await handleRateLimits(callApi);
19665
19666
  if (!apiResponse.ok) {
19666
19667
  let message = "";
19667
19668
  try {
@@ -19707,8 +19708,8 @@ var ApiClient = class {
19707
19708
  ensureApiHost(apiHost) {
19708
19709
  if (!apiHost)
19709
19710
  return "https://uniform.app";
19710
- if (!(apiHost == null ? void 0 : apiHost.startsWith("https://"))) {
19711
- throw new Error('Your apiHost must start with "https://"');
19711
+ if (!(apiHost == null ? void 0 : apiHost.startsWith("http"))) {
19712
+ throw new Error('Your apiHost must start with "http"');
19712
19713
  }
19713
19714
  if (apiHost.indexOf("/", 8) > -1) {
19714
19715
  throw new Error("Your apiHost must not contain a path element after the domain");
@@ -19729,6 +19730,34 @@ var ApiClient = class {
19729
19730
  return void 0;
19730
19731
  }
19731
19732
  };
19733
+ async function handleRateLimits(callApi) {
19734
+ var _a2;
19735
+ const backoffRetries = 5;
19736
+ let backoffRetriesLeft = backoffRetries;
19737
+ let response;
19738
+ while (backoffRetriesLeft > 0) {
19739
+ response = await callApi();
19740
+ if (response.status !== 429) {
19741
+ break;
19742
+ }
19743
+ let resetWait = 0;
19744
+ try {
19745
+ const dateHeader = response.headers.get("date");
19746
+ const serverTime = dateHeader ? new Date(dateHeader).getTime() : void 0;
19747
+ const body = await response.json();
19748
+ const resetTime = (_a2 = body == null ? void 0 : body.info) == null ? void 0 : _a2.reset;
19749
+ if (typeof serverTime === "number" && typeof resetTime === "number") {
19750
+ resetWait = Math.max(0, Math.min(Math.round(1.1 * (resetTime - serverTime)), 1e4));
19751
+ }
19752
+ } catch (err) {
19753
+ }
19754
+ const base = Math.pow(2, backoffRetries - backoffRetriesLeft) * 333;
19755
+ const backoffWait = base + Math.round(Math.random() * (base / 2)) * (Math.random() > 0.5 ? 1 : -1);
19756
+ await new Promise((resolve5) => setTimeout(resolve5, resetWait + backoffWait));
19757
+ backoffRetriesLeft -= 1;
19758
+ }
19759
+ return response;
19760
+ }
19732
19761
 
19733
19762
  // src/api/AggregateClient.ts
19734
19763
  var _url;
package/dist/cli/cli.mjs CHANGED
@@ -19652,13 +19652,14 @@ var ApiClient = class {
19652
19652
  if (this.options.bypassCache) {
19653
19653
  coreHeaders["x-bypass-cache"] = "true";
19654
19654
  }
19655
- const apiResponse = await this.options.fetch(fetchUri.toString(), {
19655
+ const callApi = () => this.options.fetch(fetchUri.toString(), {
19656
19656
  ...options,
19657
19657
  headers: {
19658
19658
  ...options == null ? void 0 : options.headers,
19659
19659
  ...coreHeaders
19660
19660
  }
19661
19661
  });
19662
+ const apiResponse = await handleRateLimits(callApi);
19662
19663
  if (!apiResponse.ok) {
19663
19664
  let message = "";
19664
19665
  try {
@@ -19704,8 +19705,8 @@ var ApiClient = class {
19704
19705
  ensureApiHost(apiHost) {
19705
19706
  if (!apiHost)
19706
19707
  return "https://uniform.app";
19707
- if (!(apiHost == null ? void 0 : apiHost.startsWith("https://"))) {
19708
- throw new Error('Your apiHost must start with "https://"');
19708
+ if (!(apiHost == null ? void 0 : apiHost.startsWith("http"))) {
19709
+ throw new Error('Your apiHost must start with "http"');
19709
19710
  }
19710
19711
  if (apiHost.indexOf("/", 8) > -1) {
19711
19712
  throw new Error("Your apiHost must not contain a path element after the domain");
@@ -19726,6 +19727,34 @@ var ApiClient = class {
19726
19727
  return void 0;
19727
19728
  }
19728
19729
  };
19730
+ async function handleRateLimits(callApi) {
19731
+ var _a2;
19732
+ const backoffRetries = 5;
19733
+ let backoffRetriesLeft = backoffRetries;
19734
+ let response;
19735
+ while (backoffRetriesLeft > 0) {
19736
+ response = await callApi();
19737
+ if (response.status !== 429) {
19738
+ break;
19739
+ }
19740
+ let resetWait = 0;
19741
+ try {
19742
+ const dateHeader = response.headers.get("date");
19743
+ const serverTime = dateHeader ? new Date(dateHeader).getTime() : void 0;
19744
+ const body = await response.json();
19745
+ const resetTime = (_a2 = body == null ? void 0 : body.info) == null ? void 0 : _a2.reset;
19746
+ if (typeof serverTime === "number" && typeof resetTime === "number") {
19747
+ resetWait = Math.max(0, Math.min(Math.round(1.1 * (resetTime - serverTime)), 1e4));
19748
+ }
19749
+ } catch (err) {
19750
+ }
19751
+ const base = Math.pow(2, backoffRetries - backoffRetriesLeft) * 333;
19752
+ const backoffWait = base + Math.round(Math.random() * (base / 2)) * (Math.random() > 0.5 ? 1 : -1);
19753
+ await new Promise((resolve5) => setTimeout(resolve5, resetWait + backoffWait));
19754
+ backoffRetriesLeft -= 1;
19755
+ }
19756
+ return response;
19757
+ }
19729
19758
 
19730
19759
  // src/api/AggregateClient.ts
19731
19760
  var _url;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/context",
3
- "version": "18.8.1-alpha.1+bd6b62da8",
3
+ "version": "18.9.0",
4
4
  "description": "Uniform Context core package",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "main": "./dist/index.js",
@@ -59,7 +59,7 @@
59
59
  "devDependencies": {
60
60
  "@types/js-cookie": "3.0.2",
61
61
  "@types/yargs": "17.0.22",
62
- "@uniformdev/cli": "18.8.1-alpha.1+bd6b62da8",
62
+ "@uniformdev/cli": "18.9.0",
63
63
  "benny": "3.7.1",
64
64
  "yargs": "17.6.2"
65
65
  },
@@ -76,5 +76,5 @@
76
76
  "publishConfig": {
77
77
  "access": "public"
78
78
  },
79
- "gitHead": "bd6b62da892304cf7aa52da4d5214858fd6ee5ba"
79
+ "gitHead": "684988a2ac324b69e6b4799e55f2d33db0c7b652"
80
80
  }