UNPKG

@localazy/api-client

Version:

Official Node.js client for the Localazy API, providing a simple and type-safe way to integrate localization features into your JavaScript and TypeScript projects.

1,493 lines 115 kB
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); /* @localazy/api-client@2.1.9 * (c) 2025 Localazy <team@localazy.com> * @license MIT */ class ApiBase { constructor(api) { __publicField(this, "api"); this.api = api; } static getId(val, prop) { const id = typeof val === "string" ? val : val.id || ""; if (!id) { throw new Error(`Invalid ${prop} ID.`); } return id; } } class ApiExport extends ApiBase { /** * Export translated keys as JSON object. * * @param request Export JSON request config. * @param config Request config. */ async json(request, config) { const { project, file, langs } = request; const result = await Promise.all( langs.map((lang) => this.api.files.listKeys({ project, file, lang }, config)) ); return Object.fromEntries(ApiExport.mapLanguages(langs, result)); } static mapLanguages(languages, result) { return languages.map((lang, index) => [lang, ApiExport.mapResult(result[index])]); } static mapResult(keysList) { return keysList.reduce((acc, cur) => { const parts = cur.key; if (parts.length <= 1) { const only = parts[0]; if (only !== void 0) { acc[only] = cur.value; } return acc; } let node = acc; for (let i = 0; i < parts.length; i++) { const seg = parts[i]; const isLast = i === parts.length - 1; if (seg !== void 0) { if (isLast) { node[seg] = cur.value; } else { if (node[seg] === void 0) { node[seg] = {}; } node = node[seg]; } } } return acc; }, {}); } } class ApiFiles extends ApiBase { /** * List all {@link File files} in the project. * * @param request Files list request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/files#list-files-in-project Localazy API Docs} */ async list(request, config) { const { project } = request; const projectId = ApiBase.getId(project, "project"); return await this.api.client.get(`/projects/${projectId}/files`, config); } /** * First {@link File file} in the project. * * @param request Files list request config. * @param config Request config. * @throws Error At least one file must exist, otherwise an error is thrown. * * @see {@link https://localazy.com/docs/api/files#list-files-in-project Localazy API Docs} */ async first(request, config) { const files = await this.list(request, config); if (typeof files[0] !== "undefined") { return files[0]; } throw new Error("File not found."); } /** * List all {@link Key keys} for the language in the {@link File file}. * * @param request File list keys request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/files#retrieve-a-list-of-keys-and-translations-from-file * | Localazy API Docs} */ async listKeys(request, config) { const keys = []; let pageResult = { keys: [], next: "" }; do { pageResult = await this.listKeysPage({ ...request, next: pageResult.next }, config); keys.push(...pageResult.keys); } while (pageResult.next); return keys; } /** * List all {@link Key keys} for the language in the {@link File file}. Result is paginated. * * @param request File list keys request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/files#retrieve-a-list-of-keys-and-translations-from-file * | Localazy API Docs} */ async listKeysPage(request, config) { const { project, file, lang, ...params } = request; const projectId = ApiBase.getId(project, "project"); const fileId = ApiBase.getId(file, "file"); return await this.api.client.get(`/projects/${projectId}/files/${fileId}/keys/${lang}`, { ...config, params }); } /** * Get the contents of the {@link File file}. * * @param request File get contents request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/files#list-file-content Localazy API Docs} */ async getContents(request, config) { const { project, file, lang } = request; const projectId = ApiBase.getId(project, "project"); const fileId = ApiBase.getId(file, "file"); return await this.api.client.get(`/projects/${projectId}/files/${fileId}/download/${lang}`, { ...config, responseType: "blob" }); } } class ApiFormats extends ApiBase { /** * List all {@link Format formats} and related options. * * @param config Request config. * * @see {@link https://localazy.com/docs/api/import#list-available-file-types Localazy API Docs} */ async list(config) { return await this.api.client.get("/import/formats", config); } } class ApiGlossary extends ApiBase { /** * List all {@link GlossaryRecord glossary records} in the project. * * @param request Glossary records list request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/glossary#list-all-glossary-terms Localazy API Docs} */ async list(request, config) { const { project } = request; const projectId = ApiBase.getId(project, "project"); const response = await this.api.client.get( `/projects/${projectId}/glossary`, config ); return response.glossaries; } /** * Find {@link GlossaryRecord glossary record} specified by `id`. * * @param request Glossary record find request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/glossary#get-glossary-term Localazy API Docs} */ async find(request, config) { const { project, glossaryRecord } = request; const projectId = ApiBase.getId(project, "project"); const id = typeof glossaryRecord === "string" ? glossaryRecord : glossaryRecord.id; return await this.api.client.get(`/projects/${projectId}/glossary/${id}`, config); } /** * Create {@link GlossaryRecord glossary record}. * There is a limit of 1000 glossary records per project. * * @param request Glossary record create request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/glossary#create-new-glossary-term Localazy API Docs} */ async create(request, config) { const { project, ...data } = request; const projectId = ApiBase.getId(project, "project"); const response = await this.api.client.post( `/projects/${projectId}/glossary`, data, config ); return response.result; } /** * Update {@link GlossaryRecord glossary record} specified by `id`. * * @param request Glossary record update request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/glossary#update-glossary-term Localazy API Docs} */ async update(request, config) { const { project, glossaryRecord, ...data } = request; const projectId = ApiBase.getId(project, "project"); const id = typeof glossaryRecord === "string" ? glossaryRecord : glossaryRecord.id; await this.api.client.put(`/projects/${projectId}/glossary/${id}`, data, config); } /** * Delete {@link GlossaryRecord glossary record} specified by `id`. * * @param request Glossary record delete request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/glossary#delete-glossary-term Localazy API Docs} */ async delete(request, config) { const { project, glossaryRecord } = request; const projectId = ApiBase.getId(project, "project"); const id = typeof glossaryRecord === "string" ? glossaryRecord : glossaryRecord.id; await this.api.client.delete(`/projects/${projectId}/glossary/${id}`, config); } } const importDataFactory = (request, chunks) => ({ ...request.i18nOptions, files: [ ...chunks.map( (chunk2) => ({ name: "content.json", ...request.fileOptions, content: { type: "json", ...request.contentOptions, ...chunk2 } }) ) ] }); const delay = (ms = 150) => new Promise((resolve) => { setTimeout(resolve, ms); }); var freeGlobal = typeof global == "object" && global && global.Object === Object && global; var freeSelf = typeof self == "object" && self && self.Object === Object && self; var root = freeGlobal || freeSelf || Function("return this")(); var Symbol$1 = root.Symbol; var objectProto$9 = Object.prototype; var hasOwnProperty$7 = objectProto$9.hasOwnProperty; var nativeObjectToString$1 = objectProto$9.toString; var symToStringTag$1 = Symbol$1 ? Symbol$1.toStringTag : void 0; function getRawTag(value) { var isOwn = hasOwnProperty$7.call(value, symToStringTag$1), tag = value[symToStringTag$1]; try { value[symToStringTag$1] = void 0; var unmasked = true; } catch (e) { } var result = nativeObjectToString$1.call(value); if (unmasked) { if (isOwn) { value[symToStringTag$1] = tag; } else { delete value[symToStringTag$1]; } } return result; } var objectProto$8 = Object.prototype; var nativeObjectToString = objectProto$8.toString; function objectToString(value) { return nativeObjectToString.call(value); } var nullTag = "[object Null]", undefinedTag = "[object Undefined]"; var symToStringTag = Symbol$1 ? Symbol$1.toStringTag : void 0; function baseGetTag(value) { if (value == null) { return value === void 0 ? undefinedTag : nullTag; } return symToStringTag && symToStringTag in Object(value) ? getRawTag(value) : objectToString(value); } function isObjectLike(value) { return value != null && typeof value == "object"; } var symbolTag = "[object Symbol]"; function isSymbol(value) { return typeof value == "symbol" || isObjectLike(value) && baseGetTag(value) == symbolTag; } function arrayMap(array, iteratee) { var index = -1, length = array == null ? 0 : array.length, result = Array(length); while (++index < length) { result[index] = iteratee(array[index], index, array); } return result; } var isArray = Array.isArray; var symbolProto = Symbol$1 ? Symbol$1.prototype : void 0, symbolToString = symbolProto ? symbolProto.toString : void 0; function baseToString(value) { if (typeof value == "string") { return value; } if (isArray(value)) { return arrayMap(value, baseToString) + ""; } if (isSymbol(value)) { return symbolToString ? symbolToString.call(value) : ""; } var result = value + ""; return result == "0" && 1 / value == -Infinity ? "-0" : result; } var reWhitespace = /\s/; function trimmedEndIndex(string) { var index = string.length; while (index-- && reWhitespace.test(string.charAt(index))) { } return index; } var reTrimStart = /^\s+/; function baseTrim(string) { return string ? string.slice(0, trimmedEndIndex(string) + 1).replace(reTrimStart, "") : string; } function isObject(value) { var type = typeof value; return value != null && (type == "object" || type == "function"); } var NAN = 0 / 0; var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; var reIsBinary = /^0b[01]+$/i; var reIsOctal = /^0o[0-7]+$/i; var freeParseInt = parseInt; function toNumber(value) { if (typeof value == "number") { return value; } if (isSymbol(value)) { return NAN; } if (isObject(value)) { var other = typeof value.valueOf == "function" ? value.valueOf() : value; value = isObject(other) ? other + "" : other; } if (typeof value != "string") { return value === 0 ? value : +value; } value = baseTrim(value); var isBinary = reIsBinary.test(value); return isBinary || reIsOctal.test(value) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : reIsBadHex.test(value) ? NAN : +value; } var INFINITY = 1 / 0, MAX_INTEGER = 17976931348623157e292; function toFinite(value) { if (!value) { return value === 0 ? value : 0; } value = toNumber(value); if (value === INFINITY || value === -Infinity) { var sign = value < 0 ? -1 : 1; return sign * MAX_INTEGER; } return value === value ? value : 0; } function toInteger(value) { var result = toFinite(value), remainder = result % 1; return result === result ? remainder ? result - remainder : result : 0; } function identity(value) { return value; } var asyncTag = "[object AsyncFunction]", funcTag$1 = "[object Function]", genTag = "[object GeneratorFunction]", proxyTag = "[object Proxy]"; function isFunction(value) { if (!isObject(value)) { return false; } var tag = baseGetTag(value); return tag == funcTag$1 || tag == genTag || tag == asyncTag || tag == proxyTag; } var coreJsData = root["__core-js_shared__"]; var maskSrcKey = function() { var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || ""); return uid ? "Symbol(src)_1." + uid : ""; }(); function isMasked(func) { return !!maskSrcKey && maskSrcKey in func; } var funcProto$2 = Function.prototype; var funcToString$2 = funcProto$2.toString; function toSource(func) { if (func != null) { try { return funcToString$2.call(func); } catch (e) { } try { return func + ""; } catch (e) { } } return ""; } var reRegExpChar = /[\\^$.*+?()[\]{}|]/g; var reIsHostCtor = /^\[object .+?Constructor\]$/; var funcProto$1 = Function.prototype, objectProto$7 = Object.prototype; var funcToString$1 = funcProto$1.toString; var hasOwnProperty$6 = objectProto$7.hasOwnProperty; var reIsNative = RegExp( "^" + funcToString$1.call(hasOwnProperty$6).replace(reRegExpChar, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$" ); function baseIsNative(value) { if (!isObject(value) || isMasked(value)) { return false; } var pattern = isFunction(value) ? reIsNative : reIsHostCtor; return pattern.test(toSource(value)); } function getValue(object, key) { return object == null ? void 0 : object[key]; } function getNative(object, key) { var value = getValue(object, key); return baseIsNative(value) ? value : void 0; } var objectCreate = Object.create; var baseCreate = /* @__PURE__ */ function() { function object() { } return function(proto) { if (!isObject(proto)) { return {}; } if (objectCreate) { return objectCreate(proto); } object.prototype = proto; var result = new object(); object.prototype = void 0; return result; }; }(); function apply(func, thisArg, args) { switch (args.length) { case 0: return func.call(thisArg); case 1: return func.call(thisArg, args[0]); case 2: return func.call(thisArg, args[0], args[1]); case 3: return func.call(thisArg, args[0], args[1], args[2]); } return func.apply(thisArg, args); } function copyArray(source, array) { var index = -1, length = source.length; array || (array = Array(length)); while (++index < length) { array[index] = source[index]; } return array; } var HOT_COUNT = 800, HOT_SPAN = 16; var nativeNow = Date.now; function shortOut(func) { var count = 0, lastCalled = 0; return function() { var stamp = nativeNow(), remaining = HOT_SPAN - (stamp - lastCalled); lastCalled = stamp; if (remaining > 0) { if (++count >= HOT_COUNT) { return arguments[0]; } } else { count = 0; } return func.apply(void 0, arguments); }; } function constant(value) { return function() { return value; }; } var defineProperty = function() { try { var func = getNative(Object, "defineProperty"); func({}, "", {}); return func; } catch (e) { } }(); var baseSetToString = !defineProperty ? identity : function(func, string) { return defineProperty(func, "toString", { "configurable": true, "enumerable": false, "value": constant(string), "writable": true }); }; var setToString = shortOut(baseSetToString); var MAX_SAFE_INTEGER$1 = 9007199254740991; var reIsUint = /^(?:0|[1-9]\d*)$/; function isIndex(value, length) { var type = typeof value; length = length == null ? MAX_SAFE_INTEGER$1 : length; return !!length && (type == "number" || type != "symbol" && reIsUint.test(value)) && (value > -1 && value % 1 == 0 && value < length); } function baseAssignValue(object, key, value) { if (key == "__proto__" && defineProperty) { defineProperty(object, key, { "configurable": true, "enumerable": true, "value": value, "writable": true }); } else { object[key] = value; } } function eq(value, other) { return value === other || value !== value && other !== other; } var objectProto$6 = Object.prototype; var hasOwnProperty$5 = objectProto$6.hasOwnProperty; function assignValue(object, key, value) { var objValue = object[key]; if (!(hasOwnProperty$5.call(object, key) && eq(objValue, value)) || value === void 0 && !(key in object)) { baseAssignValue(object, key, value); } } function copyObject(source, props, object, customizer) { var isNew = !object; object || (object = {}); var index = -1, length = props.length; while (++index < length) { var key = props[index]; var newValue = void 0; if (newValue === void 0) { newValue = source[key]; } if (isNew) { baseAssignValue(object, key, newValue); } else { assignValue(object, key, newValue); } } return object; } var nativeMax$1 = Math.max; function overRest(func, start, transform) { start = nativeMax$1(start === void 0 ? func.length - 1 : start, 0); return function() { var args = arguments, index = -1, length = nativeMax$1(args.length - start, 0), array = Array(length); while (++index < length) { array[index] = args[start + index]; } index = -1; var otherArgs = Array(start + 1); while (++index < start) { otherArgs[index] = args[index]; } otherArgs[start] = transform(array); return apply(func, this, otherArgs); }; } function baseRest(func, start) { return setToString(overRest(func, start, identity), func + ""); } var MAX_SAFE_INTEGER = 9007199254740991; function isLength(value) { return typeof value == "number" && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER; } function isArrayLike(value) { return value != null && isLength(value.length) && !isFunction(value); } function isIterateeCall(value, index, object) { if (!isObject(object)) { return false; } var type = typeof index; if (type == "number" ? isArrayLike(object) && isIndex(index, object.length) : type == "string" && index in object) { return eq(object[index], value); } return false; } function createAssigner(assigner) { return baseRest(function(object, sources) { var index = -1, length = sources.length, customizer = length > 1 ? sources[length - 1] : void 0, guard = length > 2 ? sources[2] : void 0; customizer = assigner.length > 3 && typeof customizer == "function" ? (length--, customizer) : void 0; if (guard && isIterateeCall(sources[0], sources[1], guard)) { customizer = length < 3 ? void 0 : customizer; length = 1; } object = Object(object); while (++index < length) { var source = sources[index]; if (source) { assigner(object, source, index, customizer); } } return object; }); } var objectProto$5 = Object.prototype; function isPrototype(value) { var Ctor = value && value.constructor, proto = typeof Ctor == "function" && Ctor.prototype || objectProto$5; return value === proto; } function baseTimes(n, iteratee) { var index = -1, result = Array(n); while (++index < n) { result[index] = iteratee(index); } return result; } var argsTag$1 = "[object Arguments]"; function baseIsArguments(value) { return isObjectLike(value) && baseGetTag(value) == argsTag$1; } var objectProto$4 = Object.prototype; var hasOwnProperty$4 = objectProto$4.hasOwnProperty; var propertyIsEnumerable = objectProto$4.propertyIsEnumerable; var isArguments = baseIsArguments(/* @__PURE__ */ function() { return arguments; }()) ? baseIsArguments : function(value) { return isObjectLike(value) && hasOwnProperty$4.call(value, "callee") && !propertyIsEnumerable.call(value, "callee"); }; function stubFalse() { return false; } var freeExports$2 = typeof exports == "object" && exports && !exports.nodeType && exports; var freeModule$2 = freeExports$2 && typeof module == "object" && module && !module.nodeType && module; var moduleExports$2 = freeModule$2 && freeModule$2.exports === freeExports$2; var Buffer$1 = moduleExports$2 ? root.Buffer : void 0; var nativeIsBuffer = Buffer$1 ? Buffer$1.isBuffer : void 0; var isBuffer = nativeIsBuffer || stubFalse; var argsTag = "[object Arguments]", arrayTag = "[object Array]", boolTag = "[object Boolean]", dateTag = "[object Date]", errorTag = "[object Error]", funcTag = "[object Function]", mapTag = "[object Map]", numberTag = "[object Number]", objectTag$1 = "[object Object]", regexpTag = "[object RegExp]", setTag = "[object Set]", stringTag = "[object String]", weakMapTag = "[object WeakMap]"; var arrayBufferTag = "[object ArrayBuffer]", dataViewTag = "[object DataView]", float32Tag = "[object Float32Array]", float64Tag = "[object Float64Array]", int8Tag = "[object Int8Array]", int16Tag = "[object Int16Array]", int32Tag = "[object Int32Array]", uint8Tag = "[object Uint8Array]", uint8ClampedTag = "[object Uint8ClampedArray]", uint16Tag = "[object Uint16Array]", uint32Tag = "[object Uint32Array]"; var typedArrayTags = {}; typedArrayTags[float32Tag] = typedArrayTags[float64Tag] = typedArrayTags[int8Tag] = typedArrayTags[int16Tag] = typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] = typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] = typedArrayTags[uint32Tag] = true; typedArrayTags[argsTag] = typedArrayTags[arrayTag] = typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] = typedArrayTags[dataViewTag] = typedArrayTags[dateTag] = typedArrayTags[errorTag] = typedArrayTags[funcTag] = typedArrayTags[mapTag] = typedArrayTags[numberTag] = typedArrayTags[objectTag$1] = typedArrayTags[regexpTag] = typedArrayTags[setTag] = typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false; function baseIsTypedArray(value) { return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[baseGetTag(value)]; } function baseUnary(func) { return function(value) { return func(value); }; } var freeExports$1 = typeof exports == "object" && exports && !exports.nodeType && exports; var freeModule$1 = freeExports$1 && typeof module == "object" && module && !module.nodeType && module; var moduleExports$1 = freeModule$1 && freeModule$1.exports === freeExports$1; var freeProcess = moduleExports$1 && freeGlobal.process; var nodeUtil = function() { try { var types = freeModule$1 && freeModule$1.require && freeModule$1.require("util").types; if (types) { return types; } return freeProcess && freeProcess.binding && freeProcess.binding("util"); } catch (e) { } }(); var nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray; var isTypedArray = nodeIsTypedArray ? baseUnary(nodeIsTypedArray) : baseIsTypedArray; function arrayLikeKeys(value, inherited) { var isArr = isArray(value), isArg = !isArr && isArguments(value), isBuff = !isArr && !isArg && isBuffer(value), isType = !isArr && !isArg && !isBuff && isTypedArray(value), skipIndexes = isArr || isArg || isBuff || isType, result = skipIndexes ? baseTimes(value.length, String) : [], length = result.length; for (var key in value) { if (!(skipIndexes && // Safari 9 has enumerable `arguments.length` in strict mode. (key == "length" || // Node.js 0.10 has enumerable non-index properties on buffers. isBuff && (key == "offset" || key == "parent") || // PhantomJS 2 has enumerable non-index properties on typed arrays. isType && (key == "buffer" || key == "byteLength" || key == "byteOffset") || // Skip index properties. isIndex(key, length)))) { result.push(key); } } return result; } function overArg(func, transform) { return function(arg) { return func(transform(arg)); }; } function nativeKeysIn(object) { var result = []; if (object != null) { for (var key in Object(object)) { result.push(key); } } return result; } var objectProto$3 = Object.prototype; var hasOwnProperty$3 = objectProto$3.hasOwnProperty; function baseKeysIn(object) { if (!isObject(object)) { return nativeKeysIn(object); } var isProto = isPrototype(object), result = []; for (var key in object) { if (!(key == "constructor" && (isProto || !hasOwnProperty$3.call(object, key)))) { result.push(key); } } return result; } function keysIn(object) { return isArrayLike(object) ? arrayLikeKeys(object) : baseKeysIn(object); } var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/, reIsPlainProp = /^\w*$/; function isKey(value, object) { if (isArray(value)) { return false; } var type = typeof value; if (type == "number" || type == "symbol" || type == "boolean" || value == null || isSymbol(value)) { return true; } return reIsPlainProp.test(value) || !reIsDeepProp.test(value) || object != null && value in Object(object); } var nativeCreate = getNative(Object, "create"); function hashClear() { this.__data__ = nativeCreate ? nativeCreate(null) : {}; this.size = 0; } function hashDelete(key) { var result = this.has(key) && delete this.__data__[key]; this.size -= result ? 1 : 0; return result; } var HASH_UNDEFINED$1 = "__lodash_hash_undefined__"; var objectProto$2 = Object.prototype; var hasOwnProperty$2 = objectProto$2.hasOwnProperty; function hashGet(key) { var data = this.__data__; if (nativeCreate) { var result = data[key]; return result === HASH_UNDEFINED$1 ? void 0 : result; } return hasOwnProperty$2.call(data, key) ? data[key] : void 0; } var objectProto$1 = Object.prototype; var hasOwnProperty$1 = objectProto$1.hasOwnProperty; function hashHas(key) { var data = this.__data__; return nativeCreate ? data[key] !== void 0 : hasOwnProperty$1.call(data, key); } var HASH_UNDEFINED = "__lodash_hash_undefined__"; function hashSet(key, value) { var data = this.__data__; this.size += this.has(key) ? 0 : 1; data[key] = nativeCreate && value === void 0 ? HASH_UNDEFINED : value; return this; } function Hash(entries) { var index = -1, length = entries == null ? 0 : entries.length; this.clear(); while (++index < length) { var entry = entries[index]; this.set(entry[0], entry[1]); } } Hash.prototype.clear = hashClear; Hash.prototype["delete"] = hashDelete; Hash.prototype.get = hashGet; Hash.prototype.has = hashHas; Hash.prototype.set = hashSet; function listCacheClear() { this.__data__ = []; this.size = 0; } function assocIndexOf(array, key) { var length = array.length; while (length--) { if (eq(array[length][0], key)) { return length; } } return -1; } var arrayProto = Array.prototype; var splice = arrayProto.splice; function listCacheDelete(key) { var data = this.__data__, index = assocIndexOf(data, key); if (index < 0) { return false; } var lastIndex = data.length - 1; if (index == lastIndex) { data.pop(); } else { splice.call(data, index, 1); } --this.size; return true; } function listCacheGet(key) { var data = this.__data__, index = assocIndexOf(data, key); return index < 0 ? void 0 : data[index][1]; } function listCacheHas(key) { return assocIndexOf(this.__data__, key) > -1; } function listCacheSet(key, value) { var data = this.__data__, index = assocIndexOf(data, key); if (index < 0) { ++this.size; data.push([key, value]); } else { data[index][1] = value; } return this; } function ListCache(entries) { var index = -1, length = entries == null ? 0 : entries.length; this.clear(); while (++index < length) { var entry = entries[index]; this.set(entry[0], entry[1]); } } ListCache.prototype.clear = listCacheClear; ListCache.prototype["delete"] = listCacheDelete; ListCache.prototype.get = listCacheGet; ListCache.prototype.has = listCacheHas; ListCache.prototype.set = listCacheSet; var Map = getNative(root, "Map"); function mapCacheClear() { this.size = 0; this.__data__ = { "hash": new Hash(), "map": new (Map || ListCache)(), "string": new Hash() }; } function isKeyable(value) { var type = typeof value; return type == "string" || type == "number" || type == "symbol" || type == "boolean" ? value !== "__proto__" : value === null; } function getMapData(map, key) { var data = map.__data__; return isKeyable(key) ? data[typeof key == "string" ? "string" : "hash"] : data.map; } function mapCacheDelete(key) { var result = getMapData(this, key)["delete"](key); this.size -= result ? 1 : 0; return result; } function mapCacheGet(key) { return getMapData(this, key).get(key); } function mapCacheHas(key) { return getMapData(this, key).has(key); } function mapCacheSet(key, value) { var data = getMapData(this, key), size = data.size; data.set(key, value); this.size += data.size == size ? 0 : 1; return this; } function MapCache(entries) { var index = -1, length = entries == null ? 0 : entries.length; this.clear(); while (++index < length) { var entry = entries[index]; this.set(entry[0], entry[1]); } } MapCache.prototype.clear = mapCacheClear; MapCache.prototype["delete"] = mapCacheDelete; MapCache.prototype.get = mapCacheGet; MapCache.prototype.has = mapCacheHas; MapCache.prototype.set = mapCacheSet; var FUNC_ERROR_TEXT = "Expected a function"; function memoize(func, resolver) { if (typeof func != "function" || resolver != null && typeof resolver != "function") { throw new TypeError(FUNC_ERROR_TEXT); } var memoized = function() { var args = arguments, key = resolver ? resolver.apply(this, args) : args[0], cache = memoized.cache; if (cache.has(key)) { return cache.get(key); } var result = func.apply(this, args); memoized.cache = cache.set(key, result) || cache; return result; }; memoized.cache = new (memoize.Cache || MapCache)(); return memoized; } memoize.Cache = MapCache; var MAX_MEMOIZE_SIZE = 500; function memoizeCapped(func) { var result = memoize(func, function(key) { if (cache.size === MAX_MEMOIZE_SIZE) { cache.clear(); } return key; }); var cache = result.cache; return result; } var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g; var reEscapeChar = /\\(\\)?/g; var stringToPath = memoizeCapped(function(string) { var result = []; if (string.charCodeAt(0) === 46) { result.push(""); } string.replace(rePropName, function(match, number, quote, subString) { result.push(quote ? subString.replace(reEscapeChar, "$1") : number || match); }); return result; }); function toString(value) { return value == null ? "" : baseToString(value); } function castPath(value, object) { if (isArray(value)) { return value; } return isKey(value, object) ? [value] : stringToPath(toString(value)); } function toKey(value) { if (typeof value == "string" || isSymbol(value)) { return value; } var result = value + ""; return result == "0" && 1 / value == -Infinity ? "-0" : result; } var getPrototype = overArg(Object.getPrototypeOf, Object); var objectTag = "[object Object]"; var funcProto = Function.prototype, objectProto = Object.prototype; var funcToString = funcProto.toString; var hasOwnProperty = objectProto.hasOwnProperty; var objectCtorString = funcToString.call(Object); function isPlainObject(value) { if (!isObjectLike(value) || baseGetTag(value) != objectTag) { return false; } var proto = getPrototype(value); if (proto === null) { return true; } var Ctor = hasOwnProperty.call(proto, "constructor") && proto.constructor; return typeof Ctor == "function" && Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString; } function baseSlice(array, start, end) { var index = -1, length = array.length; if (start < 0) { start = -start > length ? 0 : length + start; } end = end > length ? length : end; if (end < 0) { end += length; } length = start > end ? 0 : end - start >>> 0; start >>>= 0; var result = Array(length); while (++index < length) { result[index] = array[index + start]; } return result; } var nativeCeil = Math.ceil, nativeMax = Math.max; function chunk(array, size, guard) { if (size === void 0) { size = 1; } else { size = nativeMax(toInteger(size), 0); } var length = array == null ? 0 : array.length; if (!length || size < 1) { return []; } var index = 0, resIndex = 0, result = Array(nativeCeil(length / size)); while (index < length) { result[resIndex++] = baseSlice(array, index, index += size); } return result; } function stackClear() { this.__data__ = new ListCache(); this.size = 0; } function stackDelete(key) { var data = this.__data__, result = data["delete"](key); this.size = data.size; return result; } function stackGet(key) { return this.__data__.get(key); } function stackHas(key) { return this.__data__.has(key); } var LARGE_ARRAY_SIZE = 200; function stackSet(key, value) { var data = this.__data__; if (data instanceof ListCache) { var pairs = data.__data__; if (!Map || pairs.length < LARGE_ARRAY_SIZE - 1) { pairs.push([key, value]); this.size = ++data.size; return this; } data = this.__data__ = new MapCache(pairs); } data.set(key, value); this.size = data.size; return this; } function Stack(entries) { var data = this.__data__ = new ListCache(entries); this.size = data.size; } Stack.prototype.clear = stackClear; Stack.prototype["delete"] = stackDelete; Stack.prototype.get = stackGet; Stack.prototype.has = stackHas; Stack.prototype.set = stackSet; var freeExports = typeof exports == "object" && exports && !exports.nodeType && exports; var freeModule = freeExports && typeof module == "object" && module && !module.nodeType && module; var moduleExports = freeModule && freeModule.exports === freeExports; var Buffer2 = moduleExports ? root.Buffer : void 0; Buffer2 ? Buffer2.allocUnsafe : void 0; function cloneBuffer(buffer, isDeep) { { return buffer.slice(); } } var Uint8Array2 = root.Uint8Array; function cloneArrayBuffer(arrayBuffer) { var result = new arrayBuffer.constructor(arrayBuffer.byteLength); new Uint8Array2(result).set(new Uint8Array2(arrayBuffer)); return result; } function cloneTypedArray(typedArray, isDeep) { var buffer = cloneArrayBuffer(typedArray.buffer); return new typedArray.constructor(buffer, typedArray.byteOffset, typedArray.length); } function initCloneObject(object) { return typeof object.constructor == "function" && !isPrototype(object) ? baseCreate(getPrototype(object)) : {}; } function createBaseFor(fromRight) { return function(object, iteratee, keysFunc) { var index = -1, iterable = Object(object), props = keysFunc(object), length = props.length; while (length--) { var key = props[++index]; if (iteratee(iterable[key], key, iterable) === false) { break; } } return object; }; } var baseFor = createBaseFor(); function assignMergeValue(object, key, value) { if (value !== void 0 && !eq(object[key], value) || value === void 0 && !(key in object)) { baseAssignValue(object, key, value); } } function isArrayLikeObject(value) { return isObjectLike(value) && isArrayLike(value); } function safeGet(object, key) { if (key === "constructor" && typeof object[key] === "function") { return; } if (key == "__proto__") { return; } return object[key]; } function toPlainObject(value) { return copyObject(value, keysIn(value)); } function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) { var objValue = safeGet(object, key), srcValue = safeGet(source, key), stacked = stack.get(srcValue); if (stacked) { assignMergeValue(object, key, stacked); return; } var newValue = customizer ? customizer(objValue, srcValue, key + "", object, source, stack) : void 0; var isCommon = newValue === void 0; if (isCommon) { var isArr = isArray(srcValue), isBuff = !isArr && isBuffer(srcValue), isTyped = !isArr && !isBuff && isTypedArray(srcValue); newValue = srcValue; if (isArr || isBuff || isTyped) { if (isArray(objValue)) { newValue = objValue; } else if (isArrayLikeObject(objValue)) { newValue = copyArray(objValue); } else if (isBuff) { isCommon = false; newValue = cloneBuffer(srcValue); } else if (isTyped) { isCommon = false; newValue = cloneTypedArray(srcValue); } else { newValue = []; } } else if (isPlainObject(srcValue) || isArguments(srcValue)) { newValue = objValue; if (isArguments(objValue)) { newValue = toPlainObject(objValue); } else if (!isObject(objValue) || isFunction(objValue)) { newValue = initCloneObject(srcValue); } } else { isCommon = false; } } if (isCommon) { stack.set(srcValue, newValue); mergeFunc(newValue, srcValue, srcIndex, customizer, stack); stack["delete"](srcValue); } assignMergeValue(object, key, newValue); } function baseMerge(object, source, srcIndex, customizer, stack) { if (object === source) { return; } baseFor(source, function(srcValue, key) { stack || (stack = new Stack()); if (isObject(srcValue)) { baseMergeDeep(object, source, key, srcIndex, baseMerge, customizer, stack); } else { var newValue = customizer ? customizer(safeGet(object, key), srcValue, key + "", object, source, stack) : void 0; if (newValue === void 0) { newValue = srcValue; } assignMergeValue(object, key, newValue); } }, keysIn); } var merge = createAssigner(function(object, source, srcIndex) { baseMerge(object, source, srcIndex); }); function baseSet(object, path, value, customizer) { if (!isObject(object)) { return object; } path = castPath(path, object); var index = -1, length = path.length, lastIndex = length - 1, nested = object; while (nested != null && ++index < length) { var key = toKey(path[index]), newValue = value; if (key === "__proto__" || key === "constructor" || key === "prototype") { return object; } if (index != lastIndex) { var objValue = nested[key]; newValue = customizer ? customizer(objValue, key, nested) : void 0; if (newValue === void 0) { newValue = isObject(objValue) ? objValue : isIndex(path[index + 1]) ? [] : {}; } } assignValue(nested, key, newValue); nested = nested[key]; } return object; } function setWith(object, path, value, customizer) { customizer = typeof customizer == "function" ? customizer : void 0; return object == null ? object : baseSet(object, path, value, customizer); } const _JsonUtils = class _JsonUtils { static slice(json) { const values = _JsonUtils.sliceByValue(json); const chunks = chunk(values, _JsonUtils.CHUNK_LIMIT); return chunks.map((c) => _JsonUtils.mergeChunkValues(c)); } /** * Example input: * const json = { * en: { * headers: { * name: 'Name', * user: 'User', * }, * 'headers.company': 'Company', * user: { * role: ['Admin', 'Editor'], * }, * }, * }; * * Example result: * const result = [ * { en: { headers: { name: 'Name' } } }, * { en: { headers: { user: 'User' } } }, * { en: { 'headers.company': 'Company' } }, * { en: { user: { role: ['Admin', 'Editor'] } } }, * ]; */ static sliceByValue(json, keys = []) { return Object.entries(json).reduce((prev, [key, value]) => { if (isPlainObject(value)) { prev.push(..._JsonUtils.sliceByValue(value, [...keys, key])); } else if (keys.length > 1) { prev.push(setWith({}, [...keys, key].join("."), value, Object)); } else if (typeof keys[0] !== "undefined") { prev.push({ [keys[0]]: { [key]: value } }); } return prev; }, []); } static mergeChunkValues(values) { return merge(...values); } }; /** * Physical limit is 100000. */ __publicField(_JsonUtils, "CHUNK_LIMIT", 99e3); let JsonUtils = _JsonUtils; class ApiImport extends ApiBase { /** * Import source keys from JSON object. * * @param request Import JSON request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/import#import-content-to-a-project Localazy API Docs} */ async json(request, config) { const { project, json } = request; const projectId = ApiBase.getId(project, "project"); const chunks = JsonUtils.slice(json); const data = importDataFactory(request, chunks); const { result } = await this.api.client.post( `/projects/${projectId}/import`, data, config ); await delay(); return this.getImportedFile(project, data, result); } /** * Get progress of the import session. * * @param request Import session progress request. * @param config Request config. * * Not available in the Localazy API Docs yet. */ async getProgress(request, config) { const { project, importBatch } = request; const projectId = ApiBase.getId(project, "project"); return await this.api.client.get(`/projects/${projectId}/import/${importBatch}`, config); } async getImportedFile(project, data, importBatch) { const files = await this.api.files.list({ project }); const file = files.find( (f) => data.files.some( (importFile) => f.name === (importFile.name || "content.json") && f.path === importFile.path ) ); return { ...file, importBatch }; } } class ApiKeys extends ApiBase { /** * Update {@link Key key}. * * @param request Key update request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/source-keys#update-source-key Localazy API Docs} */ async update(request, config) { const { project, key, ...data } = request; const projectId = ApiBase.getId(project, "project"); const keyId = ApiBase.getId(key, "key"); await this.api.client.put(`/projects/${projectId}/keys/${keyId}`, data, config); } /** * Delete {@link Key key}. * * @param request Key delete request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/source-keys#delete-source-key Localazy API Docs} */ async delete(request, config) { const { project, key } = request; const projectId = ApiBase.getId(project, "project"); const keyId = ApiBase.getId(key, "key"); await this.api.client.delete(`/projects/${projectId}/keys/${keyId}`, config); } /** * Deprecate keys. * * @param request Key deprecate request config. * @param config Request config. */ async deprecate(request, config) { const { project, phrases } = request; const localPhrases = phrases.map((phrase) => { if (typeof phrase === "object" && "id" in phrase) { return phrase.id; } if (typeof phrase === "string") { return phrase; } return phrase; }); const projectId = ApiBase.getId(project, "project"); await this.api.client.post(`/projects/${projectId}/keys/deprecate`, { phrases: localPhrases }, config); } } class ApiProjects extends ApiBase { /** * List all {@link Project projects}. * * @param request Projects list request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/projects#list-projects Localazy API Docs} */ async list(request, config) { return await this.api.client.get("/projects", { ...config, params: request }); } /** * First {@link Project project}. * * @param request Projects list request config. * @param config Request config. * @throws Error At least one project must exist, otherwise an error is thrown. * * @see {@link https://localazy.com/docs/api/projects#list-projects Localazy API Docs} */ async first(request, config) { const projects = await this.list(request, config); if (typeof projects[0] !== "undefined") { return projects[0]; } throw new Error("Project not found."); } } class ApiScreenshots extends ApiBase { /** * List all {@link Screenshot screenshots} in the project. * * @param request Screenshots list request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/screenshot-management#list-screenshots Localazy API Docs} */ async list(request, config) { const { project } = request; const projectId = ApiBase.getId(project, "project"); return await this.api.client.get(`/projects/${projectId}/screenshots`, config); } /** * List all {@link ScreenshotTag screenshots tags} in the project. * * @param request Screenshots list tags request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/screenshot-management#list-screenshots-tags Localazy API Docs} */ async listTags(request, config) { const { project } = request; const projectId = ApiBase.getId(project, "project"); return await this.api.client.get(`/projects/${projectId}/screenshots/tags`, config); } /** * Create {@link Screenshot screenshot}. * * @param request Screenshot create request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/screenshot-management#create-a-new-screenshot Localazy API Docs} */ async create(request, config) { const { project } = request; const projectId = ApiBase.getId(project, "project"); const response = await this.api.client.post( `/projects/${projectId}/screenshots`, request.encodedData, config ); return response.id; } /** * Update the image data of {@link Screenshot screenshot}. * * @param request Screenshot update image data request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/screenshot-management#update-the-image-of-an-existing-screenshot * | Localazy API Docs} */ async updateImageData(request, config) { const { project, screenshot } = request; const projectId = ApiBase.getId(project, "project"); const screenshotId = ApiBase.getId(screenshot, "screenshot"); await this.api.client.post(`/projects/${projectId}/screenshots/${screenshotId}`, request.encodedData, config); } /** * Update {@link Screenshot screenshot}. * Image data are updated with `screenshots.updateImageData`. * * @param request Screenshot update request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/screenshot-management#update-an-existing-screenshot Localazy API Docs} */ async update(request, config) { const { project, screenshot, ...data } = request; const projectId = ApiBase.getId(project, "project"); const screenshotId = ApiBase.getId(screenshot, "screenshot"); await this.api.client.put(`/projects/${projectId}/screenshots/${screenshotId}`, data, config); } /** * Delete {@link Screenshot screenshot}. * * @param request Screenshot delete request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/screenshot-management#delete-a-screenshot Localazy API Docs} */ async delete(request, config) { const { project, screenshot } = request; const projectId = ApiBase.getId(project, "project"); const screenshotId = ApiBase.getId(screenshot, "screenshot"); await this.api.client.delete(`/projects/${projectId}/screenshots/${screenshotId}`, config); } } class ApiWebhooks extends ApiBase { /** * List all {@link Webhook webhooks} in the project. * * @param request Webhooks list request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/webhooks-api#list-webhooks-configuration Localazy API Docs} */ async list(request, config) { const { project } = request; const projectId = ApiBase.getId(project, "project"); const response = await this.api.client.get(`/projects/${projectId}/webhooks`, config); return response.items; } /** * Update all {@link Webhook webhooks} in the project. * * @param request Webhooks update request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/webhooks-api#update-webhooks-configuration Localazy API Docs} */ async update(request, config) { const { project, ...data } = request; const projectId = ApiBase.getId(project, "project"); await this.api.client.post(`/projects/${projectId}/webhooks`, data, config); } /** * Get secret for {@link Webhook webhooks} in the project. * Localazy signs the webhook events it sends to your endpoints and adds a signature in the request header * {@link https://localazy.com/docs/api/webhooks-api#security}. * * @param request Webhooks get secret request config. * @param config Request config. * * @see {@link https://localazy.com/docs/api/webhooks-api#webhook-secrets Localazy API Docs} */ async getSecret(request, config) { const { project } = request; const projectId = ApiBase.getId(project, "project"); const r