Skip to content

Commit

Permalink
fix: handle var.* in query endpoints correctly
Browse files Browse the repository at this point in the history
The var.* were not forwarded to the repository
  • Loading branch information
mvantellingen committed Apr 19, 2024
1 parent 6bf591c commit 222fa1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-kings-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": patch
---

Handle `var.*` in query endpoints correctly
21 changes: 17 additions & 4 deletions src/services/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { ParsedQs } from "qs";
import { updateRequestSchema } from "~src/schemas/update-request";
import { validateData } from "~src/validate";
import { queryParamsArray } from "../helpers";
import { AbstractResourceRepository } from "../repositories/abstract";
import {
AbstractResourceRepository,
QueryParams,
} from "../repositories/abstract";
import { getRepositoryContext } from "../repositories/helpers";

export default abstract class AbstractService {
Expand Down Expand Up @@ -44,13 +47,23 @@ export default abstract class AbstractService {
get(request: Request, response: Response) {
const limit = this._parseParam(request.query.limit);
const offset = this._parseParam(request.query.offset);

const result = this.repository.query(getRepositoryContext(request), {
const params: QueryParams = {
expand: this._parseParam(request.query.expand),
where: this._parseParam(request.query.where),
limit: limit !== undefined ? Number(limit) : undefined,
offset: offset !== undefined ? Number(offset) : undefined,
});
};

for (const key in request.query) {
if (key.startsWith("var.")) {
const items = this._parseParam(request.query[key]);
if (items) {
params[key] = items.length === 1 ? items[0] : items;
}
}
}

const result = this.repository.query(getRepositoryContext(request), params);
return response.status(200).send(result);
}

Expand Down

0 comments on commit 222fa1c

Please sign in to comment.