Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration category #4

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/api/MigrosAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
IProductSearchOptions,
} from "./onesearch-oc-seaapi/product-search";
import { migusto } from "./migusto";
import {
ICategoryListBody,
ICategoryListOptions,
} from "./onesearch-oc-seaapi/category";

export class MigrosAPI {
private _leShopToken: string | undefined;
Expand Down Expand Up @@ -84,7 +88,7 @@
getProductSupply: async (
productSupplyOptions: IProductSupplyOptions,
token: string | undefined = this.leShopToken,
): Promise<any> => {

Check warning on line 91 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 91 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!token) {
throw Error("LeShop Token is undefined");
}
Expand All @@ -97,7 +101,7 @@
getProductCards: async (
productCardOptions: IProductCardsOptions,
token: string | undefined = this.leShopToken,
): Promise<any> => {

Check warning on line 104 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 104 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!token) {
throw Error("LeShop Token is undefined");
}
Expand All @@ -108,7 +112,7 @@
getProductDetails: async (
productSupplyOptions: IProductSupplyOptions,
token: string | undefined = this.leShopToken,
): Promise<any> => {

Check warning on line 115 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 115 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!token) {
throw Error("LeShop Token is undefined");
}
Expand All @@ -118,11 +122,27 @@
},
},
productSearch: {
categoryList: async (
categoryListBody: ICategoryListBody,
categoryListOptions?: ICategoryListOptions,
token: string | undefined = this.leShopToken,
): Promise<any> => {

Check warning on line 129 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 129 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!token) {
throw Error("LeShop Token is undefined");
}
return await productSearch.listCategories(
categoryListBody,
{
leshopch: token,
},
categoryListOptions,
);
},
searchProduct: async (
productSearchBody: IProductSearchBody,
productSearchOptions?: IProductSearchOptions,
token: string | undefined = this.leShopToken,
): Promise<any> => {

Check warning on line 145 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 145 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!token) {
throw Error("LeShop Token is undefined");
}
Expand All @@ -139,14 +159,14 @@

account = {
oauth2: {
loginGuestToken: async (): Promise<any> => {

Check warning on line 162 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 162 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
const guestInfo = await oauth2.getGuestToken();
this.leShopToken = guestInfo.token;
return await guestInfo;
},
getUserInfo: async (
token: string | undefined = this.cumulusToken,
): Promise<any> => {

Check warning on line 169 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 169 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!token) {
throw Error("Cumulus Token is undefined");
}
Expand All @@ -156,7 +176,7 @@
security: {
getOptions: async (
cookies: ILoginCookies | undefined = this.loginCookies,
): Promise<any> => {

Check warning on line 179 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 179 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!cookies) {
throw Error("Login Cookies are undefined");
}
Expand All @@ -167,7 +187,7 @@
},
getPaymentDevices: async (
cookies: ILoginCookies | undefined = this.loginCookies,
): Promise<any> => {

Check warning on line 190 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check warning on line 190 in src/api/MigrosAPI.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
if (!cookies) {
throw Error("Login Cookies are undefined");
}
Expand Down
1 change: 1 addition & 0 deletions src/api/apiPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const defaultMigustoApiPath = "https://migusto.migros.ch";
export const migrosApiPaths = {
["onesearch-oc-seapi"]: {
public: {
v3: defaultMigrosApiPath + "/onesearch-oc-seaapi/public/v3",
v4: defaultMigrosApiPath + "/onesearch-oc-seaapi/public/v4",
v5: defaultMigrosApiPath + "/onesearch-oc-seaapi/public/v5",
},
Expand Down
79 changes: 79 additions & 0 deletions src/api/onesearch-oc-seaapi/category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Region } from "../enums/Region";
import { Language } from "../enums/Language";
import { Algorithm } from "../enums/Algorithm";
import { SortFields } from "../enums/SortFields";
import { SortOrder } from "../enums/SortOrder";

import { postRequest } from "../../utils/requests";

import { migrosApiPaths } from "../apiPaths";
import { IMigrosNecessaryHeaders } from "../interfaces/headers";

const url = migrosApiPaths["onesearch-oc-seapi"].public.v3 + "/search/category";

// eslint-disable-next-line @typescript-eslint/naming-convention
export type ICategoryListOptions = Record<string, any>;

const defaultCategoryListOptions: ICategoryListOptions = {};

export interface ICategoryListBody extends Record<string, any> {
algorithm?: Algorithm;
categoryId: number;
filters?: Record<any, any>;
from: number;
language?: Language;
productIds?: string[];
regionId?: Region;
requestSponsoredProducts?: boolean;
sortFields?: SortFields[];
sortOrder?: SortOrder;
}

const defaultCategoryListBody: ICategoryListBody = {
algorithm: Algorithm.DEFAULT,
regionId: Region.NATIONAL,
language: Language.EN,
productIds: [],
sortFields: [],
sortOrder: SortOrder.ASC,
requestSponsoredProducts: false,
from: 0,
categoryId: 0,
};

async function postCategoryListRequest(
url: string,
body: ICategoryListBody,
options: ICategoryListOptions,
headers: IMigrosNecessaryHeaders,
): Promise<Record<string, any>> {
const necessaryHeaders = {
accept: "application/json, text/plain, *!/!*",
// eslint-disable-next-line @typescript-eslint/naming-convention
"content-type": "application/json",
...headers,
};

const response = await postRequest(url, body, options, necessaryHeaders);

return await response.json();
}

export async function categoryList(
categoryListBody: ICategoryListBody,
headers: IMigrosNecessaryHeaders,
categoryListOptions?: ICategoryListOptions,
): Promise<any> {
categoryListOptions = {
...defaultCategoryListOptions,
...categoryListOptions,
};
categoryListBody = { ...defaultCategoryListBody, ...categoryListBody };

return postCategoryListRequest(
url,
categoryListBody,
categoryListOptions,
headers,
);
}
6 changes: 4 additions & 2 deletions src/api/onesearch-oc-seaapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { postProductSearch } from "./product-search";
import { categoryList } from "./category";

export const productSearch = {
searchProduct: postProductSearch
}
searchProduct: postProductSearch,
listCategories: categoryList,
};
22 changes: 22 additions & 0 deletions tests/category-list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, test } from "@jest/globals";
import { MigrosAPI } from "../src";
import { ICategoryListBody } from "../src/api/onesearch-oc-seaapi/category";

describe("Get a list of categories", () => {
test("Search for Dairy", async () => {
const guestInfo = await MigrosAPI.account.oauth2.getGuestToken();
const categoryListBody: ICategoryListBody = {
from: 0,
categoryId: 7494731,
};
const response = await MigrosAPI.products.productSearch.listCategories(
categoryListBody,
{
leshopch: guestInfo.token,
},
);
expect(response.categories[0].name).toBe(
"Dairy, eggs & fresh convenience food",
);
});
});
2 changes: 1 addition & 1 deletion tests/migusto-recipe-products.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ describe("Search for recipe products", () => {
};
const response =
await MigrosAPI.migusto.recipeProducts(recipeProductOptions);
expect(response.productIds).toContain("103322000000");
expect(response.productIds).toContain("220221000110");
});
});
Loading