Skip to content

fix: method chaining for entry queryable class methods #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ fileignoreconfig:
- filename: src/lib/query.ts
checksum: c4529069bc974d15c104303c5ae573c9341185a869c612ab07f0ee7f42e8b149
- filename: package-lock.json
checksum: 815fd2251550d87a0f113dd2e14266879be37af3a264041e871436a1cc84ae1c
checksum: f9c5af529a2c4c6576d67bd6c25dc6c3088ddedf2482757d382953f2d4521995
- filename: src/lib/entries.ts
checksum: 1c9a58570f26d3e53526e89b404581a523d3f035234bc099fda96d144dee40f6
- filename: src/lib/entry.ts
checksum: 8826fe3147a2c640b0780dae02345611ed24e562562e7df7b3785cb0fa6f1f14
- filename: .husky/pre-commit
checksum: 5baabd7d2c391648163f9371f0e5e9484f8fb90fa2284cfc378732ec3192c193
version: ""
1,664 changes: 854 additions & 810 deletions package-lock.json

Large diffs are not rendered by default.

97 changes: 75 additions & 22 deletions src/lib/entries.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AxiosInstance, getData } from '@contentstack/core';
import { EntryQueryable } from './entry-queryable';
import { Query } from './query';
import { BaseQuery } from './base-query';

