UNPKG

@wix/cli

Version:

CLI tool for building Wix sites and applications

383 lines (375 loc) 11.1 kB
import { createRequire as _createRequire } from 'node:module'; const require = _createRequire(import.meta.url); import { useHttpClient } from "./chunk-FYLO6WOU.js"; import { usePanoramaMethodLogger } from "./chunk-3B6VL2WO.js"; import { isHttpApplicationError, isHttpError, pRetry, resolveUrl, serializer, toURLSearchParams } from "./chunk-CCWE4DS6.js"; import { require_react } from "./chunk-QNHANFRW.js"; import { getTestOverrides } from "./chunk-XZUGLEBK.js"; import { external_exports } from "./chunk-SDLCFZRL.js"; import { CliError, CliErrorCode } from "./chunk-CO5A7CZG.js"; import { __toESM, init_esm_shims } from "./chunk-EXLZF52D.js"; // ../multilingual-client/src/useMultilingualClient.ts init_esm_shims(); var import_react = __toESM(require_react(), 1); // ../multilingual-client/src/multilingual-client.ts init_esm_shims(); // ../../node_modules/@wix/ambassador-multilingual-locales-v2-locale/build/es/http.impl.js init_esm_shims(); var _locale = { createdDate: "google.protobuf.Timestamp", updatedDate: "google.protobuf.Timestamp" }; var _queryLocalesRequest = {}; var _queryLocalesResponse = { locales: "_locale" }; function resolveWixMultilingualLocalesV2LocalesServiceUrl(opts) { var domainToMappings = { "manage._base_domain_": [ { srcPath: "/_api/locales", destPath: "" } ], "editor._base_domain_": [ { srcPath: "/_api/locales", destPath: "" } ], "blocks._base_domain_": [ { srcPath: "/_api/locales", destPath: "" } ], "create.editorx": [ { srcPath: "/_api/locales", destPath: "" } ], _: [ { srcPath: "/_api/locales", destPath: "" } ], "*.dev.wix-code.com": [ { srcPath: "/_api/locales", destPath: "" } ], "www.wixapis.com": [ { srcPath: "/locales", destPath: "" } ] }; return resolveUrl(Object.assign(opts, { domainToMappings })); } function queryLocales(payload) { var _a = serializer(_queryLocalesRequest, {}), toReq = _a.toJSON, fromReq = _a.fromJSON; var fromRes = serializer(_queryLocalesResponse, { _locale }).fromJSON; function __queryLocales(_a2) { var host = _a2.host; var serializedData = toReq(payload); var metadata = { entityFqdn: "wix.multilingual.locales.v2.locale", method: "GET", methodFqn: "wix.multilingual.locales.v2.LocalesService.QueryLocales", migrationOptions: { optInTransformResponse: true }, url: resolveWixMultilingualLocalesV2LocalesServiceUrl({ protoPath: "/v2/locale/query", data: serializedData, host }), params: toURLSearchParams(serializedData, true), transformResponse: fromRes, fallback: [ { method: "POST", url: resolveWixMultilingualLocalesV2LocalesServiceUrl({ protoPath: "/v2/locale/query", data: serializedData, host }), data: serializedData } ] }; return metadata; } __queryLocales.fromReq = fromReq; __queryLocales.__isAmbassador = true; return __queryLocales; } // ../../node_modules/@wix/ambassador-headless-multilingual-v1-no-entity/build/es/http.impl.js init_esm_shims(); var _pullTranslationsRequest = {}; var _pullTranslationsResponse = {}; var _pushTranslationsRequest = {}; var _pushTranslationsResponse = {}; function resolveWixHeadlessMultilingualV1HeadlessMultilingualServiceUrl(opts) { var domainToMappings = { "www.wixapis.com": [ { srcPath: "/headless/multilingual", destPath: "" } ] }; return resolveUrl(Object.assign(opts, { domainToMappings })); } function pushTranslations(payload) { var _a = serializer(_pushTranslationsRequest, {}), toReq = _a.toJSON, fromReq = _a.fromJSON; var fromRes = serializer(_pushTranslationsResponse, {}).fromJSON; function __pushTranslations(_a2) { var host = _a2.host; var serializedData = toReq(payload); var metadata = { entityFqdn: "wix.headless.multilingual.v1.no_entity", method: "POST", methodFqn: "wix.headless.multilingual.v1.HeadlessMultilingualService.PushTranslations", migrationOptions: { optInTransformResponse: true }, url: resolveWixHeadlessMultilingualV1HeadlessMultilingualServiceUrl({ protoPath: "/v1/headless-multilingual", data: serializedData, host }), data: serializedData, transformResponse: fromRes }; return metadata; } __pushTranslations.fromReq = fromReq; __pushTranslations.__isAmbassador = true; return __pushTranslations; } function pullTranslations(payload) { var _a = serializer(_pullTranslationsRequest, {}), toReq = _a.toJSON, fromReq = _a.fromJSON; var fromRes = serializer(_pullTranslationsResponse, {}).fromJSON; function __pullTranslations(_a2) { var host = _a2.host; var serializedData = toReq(payload); var metadata = { entityFqdn: "wix.headless.multilingual.v1.no_entity", method: "GET", methodFqn: "wix.headless.multilingual.v1.HeadlessMultilingualService.PullTranslations", migrationOptions: { optInTransformResponse: true }, url: resolveWixHeadlessMultilingualV1HeadlessMultilingualServiceUrl({ protoPath: "/v1/headless-multilingual", data: serializedData, host }), params: toURLSearchParams(serializedData), transformResponse: fromRes }; return metadata; } __pullTranslations.fromReq = fromReq; __pullTranslations.__isAmbassador = true; return __pullTranslations; } // ../multilingual-client/src/multilingual-client.ts var flatTranslationsSchema = external_exports.record(external_exports.string(), external_exports.string()); var namespacedTranslationsSchema = external_exports.record( external_exports.string(), external_exports.record(external_exports.string(), external_exports.string()) ); var MultilingualClient = class { constructor(httpClient) { this.httpClient = httpClient; const { minRetryTimeout } = getTestOverrides(); this.retryOptions = { retries: 3, minTimeout: minRetryTimeout, maxTimeout: 3 * 1e3 }; } retryOptions; queryLocales = async (request) => { try { const { data } = await pRetry( () => this.httpClient.request( queryLocales({ ...request }) ), this.retryOptions ); return data; } catch (error) { if (isHttpError(error) && isHttpApplicationError(error)) { throw new CliError({ code: CliErrorCode.FailedToQueryLocales(), cause: error }); } throw error; } }; getPrimaryLanguage = async () => { const localesResponse = await this.queryLocales(); const primaryLocale = localesResponse.locales?.find( (locale) => locale.primaryLocale === true ); if (!primaryLocale?.languageCode) { throw new CliError({ code: CliErrorCode.FailedToFetchPrimaryLanguage(), cause: new Error( "Failed to fetch the primary language. Ensure Wix Multilingual is installed on your site." ) }); } return primaryLocale.languageCode; }; pushTranslations = async (translations) => { const translationKeys = this.convertToTranslationKeysPayload(translations); try { await pRetry( () => this.httpClient.request( pushTranslations({ translationKeys }) ), this.retryOptions ); } catch (error) { if (isHttpError(error) && isHttpApplicationError(error)) { this.handleApplicationError(error); } throw new CliError({ code: CliErrorCode.FailedToPushTranslations(), cause: error }); } }; pullAllTranslations = async () => { const allTranslations = {}; let cursor; let hasMore = true; try { while (hasMore) { const { data } = await pRetry( () => this.httpClient.request( pullTranslations({ paging: cursor ? { cursor } : void 0 }) ), this.retryOptions ); for (const [locale, payload] of Object.entries( data.translationsByLanguage ?? {} )) { const flatKeys = this.flattenTranslationPayload(payload); if (!allTranslations[locale]) { allTranslations[locale] = {}; } Object.assign(allTranslations[locale], flatKeys); } hasMore = data.pagingMetadata?.hasNext ?? false; cursor = data.pagingMetadata?.cursors?.next; } if (Object.keys(allTranslations).length === 0) { throw new CliError({ code: CliErrorCode.NoTranslationPublishedContents(), cause: null }); } return allTranslations; } catch (error) { if (error instanceof CliError) { throw error; } if (isHttpError(error) && isHttpApplicationError(error)) { this.handleApplicationError(error); } throw new CliError({ code: CliErrorCode.FailedToPullTranslations(), cause: error }); } }; convertToTranslationKeysPayload(translations) { const flatResult = flatTranslationsSchema.safeParse(translations); if (flatResult.success) { return { translationKeys: { keys: flatResult.data } }; } const namespacedResult = namespacedTranslationsSchema.parse(translations); const namespacedKeys = {}; for (const [ns, keys] of Object.entries(namespacedResult)) { namespacedKeys[ns] = { keys }; } return { namespacedTranslationKeys: { namespacedKeys } }; } flattenTranslationPayload(payload) { if (payload.translationKeys?.keys) { return payload.translationKeys.keys; } if (payload.namespacedTranslationKeys?.namespacedKeys) { const result = {}; for (const [ns, translationKeys] of Object.entries( payload.namespacedTranslationKeys.namespacedKeys )) { for (const [key, value] of Object.entries(translationKeys.keys ?? {})) { result[`${ns}:${key}`] = value; } } return result; } return {}; } handleApplicationError(error) { const applicationError = error.response?.data.details.applicationError; if (applicationError?.code === "APP_NOT_INSTALLED" && error.response?.status === 428) { throw new CliError({ code: CliErrorCode.MultilingualAppNotInstalled(), cause: error }); } throw error; } }; // ../multilingual-client/src/useMultilingualClient.ts function useMultilingualClient() { const httpClient = useHttpClient({ type: "api" }); const client = (0, import_react.useMemo)( () => new MultilingualClient(httpClient), [httpClient] ); return usePanoramaMethodLogger(client); } // ../multilingual-client/src/index.ts init_esm_shims(); export { useMultilingualClient }; //# sourceMappingURL=chunk-RY2IFAQA.js.map