@dyrected/sdk 2.5.60 → 2.5.61

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
@@ -22,7 +22,9 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  DyrectedClient: () => DyrectedClient,
24
24
  DyrectedError: () => DyrectedError,
25
- createClient: () => createClient
25
+ PREVIEW_TOKEN_PARAM: () => PREVIEW_TOKEN_PARAM,
26
+ createClient: () => createClient,
27
+ getPreviewToken: () => getPreviewToken
26
28
  });
27
29
  module.exports = __toCommonJS(index_exports);
28
30
 
@@ -163,6 +165,13 @@ var DyrectedClient = class {
163
165
  getBaseUrl() {
164
166
  return this.baseUrl;
165
167
  }
168
+ /**
169
+ * Inject the client's configured `defaultDepth` when a read did not specify
170
+ * its own `depth`. A per-call `depth` (including `0`) always wins.
171
+ */
172
+ applyDefaultDepth(args) {
173
+ return args.depth === void 0 ? { ...args, depth: this.defaultDepth } : args;
174
+ }
166
175
  async getSchemas() {
167
176
  return this.request("/api/schemas");
168
177
  }
@@ -197,7 +206,20 @@ var DyrectedClient = class {
197
206
  * Used in "token" preview mode.
198
207
  */
199
208
  async getPreviewData(token) {
200
- return this.request(`/api/preview-data?token=${token}`);
209
+ return this.request(`/api/preview-data?token=${encodeURIComponent(token)}`);
210
+ }
211
+ /**
212
+ * Mint a short-lived preview token that carries the current (unsaved) draft
213
+ * data. Used by the Admin in `previewMode: "token"` to hand draft content to
214
+ * a server-rendered frontend that cannot receive it over `postMessage`.
215
+ *
216
+ * Requires an authenticated request (the Admin is logged in).
217
+ */
218
+ async createPreviewToken(input) {
219
+ return this.request(`/api/preview-token`, {
220
+ method: "POST",
221
+ body: JSON.stringify(input)
222
+ });
201
223
  }
202
224
  async find(collection, args = {}) {
203
225
  const { initialData, ...queryArgs } = args;
@@ -205,7 +227,7 @@ var DyrectedClient = class {
205
227
  if (queryArgs.where && typeof queryArgs.where === "object") {
206
228
  normalizedArgs.where = JSON.stringify(queryArgs.where);
207
229
  }
208
- const query = stringifyQuery(normalizedArgs, { addQueryPrefix: true });
230
+ const query = stringifyQuery(this.applyDefaultDepth(normalizedArgs), { addQueryPrefix: true });
209
231
  const res = await this.request(`/api/collections/${collection}${query}`);
210
232
  if (res.docs.length === 0 && initialData && initialData.length > 0) {
211
233
  this.request(`/api/collections/${collection}/seed`, {
@@ -239,7 +261,7 @@ var DyrectedClient = class {
239
261
  if (args.sort) qb.sort(args.sort);
240
262
  if (args.limit) qb.limit(args.limit);
241
263
  if (args.page) qb.page(args.page);
242
- if (args.depth) qb.depth(args.depth);
264
+ if (args.depth !== void 0) qb.depth(args.depth);
243
265
  if (args.initialData) qb.seed(args.initialData);
244
266
  }
245
267
  return qb;
@@ -362,7 +384,7 @@ var DyrectedClient = class {
362
384
  }
363
385
  async findOne(collection, id, args = {}) {
364
386
  const { initialData, ...queryArgs } = args;
365
- const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
387
+ const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
366
388
  try {
367
389
  return await this.request(`/api/collections/${collection}/${id}${query}`);
368
390
  } catch (err) {
@@ -454,7 +476,7 @@ var DyrectedClient = class {
454
476
  }
455
477
  async getGlobal(slug, args = {}) {
456
478
  const { initialData, ...queryArgs } = args;
457
- const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
479
+ const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
458
480
  try {
459
481
  const res = await this.request(`/api/globals/${slug}${query}`);
460
482
  if ((!res || isFunctionallyEmpty(res)) && !isFunctionallyEmpty(initialData)) {
@@ -484,7 +506,7 @@ var DyrectedClient = class {
484
506
  });
485
507
  }
486
508
  async listMedia(args = {}, collection = "media") {
487
- const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
509
+ const query = stringifyQuery(this.applyDefaultDepth(normalizeQueryArgs(args)), { addQueryPrefix: true });
488
510
  return this.request(`/api/collections/${collection}${query}`);
489
511
  }
490
512
  /** @deprecated Use client.collection('media').upload(file, data) instead */
@@ -596,6 +618,20 @@ var DyrectedClient = class {
596
618
  function createClient(config) {
597
619
  return new DyrectedClient(config);
598
620
  }
621
+ var PREVIEW_TOKEN_PARAM = "dyPreview";
622
+ function getPreviewToken(search) {
623
+ if (!search) return null;
624
+ let value;
625
+ if (typeof search === "string") {
626
+ value = new URLSearchParams(search.startsWith("?") ? search.slice(1) : search).get(PREVIEW_TOKEN_PARAM);
627
+ } else if (search instanceof URLSearchParams) {
628
+ value = search.get(PREVIEW_TOKEN_PARAM);
629
+ } else {
630
+ value = search[PREVIEW_TOKEN_PARAM];
631
+ }
632
+ if (Array.isArray(value)) value = value[0];
633
+ return typeof value === "string" && value.length > 0 ? value : null;
634
+ }
599
635
  function isFunctionallyEmpty(obj) {
600
636
  if (obj === null || obj === void 0 || obj === "") return true;
601
637
  if (Array.isArray(obj)) {
@@ -639,5 +675,7 @@ function normalizeQueryArgs(args) {
639
675
  0 && (module.exports = {
640
676
  DyrectedClient,
641
677
  DyrectedError,
642
- createClient
678
+ PREVIEW_TOKEN_PARAM,
679
+ createClient,
680
+ getPreviewToken
643
681
  });
package/dist/index.d.cts CHANGED
@@ -129,7 +129,11 @@ interface DyrectedClientConfig {
129
129
  siteId?: string;
130
130
  headers?: Record<string, string>;
131
131
  fetch?: typeof fetch;
132
- /** Default depth for relationship population. Applied to every request unless overridden per-call. */
132
+ /**
133
+ * Default relationship population depth applied to document reads
134
+ * (`find`, `findOne`, `global().get()`, and media listing) when a call
135
+ * does not pass its own `depth`. Defaults to `1`.
136
+ */
133
137
  defaultDepth?: number;
134
138
  }
135
139
  interface BaseSchema {
@@ -171,6 +175,11 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
171
175
  */
172
176
  getAuthHeaders(): Record<string, string>;
173
177
  getBaseUrl(): string;
178
+ /**
179
+ * Inject the client's configured `defaultDepth` when a read did not specify
180
+ * its own `depth`. A per-call `depth` (including `0`) always wins.
181
+ */
182
+ private applyDefaultDepth;
174
183
  getSchemas(): Promise<SchemaResponse>;
175
184
  getAdminAuthConfig(): Promise<PublicAdminAuthConfig>;
176
185
  exchangeAdminAuth(providerId: string, body: Record<string, unknown>): Promise<{
@@ -200,6 +209,21 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
200
209
  * Used in "token" preview mode.
201
210
  */
202
211
  getPreviewData<T = unknown>(token: string): Promise<T>;
212
+ /**
213
+ * Mint a short-lived preview token that carries the current (unsaved) draft
214
+ * data. Used by the Admin in `previewMode: "token"` to hand draft content to
215
+ * a server-rendered frontend that cannot receive it over `postMessage`.
216
+ *
217
+ * Requires an authenticated request (the Admin is logged in).
218
+ */
219
+ createPreviewToken(input: {
220
+ collectionSlug: string;
221
+ documentId?: string;
222
+ data: unknown;
223
+ }): Promise<{
224
+ token: string;
225
+ expiresAt: string;
226
+ }>;
203
227
  find<K extends keyof TSchema["collections"]>(collection: K & string, args?: QueryArgs<TSchema["collections"][K]>): Promise<PaginatedResult<TSchema["collections"][K]>>;
204
228
  /**
205
229
  * Returns a fluent query builder for a collection.
@@ -413,5 +437,23 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
413
437
  private request;
414
438
  }
415
439
  declare function createClient<TSchema extends BaseSchema = BaseSchema>(config: DyrectedClientConfig): DyrectedClient<TSchema>;
440
+ /**
441
+ * The query-string parameter the Admin appends to a preview URL in
442
+ * `previewMode: "token"`. Read it on your frontend to decide whether to fetch
443
+ * draft data instead of published content.
444
+ */
445
+ declare const PREVIEW_TOKEN_PARAM = "dyPreview";
446
+ /**
447
+ * Extract the preview token from a request's query string. Accepts a raw query
448
+ * string, a `URLSearchParams`, or a plain params object (e.g. Nuxt's
449
+ * `route.query` or Next's `searchParams`). Returns `null` when absent.
450
+ *
451
+ * @example
452
+ * const token = getPreviewToken(route.query);
453
+ * const doc = token
454
+ * ? (await client.getPreviewData(token)).data
455
+ * : (await client.find("pages", { where })).docs[0];
456
+ */
457
+ declare function getPreviewToken(search: string | URLSearchParams | Record<string, unknown> | undefined | null): string | null;
416
458
 
417
- export { type AuditEntry, type BaseSchema, DyrectedClient, type DyrectedClientConfig, DyrectedError, type InferSchema, type TransitionOptions, type UploadOptions, type WorkflowDocument, type WorkflowHistoryEntry, createClient };
459
+ export { type AuditEntry, type BaseSchema, DyrectedClient, type DyrectedClientConfig, DyrectedError, type InferSchema, PREVIEW_TOKEN_PARAM, type TransitionOptions, type UploadOptions, type WorkflowDocument, type WorkflowHistoryEntry, createClient, getPreviewToken };
package/dist/index.d.ts CHANGED
@@ -129,7 +129,11 @@ interface DyrectedClientConfig {
129
129
  siteId?: string;
130
130
  headers?: Record<string, string>;
131
131
  fetch?: typeof fetch;
132
- /** Default depth for relationship population. Applied to every request unless overridden per-call. */
132
+ /**
133
+ * Default relationship population depth applied to document reads
134
+ * (`find`, `findOne`, `global().get()`, and media listing) when a call
135
+ * does not pass its own `depth`. Defaults to `1`.
136
+ */
133
137
  defaultDepth?: number;
134
138
  }
135
139
  interface BaseSchema {
@@ -171,6 +175,11 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
171
175
  */
172
176
  getAuthHeaders(): Record<string, string>;
173
177
  getBaseUrl(): string;
178
+ /**
179
+ * Inject the client's configured `defaultDepth` when a read did not specify
180
+ * its own `depth`. A per-call `depth` (including `0`) always wins.
181
+ */
182
+ private applyDefaultDepth;
174
183
  getSchemas(): Promise<SchemaResponse>;
175
184
  getAdminAuthConfig(): Promise<PublicAdminAuthConfig>;
176
185
  exchangeAdminAuth(providerId: string, body: Record<string, unknown>): Promise<{
@@ -200,6 +209,21 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
200
209
  * Used in "token" preview mode.
201
210
  */
202
211
  getPreviewData<T = unknown>(token: string): Promise<T>;
212
+ /**
213
+ * Mint a short-lived preview token that carries the current (unsaved) draft
214
+ * data. Used by the Admin in `previewMode: "token"` to hand draft content to
215
+ * a server-rendered frontend that cannot receive it over `postMessage`.
216
+ *
217
+ * Requires an authenticated request (the Admin is logged in).
218
+ */
219
+ createPreviewToken(input: {
220
+ collectionSlug: string;
221
+ documentId?: string;
222
+ data: unknown;
223
+ }): Promise<{
224
+ token: string;
225
+ expiresAt: string;
226
+ }>;
203
227
  find<K extends keyof TSchema["collections"]>(collection: K & string, args?: QueryArgs<TSchema["collections"][K]>): Promise<PaginatedResult<TSchema["collections"][K]>>;
204
228
  /**
205
229
  * Returns a fluent query builder for a collection.
@@ -413,5 +437,23 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
413
437
  private request;
414
438
  }
415
439
  declare function createClient<TSchema extends BaseSchema = BaseSchema>(config: DyrectedClientConfig): DyrectedClient<TSchema>;
440
+ /**
441
+ * The query-string parameter the Admin appends to a preview URL in
442
+ * `previewMode: "token"`. Read it on your frontend to decide whether to fetch
443
+ * draft data instead of published content.
444
+ */
445
+ declare const PREVIEW_TOKEN_PARAM = "dyPreview";
446
+ /**
447
+ * Extract the preview token from a request's query string. Accepts a raw query
448
+ * string, a `URLSearchParams`, or a plain params object (e.g. Nuxt's
449
+ * `route.query` or Next's `searchParams`). Returns `null` when absent.
450
+ *
451
+ * @example
452
+ * const token = getPreviewToken(route.query);
453
+ * const doc = token
454
+ * ? (await client.getPreviewData(token)).data
455
+ * : (await client.find("pages", { where })).docs[0];
456
+ */
457
+ declare function getPreviewToken(search: string | URLSearchParams | Record<string, unknown> | undefined | null): string | null;
416
458
 
417
- export { type AuditEntry, type BaseSchema, DyrectedClient, type DyrectedClientConfig, DyrectedError, type InferSchema, type TransitionOptions, type UploadOptions, type WorkflowDocument, type WorkflowHistoryEntry, createClient };
459
+ export { type AuditEntry, type BaseSchema, DyrectedClient, type DyrectedClientConfig, DyrectedError, type InferSchema, PREVIEW_TOKEN_PARAM, type TransitionOptions, type UploadOptions, type WorkflowDocument, type WorkflowHistoryEntry, createClient, getPreviewToken };
package/dist/index.js CHANGED
@@ -135,6 +135,13 @@ var DyrectedClient = class {
135
135
  getBaseUrl() {
136
136
  return this.baseUrl;
137
137
  }
138
+ /**
139
+ * Inject the client's configured `defaultDepth` when a read did not specify
140
+ * its own `depth`. A per-call `depth` (including `0`) always wins.
141
+ */
142
+ applyDefaultDepth(args) {
143
+ return args.depth === void 0 ? { ...args, depth: this.defaultDepth } : args;
144
+ }
138
145
  async getSchemas() {
139
146
  return this.request("/api/schemas");
140
147
  }
@@ -169,7 +176,20 @@ var DyrectedClient = class {
169
176
  * Used in "token" preview mode.
170
177
  */
171
178
  async getPreviewData(token) {
172
- return this.request(`/api/preview-data?token=${token}`);
179
+ return this.request(`/api/preview-data?token=${encodeURIComponent(token)}`);
180
+ }
181
+ /**
182
+ * Mint a short-lived preview token that carries the current (unsaved) draft
183
+ * data. Used by the Admin in `previewMode: "token"` to hand draft content to
184
+ * a server-rendered frontend that cannot receive it over `postMessage`.
185
+ *
186
+ * Requires an authenticated request (the Admin is logged in).
187
+ */
188
+ async createPreviewToken(input) {
189
+ return this.request(`/api/preview-token`, {
190
+ method: "POST",
191
+ body: JSON.stringify(input)
192
+ });
173
193
  }
174
194
  async find(collection, args = {}) {
175
195
  const { initialData, ...queryArgs } = args;
@@ -177,7 +197,7 @@ var DyrectedClient = class {
177
197
  if (queryArgs.where && typeof queryArgs.where === "object") {
178
198
  normalizedArgs.where = JSON.stringify(queryArgs.where);
179
199
  }
180
- const query = stringifyQuery(normalizedArgs, { addQueryPrefix: true });
200
+ const query = stringifyQuery(this.applyDefaultDepth(normalizedArgs), { addQueryPrefix: true });
181
201
  const res = await this.request(`/api/collections/${collection}${query}`);
182
202
  if (res.docs.length === 0 && initialData && initialData.length > 0) {
183
203
  this.request(`/api/collections/${collection}/seed`, {
@@ -211,7 +231,7 @@ var DyrectedClient = class {
211
231
  if (args.sort) qb.sort(args.sort);
212
232
  if (args.limit) qb.limit(args.limit);
213
233
  if (args.page) qb.page(args.page);
214
- if (args.depth) qb.depth(args.depth);
234
+ if (args.depth !== void 0) qb.depth(args.depth);
215
235
  if (args.initialData) qb.seed(args.initialData);
216
236
  }
217
237
  return qb;
@@ -334,7 +354,7 @@ var DyrectedClient = class {
334
354
  }
335
355
  async findOne(collection, id, args = {}) {
336
356
  const { initialData, ...queryArgs } = args;
337
- const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
357
+ const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
338
358
  try {
339
359
  return await this.request(`/api/collections/${collection}/${id}${query}`);
340
360
  } catch (err) {
@@ -426,7 +446,7 @@ var DyrectedClient = class {
426
446
  }
427
447
  async getGlobal(slug, args = {}) {
428
448
  const { initialData, ...queryArgs } = args;
429
- const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
449
+ const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
430
450
  try {
431
451
  const res = await this.request(`/api/globals/${slug}${query}`);
432
452
  if ((!res || isFunctionallyEmpty(res)) && !isFunctionallyEmpty(initialData)) {
@@ -456,7 +476,7 @@ var DyrectedClient = class {
456
476
  });
457
477
  }
458
478
  async listMedia(args = {}, collection = "media") {
459
- const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
479
+ const query = stringifyQuery(this.applyDefaultDepth(normalizeQueryArgs(args)), { addQueryPrefix: true });
460
480
  return this.request(`/api/collections/${collection}${query}`);
461
481
  }
462
482
  /** @deprecated Use client.collection('media').upload(file, data) instead */
@@ -568,6 +588,20 @@ var DyrectedClient = class {
568
588
  function createClient(config) {
569
589
  return new DyrectedClient(config);
570
590
  }
591
+ var PREVIEW_TOKEN_PARAM = "dyPreview";
592
+ function getPreviewToken(search) {
593
+ if (!search) return null;
594
+ let value;
595
+ if (typeof search === "string") {
596
+ value = new URLSearchParams(search.startsWith("?") ? search.slice(1) : search).get(PREVIEW_TOKEN_PARAM);
597
+ } else if (search instanceof URLSearchParams) {
598
+ value = search.get(PREVIEW_TOKEN_PARAM);
599
+ } else {
600
+ value = search[PREVIEW_TOKEN_PARAM];
601
+ }
602
+ if (Array.isArray(value)) value = value[0];
603
+ return typeof value === "string" && value.length > 0 ? value : null;
604
+ }
571
605
  function isFunctionallyEmpty(obj) {
572
606
  if (obj === null || obj === void 0 || obj === "") return true;
573
607
  if (Array.isArray(obj)) {
@@ -610,5 +644,7 @@ function normalizeQueryArgs(args) {
610
644
  export {
611
645
  DyrectedClient,
612
646
  DyrectedError,
613
- createClient
647
+ PREVIEW_TOKEN_PARAM,
648
+ createClient,
649
+ getPreviewToken
614
650
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dyrected/sdk",
3
- "version": "2.5.60",
3
+ "version": "2.5.61",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -30,7 +30,7 @@
30
30
  "tsup": "^8.5.1",
31
31
  "typescript": "^5.4.5",
32
32
  "vitest": "^1.0.0",
33
- "@dyrected/core": "2.5.60"
33
+ "@dyrected/core": "2.5.61"
34
34
  },
35
35
  "license": "BSL-1.1",
36
36
  "author": "Busola Okeowo <busolaokemoney@gmail.com>",