@@ -11,7 +11,8 @@ import {
1111 IListCustomObjectRecordsResponse ,
1212 ICustomObjectRecord ,
1313 ListCutomObjectRecordsSortingOptions ,
14- ICustomObjectRecordField
14+ ICustomObjectRecordField ,
15+ ISearchCustomObjectRecordsFilter
1516} from "@models/index" ;
1617import { Client } from "@zendesk/sell-zaf-app-toolbox" ;
1718
@@ -140,30 +141,9 @@ export class CustomObjectService {
140141 key : string ,
141142 sortOptions ?: { sort : ListCutomObjectRecordsSortingOptions }
142143 ) : Promise < ICustomObjectRecord < T > [ ] > {
143- let hasMore = true ;
144- let data : IListCustomObjectRecordsFilter = { } ;
145- let objects : ICustomObjectRecord < T > [ ] = [ ] ;
146-
147- do {
148- const response = await this . client . request < any , IListCustomObjectRecordsResponse < T > > ( {
149- url : `/api/v2/custom_objects/${ key } /records` ,
150- type : "GET" ,
151- contentType : CONTENT_TYPE ,
152- data
153- } ) ;
154-
155- objects = [ ...objects , ...response . custom_object_records ] ;
156-
157- hasMore = response . meta . has_more ;
158- data = {
159- page : {
160- after : response . meta . after_cursor
161- } ,
162- ...sortOptions
163- } ;
164- } while ( hasMore ) ;
165-
166- return objects ;
144+ return this . fetchAllPaginatedRecords < T > ( `/api/v2/custom_objects/${ key } /records` , {
145+ sort : sortOptions ?. sort
146+ } as IListCustomObjectRecordsFilter ) ;
167147 }
168148
169149 /**
@@ -237,4 +217,63 @@ export class CustomObjectService {
237217 contentType : CONTENT_TYPE
238218 } ) ;
239219 }
220+
221+ /**
222+ * Get custom object records by search
223+ * The query parameter is used to search text-based fields for records that match specific query terms. The query can be multiple words or numbers. Every record that matches the beginning of any word or number in the query string is returned.
224+ * Fuzzy search is supported for the following text-based field types: : Text fields, Multi Line Text fields, and RegExp fields.
225+ *
226+ * @param key - The custom object key
227+ * @param query - The search query
228+ * @see https://developer.zendesk.com/api-reference/custom-data/custom-objects/custom_object_records/#search-custom-object-records
229+ */
230+ public async searchRecords < T extends ICustomObjectRecordField > ( key : string , query : string ) {
231+ return this . fetchAllPaginatedRecords < T > ( `/api/v2/custom_objects/${ key } /records/search` , {
232+ query
233+ } as ISearchCustomObjectRecordsFilter ) ;
234+ }
235+
236+ /**
237+ * Generic method to fetch all records using pagination
238+ *
239+ * @param url - The API endpoint URL
240+ * @param initialData - Initial request data
241+ * @param getNextPageData - Function to build the next page request data
242+ * @returns Array of all fetched records
243+ */
244+ private async fetchAllPaginatedRecords < T extends ICustomObjectRecordField > (
245+ url : string ,
246+ initialData : IListCustomObjectRecordsFilter
247+ ) : Promise < ICustomObjectRecord < T > [ ] > {
248+ let hasMore = true ;
249+ let data = initialData ;
250+ let objects : ICustomObjectRecord < T > [ ] = [ ] ;
251+
252+ do {
253+ const response = await this . client . request < any , IListCustomObjectRecordsResponse < T > > ( {
254+ url,
255+ type : "GET" ,
256+ contentType : CONTENT_TYPE ,
257+ data
258+ } ) ;
259+
260+ objects = [ ...objects , ...response . custom_object_records ] ;
261+
262+ hasMore = response . meta . has_more ;
263+ if ( hasMore ) {
264+ data = {
265+ page : {
266+ after : response . meta . after_cursor
267+ } ,
268+ ...( initialData . sort
269+ ? {
270+ sort : initialData . sort
271+ }
272+ : undefined )
273+ } as IListCustomObjectRecordsFilter ;
274+ }
275+ } while ( hasMore ) ;
276+
277+ return objects ;
278+ }
240279}
0 commit comments