dff-util 1.3.7 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -9,3 +9,4 @@ export * from './main/const-messages';
9
9
  export * from './main/countries';
10
10
  export * from './main/languages';
11
11
  export * from './main/case-mapper';
12
+ export * from './main/api-call';
package/dist/index.js CHANGED
@@ -25,3 +25,4 @@ __exportStar(require("./main/const-messages"), exports);
25
25
  __exportStar(require("./main/countries"), exports);
26
26
  __exportStar(require("./main/languages"), exports);
27
27
  __exportStar(require("./main/case-mapper"), exports);
28
+ __exportStar(require("./main/api-call"), exports);
@@ -0,0 +1,6 @@
1
+ import { RequestBodyType } from './const-type';
2
+ /** Render an inline Handlebars template using the hosted HBS service. */
3
+ export declare function CallHbs(template: string, data: RequestBodyType | RequestBodyType[], multi?: boolean): string | string[];
4
+ export declare const CallLangText: (data: string, source: string, target: string) => string;
5
+ /** Return the exchange rate for one unit of the source currency. */
6
+ export declare const CallCurrencyConvert: (from: string, to: string) => number;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CallCurrencyConvert = exports.CallLangText = void 0;
4
+ exports.CallHbs = CallHbs;
5
+ // All API calls are synchronous and require XMLHttpRequest support.
6
+ function requestText(url, label, body) {
7
+ if (typeof XMLHttpRequest === 'undefined') {
8
+ throw new Error(`${label} requires a browser with synchronous XMLHttpRequest support`);
9
+ }
10
+ const request = new XMLHttpRequest();
11
+ request.open(body === undefined ? 'GET' : 'POST', url, false);
12
+ if (body !== undefined)
13
+ request.setRequestHeader('Content-Type', 'application/json');
14
+ request.send(body === undefined ? null : JSON.stringify(body));
15
+ if (request.status < 200 || request.status >= 300) {
16
+ const message = request.responseText;
17
+ throw new Error(`${label} failed: ${request.status} ${request.statusText}${message ? `: ${message}` : ''}`);
18
+ }
19
+ return request.responseText;
20
+ }
21
+ /** Render an inline Handlebars template using the hosted HBS service. */
22
+ function CallHbs(template, data, multi = false) {
23
+ if (typeof template !== 'string' || data === undefined) {
24
+ throw new Error('Invalid input: template must be a string and data is required');
25
+ }
26
+ const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
27
+ if (!(Array.isArray(data) ? data.every(isObject) : isObject(data))) {
28
+ throw new Error('Invalid input: data must be a JSON object or an array of JSON objects');
29
+ }
30
+ if (multi && !Array.isArray(data)) {
31
+ throw new Error('Invalid input: data must be an array for multi rendering');
32
+ }
33
+ const text = requestText(`https://hbs.rndpro.in/${multi ? 'multi' : 'render'}`, 'HBS rendering', { template, data });
34
+ if (!multi)
35
+ return text;
36
+ const rendered = JSON.parse(text);
37
+ if (!Array.isArray(rendered) || !rendered.every((item) => typeof item === 'string')) {
38
+ throw new Error('Invalid HBS response: expected an array of strings');
39
+ }
40
+ return rendered;
41
+ }
42
+ const CallLangText = (data, source, target) => {
43
+ if (!data || !source || !target) {
44
+ throw new Error('Invalid input: data, source, or target is missing');
45
+ }
46
+ const src = source.split('-')[0]; // e.g., "en-US" -> "en"
47
+ const tgt = target.split('-')[0]; // e.g., "te-IN" -> "te"
48
+ if (!/^[a-zA-Z]{2}$/.test(src) || !/^[a-zA-Z]{2}$/.test(tgt)) {
49
+ throw new Error('Invalid language code format');
50
+ }
51
+ const encodedData = encodeURIComponent(data);
52
+ const url = `https://lingva.ml/api/v1/${src}/${tgt}/${encodedData}`;
53
+ const json = JSON.parse(requestText(url, 'Translation'));
54
+ if (json.error) {
55
+ throw new Error(`API error: ${json.error}`);
56
+ }
57
+ if (json.translation !== undefined && typeof json.translation !== 'string') {
58
+ throw new Error('Invalid translation response: expected a string');
59
+ }
60
+ return json.translation || '';
61
+ };
62
+ exports.CallLangText = CallLangText;
63
+ /** Return the exchange rate for one unit of the source currency. */
64
+ const CallCurrencyConvert = (from, to) => {
65
+ var _a;
66
+ if (!from || !to) {
67
+ throw new Error('Invalid input: from or to is missing');
68
+ }
69
+ if (from === to)
70
+ return 1;
71
+ const url = `https://api.frankfurter.dev/v1/latest?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;
72
+ const json = JSON.parse(requestText(url, 'Currency conversion'));
73
+ const rate = (_a = json.rates) === null || _a === void 0 ? void 0 : _a[to];
74
+ if (typeof rate !== 'number' || !Number.isFinite(rate)) {
75
+ throw new Error('Invalid currency response: expected a numeric exchange rate');
76
+ }
77
+ return rate;
78
+ };
79
+ exports.CallCurrencyConvert = CallCurrencyConvert;
@@ -27,7 +27,7 @@ export type ResponseType = {
27
27
  skip?: number;
28
28
  limit?: number;
29
29
  meta?: {
30
- traceId?: string | number;
30
+ requestId?: string | number;
31
31
  timestamp?: string | number;
32
32
  duration?: number;
33
33
  };
@@ -1,2 +1,2 @@
1
1
  import { CountryType } from "./const-type";
2
- export declare const countries: CountryType[];
2
+ export declare const Countries: CountryType[];
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.countries = void 0;
4
- exports.countries = [
3
+ exports.Countries = void 0;
4
+ exports.Countries = [
5
5
  {
6
6
  code: "AF",
7
7
  name: "Afghanistan",
@@ -11,9 +11,7 @@ export declare const DateTime24HrFormat: (time: string) => string;
11
11
  export declare const DateTime12HrFormat: (timestamp: Date, showDate?: boolean, showTime?: boolean) => string;
12
12
  export declare const DateAndTime: (dateString: string, timeString: string) => Date | null;
13
13
  export declare const TimeAgo: (date: Date) => string;
14
- export declare const LangText: (data: string, source: string, target: string) => Promise<any>;
15
14
  export declare const LangCountryCode: (lang: string) => LangCountryType;
16
- export declare const CurrencyConvert: (from: string, to: string) => Promise<number>;
17
15
  export declare function EncodeBase64(input: string): string;
18
16
  export declare function DecodeBase64(encoded: string): string;
19
17
  export declare function EncodeURL(input: string): string;
package/dist/main/util.js CHANGED
@@ -1,15 +1,6 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.QueryCond = exports.CurrencyConvert = exports.LangCountryCode = exports.LangText = exports.TimeAgo = exports.DateAndTime = exports.DateTime12HrFormat = exports.DateTime24HrFormat = exports.TruncateText = exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.AppCode = exports.AppCodeByType = exports.AppUniqueCode = exports.AppRandomString = void 0;
3
+ exports.QueryCond = exports.LangCountryCode = exports.TimeAgo = exports.DateAndTime = exports.DateTime12HrFormat = exports.DateTime24HrFormat = exports.TruncateText = exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.AppCode = exports.AppCodeByType = exports.AppUniqueCode = exports.AppRandomString = void 0;
13
4
  exports.EncodeBase64 = EncodeBase64;
14
5
  exports.DecodeBase64 = DecodeBase64;
15
6
  exports.EncodeURL = EncodeURL;
@@ -158,33 +149,9 @@ const TimeAgo = (date) => {
158
149
  return 'Just now';
159
150
  };
160
151
  exports.TimeAgo = TimeAgo;
161
- const LangText = (data, source, target) => __awaiter(void 0, void 0, void 0, function* () {
162
- if (!data || !source || !target) {
163
- throw new Error('Invalid input: data, source, or target is missing');
164
- }
165
- const src = source.split('-')[0]; // e.g., "en-US" -> "en"
166
- const tgt = target.split('-')[0]; // e.g., "te-IN" -> "te"
167
- if (!/^[a-zA-Z]{2}$/.test(src) || !/^[a-zA-Z]{2}$/.test(tgt)) {
168
- throw new Error('Invalid language code format');
169
- }
170
- const encodedData = encodeURIComponent(data);
171
- const url = `https://lingva.ml/api/v1/${src}/${tgt}/${encodedData}`;
172
- const res = yield fetch(url, {
173
- method: 'GET',
174
- });
175
- if (!res.ok) {
176
- throw new Error(`Translation failed: ${res.status} ${res.statusText}`);
177
- }
178
- const json = yield res.json();
179
- if (json.error) {
180
- throw new Error(`API error: ${json.error}`);
181
- }
182
- return json.translation || '';
183
- });
184
- exports.LangText = LangText;
185
152
  const LangCountryCode = (lang) => {
186
153
  const countryCode = lang.split('-')[1];
187
- const country = countries_1.countries.find((c) => c.code === countryCode) || {};
154
+ const country = countries_1.Countries.find((c) => c.code === countryCode) || {};
188
155
  const language = languages_1.languages.find((l) => l.lang === lang) || {};
189
156
  return {
190
157
  lang: lang,
@@ -199,29 +166,6 @@ const LangCountryCode = (lang) => {
199
166
  };
200
167
  };
201
168
  exports.LangCountryCode = LangCountryCode;
202
- const CurrencyConvert = (from, to) => __awaiter(void 0, void 0, void 0, function* () {
203
- if (!from || !to) {
204
- throw new Error('Invalid input: from or to is missing');
205
- }
206
- if (from === to) {
207
- return 1;
208
- }
209
- try {
210
- const url = `https://api.frankfurter.app/latest?from=${from}&to=${to}`;
211
- const res = yield fetch(url, {
212
- method: 'GET',
213
- });
214
- if (!res.ok) {
215
- throw new Error(`Currency conversion failed: ${res.status} ${res.statusText}`);
216
- }
217
- const json = yield res.json();
218
- return json.rates[to];
219
- }
220
- catch (error) {
221
- throw new Error(`Currency conversion failed: ${error}`);
222
- }
223
- });
224
- exports.CurrencyConvert = CurrencyConvert;
225
169
  function EncodeBase64(input) {
226
170
  return btoa(input);
227
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dff-util",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {