diff --git a/src/api/apiPaths.ts b/src/api/apiPaths.ts index f561ba1..110efb5 100644 --- a/src/api/apiPaths.ts +++ b/src/api/apiPaths.ts @@ -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", }, diff --git a/src/api/onesearch-oc-seaapi/category.ts b/src/api/onesearch-oc-seaapi/category.ts new file mode 100644 index 0000000..93a822b --- /dev/null +++ b/src/api/onesearch-oc-seaapi/category.ts @@ -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; + +const defaultCategoryListOptions: ICategoryListOptions = {}; + +export interface ICategoryListBody extends Record { + algorithm?: Algorithm; + categoryId: number; + filters?: Record; + 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> { + 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 { + categoryListOptions = { + ...defaultCategoryListOptions, + ...categoryListOptions, + }; + categoryListBody = { ...defaultCategoryListBody, ...categoryListBody }; + + return postCategoryListRequest( + url, + categoryListBody, + categoryListOptions, + headers, + ); +} diff --git a/src/api/onesearch-oc-seaapi/index.ts b/src/api/onesearch-oc-seaapi/index.ts index 84dbb61..76e4fcb 100644 --- a/src/api/onesearch-oc-seaapi/index.ts +++ b/src/api/onesearch-oc-seaapi/index.ts @@ -1,5 +1,7 @@ import { postProductSearch } from "./product-search"; +import { categoryList } from "./category"; export const productSearch = { - searchProduct: postProductSearch + searchProduct: postProductSearch, + category: categoryList }