Skip to content

Commit 5e89a54

Browse files
Added assetFields for DAM2.0 support
1 parent 0b0d42c commit 5e89a54

13 files changed

Lines changed: 224 additions & 206 deletions

package-lock.json

Lines changed: 0 additions & 205 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/asset-query.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ export class AssetQuery extends BaseQuery {
126126

127127
return this;
128128
}
129+
130+
/**
131+
* @method assetFields
132+
* @memberof AssetQuery
133+
* @description Include specific asset fields in the response (CDA getAssets).
134+
* Use with asset_fields[]: user_defined_fields, embedded, ai_suggested, visual_markups.
135+
* @example
136+
* import contentstack from '@contentstack/delivery-sdk'
137+
*
138+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
139+
* const result = await stack.asset().assetFields("user_defined_fields", "embedded").find();
140+
*
141+
* @param {...string} fields - Asset field names to include (e.g. user_defined_fields, embedded, ai_suggested, visual_markups)
142+
* @returns {AssetQuery} - Returns the AssetQuery instance for chaining.
143+
*/
144+
assetFields(...fields: string[]): this {
145+
if (fields.length > 0) {
146+
this._queryParams['asset_fields[]'] = fields;
147+
}
148+
return this;
149+
}
150+
129151
/**
130152
* @method query
131153
* @memberof AssetQuery

src/lib/asset.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AxiosInstance, getData } from '@contentstack/core';
33
export class Asset {
44
private _client: AxiosInstance;
55
private _urlPath: string;
6-
_queryParams: { [key: string]: string | number } = {};
6+
_queryParams: { [key: string]: string | number | string[] } = {};
77

88
constructor(client: AxiosInstance, assetUid: string) {
99
this._client = client;
@@ -129,6 +129,27 @@ export class Asset {
129129
return this;
130130
}
131131

132+
/**
133+
* @method assetFields
134+
* @memberof Asset
135+
* @description Include specific asset fields in the response (CDA getAssets - single asset).
136+
* Use with asset_fields[]: user_defined_fields, embedded, ai_suggested, visual_markups.
137+
* @example
138+
* import contentstack from '@contentstack/delivery-sdk'
139+
*
140+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
141+
* const result = await stack.asset("assetUid").assetFields("user_defined_fields", "embedded").fetch();
142+
*
143+
* @param {...string} fields - Asset field names to include (e.g. user_defined_fields, embedded, ai_suggested, visual_markups)
144+
* @returns {Asset} - Returns the Asset instance for chaining.
145+
*/
146+
assetFields(...fields: string[]): this {
147+
if (fields.length > 0) {
148+
this._queryParams['asset_fields[]'] = fields;
149+
}
150+
return this;
151+
}
152+
132153
/**
133154
* @method fetch
134155
* @memberof Asset

src/lib/entries.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,27 @@ export class Entries extends BaseQuery {
277277
return this;
278278
}
279279

280+
/**
281+
* @method assetFields
282+
* @memberof Entries
283+
* @description Include specific asset fields in the response (CDA getEntry/entries).
284+
* Use with asset_fields[]: user_defined_fields, embedded, ai_suggested, visual_markups.
285+
* @example
286+
* import contentstack from '@contentstack/delivery-sdk'
287+
*
288+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
289+
* const result = await stack.contentType("contentTypeUid").entry().assetFields("user_defined_fields", "embedded").find();
290+
*
291+
* @param {...string} fields - Asset field names to include (e.g. user_defined_fields, embedded, ai_suggested, visual_markups)
292+
* @returns {Entries} - Returns the Entries instance for chaining.
293+
*/
294+
assetFields(...fields: string[]): this {
295+
if (fields.length > 0) {
296+
this._queryParams['asset_fields[]'] = fields;
297+
}
298+
return this;
299+
}
300+
280301
/**
281302
* Override find method to include content type UID directly for better caching
282303
*/

src/lib/entry.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,25 @@ export class Entry {
273273
}
274274
return this;
275275
}
276+
277+
/**
278+
* @method assetFields
279+
* @memberof Entry
280+
* @description Include specific asset fields in the response (CDA getEntry).
281+
* Use with asset_fields[]: user_defined_fields, embedded, ai_suggested, visual_markups.
282+
* @example
283+
* import contentstack from '@contentstack/delivery-sdk'
284+
*
285+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
286+
* const result = await stack.contentType("contentTypeUid").entry("entryUid").assetFields("user_defined_fields", "embedded").fetch();
287+
*
288+
* @param {...string} fields - Asset field names to include (e.g. user_defined_fields, embedded, ai_suggested, visual_markups)
289+
* @returns {Entry} - Returns the Entry instance for chaining.
290+
*/
291+
assetFields(...fields: string[]): this {
292+
if (fields.length > 0) {
293+
this._queryParams['asset_fields[]'] = fields;
294+
}
295+
return this;
296+
}
276297
}

test/api/asset-query.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ describe("AssetQuery API tests", () => {
130130
console.log('No assets found in stack - test data dependent');
131131
}
132132
});
133+
it("should query assets with asset_fields[] CDA param (user_defined_fields, embedded, ai_suggested, visual_markups)", async () => {
134+
const result = await makeAssetQuery()
135+
.assetFields("user_defined_fields", "embedded", "ai_suggested", "visual_markups")
136+
.limit(2)
137+
.find<TAsset>();
138+
if (result.assets) {
139+
expect(result.assets).toBeDefined();
140+
if (result.assets.length > 0) {
141+
expect(result.assets[0].uid).toBeDefined();
142+
expect(result.assets[0]._version).toBeDefined();
143+
expect(result.assets[0].content_type).toBeDefined();
144+
}
145+
}
146+
});
133147
});
134148
function makeAssetQuery(): AssetQuery {
135149
const asset = stack.asset();

test/api/asset.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ describe('Asset API tests', () => {
103103
expect(result.created_by).toBeDefined();
104104
expect(result.updated_by).toBeDefined();
105105
});
106+
107+
it('should fetch asset with asset_fields[] CDA param (user_defined_fields, embedded, ai_suggested, visual_markups)', async () => {
108+
const result = await makeAsset(assetUid)
109+
.assetFields('user_defined_fields', 'embedded', 'ai_suggested', 'visual_markups')
110+
.fetch<BaseAsset>();
111+
expect(result).toBeDefined();
112+
expect(result.uid).toBeDefined();
113+
expect(result._version).toBeDefined();
114+
expect(result.url).toBeDefined();
115+
expect(result.filename).toBeDefined();
116+
});
106117
});
107118
function makeAsset(uid = ''): Asset {
108119
const asset = stack.asset(uid);

0 commit comments

Comments
 (0)