@ambasada/sdk 0.8.5 → 0.8.7

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/client.d.ts CHANGED
@@ -15,7 +15,10 @@ export interface AmbasadaOptions {
15
15
  fetch?: typeof fetch;
16
16
  /** Per-request timeout in ms (default 45 s). */
17
17
  timeoutMs?: number;
18
- /** Extra env source, e.g. a Worker's `env`. Default: `process.env` where it exists. */
18
+ /**
19
+ * Extra env source, e.g. a Worker's `env`. Default: `process.env` where it exists. A Worker hosted on ambasada.dev has
20
+ * an `AMBASADA_API` service binding there: the client calls the API through it (no zone in between, so no 522).
21
+ */
19
22
  env?: Record<string, unknown>;
20
23
  /** Sign in with Ambasada credentials. Default: `AMBASADA_AUTH_CLIENT_ID`, `_CLIENT_SECRET`, `_ISSUER`, and `PUBLIC_URL`. */
21
24
  auth?: {
@@ -42,6 +45,8 @@ export interface RequestOptions {
42
45
  signal?: AbortSignal;
43
46
  /** Retry once on a network error (only safe with an idempotency key). */
44
47
  retry?: boolean;
48
+ /** This request's timeout in ms, over the client's `timeoutMs` (an LLM call may take the API's 120 s). */
49
+ timeoutMs?: number;
45
50
  }
46
51
  export interface RawResponse {
47
52
  status: number;
@@ -58,6 +63,11 @@ export declare class Ambasada {
58
63
  private projectPromise?;
59
64
  constructor(options?: AmbasadaOptions);
60
65
  protected env(name: string): string | undefined;
66
+ /**
67
+ * How this client fetches: `options.fetch`; else, with an `AMBASADA_API` service binding in `env` (hosted on
68
+ * ambasada.dev), the API's own URLs go through it and everything else (the hub, the issuer) through `fetch`.
69
+ */
70
+ protected get fetcher(): typeof fetch;
61
71
  get apiUrl(): string;
62
72
  protected get apiKey(): string;
63
73
  /**
package/dist/platform.js CHANGED
@@ -590,6 +590,7 @@ var AmbasadaAuth = class {
590
590
 
591
591
  // src/client.ts
592
592
  var DEFAULT_API_URL = "https://api.ambasada.dev";
593
+ var isBinding = (v) => typeof v === "object" && v !== null && typeof v.fetch === "function";
593
594
  function processEnv() {
594
595
  const p = globalThis.process;
595
596
  return p?.env ?? {};
@@ -605,6 +606,22 @@ var Ambasada = class {
605
606
  const v = this.options.env?.[name];
606
607
  return typeof v === "string" ? v : processEnv()[name];
607
608
  }
609
+ /**
610
+ * How this client fetches: `options.fetch`; else, with an `AMBASADA_API` service binding in `env` (hosted on
611
+ * ambasada.dev), the API's own URLs go through it and everything else (the hub, the issuer) through `fetch`.
612
+ */
613
+ get fetcher() {
614
+ if (this.options.fetch) return this.options.fetch;
615
+ const binding = this.options.env?.AMBASADA_API;
616
+ if (!isBinding(binding)) return (input, init) => fetch(input, init);
617
+ const api = new Set(
618
+ [this.apiUrl, this.env("AMBASADA_API_URL"), this.env("AMBASADA_VILLAGE_API_URL")].filter((u) => !!u && URL.canParse(u)).map((u) => new URL(u).origin)
619
+ );
620
+ return (input, init) => {
621
+ const request = new Request(input, init);
622
+ return api.has(new URL(request.url).origin) ? binding.fetch(request) : fetch(request);
623
+ };
624
+ }
608
625
  get apiUrl() {
609
626
  return (this.options.apiUrl ?? this.env("AMBASADA_API_URL") ?? this.env("VILLAGE_API_URL") ?? DEFAULT_API_URL).replace(/\/+$/, "");
610
627
  }
@@ -626,7 +643,7 @@ var Ambasada = class {
626
643
  // Silent sign-in: a shell token must be for this app, by id or origin.
627
644
  audiences: [this.appOrigin, this.options.app?.id ?? this.env("AMBASADA_APP_ID") ?? this.env("AMBASADA_COMPANY")].filter((a) => !!a),
628
645
  hubUrl: this.hubUrl,
629
- fetch: this.options.fetch
646
+ fetch: this.fetcher
630
647
  }));
631
648
  get appOrigin() {
632
649
  const url = this.options.app?.origin ?? this.env("AMBASADA_APP_ORIGIN") ?? this.options.auth?.publicUrl ?? this.env("PUBLIC_URL");
@@ -643,7 +660,7 @@ var Ambasada = class {
643
660
  appSecret: this.options.app?.secret ?? this.env("AMBASADA_APP_SECRET"),
644
661
  hubUrl: this.hubUrl,
645
662
  origin: this.appOrigin,
646
- fetch: this.options.fetch
663
+ fetch: this.fetcher
647
664
  }));
648
665
  // ── HTTP ──────────────────────────────────────────────────────────────────
649
666
  async send(method, path, opts = {}) {
@@ -657,9 +674,9 @@ var Ambasada = class {
657
674
  headers["content-type"] = "application/json";
658
675
  }
659
676
  for (const [k, v] of Object.entries(opts.headers ?? {})) if (v !== void 0) headers[k.toLowerCase()] = v;
660
- const doFetch = this.options.fetch ?? fetch;
677
+ const doFetch = this.fetcher;
661
678
  const attempt = () => {
662
- const timeout = AbortSignal.timeout(this.options.timeoutMs ?? 45e3);
679
+ const timeout = AbortSignal.timeout(opts.timeoutMs ?? this.options.timeoutMs ?? 45e3);
663
680
  const signal = opts.signal ? AbortSignal.any([opts.signal, timeout]) : timeout;
664
681
  return doFetch(url, { method, headers, body, signal });
665
682
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ambasada/sdk",
3
- "version": "0.8.5",
3
+ "version": "0.8.7",
4
4
  "description": "The Ambasada platform SDK: Sign in with Ambasada, mini-app notifications, the project store, files.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -27,7 +27,7 @@
27
27
  "node": ">=22"
28
28
  },
29
29
  "dependencies": {
30
- "@ambasada/village": "^0.8.5"
30
+ "@ambasada/village": "^0.8.7"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/node": "^22.20.5",