Skip to content

Commit

Permalink
Add support for search query
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelreichor committed Dec 30, 2024
1 parent 969381d commit db0c83e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-cows-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'js-craftcms-api': patch
---

Add support for search query
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface CommonQueryParams {
offset?: number;
orderBy?: string;
fields?: string | string[];
search?: string;
}

// Specific query parameters for each element type
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -169,6 +171,10 @@ export function buildCraftQueryUrl<T extends ElementType>(elementType: T): Query
params.fields = value;
return this;
},
search(value) {
params.search = value;
return this;
},
buildBaseUrl(value) {
if (value === 'all') {
params.one = undefined;
Expand Down
5 changes: 3 additions & 2 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
);
});
});
Expand Down

0 comments on commit db0c83e

Please sign in to comment.