|
| 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 | +} |
0 commit comments