@dragonaxe/i18n
Version:
A tool for i18n content.
178 lines (177 loc) • 5.16 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
import { createClient } from "@prismicio/client";
class PrismicMultilangualCdnService {
constructor(_client) {
__publicField(this, "_locales");
__publicField(this, "_selectedLocale");
__publicField(this, "_getResourceWithId", async (key, locale) => {
try {
const { doctype, uid } = this._parseKey(key);
if (!uid) {
return await this._getPageDoc(key, locale);
}
const document = await this._client.getByUID(doctype, uid, {
lang: locale
});
return document.data;
} catch (_) {
return void 0;
}
});
__publicField(this, "_getResourceField", async (key, locale) => {
try {
const { doctype, uid, field } = this._parseFieldKey(key);
const document = await this._client.getByUID(doctype, uid, {
lang: locale
});
const value = document.data[field];
return value;
} catch (_) {
return void 0;
}
});
__publicField(this, "_parseFieldKey", (key) => {
const [doctype, uid, field] = key.split(".");
const doctypeT = doctype;
return { doctype: doctypeT, uid, field };
});
__publicField(this, "_parseKey", (key) => {
const [doctype, uid] = key.split(".");
const doctypeT = doctype;
return { doctype: doctypeT, uid };
});
__publicField(this, "_getPageDoc", async (name, userLocale) => {
const document = await this._client.getSingle(name, {
lang: userLocale
});
if (!document) {
return void 0;
}
return document.data;
});
__publicField(this, "_convertResultToDocuments", (values) => {
const docs = values.map((value) => {
const d = value.data;
return d;
});
return docs;
});
this._client = _client;
}
async getLocales() {
const repo = await this._client.getRepository();
const localeDtos = repo.languages;
const localeInfos = [];
for (const localeDto of localeDtos) {
const containsLocale = this._hasLocale(
{
id: localeDto.id,
name: localeDto.name
},
localeInfos
);
if (!containsLocale) {
localeInfos.push({
id: localeDto.id,
name: localeDto.name
});
}
}
return localeInfos;
}
_hasLocale(locale, arr) {
return arr.some((l) => l.id === locale.id);
}
async _tryInit() {
if (!this._locales) {
this._locales = await this.getLocales();
}
if (!this._selectedLocale) {
this._selectedLocale = this._locales[0].id;
}
}
async setLocale(localeId) {
this._selectedLocale = localeId;
}
async getLocale() {
var _a;
await this._tryInit();
const id = this._selectedLocale;
const locale = (_a = this._locales) == null ? void 0 : _a.find((l) => l.id === id);
if (!locale) {
throw new Error(`Locale ${id} is not found`);
}
return locale;
}
/**
* @description
* format of the key to get the field: type.uid.fieldName
* format of the key to get the document: type.uid
* The type represent the Prismic type of the document holding the string field.
*/
async getString(key, locale) {
return await this.getResource(key, locale);
}
/**
* @description
* Will fetch all the resources of a given type.
* format of the key to get the field: type
*/
async getResources(key, locale, limit) {
try {
const params = {
lang: locale,
limit
};
const results = await this._client.getAllByType(key, params);
return this._convertResultToDocuments(results);
} catch (_) {
return [];
}
}
/**
* @description
* Will fetch a resource of a given type, with a uid. If the field is provided, that particular field will be fetched.
* format of the key to get the field: type.uid.fieldName
* format of the key to get the document: type.uid
*/
async getResource(key, locale) {
const keyType = this._getKeyType(key);
if (keyType === "field") {
return this._getResourceField(key, locale);
} else {
return this._getResourceWithId(key, locale) || this._getResourceField(key, locale);
}
}
_getKeyType(key) {
const items = key.split(".");
if (items.length === 2) {
return "document";
}
return "field";
}
}
class PrismicClientFactory {
constructor() {
__publicField(this, "getClient", (repoName, publicToken) => {
return createClient(repoName, {
accessToken: publicToken
// fetchOptions: {
// cache: 'no-store',
// headers: {
// 'access-control-allow-origin': '*',
// },
// },
});
});
}
}
export {
PrismicMultilangualCdnService as P,
PrismicClientFactory as a
};