UNPKG

@etsoo/smarterp-core

Version:

TypeScript APIs for SmartERP Core

234 lines (233 loc) 6.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PublicApi = void 0; const shared_1 = require("@etsoo/shared"); const appscript_1 = require("@etsoo/appscript"); const unitPrefix = "unit"; /** * Public API * 公共接口 */ class PublicApi extends appscript_1.BaseApi { /** * Constructor * @param app Application * @param api API */ constructor(app, api = app.api) { super(app, api); } /** * Accept invitation * @param rq Request data * @param payload Payload * @returns Result */ acceptInvitation(rq, payload) { return this.api.post("Public/AcceptInvitation", rq, payload); } /** * Create barcode * @param rq Request data * @param payload Payload * @returns Result */ createBarcode(rq, payload) { return this.api.post("Public/CreateBarcode", rq, payload); } /** * Get currency label * @param currency Currency * @returns Label */ getCurrencyLabel(currency) { const c = `currency${currency}`; return this.app.get(c) ?? c; } /** * Get cultures * @param ids Culture ids to include and order by * @param payload Payload * @returns Result */ getCultures(ids, payload) { return this.api.post("Public/GetCultures", ids, { ...payload, contentType: "application/json" }); } /** * Get currencies * @param ids Currency ids to include and order by * @param payload Payload * @returns Result */ getCurrencies(ids, payload) { return this.api.post("Public/GetCurrencies", ids, { ...payload, contentType: "application/json" }); } /** * Get custom resources * @param culture Culture * @param payload Payload * @returns Result */ getCustomResources(culture, payload) { return this.api.get(`Public/GetCustomResources/${culture}`, undefined, payload); } /** * Get Pinyin * @param rq Request data * @param payload Payload * @returns Result */ getPinyin(rq, payload) { return this.api.post("Public/GetPinyin", rq, payload); } /** * Get regions * @param ids Region ids to include and order by * @param payload Payload * @returns Result */ getRegions(ids, payload) { return this.api.post("Public/GetRegions", ids, { ...payload, contentType: "application/json" }); } /** * Get product unit's label * Please define the label in culture with key 'unitPC' for ProductUnit.PC like that * @param unit Unit * @param isJoined Add the join label like 'per Kg' for Kg * @returns Label */ getUnitLabel(unit, isJoined) { const key = appscript_1.ProductUnit[unit]; const label = this.app.get(unitPrefix + key) ?? key; const join = this.getUnitJoin(isJoined); if (join) { return join.format(label); } return label; } getUnitJoin(isJoined) { return typeof isJoined === "string" ? this.app.get(isJoined) ?? isJoined : isJoined ? this.app.get("unitJoin") : undefined; } /** * Get mobile base64 QRCode * @param id User id * @param host Host URL * @param payload Payload */ mobileQRCode(id, host, payload) { return this.api.post("Public/MobileQRCode", { id, host }, payload); } /** * Parse China PIN * @param pin Pin code * @param payload Payload * @returns Result */ parseChinaPin(pin, payload) { return this.api.get(`Public/ParseChinaPin/${pin}`, undefined, payload); } /** * Parse name * @param rq Request data * @param payload Payload * @returns Result */ parseName(rq, payload) { return this.api.post("Public/ParseName", rq, payload); } /** * Query place * @param rq Request data * @param payload Payload * @returns Result */ async queryPlace(rq, payload) { // For simplified Chinese, or CN region, use China map if (!rq.provider && (rq.language === "zh-Hans" || rq.language === "zh-CN" || rq.region === "CN")) { // Amap first const amapRq = { ...rq, region: undefined, provider: appscript_1.MapApiProvider.Amap }; const amapResult = await this.api.post("Public/QueryPlace", amapRq, payload); if (amapResult && amapResult.length > 0) return [appscript_1.MapApiProvider.Amap, amapResult]; // Baidu const baiduRq = { ...rq, region: undefined, provider: appscript_1.MapApiProvider.Baidu }; const baiduResult = await this.api.post("Public/QueryPlace", baiduRq, payload); if (baiduResult && baiduResult.length > 0) return [appscript_1.MapApiProvider.Baidu, baiduResult]; } return [ rq.provider ?? appscript_1.MapApiProvider.Google, await this.api.post("Public/QueryPlace", rq, payload) ]; } /** * Read member invitation * @param id Id * @param payload Payload * @returns Result */ readInvitation(id, payload) { return this.api.get(`Public/ReadInvitation/${id}`, undefined, payload); } /** * * Get all repeat options * @param options Define the order and limit the items * @param isJoined Add the join label * @returns Result */ repeatOptions(options, isJoined = true) { options ??= shared_1.DataTypes.getEnumKeys(appscript_1.RepeatOption); return this.units(options, isJoined); } /** * Get all supported cultures * @param payload Payload * @returns Result */ supportedCultures(payload) { return this.api.get("Public/SupportedCultures", undefined, payload); } /** * * Get all product units * @param options Define the order and limit the items * @param isJoined Add the join label like 'per Kg' for Kg * @returns Units */ units(options, isJoined) { options ??= shared_1.DataTypes.getEnumKeys(appscript_1.ProductUnit); return options.map((key) => { const id = shared_1.DataTypes.getEnumByKey(appscript_1.ProductUnit, key); return { id, label: this.getUnitLabel(id, this.getUnitJoin(isJoined)).formatInitial(true) }; }); } } exports.PublicApi = PublicApi;