@foir/sdk 0.4.0 → 0.5.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/client.d.ts CHANGED
@@ -80,4 +80,56 @@ interface FoirRequestOptions {
80
80
  }
81
81
  declare function foirRequest<TData, TVariables>(env: FoirEnv, document: TypedDocumentNode<TData, TVariables>, variables?: TVariables, options?: FoirRequestOptions): Promise<TData>;
82
82
 
83
- export { FOIR_CDN_BASE, type FoirEnv, FoirRequestError, type FoirRequestOptions, absolutiseFoirMediaUrl, foirRequest, isFoirPreview };
83
+ /**
84
+ * Session-bound content client — forwards the customer bearer on every read
85
+ * when one is available, so auth/segment-targeted variants and customer-scoped
86
+ * records resolve out of the box, with no per-query wiring to forget.
87
+ *
88
+ * Resolution keys off the request's principal (see the platform's two-gate
89
+ * model): the `x-api-key` alone resolves the *anonymous baseline*
90
+ * (`context.authStatus = ""`); adding the customer bearer resolves the request
91
+ * as that customer (`context.authStatus = "authenticated"`, `customer_id` set).
92
+ *
93
+ * Caching: an api-key-only read is identical for every anonymous visitor and is
94
+ * shared-cacheable; a bearer-bearing read resolves per-customer and is not.
95
+ * Because the bearer only exists once a customer signs in, anonymous traffic
96
+ * keeps hitting the shared-cacheable baseline — only signed-in reads become
97
+ * per-customer. Bind this client wherever content is personalized (for most
98
+ * apps, everywhere the customer may be signed in) and use plain `foirRequest`
99
+ * for reads that must stay anonymous regardless of session.
100
+ */
101
+
102
+ /**
103
+ * Supplies the current customer bearer. Sync or async so consumers can read a
104
+ * cookie session (Hydrogen/Remix), a KV store, an in-memory value, etc. Returns
105
+ * the token when signed in, or `null`/`undefined` when signed out.
106
+ */
107
+ type FoirTokenProvider = () => string | null | undefined | Promise<string | null | undefined>;
108
+ interface FoirContentClientConfig {
109
+ env: FoirEnv;
110
+ /**
111
+ * Resolves the current customer bearer before every request. When it yields a
112
+ * token the token is forwarded, so `authStatus`/segment targeting and
113
+ * customer-scoped records resolve as that customer; when it yields
114
+ * null/undefined the read hits the anonymous baseline. Omit for a client that
115
+ * is always anonymous (equivalent to calling `foirRequest` directly).
116
+ */
117
+ getToken?: FoirTokenProvider;
118
+ }
119
+ interface FoirContentClient {
120
+ /**
121
+ * Run a typed document, forwarding the bound customer bearer when available.
122
+ * An explicit `options.bearerToken` still wins, so a single call can override
123
+ * the bound token (or force anonymous by passing `undefined` — the bound
124
+ * token only fills in when `options.bearerToken` is not supplied).
125
+ */
126
+ request<TData, TVariables>(document: TypedDocumentNode<TData, TVariables>, variables?: TVariables, options?: FoirRequestOptions): Promise<TData>;
127
+ }
128
+ /**
129
+ * Create a {@link FoirContentClient} bound to an env + token source. Every
130
+ * `request` forwards the customer bearer when `getToken` yields one, upgrading
131
+ * that read from the anonymous baseline to the signed-in customer.
132
+ */
133
+ declare function createFoirContentClient(config: FoirContentClientConfig): FoirContentClient;
134
+
135
+ export { FOIR_CDN_BASE, type FoirContentClient, type FoirContentClientConfig, type FoirEnv, FoirRequestError, type FoirRequestOptions, type FoirTokenProvider, absolutiseFoirMediaUrl, createFoirContentClient, foirRequest, isFoirPreview };
package/dist/client.js CHANGED
@@ -72,10 +72,24 @@ async function foirRequest(env, document, variables, options) {
72
72
  }
73
73
  return json.data;
74
74
  }
75
+
76
+ // src/graphql/content-client.ts
77
+ function createFoirContentClient(config) {
78
+ return {
79
+ async request(document, variables, options) {
80
+ const bound = config.getToken ? await config.getToken() : null;
81
+ return foirRequest(config.env, document, variables, {
82
+ ...options,
83
+ bearerToken: options?.bearerToken ?? bound ?? void 0
84
+ });
85
+ }
86
+ };
87
+ }
75
88
  export {
76
89
  FOIR_CDN_BASE,
77
90
  FoirRequestError,
78
91
  absolutiseFoirMediaUrl,
92
+ createFoirContentClient,
79
93
  foirRequest,
80
94
  isFoirPreview
81
95
  };
package/dist/server.d.ts CHANGED
@@ -519,8 +519,8 @@ interface CallbackClient<TResult = unknown> {
519
519
  * payload structure on every call.
520
520
  *
521
521
  * `signingSecret` lives on the extension (set as `FOIR_SIGNING_SECRET` or
522
- * equivalent env var). The CLI publishes it during `foir push` so it's
523
- * available before any callback runs — see foir-cli writeConfigCredential.
522
+ * equivalent env var). `foir push` writes it there from the project's
523
+ * signing secret, so it's available before any callback runs.
524
524
  *
525
525
  * `fetch` is overridable for testability and to support runtimes (CF
526
526
  * Workers, Node) where `fetch` may not be the global.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foir/sdk",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Official Foir SDK — editor iframe client + server-side platform helpers",
5
5
  "type": "module",
6
6
  "publishConfig": {