diff --git a/.changeset/long-cows-compare.md b/.changeset/long-cows-compare.md new file mode 100644 index 0000000..b41b1ea --- /dev/null +++ b/.changeset/long-cows-compare.md @@ -0,0 +1,5 @@ +--- +'js-craftcms-api': patch +--- + +Add support for search query diff --git a/src/index.ts b/src/index.ts index 0492f93..c3dd05d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ export interface CommonQueryParams { offset?: number; orderBy?: string; fields?: string | string[]; + search?: string; } // Specific query parameters for each element type @@ -85,6 +86,7 @@ export interface CommonQueryBuilder { offset: (value: CommonQueryParams['offset']) => this; orderBy: (value: CommonQueryParams['orderBy']) => this; fields: (value: CommonQueryParams['fields']) => this; + search: (value: CommonQueryParams['search']) => this; buildBaseUrl: (value: ExecutionMethod) => string; } @@ -169,6 +171,10 @@ export function buildCraftQueryUrl(elementType: T): Query params.fields = value; return this; }, + search(value) { + params.search = value; + return this; + }, buildBaseUrl(value) { if (value === 'all') { params.one = undefined; diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts index c43c0f6..b3bfa2b 100644 --- a/src/tests/index.test.ts +++ b/src/tests/index.test.ts @@ -15,14 +15,15 @@ describe('buildCraftQueryUrl Tests', () => { .limit(5) .offset(2) .orderBy('name') - .fields(['title', 'heroImage']); + .fields(['title', 'heroImage']) + .search('q'); arrOfExecutionTypes.forEach((executionType) => { it(`Should execute all commonQueryBuilder functions for ${elementType} with ${executionType}()`, async () => { const queryUrlOne = baseQuery.buildBaseUrl(executionType); expect(queryUrlOne).toContain( - `elementType=${elementType}&id=12%2C34%2Cnot&limit=5&offset=2&orderBy=name&fields=title%2CheroImage&${executionType}=1`, + `elementType=${elementType}&id=12%2C34%2Cnot&limit=5&offset=2&orderBy=name&fields=title%2CheroImage&search=q&${executionType}=1`, ); }); });