lifeastroapi 0.9.0 → 0.12.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.cjs CHANGED
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
23
  AuthenticationError: () => AuthenticationError,
24
24
  BadRequestError: () => BadRequestError,
25
25
  LifeAstro: () => LifeAstro,
@@ -31,9 +31,9 @@ __export(index_exports, {
31
31
  RateLimitError: () => RateLimitError,
32
32
  ServerError: () => ServerError,
33
33
  VERSION: () => VERSION,
34
- default: () => index_default
34
+ default: () => src_default
35
35
  });
36
- module.exports = __toCommonJS(index_exports);
36
+ module.exports = __toCommonJS(src_exports);
37
37
 
38
38
  // src/errors.ts
39
39
  var LifeAstroError = class extends Error {
@@ -102,6 +102,40 @@ function httpStatusCode(status) {
102
102
  }
103
103
 
104
104
  // src/http.ts
105
+ function responseInfo(res, path) {
106
+ const h = res.headers;
107
+ const num = (name) => {
108
+ const v = h.get(name);
109
+ if (v === null || v.trim() === "") return void 0;
110
+ const n = Number(v);
111
+ return Number.isFinite(n) ? n : void 0;
112
+ };
113
+ const info = { status: res.status, path };
114
+ const rid = h.get("x-request-id");
115
+ if (rid) info.requestId = rid;
116
+ const credits = num("x-credits-remaining");
117
+ if (credits !== void 0) info.creditsRemaining = credits;
118
+ const limit = num("x-ratelimit-limit");
119
+ const remaining = num("x-ratelimit-remaining");
120
+ const reset = num("x-ratelimit-reset");
121
+ if (limit !== void 0 && remaining !== void 0 && reset !== void 0) {
122
+ info.rateLimit = { limit, remaining, resetAt: new Date(reset * 1e3) };
123
+ }
124
+ const cache = h.get("x-cache");
125
+ if (cache) info.cache = cache;
126
+ const lang = h.get("content-language");
127
+ if (lang) info.language = lang;
128
+ return info;
129
+ }
130
+ function emit(hooks, info) {
131
+ for (const hook of hooks) {
132
+ if (!hook) continue;
133
+ try {
134
+ hook(info);
135
+ } catch {
136
+ }
137
+ }
138
+ }
105
139
  var RETRYABLE_STATUS = /* @__PURE__ */ new Set([408, 425, 429, 500, 502, 503, 504]);
106
140
  function encodeQuery(params) {
107
141
  const usp = new URLSearchParams();
@@ -179,7 +213,7 @@ async function performRequest(config, path, query, opts = {}) {
179
213
  });
180
214
  text = await res.text();
181
215
  } catch (cause) {
182
- const aborted = cause?.name === "AbortError";
216
+ const aborted = controller.signal.aborted || cause?.name === "AbortError";
183
217
  if (aborted && opts.signal?.aborted) {
184
218
  throw new LifeAstroConnectionError("Request aborted by caller", { cause, code: "aborted" });
185
219
  }
@@ -203,6 +237,7 @@ async function performRequest(config, path, query, opts = {}) {
203
237
  body = text;
204
238
  }
205
239
  if (res.ok) {
240
+ emit([config.onResponse, opts.onResponse], responseInfo(res, path));
206
241
  if (opts.raw) return body;
207
242
  const envelope = body;
208
243
  if (envelope && typeof envelope === "object" && "data" in envelope) {
@@ -216,6 +251,7 @@ async function performRequest(config, path, query, opts = {}) {
216
251
  await sleep(backoffMs(attempt, retryAfter), opts.signal);
217
252
  continue;
218
253
  }
254
+ emit([config.onResponse, opts.onResponse], responseInfo(res, path));
219
255
  throw lastError;
220
256
  }
221
257
  throw lastError ?? new LifeAstroConnectionError("Request failed after retries");
@@ -2012,7 +2048,7 @@ var Resources = class {
2012
2048
  };
2013
2049
 
2014
2050
  // src/client.ts
2015
- var VERSION = "0.9.0";
2051
+ var VERSION = "0.12.0";
2016
2052
  var DEFAULT_BASE_URL = "https://api.lifeastroapi.com";
2017
2053
  function resolveConfig(options) {
2018
2054
  const opts = typeof options === "string" ? { apiKey: options } : options ?? {};
@@ -2036,7 +2072,8 @@ function resolveConfig(options) {
2036
2072
  maxRetries: Math.max(0, opts.maxRetries ?? 2),
2037
2073
  fetch: fetchImpl,
2038
2074
  userAgent: `lifeastroapi-node/${VERSION}`,
2039
- defaultHeaders: opts.defaultHeaders ?? {}
2075
+ defaultHeaders: opts.defaultHeaders ?? {},
2076
+ onResponse: opts.onResponse
2040
2077
  };
2041
2078
  }
2042
2079
  var LifeAstro = class extends Resources {
@@ -2070,7 +2107,7 @@ var LifeAstro = class extends Resources {
2070
2107
  };
2071
2108
 
2072
2109
  // src/index.ts
2073
- var index_default = LifeAstro;
2110
+ var src_default = LifeAstro;
2074
2111
  // Annotate the CommonJS export names for ESM import in node:
2075
2112
  0 && (module.exports = {
2076
2113
  AuthenticationError,