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.d.cts CHANGED
@@ -191,6 +191,34 @@ type QueryValue = string | number | boolean | null | undefined;
191
191
  type QueryParams = Record<string, QueryValue>;
192
192
 
193
193
  type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
194
+ /**
195
+ * What the API reported about one call besides its body: the request id, your credit balance
196
+ * after the call, your plan's per-minute rate limit and whether the answer came from the API's
197
+ * cache. Passed to `onResponse` (client option or per call) for every final HTTP response,
198
+ * success or error; not for network failures or for attempts that were retried.
199
+ */
200
+ interface ResponseInfo {
201
+ /** HTTP status of the response. */
202
+ status: number;
203
+ /** Request path, e.g. `/v1/panchang/basic`. */
204
+ path: string;
205
+ /** `X-Request-ID`. Quote it to support. */
206
+ requestId?: string;
207
+ /** `X-Credits-Remaining`: the balance after this call (unchanged after a failed call). Absent on free-tier accounts, whose allowance is not a balance, and on calls that cost nothing. */
208
+ creditsRemaining?: number;
209
+ /** `X-RateLimit-*`: requests per minute for your plan, how many are left, and when the window resets. */
210
+ rateLimit?: {
211
+ limit: number;
212
+ remaining: number;
213
+ resetAt: Date;
214
+ };
215
+ /** `X-Cache`, e.g. `HIT` when the API answered from its cache (the call is billed the same). */
216
+ cache?: string;
217
+ /** `Content-Language`: the language the answer is in. */
218
+ language?: string;
219
+ }
220
+ /** Receives the {@link ResponseInfo} of each call. Errors thrown inside it are ignored. */
221
+ type ResponseHook = (info: ResponseInfo) => void;
194
222
  interface RequestOptions {
195
223
  /** Per-call abort signal (in addition to the timeout). */
196
224
  signal?: AbortSignal;
@@ -198,6 +226,8 @@ interface RequestOptions {
198
226
  timeoutMs?: number;
199
227
  /** Return the response body exactly as received, without unwrapping a `{ data, meta }` envelope if one is present. */
200
228
  raw?: boolean;
229
+ /** Called with this call's {@link ResponseInfo} (after the client-level `onResponse`). */
230
+ onResponse?: ResponseHook;
201
231
  }
202
232
 
203
233
  declare class Ashtakavarga {
@@ -2031,7 +2061,7 @@ declare class Resources {
2031
2061
  */
2032
2062
 
2033
2063
  /** SDK version, surfaced in the User-Agent header. */
2034
- declare const VERSION = "0.9.0";
2064
+ declare const VERSION = "0.12.0";
2035
2065
  interface LifeAstroOptions {
2036
2066
  /**
2037
2067
  * API key (`dv_live_...`). Falls back to the `LIFEASTRO_API_KEY` environment
@@ -2048,6 +2078,11 @@ interface LifeAstroOptions {
2048
2078
  fetch?: FetchLike;
2049
2079
  /** Extra headers sent on every request. */
2050
2080
  defaultHeaders?: Record<string, string>;
2081
+ /**
2082
+ * Called with the {@link ResponseInfo} of every call: request id, credits left, rate limit,
2083
+ * cache and language. Use it to log usage or warn when credits run low.
2084
+ */
2085
+ onResponse?: ResponseHook;
2051
2086
  }
2052
2087
  /** The minimal surface a resource namespace needs from the client. @internal */
2053
2088
  interface ClientCore {
@@ -2165,4 +2200,4 @@ declare class LifeAstroConnectionError extends LifeAstroError {
2165
2200
  * @packageDocumentation
2166
2201
  */
2167
2202
 
2168
- export { type ApiErrorBody, AuthenticationError, type Ayanamsa, BadRequestError, type BirthInput, type BoyGirlInput, type ClientCore, type DateRangeInput, type FetchLike, type HouseSystem, LifeAstro, LifeAstroConnectionError, LifeAstroError, type LifeAstroOptions, type Locale, type MomentInput, type MuhurtaInput, type NatalTransitInput, type NatalWindowBirth, type NatalWindowInput, NotFoundError, type NumerologyInput, type OptionalNatalWindowInput, PaymentRequiredError, PermissionError, type QueryParams, type QueryValue, type RashiInput, RateLimitError, type RequestOptions, type ResponseEnvelope, type ResponseMeta, ServerError, type TwoPersonInput, VERSION, type VedicBirthSettings, type VedicMomentSettings, type WesternMomentInput, type WesternNatalInput, LifeAstro as default };
2203
+ export { type ApiErrorBody, AuthenticationError, type Ayanamsa, BadRequestError, type BirthInput, type BoyGirlInput, type ClientCore, type DateRangeInput, type FetchLike, type HouseSystem, LifeAstro, LifeAstroConnectionError, LifeAstroError, type LifeAstroOptions, type Locale, type MomentInput, type MuhurtaInput, type NatalTransitInput, type NatalWindowBirth, type NatalWindowInput, NotFoundError, type NumerologyInput, type OptionalNatalWindowInput, PaymentRequiredError, PermissionError, type QueryParams, type QueryValue, type RashiInput, RateLimitError, type RequestOptions, type ResponseEnvelope, type ResponseHook, type ResponseInfo, type ResponseMeta, ServerError, type TwoPersonInput, VERSION, type VedicBirthSettings, type VedicMomentSettings, type WesternMomentInput, type WesternNatalInput, LifeAstro as default };
package/dist/index.d.ts CHANGED
@@ -191,6 +191,34 @@ type QueryValue = string | number | boolean | null | undefined;
191
191
  type QueryParams = Record<string, QueryValue>;
192
192
 
193
193
  type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
194
+ /**
195
+ * What the API reported about one call besides its body: the request id, your credit balance
196
+ * after the call, your plan's per-minute rate limit and whether the answer came from the API's
197
+ * cache. Passed to `onResponse` (client option or per call) for every final HTTP response,
198
+ * success or error; not for network failures or for attempts that were retried.
199
+ */
200
+ interface ResponseInfo {
201
+ /** HTTP status of the response. */
202
+ status: number;
203
+ /** Request path, e.g. `/v1/panchang/basic`. */
204
+ path: string;
205
+ /** `X-Request-ID`. Quote it to support. */
206
+ requestId?: string;
207
+ /** `X-Credits-Remaining`: the balance after this call (unchanged after a failed call). Absent on free-tier accounts, whose allowance is not a balance, and on calls that cost nothing. */
208
+ creditsRemaining?: number;
209
+ /** `X-RateLimit-*`: requests per minute for your plan, how many are left, and when the window resets. */
210
+ rateLimit?: {
211
+ limit: number;
212
+ remaining: number;
213
+ resetAt: Date;
214
+ };
215
+ /** `X-Cache`, e.g. `HIT` when the API answered from its cache (the call is billed the same). */
216
+ cache?: string;
217
+ /** `Content-Language`: the language the answer is in. */
218
+ language?: string;
219
+ }
220
+ /** Receives the {@link ResponseInfo} of each call. Errors thrown inside it are ignored. */
221
+ type ResponseHook = (info: ResponseInfo) => void;
194
222
  interface RequestOptions {
195
223
  /** Per-call abort signal (in addition to the timeout). */
196
224
  signal?: AbortSignal;
@@ -198,6 +226,8 @@ interface RequestOptions {
198
226
  timeoutMs?: number;
199
227
  /** Return the response body exactly as received, without unwrapping a `{ data, meta }` envelope if one is present. */
200
228
  raw?: boolean;
229
+ /** Called with this call's {@link ResponseInfo} (after the client-level `onResponse`). */
230
+ onResponse?: ResponseHook;
201
231
  }
202
232
 
203
233
  declare class Ashtakavarga {
@@ -2031,7 +2061,7 @@ declare class Resources {
2031
2061
  */
2032
2062
 
2033
2063
  /** SDK version, surfaced in the User-Agent header. */
2034
- declare const VERSION = "0.9.0";
2064
+ declare const VERSION = "0.12.0";
2035
2065
  interface LifeAstroOptions {
2036
2066
  /**
2037
2067
  * API key (`dv_live_...`). Falls back to the `LIFEASTRO_API_KEY` environment
@@ -2048,6 +2078,11 @@ interface LifeAstroOptions {
2048
2078
  fetch?: FetchLike;
2049
2079
  /** Extra headers sent on every request. */
2050
2080
  defaultHeaders?: Record<string, string>;
2081
+ /**
2082
+ * Called with the {@link ResponseInfo} of every call: request id, credits left, rate limit,
2083
+ * cache and language. Use it to log usage or warn when credits run low.
2084
+ */
2085
+ onResponse?: ResponseHook;
2051
2086
  }
2052
2087
  /** The minimal surface a resource namespace needs from the client. @internal */
2053
2088
  interface ClientCore {
@@ -2165,4 +2200,4 @@ declare class LifeAstroConnectionError extends LifeAstroError {
2165
2200
  * @packageDocumentation
2166
2201
  */
2167
2202
 
2168
- export { type ApiErrorBody, AuthenticationError, type Ayanamsa, BadRequestError, type BirthInput, type BoyGirlInput, type ClientCore, type DateRangeInput, type FetchLike, type HouseSystem, LifeAstro, LifeAstroConnectionError, LifeAstroError, type LifeAstroOptions, type Locale, type MomentInput, type MuhurtaInput, type NatalTransitInput, type NatalWindowBirth, type NatalWindowInput, NotFoundError, type NumerologyInput, type OptionalNatalWindowInput, PaymentRequiredError, PermissionError, type QueryParams, type QueryValue, type RashiInput, RateLimitError, type RequestOptions, type ResponseEnvelope, type ResponseMeta, ServerError, type TwoPersonInput, VERSION, type VedicBirthSettings, type VedicMomentSettings, type WesternMomentInput, type WesternNatalInput, LifeAstro as default };
2203
+ export { type ApiErrorBody, AuthenticationError, type Ayanamsa, BadRequestError, type BirthInput, type BoyGirlInput, type ClientCore, type DateRangeInput, type FetchLike, type HouseSystem, LifeAstro, LifeAstroConnectionError, LifeAstroError, type LifeAstroOptions, type Locale, type MomentInput, type MuhurtaInput, type NatalTransitInput, type NatalWindowBirth, type NatalWindowInput, NotFoundError, type NumerologyInput, type OptionalNatalWindowInput, PaymentRequiredError, PermissionError, type QueryParams, type QueryValue, type RashiInput, RateLimitError, type RequestOptions, type ResponseEnvelope, type ResponseHook, type ResponseInfo, type ResponseMeta, ServerError, type TwoPersonInput, VERSION, type VedicBirthSettings, type VedicMomentSettings, type WesternMomentInput, type WesternNatalInput, LifeAstro as default };
package/dist/index.js CHANGED
@@ -65,6 +65,40 @@ function httpStatusCode(status) {
65
65
  }
66
66
 
67
67
  // src/http.ts
68
+ function responseInfo(res, path) {
69
+ const h = res.headers;
70
+ const num = (name) => {
71
+ const v = h.get(name);
72
+ if (v === null || v.trim() === "") return void 0;
73
+ const n = Number(v);
74
+ return Number.isFinite(n) ? n : void 0;
75
+ };
76
+ const info = { status: res.status, path };
77
+ const rid = h.get("x-request-id");
78
+ if (rid) info.requestId = rid;
79
+ const credits = num("x-credits-remaining");
80
+ if (credits !== void 0) info.creditsRemaining = credits;
81
+ const limit = num("x-ratelimit-limit");
82
+ const remaining = num("x-ratelimit-remaining");
83
+ const reset = num("x-ratelimit-reset");
84
+ if (limit !== void 0 && remaining !== void 0 && reset !== void 0) {
85
+ info.rateLimit = { limit, remaining, resetAt: new Date(reset * 1e3) };
86
+ }
87
+ const cache = h.get("x-cache");
88
+ if (cache) info.cache = cache;
89
+ const lang = h.get("content-language");
90
+ if (lang) info.language = lang;
91
+ return info;
92
+ }
93
+ function emit(hooks, info) {
94
+ for (const hook of hooks) {
95
+ if (!hook) continue;
96
+ try {
97
+ hook(info);
98
+ } catch {
99
+ }
100
+ }
101
+ }
68
102
  var RETRYABLE_STATUS = /* @__PURE__ */ new Set([408, 425, 429, 500, 502, 503, 504]);
69
103
  function encodeQuery(params) {
70
104
  const usp = new URLSearchParams();
@@ -142,7 +176,7 @@ async function performRequest(config, path, query, opts = {}) {
142
176
  });
143
177
  text = await res.text();
144
178
  } catch (cause) {
145
- const aborted = cause?.name === "AbortError";
179
+ const aborted = controller.signal.aborted || cause?.name === "AbortError";
146
180
  if (aborted && opts.signal?.aborted) {
147
181
  throw new LifeAstroConnectionError("Request aborted by caller", { cause, code: "aborted" });
148
182
  }
@@ -166,6 +200,7 @@ async function performRequest(config, path, query, opts = {}) {
166
200
  body = text;
167
201
  }
168
202
  if (res.ok) {
203
+ emit([config.onResponse, opts.onResponse], responseInfo(res, path));
169
204
  if (opts.raw) return body;
170
205
  const envelope = body;
171
206
  if (envelope && typeof envelope === "object" && "data" in envelope) {
@@ -179,6 +214,7 @@ async function performRequest(config, path, query, opts = {}) {
179
214
  await sleep(backoffMs(attempt, retryAfter), opts.signal);
180
215
  continue;
181
216
  }
217
+ emit([config.onResponse, opts.onResponse], responseInfo(res, path));
182
218
  throw lastError;
183
219
  }
184
220
  throw lastError ?? new LifeAstroConnectionError("Request failed after retries");
@@ -1975,7 +2011,7 @@ var Resources = class {
1975
2011
  };
1976
2012
 
1977
2013
  // src/client.ts
1978
- var VERSION = "0.9.0";
2014
+ var VERSION = "0.12.0";
1979
2015
  var DEFAULT_BASE_URL = "https://api.lifeastroapi.com";
1980
2016
  function resolveConfig(options) {
1981
2017
  const opts = typeof options === "string" ? { apiKey: options } : options ?? {};
@@ -1999,7 +2035,8 @@ function resolveConfig(options) {
1999
2035
  maxRetries: Math.max(0, opts.maxRetries ?? 2),
2000
2036
  fetch: fetchImpl,
2001
2037
  userAgent: `lifeastroapi-node/${VERSION}`,
2002
- defaultHeaders: opts.defaultHeaders ?? {}
2038
+ defaultHeaders: opts.defaultHeaders ?? {},
2039
+ onResponse: opts.onResponse
2003
2040
  };
2004
2041
  }
2005
2042
  var LifeAstro = class extends Resources {
@@ -2033,7 +2070,7 @@ var LifeAstro = class extends Resources {
2033
2070
  };
2034
2071
 
2035
2072
  // src/index.ts
2036
- var index_default = LifeAstro;
2073
+ var src_default = LifeAstro;
2037
2074
  export {
2038
2075
  AuthenticationError,
2039
2076
  BadRequestError,
@@ -2046,6 +2083,6 @@ export {
2046
2083
  RateLimitError,
2047
2084
  ServerError,
2048
2085
  VERSION,
2049
- index_default as default
2086
+ src_default as default
2050
2087
  };
2051
2088
  //# sourceMappingURL=index.js.map