Skip to content

Commit c4df267

Browse files
aliyssperfaram
andauthored
Integration category (#4)
* Support search by category endpoint * added category lists --------- Co-authored-by: Perceval Faramaz <[email protected]>
1 parent 098aaf5 commit c4df267

File tree

6 files changed

+127
-3
lines changed

6 files changed

+127
-3
lines changed

src/api/MigrosAPI.ts

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import {
2121
IProductSearchOptions,
2222
} from "./onesearch-oc-seaapi/product-search";
2323
import { migusto } from "./migusto";
24+
import {
25+
ICategoryListBody,
26+
ICategoryListOptions,
27+
} from "./onesearch-oc-seaapi/category";
2428

2529
export class MigrosAPI {
2630
private _leShopToken: string | undefined;
@@ -118,6 +122,22 @@ export class MigrosAPI {
118122
},
119123
},
120124
productSearch: {
125+
categoryList: async (
126+
categoryListBody: ICategoryListBody,
127+
categoryListOptions?: ICategoryListOptions,
128+
token: string | undefined = this.leShopToken,
129+
): Promise<any> => {
130+
if (!token) {
131+
throw Error("LeShop Token is undefined");
132+
}
133+
return await productSearch.listCategories(
134+
categoryListBody,
135+
{
136+
leshopch: token,
137+
},
138+
categoryListOptions,
139+
);
140+
},
121141
searchProduct: async (
122142
productSearchBody: IProductSearchBody,
123143
productSearchOptions?: IProductSearchOptions,

src/api/apiPaths.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const defaultMigustoApiPath = "https://migusto.migros.ch";
55
export const migrosApiPaths = {
66
["onesearch-oc-seapi"]: {
77
public: {
8+
v3: defaultMigrosApiPath + "/onesearch-oc-seaapi/public/v3",
89
v4: defaultMigrosApiPath + "/onesearch-oc-seaapi/public/v4",
910
v5: defaultMigrosApiPath + "/onesearch-oc-seaapi/public/v5",
1011
},
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Region } from "../enums/Region";
2+
import { Language } from "../enums/Language";
3+
import { Algorithm } from "../enums/Algorithm";
4+
import { SortFields } from "../enums/SortFields";
5+
import { SortOrder } from "../enums/SortOrder";
6+
7+
import { postRequest } from "../../utils/requests";
8+
9+
import { migrosApiPaths } from "../apiPaths";
10+
import { IMigrosNecessaryHeaders } from "../interfaces/headers";
11+
12+
const url = migrosApiPaths["onesearch-oc-seapi"].public.v3 + "/search/category";
13+
14+
// eslint-disable-next-line @typescript-eslint/naming-convention
15+
export type ICategoryListOptions = Record<string, any>;
16+
17+
const defaultCategoryListOptions: ICategoryListOptions = {};
18+
19+
export interface ICategoryListBody extends Record<string, any> {
20+
algorithm?: Algorithm;
21+
categoryId: number;
22+
filters?: Record<any, any>;
23+
from: number;
24+
language?: Language;
25+
productIds?: string[];
26+
regionId?: Region;
27+
requestSponsoredProducts?: boolean;
28+
sortFields?: SortFields[];
29+
sortOrder?: SortOrder;
30+
}
31+
32+
const defaultCategoryListBody: ICategoryListBody = {
33+
algorithm: Algorithm.DEFAULT,
34+
regionId: Region.NATIONAL,
35+
language: Language.EN,
36+
productIds: [],
37+
sortFields: [],
38+
sortOrder: SortOrder.ASC,
39+
requestSponsoredProducts: false,
40+
from: 0,
41+
categoryId: 0,
42+
};
43+
44+
async function postCategoryListRequest(
45+
url: string,
46+
body: ICategoryListBody,
47+
options: ICategoryListOptions,
48+
headers: IMigrosNecessaryHeaders,
49+
): Promise<Record<string, any>> {
50+
const necessaryHeaders = {
51+
accept: "application/json, text/plain, *!/!*",
52+
// eslint-disable-next-line @typescript-eslint/naming-convention
53+
"content-type": "application/json",
54+
...headers,
55+
};
56+
57+
const response = await postRequest(url, body, options, necessaryHeaders);
58+
59+
return await response.json();
60+
}
61+
62+
export async function categoryList(
63+
categoryListBody: ICategoryListBody,
64+
headers: IMigrosNecessaryHeaders,
65+
categoryListOptions?: ICategoryListOptions,
66+
): Promise<any> {
67+
categoryListOptions = {
68+
...defaultCategoryListOptions,
69+
...categoryListOptions,
70+
};
71+
categoryListBody = { ...defaultCategoryListBody, ...categoryListBody };
72+
73+
return postCategoryListRequest(
74+
url,
75+
categoryListBody,
76+
categoryListOptions,
77+
headers,
78+
);
79+
}

src/api/onesearch-oc-seaapi/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { postProductSearch } from "./product-search";
2+
import { categoryList } from "./category";
23

34
export const productSearch = {
4-
searchProduct: postProductSearch
5-
}
5+
searchProduct: postProductSearch,
6+
listCategories: categoryList,
7+
};

tests/category-list.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, test } from "@jest/globals";
2+
import { MigrosAPI } from "../src";
3+
import { ICategoryListBody } from "../src/api/onesearch-oc-seaapi/category";
4+
5+
describe("Get a list of categories", () => {
6+
test("Search for Dairy", async () => {
7+
const guestInfo = await MigrosAPI.account.oauth2.getGuestToken();
8+
const categoryListBody: ICategoryListBody = {
9+
from: 0,
10+
categoryId: 7494731,
11+
};
12+
const response = await MigrosAPI.products.productSearch.listCategories(
13+
categoryListBody,
14+
{
15+
leshopch: guestInfo.token,
16+
},
17+
);
18+
expect(response.categories[0].name).toBe(
19+
"Dairy, eggs & fresh convenience food",
20+
);
21+
});
22+
});

tests/migusto-recipe-products.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ describe("Search for recipe products", () => {
1212
};
1313
const response =
1414
await MigrosAPI.migusto.recipeProducts(recipeProductOptions);
15-
expect(response.productIds).toContain("103322000000");
15+
expect(response.productIds).toContain("220221000110");
1616
});
1717
});

0 commit comments

Comments
 (0)