export class Entries extends EntryQueryable {
export class Entries extends BaseQuery {
private _contentTypeUid: string;

constructor(client: AxiosInstance, contentTypeUid: string) {
Expand All @@ -14,35 +14,62 @@ export class Entries extends EntryQueryable {
}

/**
* @method includeFallback
* @method except
* @memberof Entries
* @description When an entry is not published in a specific language, content can be fetched from its fallback language
* @description Excludes specific field/fields of an entry
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().except("fieldUID").find()
*
* @param {string} fieldUid - field uid to exclude
* @returns {Entries} - returns Entries object for chaining method calls
*/
except(fieldUid: string|string[]): this {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`except[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["except[BASE][]"] = fieldUid;
}

return this;
}

/**
* @method includeBranch
* @memberof Entries
* @description Includes the branch in result
* @returns {Entries}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry().includeFallback().find();
* const result = await stack.contentType(contentType_uid).entry().includeBranch().find();
*/
includeFallback(): Entries {
this._queryParams.include_fallback = 'true';
includeBranch(): Entries {
this._queryParams.include_branch = 'true';

return this;
}

/**
* @method includeMetadata
* @method includeContentType
* @memberof Entries
* @description Include the metadata for getting metadata content for the entry.
* @description IInclude the details of the content type along with the entries details
* @returns {Entries}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry().includeMetadata().find();
* const result = await stack.contentType(contentType_uid).entry().includeContentType().fetch();
*/
includeMetadata(): Entries {
this._queryParams.include_metadata = 'true';
includeContentType(): Entries {
this._queryParams.include_content_type = 'true';

return this;
}
Expand All @@ -65,35 +92,35 @@ export class Entries extends EntryQueryable {
}

/**
* @method includeContentType
* @method includeFallback
* @memberof Entries
* @description IInclude the details of the content type along with the entries details
* @description When an entry is not published in a specific language, content can be fetched from its fallback language
* @returns {Entries}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry().includeContentType().fetch();
* const result = await stack.contentType(contentType_uid).entry().includeFallback().find();
*/
includeContentType(): Entries {
this._queryParams.include_content_type = 'true';
includeFallback(): Entries {
this._queryParams.include_fallback = 'true';

return this;
}

/**
* @method includeBranch
* @method includeMetadata
* @memberof Entries
* @description Includes the branch in result
* @description Include the metadata for getting metadata content for the entry.
* @returns {Entries}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry().includeBranch().find();
* const result = await stack.contentType(contentType_uid).entry().includeMetadata().find();
*/
includeBranch(): Entries {
this._queryParams.include_branch = 'true';
includeMetadata(): Entries {
this._queryParams.include_metadata = 'true';

return this;
}
Expand Down Expand Up @@ -177,6 +204,32 @@ export class Entries extends EntryQueryable {
return this;
}

/**
* @method only
* @memberof Entries
* @description Selects specific field/fields of an entry
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().only("fieldUID").find()
*
* @param {string} fieldUid - field uid to select
* @returns {Entries} - returns Entries object for chaining method calls
*/
only(fieldUid: string|string[]): this {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`only[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["only[BASE][]"] = fieldUid;
}
return this;
}

/**
* @method query
* @memberof Entries
Expand Down
53 changes: 1 addition & 52 deletions src/lib/entry-queryable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,5 @@ import { BaseQuery } from './base-query';

/* eslint-disable @cspell/spellchecker */
export class EntryQueryable extends BaseQuery {
/**
* @method only
* @memberof EntryQueryable
* @description Selects specific field/fields of an entry
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().only("fieldUID").find()
*
* @param {string} fieldUid - field uid to select
* @returns {EntryQueryable} - returns EntryQueryable object for chaining method calls
*/
only(fieldUid: string|string[]): this {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`only[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["only[BASE][]"] = fieldUid;
}
return this;
}

/**
* @method except
* @memberof EntryQueryable
* @description Excludes specific field/fields of an entry
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().except("fieldUID").find()
*
* @param {string} fieldUid - field uid to exclude
* @returns {EntryQueryable} - returns EntryQueryable object for chaining method calls
*/
except(fieldUid: string|string[]): this {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`except[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["except[BASE][]"] = fieldUid;
}

return this;
}

}
73 changes: 63 additions & 10 deletions src/lib/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Entry {
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry(entry_uid).includeFallback().fetch();
*/
includeFallback(): Entry {
includeFallback(): this {
this._queryParams.include_fallback = 'true';

return this;
Expand All @@ -46,7 +46,7 @@ export class Entry {
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType('abc').entry('entry_uid').variants('xyz').fetch();
*/
variants(variants: string | string[]): Entry {
variants(variants: string | string[]): this {
if (Array.isArray(variants) && variants.length > 0) {
this._variants = variants.join(',');
} else if (typeof variants == 'string' && variants.length > 0) {
Expand All @@ -67,7 +67,7 @@ export class Entry {
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry(entry_uid).includeMetadata().fetch();
*/
includeMetadata(): Entry {
includeMetadata(): this {
this._queryParams.include_metadata = 'true';

return this;
Expand All @@ -84,7 +84,7 @@ export class Entry {
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry(entry_uid).includeEmbeddedItems().fetch();
*/
includeEmbeddedItems(): Entry {
includeEmbeddedItems(): this {
this._queryParams['include_embedded_items[]'] = 'BASE';

return this;
Expand All @@ -103,7 +103,7 @@ export class Entry {
* @param {string} referenceFieldUid - UID of the reference field to include.
* @returns {Entry} - Returns the Entry instance for chaining.
*/
includeReference(...referenceFieldUid: (string | string[])[]): Entry {
includeReference(...referenceFieldUid: (string | string[])[]): this {
if (referenceFieldUid.length) {
referenceFieldUid.forEach(value => {
if (!Array.isArray(this._queryParams['include[]'])) {
Expand All @@ -128,7 +128,7 @@ export class Entry {
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry(entry_uid).includeContentType().fetch();
*/
includeContentType(): Entry {
includeContentType(): this {
this._queryParams.include_content_type = 'true';

return this;
Expand All @@ -145,7 +145,7 @@ export class Entry {
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType(contentType_uid).entry(entry_uid).includeBranch().fetch();
*/
includeBranch(): Entry {
includeBranch(): this {
this._queryParams.include_branch = 'true';

return this;
Expand All @@ -162,7 +162,7 @@ export class Entry {
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.assetQuery().locale('en-us').fetch();
*/
locale(locale: string): Entry {
locale(locale: string): this {
this._queryParams.locale = locale;

return this;
Expand Down Expand Up @@ -195,7 +195,7 @@ export class Entry {
return response;
}

/**
/**
* @method addParams
* @memberof Entry
* @description Adds a query parameter to the query.
Expand All @@ -207,9 +207,62 @@ export class Entry {
*
* @returns {Entry}
*/
addParams(paramObj: { [key: string]: string | number | string[] }): Entry {
addParams(paramObj: { [key: string]: string | number | string[] }): this {
this._queryParams = { ...this._queryParams, ...paramObj };

return this;
}

/**
* @method except
* @memberof Entry
* @description Excludes specific field/fields of an entry
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().except("fieldUID").find()
*
* @param {string} fieldUid - field uid to exclude
* @returns {Entry} - returns Entry object for chaining method calls
*/
except(fieldUid: string|string[]): this {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`except[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["except[BASE][]"] = fieldUid;
}

return this;
}

/**
* @method only
* @memberof Entry
* @description Selects specific field/fields of an entry
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().only("fieldUID").find()
*
* @param {string} fieldUid - field uid to select
* @returns {Entry} - returns Entry object for chaining method calls
*/
only(fieldUid: string|string[]): this {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`only[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["only[BASE][]"] = fieldUid;
}
return this;
}
}
Loading