@oxyhq/core 3.15.0 → 3.16.1

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.
@@ -16,51 +16,30 @@ export function OxyServicesAssetsMixin(Base) {
16
16
  }
17
17
  }
18
18
  /**
19
- * Build a synchronous file URL from an Oxy asset id.
19
+ * Build a synchronous, `<img src>`-ready file URL from an Oxy asset id.
20
20
  *
21
- * This is the single chokepoint every Oxy app uses to turn a stored file id
22
- * (avatars, post media, etc.) into a `<img src>`-ready URL, so it resolves to
23
- * one of two forms depending on whether the caller needs a signed/private URL:
24
- *
25
- * - **Public asset (default)** no access token planted on the client AND no
26
- * `expiresIn` requested returns the clean CDN form
27
- * `${cloudURL}/<id>[?variant=...]` (e.g. `https://cloud.oxy.so/<id>?variant=thumb`).
28
- * CloudFront resolves the id against the public media origin. No token,
29
- * `fallback`, or origin query params are emitted — these URLs are cacheable
30
- * and shareable.
31
- * - **Signed / private asset** — an access token is present on the client OR
32
- * `expiresIn` was passed (the caller explicitly wants an expiring/authorized
33
- * URL) → keeps the authenticated origin form
34
- * `${baseURL}/assets/<id>/stream?...&token=...`. Private assets are NOT on
35
- * the public CDN, so they must go through the API origin that can authorize
36
- * the request.
37
- *
38
- * `cloudURL` (default `https://cloud.oxy.so`) is configured once on the
39
- * `OxyServices` constructor and read via `getCloudURL()`; the API origin is
40
- * `getBaseURL()` (e.g. `https://api.oxy.so`).
41
- *
42
- * For a CDN-signed URL fetched from the API, use {@link getFileDownloadUrlAsync}.
21
+ * This method must never embed the caller's general access token in the
22
+ * returned URL. The URL is commonly rendered into DOM attributes, browser
23
+ * network panels, caches, and logs. Public asset URLs use the clean CDN
24
+ * origin; callers that need authorized/private access should use
25
+ * {@link getFileDownloadUrlAsync}, which asks the API for a scoped download
26
+ * URL instead of exposing the in-memory bearer token in a query string.
43
27
  */
