@izak0s/spacebring-api 1.1.0 → 1.2.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/index.js CHANGED
@@ -25,7 +25,7 @@ function createClient(clientOptions) {
25
25
  const {
26
26
  baseUrl: localBaseUrl,
27
27
  fetch = baseFetch,
28
- Request = CustomRequest,
28
+ Request: Request2 = CustomRequest,
29
29
  headers,
30
30
  params = {},
31
31
  parseAs = "json",
@@ -77,7 +77,7 @@ function createClient(clientOptions) {
77
77
  };
78
78
  let id;
79
79
  let options;
80
- let request = new Request(
80
+ let request = new Request2(
81
81
  createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer, pathSerializer }),
82
82
  requestInit
83
83
  );
@@ -107,7 +107,7 @@ function createClient(clientOptions) {
107
107
  id
108
108
  });
109
109
  if (result) {
110
- if (result instanceof Request) {
110
+ if (result instanceof Request2) {
111
111
  request = result;
112
112
  } else if (result instanceof Response) {
113
113
  response = result;
@@ -1695,8 +1695,8 @@ function createSubscriptions(client, defaults) {
1695
1695
  *
1696
1696
  * Retrieve a certain subscription.
1697
1697
  */
1698
- async get(id) {
1699
- return unwrapProp(await client.GET("/subscriptions/v1/{id}", { params: { path: { id } } }), "subscription");
1698
+ async get(subscriptionId) {
1699
+ return unwrapProp(await client.GET("/subscriptions/v1/{subscriptionId}", { params: { path: { subscriptionId } } }), "subscription");
1700
1700
  },
1701
1701
  /**
1702
1702
  * Create a subscription
@@ -1711,28 +1711,28 @@ function createSubscriptions(client, defaults) {
1711
1711
  *
1712
1712
  * Update a certain subscription.
1713
1713
  */
1714
- async update(id, body) {
1715
- return unwrap(await client.PATCH("/subscriptions/v1/{id}", { params: { path: { id } }, body }));
1714
+ async update(subscriptionId, body) {
1715
+ return unwrap(await client.PATCH("/subscriptions/v1/{subscriptionId}", { params: { path: { subscriptionId } }, body }));
1716
1716
  },
1717
1717
  /**
1718
1718
  * Delete a subscription
1719
1719
  *
1720
1720
  * Delete a certain subscription.
1721
1721
  */
1722
- async delete(id) {
1723
- return unwrap(await client.DELETE("/subscriptions/v1/{id}", { params: { path: { id } } }));
1722
+ async delete(subscriptionId) {
1723
+ return unwrap(await client.DELETE("/subscriptions/v1/{subscriptionId}", { params: { path: { subscriptionId } } }));
1724
1724
  },
1725
1725
  /** Create a subscription item */
1726
- async createItem(id, body) {
1727
- return unwrapProp(await client.POST("/subscriptions/v1/{id}/items", { params: { path: { id } }, body }), "subscription");
1726
+ async createItem(subscriptionId, body) {
1727
+ return unwrapProp(await client.POST("/subscriptions/v1/{subscriptionId}/items", { params: { path: { subscriptionId } }, body }), "subscription");
1728
1728
  },
1729
1729
  /** Delete a subscription item */
1730
- async deleteItem(id, itemId) {
1731
- return unwrap(await client.DELETE("/subscriptions/v1/{id}/items/{itemId}", { params: { path: { id, itemId } } }));
1730
+ async deleteItem(subscriptionId, itemId) {
1731
+ return unwrap(await client.DELETE("/subscriptions/v1/{subscriptionId}/items/{itemId}", { params: { path: { subscriptionId, itemId } } }));
1732
1732
  },
1733
1733
  /** Update a subscription item */
1734
- async updateItem(id, itemId, body) {
1735
- return unwrap(await client.PATCH("/subscriptions/v1/{id}/items/{itemId}", { params: { path: { id, itemId } }, body }));
1734
+ async updateItem(subscriptionId, itemId, body) {
1735
+ return unwrap(await client.PATCH("/subscriptions/v1/{subscriptionId}/items/{itemId}", { params: { path: { subscriptionId, itemId } }, body }));
1736
1736
  }
1737
1737
  };
1738
1738
  }
@@ -2062,11 +2062,25 @@ var Spacebring = class {
2062
2062
  this.raw = createClient({
2063
2063
  baseUrl: config.baseUrl ?? "https://api.spacebring.com",
2064
2064
  headers,
2065
- fetch: config.fetch
2065
+ fetch: withRetry(config.fetch ?? globalThis.fetch, config.maxRetries ?? 3)
2066
2066
  });
2067
2067
  Object.assign(this, createResources2(this.raw, { networkId: config.networkId }));
2068
2068
  }
2069
2069
  };
2070
+ function withRetry(fetchImpl, maxRetries) {
2071
+ if (maxRetries <= 0) return fetchImpl;
2072
+ return async (input, init) => {
2073
+ const request = input instanceof Request && init === void 0 ? input : new Request(input, init);
2074
+ for (let attempt = 0; ; attempt += 1) {
2075
+ const response = await fetchImpl(request.clone());
2076
+ if (response.status !== 429 || attempt >= maxRetries) return response;
2077
+ const retryAfterHeader = response.headers.get("retry-after");
2078
+ const retryAfter = retryAfterHeader === null ? Number.NaN : Number(retryAfterHeader);
2079
+ const delayMs = Number.isFinite(retryAfter) && retryAfter >= 0 ? Math.min(retryAfter * 1e3, 6e4) : Math.min(250 * 2 ** attempt + Math.random() * 100, 5e3);
2080
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
2081
+ }
2082
+ };
2083
+ }
2070
2084
  function toBase64(value) {
2071
2085
  if (typeof Buffer !== "undefined") {
2072
2086
  return Buffer.from(value, "utf8").toString("base64");