-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch-fixtures.ts
170 lines (150 loc) · 4.18 KB
/
fetch-fixtures.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { createAdminAPIClient } from "@shopware/api-client";
import type {
operationPaths,
operations,
} from "@shopware/api-client/admin-api-types";
export const adminApiClient = createAdminAPIClient<operations, operationPaths>({
baseURL: `${process.env.SHOP_URL}/api`,
credentials: {
grant_type: "password",
client_id: "administration",
scopes: "write",
username: process.env.SHOP_ADMIN_USERNAME as string,
password: process.env.SHOP_ADMIN_PASSWORD as string,
},
});
async function fetchSalesChannel() {
const data = await adminApiClient.invoke(
"searchSalesChannel post /search/sales-channel",
{
fields: ["id", "name", "accessKey", "domains.url", "countries.id"],
filter: [
{
type: "equals",
field: "active",
value: true,
},
{
type: "not",
queries: [
{
type: "equals",
field: "name",
value: "Headless",
},
],
},
],
},
);
const salutationIds = await adminApiClient.invoke(
"salutation post /search-ids/salutation",
);
const taxIds = await adminApiClient.invoke(
"tax post /search-ids/tax",
);
const records = data.data.map((record) => {
return {
id: record.id,
name: record.name,
accessKey: record.accessKey,
url: record.domains[0].url,
countryIds: record.countries.map((e) => e.id),
salutationIds: salutationIds.data,
taxIds: taxIds.data,
api: {
baseURL: `${process.env.SHOP_URL}/api`,
credentials: {
grant_type: "password",
client_id: "administration",
scopes: "write",
username: process.env.SHOP_ADMIN_USERNAME as string,
password: process.env.SHOP_ADMIN_PASSWORD as string,
},
},
};
});
Bun.write("fixtures/sales-channel.json", JSON.stringify(records));
console.log(`Collected ${records.length} sales channels`);
console.log(`Collected ${salutationIds.data.length} salutations`);
return records[0];
}
const salesChannel = await fetchSalesChannel();
async function fetchSeoUrls(name: string) {
const seoUrls = await adminApiClient.invoke(
"searchSeoUrl post /search/seo-url",
{
fields: ["seoPathInfo", "foreignKey"],
filter: [
{
type: "equals",
field: "routeName",
value: name,
},
{
type: "equals",
field: "isCanonical",
value: true,
},
{
type: "equals",
field: "isDeleted",
value: false,
},
{
type: "equals",
field: "salesChannelId",
value: salesChannel.id,
},
],
limit: 500,
},
);
const data = seoUrls.data.map((seoUrl) => {
return {
url: `${salesChannel.url}/${seoUrl.seoPathInfo}`,
id: seoUrl.foreignKey,
};
});
Bun.write(`fixtures/seo-${name}.json`, JSON.stringify(data));
console.log(`Collected ${data.length} seo urls for ${name}`);
}
async function fetchMedia()
{
const mediaIds = await adminApiClient.invoke(
"media post /search-ids/media",
{
limit: 500,
}
);
Bun.write(`fixtures/media.json`, JSON.stringify(mediaIds.data));
console.log(`Collected ${mediaIds.data.length} media ids`);
}
async function fetchProperties()
{
const propertyIds = await adminApiClient.invoke(
"property_group_option post /search-ids/property-group-option",
{
limit: 500,
}
);
Bun.write(`fixtures/property_group_option.json`, JSON.stringify(propertyIds.data));
console.log(`Collected ${propertyIds.data.length} property ids`);
}
await Promise.all([
fetchProperties(),
fetchMedia(),
fetchSeoUrls("frontend.navigation.page"),
fetchSeoUrls("frontend.detail.page"),
]);
const keywords = await adminApiClient.invoke(
"getProductSearchKeyword post /search/product-search-keyword",
{
limit: 500,
},
);
const uniqueKeywords = keywords.data
.map((k) => k.keyword)
.filter((value, index, array) => array.indexOf(value) === index);
Bun.write("fixtures/keywords.json", JSON.stringify(uniqueKeywords));
console.log(`Collected ${uniqueKeywords.length} search keywords`);