44
- getFileDownloadUrl(fileId, variant, expiresIn, options = {}) {
45
- const token = options.omitToken ? undefined : this.getClient().getAccessToken();
46
- // Public case: no auth token and no expiry requested → clean CDN URL.
47
- // CloudFront serves the public media origin under `${cloudURL}/<id>`.
48
- if (!token && !expiresIn) {
28
+ getFileDownloadUrl(fileId, variant, expiresIn) {
29
+ // Never embed the in-memory bearer token: this URL is rendered into DOM
30
+ // attributes, browser network panels, caches, and logs. Public assets get
31
+ // the clean CDN origin; private/authorized access goes through
32
+ // `getFileDownloadUrlAsync`.
33
+ if (!expiresIn) {
49
34
  const variantQs = variant ? `?variant=${encodeURIComponent(variant)}` : '';
50
35
  return `${this.getCloudURL()}/${encodeURIComponent(fileId)}${variantQs}`;
51
36
  }
52
- // Signed / private case: route through the authenticated API origin's
53
- // stream endpoint so the request can be authorized (private assets are not
54
- // exposed on the public CDN).
55
37
  const base = this.getBaseURL();
56
38
  const params = new URLSearchParams();
57
39
  if (variant)
58
40
  params.set('variant', variant);
59
- if (expiresIn)
60
- params.set('expiresIn', String(expiresIn));
41
+ params.set('expiresIn', String(expiresIn));
61
42
  params.set('fallback', 'placeholderVisible');
62
- if (token)
63
- params.set('token', token);
64
43
  const qs = params.toString();
65
44
  return `${base}/assets/${encodeURIComponent(fileId)}/stream${qs ? `?${qs}` : ''}`;
66
45
  }
@@ -546,7 +546,18 @@ export function OxyServicesAuthMixin(Base) {
546
546
  */
547
547
  async getCommonsApprovalInfo(authorizeCode) {
548
548
  try {
549
- return await this.makeRequest('GET', `/auth/session/approve-info/${encodeURIComponent(authorizeCode)}`, undefined, { cache: false });
549
+ const raw = await this.makeRequest('GET', `/auth/session/approve-info/${encodeURIComponent(authorizeCode)}`, undefined, { cache: false });
550
+ return {
551
+ application: raw.application,
552
+ scopes: raw.scopes,
553
+ boundOrigin: raw.boundOrigin,
554
+ // Fail-safe: only a literal boolean `true` counts as verified. A
555
+ // missing or non-boolean value (older server, malformed response)
556
+ // coerces to `false` so a stale server can never imply trust.
557
+ originVerified: raw.originVerified === true,
558
+ expiresAt: raw.expiresAt,
559
+ status: raw.status,
560
+ };
550
561
  }
551
562
  catch (error) {
552
563
  throw this.handleError(error);
@@ -0,0 +1,65 @@
1
+ import { buildUrl } from '../utils/apiUtils.js';
2
+ /**
3
+ * Maximum number of URLs sent per `POST /links/previews` request. Matches the
4
+ * server-side batch cap (`linkPreviewBatchRequestSchema`'s `.max(50)`); larger
5
+ * inputs are split into multiple chunked calls and the result maps merged,
6
+ * mirroring how `getUsersByIds` chunks at 100.
7
+ */
8
+ const LINK_PREVIEWS_CHUNK_SIZE = 50;
9
+ export function OxyServicesLinksMixin(Base) {
10
+ return class extends Base {
11
+ constructor(...args) {
12
+ super(...args);
13
+ }
14
+ /**
15
+ * Resolve a single link preview via `GET /links/preview?url=<encoded>&wait=0|1`.
16
+ *
17
+ * @param url - The URL to unfurl. Sent percent-encoded in the query string.
18
+ * @param opts.wait - When `true`, asks the server to resolve synchronously
19
+ * (`wait=1`) instead of returning a `'pending'` placeholder for a
20
+ * first-seen URL. Defaults to `false` (`wait=0`).
21
+ *
22
+ * Not cached at the SDK layer: a `'pending'` result can become `'resolved'`
23
+ * on a later read, so caching here would serve the stale placeholder.
24
+ */
25
+ async getLinkPreview(url, opts) {
26
+ try {
27
+ const path = buildUrl('/links/preview', { url, wait: opts?.wait ? 1 : 0 });
28
+ return await this.makeRequest('GET', path, undefined, { cache: false });
29
+ }
30
+ catch (error) {
31
+ throw this.handleError(error);
32
+ }
33
+ }
34
+ /**
35
+ * Resolve multiple link previews via `POST /links/previews` (body `{ urls }`).
36
+ *
37
+ * Inputs are de-duplicated and split into chunks of {@link LINK_PREVIEWS_CHUNK_SIZE}
38
+ * (the server-side cap). Chunks run concurrently and their `data` maps are
39
+ * merged into a single result keyed by the REQUESTED url (the exact string
40
+ * passed in `urls`) — matching the batch contract — so a caller can always
41
+ * look its own input back up.
42
+ *
43
+ * An empty / whitespace-only input resolves immediately with `{}` and
44
+ * performs no network call. A failure in any chunk surfaces (via
45
+ * `handleError`) rather than being swallowed.
46
+ */
47
+ async getLinkPreviews(urls) {
48
+ const uniqueUrls = Array.from(new Set(urls.filter((u) => typeof u === 'string' && u.trim().length > 0)));
49
+ if (uniqueUrls.length === 0) {
50
+ return {};
51
+ }
52
+ const chunks = [];
53
+ for (let i = 0; i < uniqueUrls.length; i += LINK_PREVIEWS_CHUNK_SIZE) {
54
+ chunks.push(uniqueUrls.slice(i, i + LINK_PREVIEWS_CHUNK_SIZE));
55
+ }
56
+ try {
57
+ const responses = await Promise.all(chunks.map((chunk) => this.makeRequest('POST', '/links/previews', { urls: chunk }, { cache: false })));
58
+ return responses.reduce((merged, response) => Object.assign(merged, response?.data ?? {}), {});
59
+ }
60
+ catch (error) {
61
+ throw this.handleError(error);
62
+ }
63
+ }
64
+ };
65
+ }
@@ -31,6 +31,7 @@ import { OxyServicesContactsMixin } from './OxyServices.contacts.js';
31
31
  import { OxyServicesAppDataMixin } from './OxyServices.appData.js';
32
32
  import { OxyServicesCivicMixin } from './OxyServices.civic.js';
33
33
  import { OxyServicesNodesMixin } from './OxyServices.nodes.js';
34
+ import { OxyServicesLinksMixin } from './OxyServices.links.js';
34
35
  /**
35
36
  * Mixin pipeline - applied in order from first to last.
36
37
  *
@@ -81,6 +82,9 @@ const MIXIN_PIPELINE = [
81
82
  // User nodes / decentralization (Fase 5): register/read/revoke/manage the
82
83
  // caller's personal data node + ingest hint.
83
84
  OxyServicesNodesMixin,
85
+ // Link previews / unfurls: SDK-owned link-metadata resolution via oxy-api,
86
+ // so apps stop scraping link metadata locally.
87
+ OxyServicesLinksMixin,
84
88
  // Utility (last, can use all above)
85
89
  OxyServicesUtilityMixin,
86
90
  ];