1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class Translation {
6 /**
7 * @ignore
8 */
9 private client: Client
10
11 /**
12 * @ignore
13 */
14 constructor(client: Client) {
15 this.client = client
16 }
17
18 /**
19 * This method retrieves a paginated list of translations. It sends a request to the
20 * [List Translations](https://docs.medusajs.com/api/admin/translations/list-translations)
21 * API route.
22 *
23 * @param query - Filters and pagination configurations.
24 * @param headers - Headers to pass in the request.
25 * @returns The paginated list of translations.
26 *
27 * @example
28 * To retrieve the list of translations:
29 *
30 * ```ts
31 * sdk.admin.translation.list()
32 * .then(({ translations, count, limit, offset }) => {
33 * console.log(translations)
34 * })
35 * ```
36 *
37 * To configure the pagination, pass the `limit` and `offset` query parameters.
38 *
39 * For example, to retrieve only 10 items and skip 10 items:
40 *
41 * ```ts
42 * sdk.admin.translation.list({
43 * limit: 10,
44 * offset: 10
45 * })
46 * .then(({ translations, count, limit, offset }) => {
47 * console.log(translations)
48 * })
49 * ```
50 *
51 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
52 * in each translation:
53 *
54 * ```ts
55 * sdk.admin.translation.list({
56 * fields: "id,name"
57 * })
58 * .then(({ translations, count, limit, offset }) => {
59 * console.log(translations)
60 * })
61 * ```
62 *
63 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
64 */
65 async list(
66 query?: HttpTypes.AdminTranslationsListParams,
67 headers?: ClientHeaders
68 ) {
69 return await this.client.fetch<HttpTypes.AdminTranslationsListResponse>(
70 `/admin/translations`,
71 {
72 headers,
73 query,
74 }
75 )
76 }
77
78 /**
79 * This method allows bulk operations on translations. It sends a request to the
80 * [Manage Translations](https://docs.medusajs.com/api/admin/translations/manage-translations)
81 * API route.
82 *
83 * @param payload - The translations to create, update, or delete.
84 * @param headers - Headers to pass in the request.
85 * @returns The translations' details.
86 *
87 * @example
88 * sdk.admin.translation.batch({
89 * create: [
90 * {
91 * reference_id: "prod_123",
92 * reference: "product",
93 * locale_code: "en-US",
94 * translations: { title: "Shirt" }
95 * }
96 * ],
97 * update: [
98 * {
99 * id: "trans_123",
100 * translations: { title: "Pants" }
101 * }
102 * ],
103 * delete: ["trans_321"]
104 * })
105 * .then(({ created, updated, deleted }) => {
106 * console.log(created, updated, deleted)
107 * })
108 */
109 async batch(body: HttpTypes.AdminBatchTranslations, headers?: ClientHeaders) {
110 return await this.client.fetch<HttpTypes.AdminTranslationsBatchResponse>(
111 `/admin/translations/batch`,
112 {
113 method: "POST",
114 headers,
115 body,
116 }
117 )
118 }
119
120 /**
121 * This method retrieves the settings for the translations for a given entity type or all entity types if no entity type is provided.
122 * It sends a request to the
123 * [Get Translation Settings](https://docs.medusajs.com/api/admin/translations/list-settings) API route.
124 *
125 * @param query - The query parameters which can optionally include the entity type to get the settings for and whether to get the settings for active/inactive entities only.
126 * @param headers - Headers to pass in the request.
127 * @returns The translation settings.
128 *
129 * @example
130 * To retrieve the settings for the translations for a given entity type:
131 *
132 * ```ts
133 * sdk.admin.translation.settings({
134 * entity_type: "product"
135 * })
136 * .then(({ translation_settings }) => {
137 * console.log(translation_settings)
138 * })
139 * ```
140 *
141 * To retrieve the settings for all entity types:
142 *
143 * ```ts
144 * sdk.admin.translation.settings()
145 * .then(({ translation_settings }) => {
146 * console.log(translation_settings)
147 * })
148 * ```
149 *
150 * To retrieve the settings for active entities only:
151 *
152 * ```ts
153 * sdk.admin.translation.settings({
154 * is_active: true
155 * })
156 * .then(({ translation_settings }) => {
157 * console.log(translation_settings)
158 * })
159 * ```
160 *
161 * To retrieve the settings for inactive entities only:
162 *
163 * ```ts
164 * sdk.admin.translation.settings({
165 * is_active: false
166 * })
167 * .then(({ translation_settings }) => {
168 * console.log(translation_settings)
169 * })
170 * ```
171 */
172 async settings(
173 query?: HttpTypes.AdminTranslationSettingsParams,
174 headers?: ClientHeaders
175 ) {
176 return await this.client.fetch<HttpTypes.AdminTranslationSettingsResponse>(
177 `/admin/translations/settings`,
178 {
179 headers,
180 query,
181 }
182 )
183 }
184
185 /**
186 * This method allows bulk operations on translation settings. It sends a request to the
187 * [Batch Translation Settings](https://docs.medusajs.com/api/admin/translations/manage-translation-settings)
188 * API route.
189 *
190 * @since 2.12.6
191 *
192 * @param body - The translation settings to create, update, or delete.
193 * @param headers - Headers to pass in the request.
194 * @returns The translation settings' details.
195 *
196 * @example
197 * sdk.admin.translation.batchSettings({
198 * create: [
199 * {
200 * entity_type: "product",
201 * fields: ["title", "description"],
202 * is_active: true
203 * }
204 * ],
205 * update: [
206 * {
207 * id: "trset_123",
208 * fields: ["title", "description", "subtitle"],
209 * is_active: true
210 * }
211 * ],
212 * delete: ["trset_456"]
213 * })
214 * .then(({ created, updated, deleted }) => {
215 * console.log(created, updated, deleted)
216 * })
217 */
218 async batchSettings(
219 body: HttpTypes.AdminBatchTranslationSettings,
220 headers?: ClientHeaders
221 ) {
222 return await this.client.fetch<HttpTypes.AdminBatchTranslationSettingsResponse>(
223 `/admin/translations/settings/batch`,
224 {
225 method: "POST",
226 headers,
227 body,
228 }
229 )
230 }
231
232 /**
233 * This method retrieves a paginated list of entities for a given entity type with only their
234 * translatable fields.
235 * It sends a request to the
236 * [Get Translation Entities](https://docs.medusajs.com/api/admin/translations/list-translatable-entities) API route.
237 *
238 * @since 2.12.4
239 *
240 * @param query - The query parameters including the entity type and pagination configurations.
241 * @param headers - Headers to pass in the request.
242 * @returns The paginated list of entities with their translatable fields.
243 *
244 * @example
245 * To retrieve the entities for a given entity type:
246 *
247 * ```ts
248 * sdk.admin.translation.entities({
249 * type: "product"
250 * })
251 * .then(({ data, count, offset, limit }) => {
252 * console.log(data)
253 * })
254 * ```
255 *
256 * To configure the pagination, pass the `limit` and `offset` query parameters.
257 *
258 * For example, to retrieve only 10 items and skip 10 items:
259 *
260 * ```ts
261 * sdk.admin.translation.entities({
262 * type: "product",
263 * limit: 10,
264 * offset: 10
265 * })
266 * .then(({ data, count, offset, limit }) => {
267 * console.log(data)
268 * })
269 * ```
270 */
271 async entities(
272 query: HttpTypes.AdminTranslationEntitiesParams,
273 headers?: ClientHeaders
274 ) {
275 return await this.client.fetch<HttpTypes.AdminTranslationEntitiesResponse>(
276 `/admin/translations/entities`,
277 {
278 headers,
279 query,
280 }
281 )
282 }
283
284 /**
285 * This method retrieves the statistics for the translations for a given entity type or all entity types if no entity type is provided.
286 * It sends a request to the
287 * [Get Translation Statistics](https://docs.medusajs.com/api/admin/translations/get-statistics) API route.
288 *
289 * @param query - The query parameters which can optionally include the entity type to get the statistics for.
290 * @param headers - Headers to pass in the request.
291 * @returns The translation statistics.
292 *
293 * @example
294 * To retrieve the statistics for the translations for a given entity type:
295 *
296 * ```ts
297 * sdk.admin.translation.statistics({
298 * entity_type: "product"
299 * })
300 * .then(({ statistics }) => {
301 * console.log(statistics)
302 * })
303 * ```
304 *
305 * To retrieve the statistics for all entity types:
306 *
307 * ```ts
308 * sdk.admin.translation.statistics()
309 * .then(({ statistics }) => {
310 * console.log(statistics)
311 * })
312 * ```
313 */
314 async statistics(
315 query?: HttpTypes.AdminTranslationStatisticsParams,
316 headers?: ClientHeaders
317 ) {
318 return await this.client.fetch<HttpTypes.AdminTranslationStatisticsResponse>(
319 `/admin/translations/statistics`,
320 {
321 headers,
322 query,
323 }
324 )
325 }
326 }