@dyrected/sdk 2.5.59 → 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 +71 -9
- package/dist/index.d.cts +72 -2
- package/dist/index.d.ts +72 -2
- package/dist/index.js +68 -8
- package/package.json +2 -2
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
|
-
|
|
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;
|
|
@@ -340,7 +362,13 @@ var DyrectedClient = class {
|
|
|
340
362
|
* @param id - Document ID.
|
|
341
363
|
* @param args - Optional `limit` (default 50, max 100).
|
|
342
364
|
*/
|
|
343
|
-
workflowHistory: (id, args = {}) => this.workflowHistory(slug, id, args)
|
|
365
|
+
workflowHistory: (id, args = {}) => this.workflowHistory(slug, id, args),
|
|
366
|
+
/**
|
|
367
|
+
* Fetch audit entries for this collection.
|
|
368
|
+
*
|
|
369
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
370
|
+
*/
|
|
371
|
+
audit: (args = {}) => this.collectionAudit(slug, args)
|
|
344
372
|
};
|
|
345
373
|
}
|
|
346
374
|
/**
|
|
@@ -356,7 +384,7 @@ var DyrectedClient = class {
|
|
|
356
384
|
}
|
|
357
385
|
async findOne(collection, id, args = {}) {
|
|
358
386
|
const { initialData, ...queryArgs } = args;
|
|
359
|
-
const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
|
|
387
|
+
const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
|
|
360
388
|
try {
|
|
361
389
|
return await this.request(`/api/collections/${collection}/${id}${query}`);
|
|
362
390
|
} catch (err) {
|
|
@@ -422,6 +450,24 @@ var DyrectedClient = class {
|
|
|
422
450
|
const query = args.limit ? `?limit=${args.limit}` : "";
|
|
423
451
|
return this.request(`/api/collections/${collection}/${id}/workflow-history${query}`);
|
|
424
452
|
}
|
|
453
|
+
/**
|
|
454
|
+
* Fetch audit entries across every audited collection the current caller can read.
|
|
455
|
+
*
|
|
456
|
+
* Sends `GET /api/audit`.
|
|
457
|
+
*/
|
|
458
|
+
async audit(args = {}) {
|
|
459
|
+
const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
|
|
460
|
+
return this.request(`/api/audit${query}`);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Fetch audit entries for a single collection.
|
|
464
|
+
*
|
|
465
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
466
|
+
*/
|
|
467
|
+
async collectionAudit(collection, args = {}) {
|
|
468
|
+
const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
|
|
469
|
+
return this.request(`/api/collections/${collection}/__audit${query}`);
|
|
470
|
+
}
|
|
425
471
|
async deleteMany(collection, ids) {
|
|
426
472
|
return this.request(`/api/collections/${collection}/delete-many`, {
|
|
427
473
|
method: "DELETE",
|
|
@@ -430,7 +476,7 @@ var DyrectedClient = class {
|
|
|
430
476
|
}
|
|
431
477
|
async getGlobal(slug, args = {}) {
|
|
432
478
|
const { initialData, ...queryArgs } = args;
|
|
433
|
-
const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
|
|
479
|
+
const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
|
|
434
480
|
try {
|
|
435
481
|
const res = await this.request(`/api/globals/${slug}${query}`);
|
|
436
482
|
if ((!res || isFunctionallyEmpty(res)) && !isFunctionallyEmpty(initialData)) {
|
|
@@ -460,7 +506,7 @@ var DyrectedClient = class {
|
|
|
460
506
|
});
|
|
461
507
|
}
|
|
462
508
|
async listMedia(args = {}, collection = "media") {
|
|
463
|
-
const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
|
|
509
|
+
const query = stringifyQuery(this.applyDefaultDepth(normalizeQueryArgs(args)), { addQueryPrefix: true });
|
|
464
510
|
return this.request(`/api/collections/${collection}${query}`);
|
|
465
511
|
}
|
|
466
512
|
/** @deprecated Use client.collection('media').upload(file, data) instead */
|
|
@@ -572,6 +618,20 @@ var DyrectedClient = class {
|
|
|
572
618
|
function createClient(config) {
|
|
573
619
|
return new DyrectedClient(config);
|
|
574
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
|
+
}
|
|
575
635
|
function isFunctionallyEmpty(obj) {
|
|
576
636
|
if (obj === null || obj === void 0 || obj === "") return true;
|
|
577
637
|
if (Array.isArray(obj)) {
|
|
@@ -615,5 +675,7 @@ function normalizeQueryArgs(args) {
|
|
|
615
675
|
0 && (module.exports = {
|
|
616
676
|
DyrectedClient,
|
|
617
677
|
DyrectedError,
|
|
618
|
-
|
|
678
|
+
PREVIEW_TOKEN_PARAM,
|
|
679
|
+
createClient,
|
|
680
|
+
getPreviewToken
|
|
619
681
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -63,6 +63,16 @@ interface WorkflowHistoryEntry {
|
|
|
63
63
|
actorId: string | null;
|
|
64
64
|
createdAt: string;
|
|
65
65
|
}
|
|
66
|
+
/** A single audit entry returned by `client.audit()` or `client.collection(slug).audit()`. */
|
|
67
|
+
interface AuditEntry {
|
|
68
|
+
id: string;
|
|
69
|
+
collection: string;
|
|
70
|
+
documentId: string | null;
|
|
71
|
+
operation: string;
|
|
72
|
+
user: string | null;
|
|
73
|
+
timestamp: string;
|
|
74
|
+
changes?: string | Record<string, unknown> | null;
|
|
75
|
+
}
|
|
66
76
|
type ExtractDoc<T> = T extends CollectionConfig<infer TDoc> ? TDoc : T extends GlobalConfig<infer TDoc> ? TDoc : never;
|
|
67
77
|
/**
|
|
68
78
|
* Derives a typed `TSchema` from your exported collection and global config constants.
|
|
@@ -119,7 +129,11 @@ interface DyrectedClientConfig {
|
|
|
119
129
|
siteId?: string;
|
|
120
130
|
headers?: Record<string, string>;
|
|
121
131
|
fetch?: typeof fetch;
|
|
122
|
-
/**
|
|
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
|
+
*/
|
|
123
137
|
defaultDepth?: number;
|
|
124
138
|
}
|
|
125
139
|
interface BaseSchema {
|
|
@@ -161,6 +175,11 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
161
175
|
*/
|
|
162
176
|
getAuthHeaders(): Record<string, string>;
|
|
163
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;
|
|
164
183
|
getSchemas(): Promise<SchemaResponse>;
|
|
165
184
|
getAdminAuthConfig(): Promise<PublicAdminAuthConfig>;
|
|
166
185
|
exchangeAdminAuth(providerId: string, body: Record<string, unknown>): Promise<{
|
|
@@ -190,6 +209,21 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
190
209
|
* Used in "token" preview mode.
|
|
191
210
|
*/
|
|
192
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
|
+
}>;
|
|
193
227
|
find<K extends keyof TSchema["collections"]>(collection: K & string, args?: QueryArgs<TSchema["collections"][K]>): Promise<PaginatedResult<TSchema["collections"][K]>>;
|
|
194
228
|
/**
|
|
195
229
|
* Returns a fluent query builder for a collection.
|
|
@@ -309,6 +343,12 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
309
343
|
workflowHistory: (id: string, args?: {
|
|
310
344
|
limit?: number;
|
|
311
345
|
}) => Promise<PaginatedResult<WorkflowHistoryEntry>>;
|
|
346
|
+
/**
|
|
347
|
+
* Fetch audit entries for this collection.
|
|
348
|
+
*
|
|
349
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
350
|
+
*/
|
|
351
|
+
audit: (args?: QueryArgs<AuditEntry>) => Promise<PaginatedResult<AuditEntry>>;
|
|
312
352
|
};
|
|
313
353
|
/**
|
|
314
354
|
* Access a global by its slug with a fluent builder.
|
|
@@ -358,6 +398,18 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
358
398
|
workflowHistory(collection: string, id: string, args?: {
|
|
359
399
|
limit?: number;
|
|
360
400
|
}): Promise<PaginatedResult<WorkflowHistoryEntry>>;
|
|
401
|
+
/**
|
|
402
|
+
* Fetch audit entries across every audited collection the current caller can read.
|
|
403
|
+
*
|
|
404
|
+
* Sends `GET /api/audit`.
|
|
405
|
+
*/
|
|
406
|
+
audit(args?: QueryArgs<AuditEntry>): Promise<PaginatedResult<AuditEntry>>;
|
|
407
|
+
/**
|
|
408
|
+
* Fetch audit entries for a single collection.
|
|
409
|
+
*
|
|
410
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
411
|
+
*/
|
|
412
|
+
collectionAudit(collection: string, args?: QueryArgs<AuditEntry>): Promise<PaginatedResult<AuditEntry>>;
|
|
361
413
|
deleteMany(collection: string, ids: string[]): Promise<{
|
|
362
414
|
message: string;
|
|
363
415
|
}>;
|
|
@@ -385,5 +437,23 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
385
437
|
private request;
|
|
386
438
|
}
|
|
387
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;
|
|
388
458
|
|
|
389
|
-
export { 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
|
@@ -63,6 +63,16 @@ interface WorkflowHistoryEntry {
|
|
|
63
63
|
actorId: string | null;
|
|
64
64
|
createdAt: string;
|
|
65
65
|
}
|
|
66
|
+
/** A single audit entry returned by `client.audit()` or `client.collection(slug).audit()`. */
|
|
67
|
+
interface AuditEntry {
|
|
68
|
+
id: string;
|
|
69
|
+
collection: string;
|
|
70
|
+
documentId: string | null;
|
|
71
|
+
operation: string;
|
|
72
|
+
user: string | null;
|
|
73
|
+
timestamp: string;
|
|
74
|
+
changes?: string | Record<string, unknown> | null;
|
|
75
|
+
}
|
|
66
76
|
type ExtractDoc<T> = T extends CollectionConfig<infer TDoc> ? TDoc : T extends GlobalConfig<infer TDoc> ? TDoc : never;
|
|
67
77
|
/**
|
|
68
78
|
* Derives a typed `TSchema` from your exported collection and global config constants.
|
|
@@ -119,7 +129,11 @@ interface DyrectedClientConfig {
|
|
|
119
129
|
siteId?: string;
|
|
120
130
|
headers?: Record<string, string>;
|
|
121
131
|
fetch?: typeof fetch;
|
|
122
|
-
/**
|
|
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
|
+
*/
|
|
123
137
|
defaultDepth?: number;
|
|
124
138
|
}
|
|
125
139
|
interface BaseSchema {
|
|
@@ -161,6 +175,11 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
161
175
|
*/
|
|
162
176
|
getAuthHeaders(): Record<string, string>;
|
|
163
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;
|
|
164
183
|
getSchemas(): Promise<SchemaResponse>;
|
|
165
184
|
getAdminAuthConfig(): Promise<PublicAdminAuthConfig>;
|
|
166
185
|
exchangeAdminAuth(providerId: string, body: Record<string, unknown>): Promise<{
|
|
@@ -190,6 +209,21 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
190
209
|
* Used in "token" preview mode.
|
|
191
210
|
*/
|
|
192
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
|
+
}>;
|
|
193
227
|
find<K extends keyof TSchema["collections"]>(collection: K & string, args?: QueryArgs<TSchema["collections"][K]>): Promise<PaginatedResult<TSchema["collections"][K]>>;
|
|
194
228
|
/**
|
|
195
229
|
* Returns a fluent query builder for a collection.
|
|
@@ -309,6 +343,12 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
309
343
|
workflowHistory: (id: string, args?: {
|
|
310
344
|
limit?: number;
|
|
311
345
|
}) => Promise<PaginatedResult<WorkflowHistoryEntry>>;
|
|
346
|
+
/**
|
|
347
|
+
* Fetch audit entries for this collection.
|
|
348
|
+
*
|
|
349
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
350
|
+
*/
|
|
351
|
+
audit: (args?: QueryArgs<AuditEntry>) => Promise<PaginatedResult<AuditEntry>>;
|
|
312
352
|
};
|
|
313
353
|
/**
|
|
314
354
|
* Access a global by its slug with a fluent builder.
|
|
@@ -358,6 +398,18 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
358
398
|
workflowHistory(collection: string, id: string, args?: {
|
|
359
399
|
limit?: number;
|
|
360
400
|
}): Promise<PaginatedResult<WorkflowHistoryEntry>>;
|
|
401
|
+
/**
|
|
402
|
+
* Fetch audit entries across every audited collection the current caller can read.
|
|
403
|
+
*
|
|
404
|
+
* Sends `GET /api/audit`.
|
|
405
|
+
*/
|
|
406
|
+
audit(args?: QueryArgs<AuditEntry>): Promise<PaginatedResult<AuditEntry>>;
|
|
407
|
+
/**
|
|
408
|
+
* Fetch audit entries for a single collection.
|
|
409
|
+
*
|
|
410
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
411
|
+
*/
|
|
412
|
+
collectionAudit(collection: string, args?: QueryArgs<AuditEntry>): Promise<PaginatedResult<AuditEntry>>;
|
|
361
413
|
deleteMany(collection: string, ids: string[]): Promise<{
|
|
362
414
|
message: string;
|
|
363
415
|
}>;
|
|
@@ -385,5 +437,23 @@ declare class DyrectedClient<TSchema extends BaseSchema = BaseSchema> {
|
|
|
385
437
|
private request;
|
|
386
438
|
}
|
|
387
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;
|
|
388
458
|
|
|
389
|
-
export { 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;
|
|
@@ -312,7 +332,13 @@ var DyrectedClient = class {
|
|
|
312
332
|
* @param id - Document ID.
|
|
313
333
|
* @param args - Optional `limit` (default 50, max 100).
|
|
314
334
|
*/
|
|
315
|
-
workflowHistory: (id, args = {}) => this.workflowHistory(slug, id, args)
|
|
335
|
+
workflowHistory: (id, args = {}) => this.workflowHistory(slug, id, args),
|
|
336
|
+
/**
|
|
337
|
+
* Fetch audit entries for this collection.
|
|
338
|
+
*
|
|
339
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
340
|
+
*/
|
|
341
|
+
audit: (args = {}) => this.collectionAudit(slug, args)
|
|
316
342
|
};
|
|
317
343
|
}
|
|
318
344
|
/**
|
|
@@ -328,7 +354,7 @@ var DyrectedClient = class {
|
|
|
328
354
|
}
|
|
329
355
|
async findOne(collection, id, args = {}) {
|
|
330
356
|
const { initialData, ...queryArgs } = args;
|
|
331
|
-
const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
|
|
357
|
+
const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
|
|
332
358
|
try {
|
|
333
359
|
return await this.request(`/api/collections/${collection}/${id}${query}`);
|
|
334
360
|
} catch (err) {
|
|
@@ -394,6 +420,24 @@ var DyrectedClient = class {
|
|
|
394
420
|
const query = args.limit ? `?limit=${args.limit}` : "";
|
|
395
421
|
return this.request(`/api/collections/${collection}/${id}/workflow-history${query}`);
|
|
396
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Fetch audit entries across every audited collection the current caller can read.
|
|
425
|
+
*
|
|
426
|
+
* Sends `GET /api/audit`.
|
|
427
|
+
*/
|
|
428
|
+
async audit(args = {}) {
|
|
429
|
+
const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
|
|
430
|
+
return this.request(`/api/audit${query}`);
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Fetch audit entries for a single collection.
|
|
434
|
+
*
|
|
435
|
+
* Sends `GET /api/collections/:collection/__audit`.
|
|
436
|
+
*/
|
|
437
|
+
async collectionAudit(collection, args = {}) {
|
|
438
|
+
const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
|
|
439
|
+
return this.request(`/api/collections/${collection}/__audit${query}`);
|
|
440
|
+
}
|
|
397
441
|
async deleteMany(collection, ids) {
|
|
398
442
|
return this.request(`/api/collections/${collection}/delete-many`, {
|
|
399
443
|
method: "DELETE",
|
|
@@ -402,7 +446,7 @@ var DyrectedClient = class {
|
|
|
402
446
|
}
|
|
403
447
|
async getGlobal(slug, args = {}) {
|
|
404
448
|
const { initialData, ...queryArgs } = args;
|
|
405
|
-
const query = stringifyQuery(queryArgs, { addQueryPrefix: true });
|
|
449
|
+
const query = stringifyQuery(this.applyDefaultDepth(queryArgs), { addQueryPrefix: true });
|
|
406
450
|
try {
|
|
407
451
|
const res = await this.request(`/api/globals/${slug}${query}`);
|
|
408
452
|
if ((!res || isFunctionallyEmpty(res)) && !isFunctionallyEmpty(initialData)) {
|
|
@@ -432,7 +476,7 @@ var DyrectedClient = class {
|
|
|
432
476
|
});
|
|
433
477
|
}
|
|
434
478
|
async listMedia(args = {}, collection = "media") {
|
|
435
|
-
const query = stringifyQuery(normalizeQueryArgs(args), { addQueryPrefix: true });
|
|
479
|
+
const query = stringifyQuery(this.applyDefaultDepth(normalizeQueryArgs(args)), { addQueryPrefix: true });
|
|
436
480
|
return this.request(`/api/collections/${collection}${query}`);
|
|
437
481
|
}
|
|
438
482
|
/** @deprecated Use client.collection('media').upload(file, data) instead */
|
|
@@ -544,6 +588,20 @@ var DyrectedClient = class {
|
|
|
544
588
|
function createClient(config) {
|
|
545
589
|
return new DyrectedClient(config);
|
|
546
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
|
+
}
|
|
547
605
|
function isFunctionallyEmpty(obj) {
|
|
548
606
|
if (obj === null || obj === void 0 || obj === "") return true;
|
|
549
607
|
if (Array.isArray(obj)) {
|
|
@@ -586,5 +644,7 @@ function normalizeQueryArgs(args) {
|
|
|
586
644
|
export {
|
|
587
645
|
DyrectedClient,
|
|
588
646
|
DyrectedError,
|
|
589
|
-
|
|
647
|
+
PREVIEW_TOKEN_PARAM,
|
|
648
|
+
createClient,
|
|
649
|
+
getPreviewToken
|
|
590
650
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dyrected/sdk",
|
|
3
|
-
"version": "2.5.
|
|
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.
|
|
33
|
+
"@dyrected/core": "2.5.61"
|
|
34
34
|
},
|
|
35
35
|
"license": "BSL-1.1",
|
|
36
36
|
"author": "Busola Okeowo <busolaokemoney@gmail.com>",
|