forked from aliyss/migros-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory.ts
79 lines (67 loc) · 2.17 KB
/
category.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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,
);
}