-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat[getAll]: ENG-11765 add fetchTotalCount param from Content API as an option for getAll in SDKs
#4352
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
feat[getAll]: ENG-11765 add fetchTotalCount param from Content API as an option for getAll in SDKs
#4352
Changes from all commits
f59f949
9d7c43e
cfad2cf
14eab1d
95aedb9
8c02f4e
f8c17f7
73b1d7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@builder.io/sdk': minor | ||
| '@builder.io/react': minor | ||
| --- | ||
|
|
||
| Added fetchTotalCount param to getAll() in gen1 sdks |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -496,6 +496,12 @@ export type GetContentOptions = AllowEnrich & { | |
| */ | ||
| includeUnpublished?: boolean; | ||
|
|
||
| /** | ||
| * When `true`, the API will also return the total number of matching entries | ||
| * regardless of `limit`/`offset`. Useful for building numbered pagination. | ||
| */ | ||
| fetchTotalCount?: boolean; | ||
|
|
||
| /** | ||
| * Options to configure how enrichment works. | ||
| * @see {@link https://www.builder.io/c/docs/content-api#code-enrich-options-code} | ||
|
|
@@ -2336,6 +2342,7 @@ export class Builder { | |
|
|
||
| private getContentQueue: null | GetContentOptions[] = null; | ||
| private priorContentQueue: null | GetContentOptions[] = null; | ||
| private totalCountByKey: Record<string, number> = {}; | ||
|
|
||
| setUserAttributes(options: object) { | ||
| assign(Builder.overrideUserAttributes, options); | ||
|
|
@@ -2775,6 +2782,7 @@ export class Builder { | |
| 'rev', | ||
| 'static', | ||
| 'includeRefs', | ||
| 'fetchTotalCount', | ||
| ]; | ||
|
|
||
| for (const key of properties) { | ||
|
|
@@ -2871,6 +2879,9 @@ export class Builder { | |
| return; | ||
| } | ||
| const data = isApiCallForCodegenOrQuery ? result[keyName] : result.results; | ||
| if (result.totalCount !== undefined) { | ||
| this.totalCountByKey[keyName] = result.totalCount; | ||
| } | ||
| const sorted = data; // sortBy(data, item => item.priority); | ||
| if (data) { | ||
| const testModifiedResults = Builder.isServer | ||
|
|
@@ -3028,6 +3039,16 @@ export class Builder { | |
| return this.queueGetContent(modelName, options); | ||
| } | ||
|
|
||
| getAll<T extends boolean = false>( | ||
| modelName: string, | ||
| options?: GetContentOptions & { | ||
| req?: IncomingMessage; | ||
| res?: ServerResponse; | ||
| apiKey?: string; | ||
| authToken?: string; | ||
| fetchTotalCount?: T; | ||
| } | ||
| ): Promise<T extends true ? { results: BuilderContent[]; totalCount: number } : BuilderContent[]>; | ||
| getAll( | ||
| modelName: string, | ||
| options: GetContentOptions & { | ||
|
|
@@ -3036,7 +3057,7 @@ export class Builder { | |
| apiKey?: string; | ||
| authToken?: string; | ||
| } = {} | ||
| ): Promise<BuilderContent[]> { | ||
| ): Promise<BuilderContent[] | { results: BuilderContent[]; totalCount: number }> { | ||
| let instance: Builder = this; | ||
| if (!Builder.isBrowser) { | ||
| instance = new Builder( | ||
|
|
@@ -3047,6 +3068,7 @@ export class Builder { | |
| options.authToken || this.authToken, | ||
| options.apiVersion || this.apiVersion | ||
| ); | ||
| instance.apiEndpoint = this.apiEndpoint; | ||
| instance.setUserAttributes(this.getUserAttributes()); | ||
| } else { | ||
| // NOTE: All these are when .init is not called and the customer | ||
|
|
@@ -3067,18 +3089,26 @@ export class Builder { | |
| options.noTraverse = true; | ||
| } | ||
|
|
||
| const key = | ||
| (options.key || Builder.isBrowser || options.fetchTotalCount) | ||
| ? `${modelName}:${hash(omit(options, 'initialContent', 'req', 'res'))}` | ||
| : undefined; | ||
|
|
||
| return instance | ||
| .getContent(modelName, { | ||
| limit: 30, | ||
| ...options, | ||
| key: | ||
| options.key || | ||
| // Make the key include all options, so we don't reuse cache for the same content fetched | ||
| // with different options | ||
| Builder.isBrowser | ||
| ? `${modelName}:${hash(omit(options, 'initialContent', 'req', 'res'))}` | ||
| : undefined, | ||
| key, | ||
| }) | ||
| .promise(); | ||
| .promise() | ||
| .then(results => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this required when we are already passing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (options.fetchTotalCount) { | ||
| return { | ||
| results, | ||
| totalCount: key !== undefined ? instance.totalCountByKey[key] ?? 0 : 0, | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale totalCount returned when key resolves to cached observableMedium Severity When Additional Locations (1) |
||
| } | ||
| return results; | ||
| }); | ||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.