@herowcode/utils 1.1.10 → 1.2.1
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/README.md +128 -1
- package/dist/api/client.d.ts +25 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/wrapper.d.ts +27 -0
- package/dist/api/wrapper.d.ts.map +1 -0
- package/dist/cjs/api/client.d.ts +25 -0
- package/dist/cjs/api/client.d.ts.map +1 -0
- package/dist/cjs/api/client.js +126 -0
- package/dist/cjs/api/client.js.map +1 -0
- package/dist/cjs/api/index.d.ts +3 -0
- package/dist/cjs/api/index.d.ts.map +1 -0
- package/dist/cjs/api/index.js +6 -0
- package/dist/cjs/api/index.js.map +1 -0
- package/dist/cjs/api/wrapper.d.ts +27 -0
- package/dist/cjs/api/wrapper.d.ts.map +1 -0
- package/dist/cjs/api/wrapper.js +152 -0
- package/dist/cjs/api/wrapper.js.map +1 -0
- package/dist/cjs/date/dayjs.d.ts +17 -0
- package/dist/cjs/date/dayjs.d.ts.map +1 -1
- package/dist/cjs/date/dayjs.js +115 -12
- package/dist/cjs/date/dayjs.js.map +1 -1
- package/dist/cjs/date/format-date.d.ts +22 -1
- package/dist/cjs/date/format-date.d.ts.map +1 -1
- package/dist/cjs/date/format-date.js +66 -8
- package/dist/cjs/date/format-date.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/date/dayjs.d.ts +17 -0
- package/dist/date/dayjs.d.ts.map +1 -1
- package/dist/date/format-date.d.ts +22 -1
- package/dist/date/format-date.d.ts.map +1 -1
- package/dist/esm/api/client.js +121 -0
- package/dist/esm/api/index.js +1 -0
- package/dist/esm/api/wrapper.js +148 -0
- package/dist/esm/date/dayjs.js +104 -6
- package/dist/esm/date/format-date.js +66 -8
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/package.json +8 -2
package/dist/cjs/date/dayjs.js
CHANGED
|
@@ -4,18 +4,121 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.dayjs = void 0;
|
|
7
|
+
exports.getDayjsGlobalIntl = getDayjsGlobalIntl;
|
|
8
|
+
exports.setDayjsGlobalIntl = setDayjsGlobalIntl;
|
|
9
|
+
exports.resetDayjsGlobalIntl = resetDayjsGlobalIntl;
|
|
10
|
+
exports.withDayjsGlobalIntl = withDayjsGlobalIntl;
|
|
11
|
+
exports.resolveDayjsIntlConfig = resolveDayjsIntlConfig;
|
|
7
12
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
const advancedFormat_1 = __importDefault(require("dayjs/plugin/advancedFormat"));
|
|
14
|
+
const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
|
|
15
|
+
const localizedFormat_1 = __importDefault(require("dayjs/plugin/localizedFormat"));
|
|
16
|
+
const relativeTime_1 = __importDefault(require("dayjs/plugin/relativeTime"));
|
|
17
|
+
const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
|
|
18
|
+
const utc_1 = __importDefault(require("dayjs/plugin/utc"));
|
|
19
|
+
require("dayjs/locale/en");
|
|
20
|
+
require("dayjs/locale/es");
|
|
21
|
+
require("dayjs/locale/fr");
|
|
22
|
+
require("dayjs/locale/pt-br");
|
|
23
|
+
const DEFAULT_INTL_CONFIG = {
|
|
24
|
+
locale: "pt-BR",
|
|
25
|
+
options: {
|
|
26
|
+
month: "long",
|
|
27
|
+
day: "numeric",
|
|
28
|
+
year: "numeric",
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
let globalIntlConfig = {
|
|
32
|
+
locale: DEFAULT_INTL_CONFIG.locale,
|
|
33
|
+
options: { ...DEFAULT_INTL_CONFIG.options },
|
|
34
|
+
};
|
|
35
|
+
function mergeIntlConfigs(base, override) {
|
|
36
|
+
var _a;
|
|
37
|
+
if (!override) {
|
|
38
|
+
return {
|
|
39
|
+
locale: base.locale,
|
|
40
|
+
options: { ...base.options },
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const mergedLocale = (_a = override.locale) !== null && _a !== void 0 ? _a : base.locale;
|
|
44
|
+
const mergedOptions = { ...base.options };
|
|
45
|
+
if (override.options) {
|
|
46
|
+
for (const key of Object.keys(override.options)) {
|
|
47
|
+
const value = override.options[key];
|
|
48
|
+
if (value === undefined || value === null) {
|
|
49
|
+
delete mergedOptions[key];
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
;
|
|
53
|
+
mergedOptions[key] = value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
locale: mergedLocale,
|
|
59
|
+
options: mergedOptions,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const intlPlugin = (pluginConfig, DayjsClass) => {
|
|
63
|
+
if (pluginConfig) {
|
|
64
|
+
globalIntlConfig = mergeIntlConfigs(globalIntlConfig, pluginConfig);
|
|
65
|
+
}
|
|
66
|
+
const formatIntl = function formatIntl(config) {
|
|
67
|
+
const resolved = mergeIntlConfigs(globalIntlConfig, config);
|
|
68
|
+
return new Intl.DateTimeFormat(resolved.locale, resolved.options).format(this.toDate());
|
|
69
|
+
};
|
|
70
|
+
const toIntlFormatter = function toIntlFormatter(config) {
|
|
71
|
+
const resolved = mergeIntlConfigs(globalIntlConfig, config);
|
|
72
|
+
return new Intl.DateTimeFormat(resolved.locale, resolved.options);
|
|
73
|
+
};
|
|
74
|
+
Object.defineProperty(DayjsClass.prototype, "formatIntl", {
|
|
75
|
+
value: formatIntl,
|
|
76
|
+
configurable: true,
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(DayjsClass.prototype, "toIntlFormatter", {
|
|
79
|
+
value: toIntlFormatter,
|
|
80
|
+
configurable: true,
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
function getDayjsGlobalIntl() {
|
|
84
|
+
return {
|
|
85
|
+
locale: globalIntlConfig.locale,
|
|
86
|
+
options: { ...globalIntlConfig.options },
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function setDayjsGlobalIntl(config) {
|
|
90
|
+
globalIntlConfig = mergeIntlConfigs(globalIntlConfig, config);
|
|
91
|
+
return getDayjsGlobalIntl();
|
|
92
|
+
}
|
|
93
|
+
function resetDayjsGlobalIntl() {
|
|
94
|
+
globalIntlConfig = {
|
|
95
|
+
locale: DEFAULT_INTL_CONFIG.locale,
|
|
96
|
+
options: { ...DEFAULT_INTL_CONFIG.options },
|
|
97
|
+
};
|
|
98
|
+
return getDayjsGlobalIntl();
|
|
99
|
+
}
|
|
100
|
+
function withDayjsGlobalIntl(config, callback) {
|
|
101
|
+
const previous = getDayjsGlobalIntl();
|
|
102
|
+
globalIntlConfig = mergeIntlConfigs(globalIntlConfig, config);
|
|
103
|
+
try {
|
|
104
|
+
return callback();
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
globalIntlConfig = {
|
|
108
|
+
locale: previous.locale,
|
|
109
|
+
options: { ...previous.options },
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function resolveDayjsIntlConfig(config) {
|
|
114
|
+
return mergeIntlConfigs(globalIntlConfig, config);
|
|
115
|
+
}
|
|
116
|
+
dayjs_1.default.extend(utc_1.default);
|
|
117
|
+
dayjs_1.default.extend(customParseFormat_1.default);
|
|
118
|
+
dayjs_1.default.extend(advancedFormat_1.default);
|
|
119
|
+
dayjs_1.default.extend(timezone_1.default);
|
|
120
|
+
dayjs_1.default.extend(localizedFormat_1.default);
|
|
121
|
+
dayjs_1.default.extend(relativeTime_1.default);
|
|
122
|
+
dayjs_1.default.extend(intlPlugin);
|
|
20
123
|
exports.dayjs = dayjs_1.default;
|
|
21
124
|
//# sourceMappingURL=dayjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dayjs.js","sourceRoot":"","sources":["../../../src/date/dayjs.ts"],"names":[],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"dayjs.js","sourceRoot":"","sources":["../../../src/date/dayjs.ts"],"names":[],"mappings":";;;;;;AA8GA,gDAKC;AAED,gDAKC;AAED,oDAMC;AAED,kDAeC;AAED,wDAIC;AAxJD,kDAAyB;AACzB,iFAAwD;AACxD,uFAA8D;AAC9D,mFAA0D;AAC1D,6EAAoD;AACpD,qEAA4C;AAC5C,2DAAkC;AAClC,2BAAwB;AACxB,2BAAwB;AACxB,2BAAwB;AACxB,8BAA2B;AAc3B,MAAM,mBAAmB,GAA6B;IACpD,MAAM,EAAE,OAAO;IACf,OAAO,EAAE;QACP,KAAK,EAAE,MAAM;QACb,GAAG,EAAE,SAAS;QACd,IAAI,EAAE,SAAS;KAChB;CACF,CAAA;AAED,IAAI,gBAAgB,GAA6B;IAC/C,MAAM,EAAE,mBAAmB,CAAC,MAAM;IAClC,OAAO,EAAE,EAAE,GAAG,mBAAmB,CAAC,OAAO,EAAE;CAC5C,CAAA;AAED,SAAS,gBAAgB,CACvB,IAA8B,EAC9B,QAA2B;;IAE3B,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO;YACL,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,OAAO,EAAE;SAC7B,CAAA;IACH,CAAC;IAED,MAAM,YAAY,GAAG,MAAA,QAAQ,CAAC,MAAM,mCAAI,IAAI,CAAC,MAAM,CAAA;IACnD,MAAM,aAAa,GAA+B,E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AgB,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAA;AACnD,CAAC;AAED,eAAK,CAAC,MAAM,CAAC,aAAG,CAAC,CAAA;AACjB,eAAK,CAAC,MAAM,CAAC,2BAAiB,CAAC,CAAA;AAC/B,eAAK,CAAC,MAAM,CAAC,wBAAc,CAAC,CAAA;AAC5B,eAAK,CAAC,MAAM,CAAC,kBAAQ,CAAC,CAAA;AACtB,eAAK,CAAC,MAAM,CAAC,yBAAe,CAAC,CAAA;AAC7B,eAAK,CAAC,MAAM,CAAC,sBAAY,CAAC,CAAA;AAC1B,eAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;AAEX,QAAA,KAAK,GAAG,eAAK,CAAA"}
|
|
@@ -1,2 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
import type { IDayjsIntlConfig } from "./dayjs";
|
|
2
|
+
export interface IFormatDateConfig {
|
|
3
|
+
/** Optional locale override applied to the underlying dayjs instance */
|
|
4
|
+
locale?: string;
|
|
5
|
+
/**
|
|
6
|
+
* When provided, format using dayjs tokens (e.g. "DD/MM/YYYY").
|
|
7
|
+
* Unless `useIntl` is true, this takes precedence over Intl formatting.
|
|
8
|
+
*/
|
|
9
|
+
format?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Intl configuration used when formatting through `Intl.DateTimeFormat`.
|
|
12
|
+
* Values are merged on top of the global Intl configuration.
|
|
13
|
+
*/
|
|
14
|
+
intl?: IDayjsIntlConfig;
|
|
15
|
+
/**
|
|
16
|
+
* Forces the use of Intl formatting even if a format string is supplied.
|
|
17
|
+
*/
|
|
18
|
+
useIntl?: boolean;
|
|
19
|
+
}
|
|
20
|
+
type TLocaleOrConfig = Intl.LocalesArgument | IFormatDateConfig;
|
|
21
|
+
export declare function formatDate(date: Date | string | number, localeOrConfig?: TLocaleOrConfig, opts?: Intl.DateTimeFormatOptions): string;
|
|
22
|
+
export {};
|
|
2
23
|
//# sourceMappingURL=format-date.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,
|
|
1
|
+
{"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAG/C,MAAM,WAAW,iBAAiB;IAChC,wEAAwE;IACxE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,IAAI,CAAC,EAAE,gBAAgB,CAAA;IACvB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,KAAK,eAAe,GAAG,IAAI,CAAC,eAAe,GAAG,iBAAiB,CAAA;AAqC/D,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,cAAc,GAAE,eAA6C,EAC7D,IAAI,CAAC,EAAE,IAAI,CAAC,qBAAqB,GAChC,MAAM,CAkDR"}
|
|
@@ -1,13 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatDate = formatDate;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
const dayjs_1 = require("./dayjs");
|
|
5
|
+
function isFormatDateConfig(value) {
|
|
6
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
function isValidLocaleTag(value) {
|
|
9
|
+
if (typeof Intl !== "undefined" &&
|
|
10
|
+
typeof Intl.getCanonicalLocales === "function") {
|
|
11
|
+
try {
|
|
12
|
+
Intl.getCanonicalLocales(value);
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
catch (_a) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/.test(value);
|
|
20
|
+
}
|
|
21
|
+
function toDayjsLocale(locale) {
|
|
22
|
+
if (!locale)
|
|
23
|
+
return undefined;
|
|
24
|
+
const first = Array.isArray(locale)
|
|
25
|
+
? locale[0]
|
|
26
|
+
: locale;
|
|
27
|
+
return typeof first === "string" ? first : String(first);
|
|
28
|
+
}
|
|
29
|
+
function formatDate(date, localeOrConfig = (0, dayjs_1.getDayjsGlobalIntl)().locale, opts) {
|
|
30
|
+
var _a;
|
|
31
|
+
const instance = (0, dayjs_1.dayjs)(date);
|
|
32
|
+
if (!instance.isValid()) {
|
|
33
|
+
throw new RangeError("Invalid time value");
|
|
34
|
+
}
|
|
35
|
+
if (isFormatDateConfig(localeOrConfig)) {
|
|
36
|
+
const { format, locale, intl, useIntl } = localeOrConfig;
|
|
37
|
+
const baseLocale = locale !== null && locale !== void 0 ? locale : toDayjsLocale(intl === null || intl === void 0 ? void 0 : intl.locale);
|
|
38
|
+
const workingInstance = baseLocale ? instance.locale(baseLocale) : instance;
|
|
39
|
+
if (format && !useIntl) {
|
|
40
|
+
return workingInstance.format(format);
|
|
41
|
+
}
|
|
42
|
+
const intlConfig = intl
|
|
43
|
+
? {
|
|
44
|
+
locale: (_a = intl.locale) !== null && _a !== void 0 ? _a : baseLocale,
|
|
45
|
+
options: intl.options,
|
|
46
|
+
}
|
|
47
|
+
: baseLocale
|
|
48
|
+
? {
|
|
49
|
+
locale: baseLocale,
|
|
50
|
+
}
|
|
51
|
+
: undefined;
|
|
52
|
+
return workingInstance.formatIntl(intlConfig);
|
|
53
|
+
}
|
|
54
|
+
if (typeof localeOrConfig === "string" && opts === undefined) {
|
|
55
|
+
if (!isValidLocaleTag(localeOrConfig)) {
|
|
56
|
+
return instance.format(localeOrConfig);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const localeForDayjs = toDayjsLocale(localeOrConfig);
|
|
60
|
+
const workingInstance = localeForDayjs
|
|
61
|
+
? instance.locale(localeForDayjs)
|
|
62
|
+
: instance;
|
|
63
|
+
const intlConfig = localeOrConfig !== undefined || opts !== undefined
|
|
64
|
+
? {
|
|
65
|
+
locale: localeOrConfig,
|
|
66
|
+
options: opts,
|
|
67
|
+
}
|
|
68
|
+
: undefined;
|
|
69
|
+
return workingInstance.formatIntl(intlConfig);
|
|
12
70
|
}
|
|
13
71
|
//# sourceMappingURL=format-date.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format-date.js","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"format-date.js","sourceRoot":"","sources":["../../../src/date/format-date.ts"],"names":[],"mappings":";;AA2DA,gCAsDC;AAhHD,mCAAmD;AAuBnD,SAAS,kBAAkB,CACzB,KAAsB;IAEtB,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC7E,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAa;IACrC,IACE,OAAO,IAAI,KAAK,WAAW;QAC3B,OAAO,IAAI,CAAC,mBAAmB,KAAK,UAAU,EAC9C,CAAC;QACD,IAAI,CAAC;YACH,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAA;YAC/B,OAAO,IAAI,CAAA;QACb,CAAC;QAAC,WAAM,CAAC;YACP,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;IAED,OAAO,oCAAoC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AACzD,CAAC;AAED,SAAS,aAAa,CACpB,MAGqC;IAErC,IAAI,CAAC,MAAM;QAAE,OAAO,SAAS,CAAA;IAC7B,MAAM,KA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nB;gBACH,CAAC,CAAC,SAAS,CAAA;QAEf,OAAO,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,CAAA;IAC/C,CAAC;IAED,IAAI,OAAO,cAAc,KAAK,QAAQ,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QAC7D,IAAI,CAAC,gBAAgB,CAAC,cAAc,CAAC,EAAE,CAAC;YACtC,OAAO,QAAQ,CAAC,MAAM,CAAC,cAAc,CAAC,CAAA;QACxC,CAAC;IACH,CAAC;IAED,MAAM,cAAc,GAAG,aAAa,CAAC,cAAc,CAAC,CAAA;IACpD,MAAM,eAAe,GAAG,cAAc;QACpC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAAC;QACjC,CAAC,CAAC,QAAQ,CAAA;IAEZ,MAAM,UAAU,GACd,cAAc,KAAK,SAAS,IAAI,IAAI,KAAK,SAAS;QAChD,CAAC,CAAC;YACE,MAAM,EAAE,cAAc;YACtB,OAAO,EAAE,IAAI;SACd;QACH,CAAC,CAAC,SAAS,CAAA;IAEf,OAAO,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,CAAA;AAC/C,CAAC"}
|
package/dist/cjs/index.d.ts
CHANGED
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api/index.js"), exports); // API utilities
|
|
17
18
|
__exportStar(require("./array/index.js"), exports); // Array utilities
|
|
18
19
|
__exportStar(require("./date/index.js"), exports); // Date utilities
|
|
19
20
|
__exportStar(require("./files/index.js"), exports); // Files utilities
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAgC,CAAC,kBAAkB;AACnD,kDAA+B,CAAC,iBAAiB;AACjD,mDAAgC,CAAC,kBAAkB;AACnD,sDAAmC,CAAC,qBAAqB;AACzD,oDAAiC,CAAC,mBAAmB;AACrD,qDAAkC,CAAC,oBAAoB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA8B,CAAC,gBAAgB;AAC/C,mDAAgC,CAAC,kBAAkB;AACnD,kDAA+B,CAAC,iBAAiB;AACjD,mDAAgC,CAAC,kBAAkB;AACnD,sDAAmC,CAAC,qBAAqB;AACzD,oDAAiC,CAAC,mBAAmB;AACrD,qDAAkC,CAAC,oBAAoB"}
|
package/dist/date/dayjs.d.ts
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
1
|
import dayJs from "dayjs";
|
|
2
|
+
import "dayjs/locale/en";
|
|
3
|
+
import "dayjs/locale/es";
|
|
4
|
+
import "dayjs/locale/fr";
|
|
5
|
+
import "dayjs/locale/pt-br";
|
|
6
|
+
export interface IDayjsIntlConfig {
|
|
7
|
+
locale?: Intl.LocalesArgument;
|
|
8
|
+
options?: Intl.DateTimeFormatOptions;
|
|
9
|
+
}
|
|
10
|
+
export interface IDayjsIntlResolvedConfig {
|
|
11
|
+
locale: Intl.LocalesArgument;
|
|
12
|
+
options: Intl.DateTimeFormatOptions;
|
|
13
|
+
}
|
|
14
|
+
export declare function getDayjsGlobalIntl(): IDayjsIntlResolvedConfig;
|
|
15
|
+
export declare function setDayjsGlobalIntl(config: IDayjsIntlConfig): IDayjsIntlResolvedConfig;
|
|
16
|
+
export declare function resetDayjsGlobalIntl(): IDayjsIntlResolvedConfig;
|
|
17
|
+
export declare function withDayjsGlobalIntl<T>(config: IDayjsIntlConfig, callback: () => T): T;
|
|
18
|
+
export declare function resolveDayjsIntlConfig(config?: IDayjsIntlConfig): IDayjsIntlResolvedConfig;
|
|
2
19
|
export declare const dayjs: typeof dayJs;
|
|
3
20
|
//# sourceMappingURL=dayjs.d.ts.map
|
package/dist/date/dayjs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../src/date/dayjs.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../src/date/dayjs.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAA;AAOzB,OAAO,iBAAiB,CAAA;AACxB,OAAO,iBAAiB,CAAA;AACxB,OAAO,iBAAiB,CAAA;AACxB,OAAO,oBAAoB,CAAA;AAI3B,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,IAAI,CAAC,eAAe,CAAA;IAC7B,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,CAAA;CACrC;AAED,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,IAAI,CAAC,eAAe,CAAA;IAC5B,OAAO,EAAE,IAAI,CAAC,qBAAqB,CAAA;CACpC;AAuFD,wBAAgB,kBAAkB,IAAI,wBAAwB,CAK7D;AAED,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,gBAAgB,GACvB,wBAAwB,CAG1B;AAED,wBAAgB,oBAAoB,IAAI,wBAAwB,CAM/D;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,MAAM,CAAC,GAChB,CAAC,CAYH;AAED,wBAAgB,sBAAsB,CACpC,MAAM,CAAC,EAAE,gBAAgB,GACxB,wBAAwB,CAE1B;AAUD,eAAO,MAAM,KAAK,cAAQ,CAAA"}
|
|
@@ -1,2 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
import type { IDayjsIntlConfig } from "./dayjs";
|
|
2
|
+
export interface IFormatDateConfig {
|
|
3
|
+
/** Optional locale override applied to the underlying dayjs instance */
|
|
4
|
+
locale?: string;
|
|
5
|
+
/**
|
|
6
|
+
* When provided, format using dayjs tokens (e.g. "DD/MM/YYYY").
|
|
7
|
+
* Unless `useIntl` is true, this takes precedence over Intl formatting.
|
|
8
|
+
*/
|
|
9
|
+
format?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Intl configuration used when formatting through `Intl.DateTimeFormat`.
|
|
12
|
+
* Values are merged on top of the global Intl configuration.
|
|
13
|
+
*/
|
|
14
|
+
intl?: IDayjsIntlConfig;
|
|
15
|
+
/**
|
|
16
|
+
* Forces the use of Intl formatting even if a format string is supplied.
|
|
17
|
+
*/
|
|
18
|
+
useIntl?: boolean;
|
|
19
|
+
}
|
|
20
|
+
type TLocaleOrConfig = Intl.LocalesArgument | IFormatDateConfig;
|
|
21
|
+
export declare function formatDate(date: Date | string | number, localeOrConfig?: TLocaleOrConfig, opts?: Intl.DateTimeFormatOptions): string;
|
|
22
|
+
export {};
|
|
2
23
|
//# sourceMappingURL=format-date.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,
|
|
1
|
+
{"version":3,"file":"format-date.d.ts","sourceRoot":"","sources":["../../src/date/format-date.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAG/C,MAAM,WAAW,iBAAiB;IAChC,wEAAwE;IACxE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,IAAI,CAAC,EAAE,gBAAgB,CAAA;IACvB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,KAAK,eAAe,GAAG,IAAI,CAAC,eAAe,GAAG,iBAAiB,CAAA;AAqC/D,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,cAAc,GAAE,eAA6C,EAC7D,IAAI,CAAC,EAAE,IAAI,CAAC,qBAAqB,GAChC,MAAM,CAkDR"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { apiWrapper } from "./wrapper.js";
|
|
2
|
+
const defaultRetryConfig = {
|
|
3
|
+
limit: 5,
|
|
4
|
+
methods: ["get"],
|
|
5
|
+
statusCodes: [413],
|
|
6
|
+
backoffLimit: 3000,
|
|
7
|
+
};
|
|
8
|
+
function buildQueryString(params) {
|
|
9
|
+
const searchParams = new URLSearchParams();
|
|
10
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
11
|
+
if (value !== null && value !== undefined) {
|
|
12
|
+
searchParams.append(key, String(value));
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const queryString = searchParams.toString();
|
|
16
|
+
return queryString ? `?${queryString}` : "";
|
|
17
|
+
}
|
|
18
|
+
async function beforeRequestHook(request, getAccessToken, getUserIP) {
|
|
19
|
+
const token = await (getAccessToken === null || getAccessToken === void 0 ? void 0 : getAccessToken());
|
|
20
|
+
const headers = new Headers(request.headers);
|
|
21
|
+
if (token) {
|
|
22
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
23
|
+
}
|
|
24
|
+
const userIP = await (getUserIP === null || getUserIP === void 0 ? void 0 : getUserIP());
|
|
25
|
+
if (userIP) {
|
|
26
|
+
headers.set("X-User-IP", userIP);
|
|
27
|
+
}
|
|
28
|
+
return new Request(request, { headers });
|
|
29
|
+
}
|
|
30
|
+
async function afterResponseHook(request, response, onSignoutUnauthorized) {
|
|
31
|
+
const possibleSignInPaths = ["signin", "sign-in", "login"];
|
|
32
|
+
const isSignInPath = possibleSignInPaths.some((path) => request.url.includes(path));
|
|
33
|
+
const needsBody = response.status === 401 && !isSignInPath;
|
|
34
|
+
let responseBody = null;
|
|
35
|
+
let responseBodyFor401 = null;
|
|
36
|
+
if (needsBody) {
|
|
37
|
+
const clone = response.clone();
|
|
38
|
+
try {
|
|
39
|
+
responseBody = await clone.json();
|
|
40
|
+
responseBodyFor401 = responseBody;
|
|
41
|
+
}
|
|
42
|
+
catch (_a) {
|
|
43
|
+
responseBody = await clone.text();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (response.status === 401 && !isSignInPath) {
|
|
47
|
+
return onSignoutUnauthorized === null || onSignoutUnauthorized === void 0 ? void 0 : onSignoutUnauthorized(responseBodyFor401);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async function fetchWithRetry(input, init) {
|
|
51
|
+
var _a, _b, _c, _d;
|
|
52
|
+
const retryConfig = (init === null || init === void 0 ? void 0 : init.retry) || defaultRetryConfig;
|
|
53
|
+
const method = ((init === null || init === void 0 ? void 0 : init.method) || "GET").toLowerCase();
|
|
54
|
+
let attempt = 0;
|
|
55
|
+
while (true) {
|
|
56
|
+
attempt++;
|
|
57
|
+
const response = await fetch(input, init);
|
|
58
|
+
const shouldRetry = ((_a = retryConfig.statusCodes) === null || _a === void 0 ? void 0 : _a.includes(response.status)) &&
|
|
59
|
+
((_b = retryConfig.methods) === null || _b === void 0 ? void 0 : _b.includes(method)) &&
|
|
60
|
+
attempt < ((_c = retryConfig.limit) !== null && _c !== void 0 ? _c : 5);
|
|
61
|
+
if (!shouldRetry)
|
|
62
|
+
return response;
|
|
63
|
+
const backoff = Math.min(2 ** attempt * 100, (_d = retryConfig.backoffLimit) !== null && _d !== void 0 ? _d : 3000);
|
|
64
|
+
await new Promise((resolve) => setTimeout(resolve, backoff));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function api(config, endpoint, options = {}) {
|
|
68
|
+
var _a;
|
|
69
|
+
let url = endpoint.startsWith("http")
|
|
70
|
+
? endpoint
|
|
71
|
+
: `${(_a = config.baseURL) === null || _a === void 0 ? void 0 : _a.replace(/\/$/, "")}/${endpoint.replace(/^\//, "")}`;
|
|
72
|
+
// Add query parameters if provided
|
|
73
|
+
if (options.params) {
|
|
74
|
+
const queryString = buildQueryString(options.params);
|
|
75
|
+
url += queryString;
|
|
76
|
+
}
|
|
77
|
+
// Apply default options
|
|
78
|
+
const init = {
|
|
79
|
+
credentials: "include",
|
|
80
|
+
...options,
|
|
81
|
+
};
|
|
82
|
+
let body;
|
|
83
|
+
if (options.json) {
|
|
84
|
+
body = JSON.stringify(options.json);
|
|
85
|
+
init.headers = {
|
|
86
|
+
...init.headers,
|
|
87
|
+
"Content-Type": "application/json",
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Create initial request
|
|
91
|
+
let request = new Request(url, { ...init, body });
|
|
92
|
+
// Run pre-request hooks
|
|
93
|
+
request = await beforeRequestHook(request, config.getAccessToken, config.getUserIP);
|
|
94
|
+
// Execute request with retry logic
|
|
95
|
+
const response = await fetchWithRetry(request, init);
|
|
96
|
+
// Run post-response hooks
|
|
97
|
+
await afterResponseHook(request, response, config.onSignoutUnauthorized);
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
throw response;
|
|
100
|
+
}
|
|
101
|
+
if (response.status === 204) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
const text = await response.text();
|
|
105
|
+
if (!text) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
return JSON.parse(text);
|
|
109
|
+
}
|
|
110
|
+
export const apiClient = (config) => {
|
|
111
|
+
const apiConfig = {
|
|
112
|
+
...config,
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
get: (url, options) => apiWrapper(() => api(apiConfig, url, options)),
|
|
116
|
+
post: (url, options) => apiWrapper(() => api(apiConfig, url, { ...options, method: "POST" })),
|
|
117
|
+
put: (url, options) => apiWrapper(() => api(apiConfig, url, { ...options, method: "PUT" })),
|
|
118
|
+
delete: (url, options) => apiWrapper(() => api(apiConfig, url, { ...options, method: "DELETE" })),
|
|
119
|
+
patch: (url, options) => apiWrapper(() => api(apiConfig, url, { ...options, method: "PATCH" })),
|
|
120
|
+
};
|
|
121
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { apiClient } from "./client.js";
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { isAxiosError } from "axios";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts error properties from Axios errors
|
|
4
|
+
*/
|
|
5
|
+
async function extractAxiosErrorDetails(error) {
|
|
6
|
+
var _a, _b, _c;
|
|
7
|
+
const errorObj = {
|
|
8
|
+
message: error.message || "Axios Error",
|
|
9
|
+
status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status,
|
|
10
|
+
path: (_b = error.config) === null || _b === void 0 ? void 0 : _b.url,
|
|
11
|
+
};
|
|
12
|
+
if (((_c = error.response) === null || _c === void 0 ? void 0 : _c.data) && typeof error.response.data === "object") {
|
|
13
|
+
return await enrichErrorWithData(errorObj, Promise.resolve(error.response.data));
|
|
14
|
+
}
|
|
15
|
+
return errorObj;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Extracts error properties from standard Error objects
|
|
19
|
+
*/
|
|
20
|
+
function extractStandardErrorDetails(error) {
|
|
21
|
+
const errorObj = {
|
|
22
|
+
message: error.message,
|
|
23
|
+
};
|
|
24
|
+
// Capture any custom properties the error might have
|
|
25
|
+
const extendedError = error;
|
|
26
|
+
if (extendedError.code)
|
|
27
|
+
errorObj.code = String(extendedError.code);
|
|
28
|
+
if (extendedError.status)
|
|
29
|
+
errorObj.status = Number(extendedError.status);
|
|
30
|
+
if (extendedError.details)
|
|
31
|
+
errorObj.details = extendedError.details;
|
|
32
|
+
return errorObj;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Extracts error properties from fetch Response objects
|
|
36
|
+
*/
|
|
37
|
+
async function extractResponseErrorDetails(response) {
|
|
38
|
+
const errorObj = {
|
|
39
|
+
message: `HTTP Error ${response.status}: ${response.statusText}`,
|
|
40
|
+
status: response.status,
|
|
41
|
+
path: response.url,
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
// Clone the response to avoid consuming the body stream
|
|
45
|
+
const clonedResponse = response.clone();
|
|
46
|
+
const errorData = await clonedResponse.json();
|
|
47
|
+
// Directly enrich the error object with the parsed data
|
|
48
|
+
if (errorData && typeof errorData === "object") {
|
|
49
|
+
if ("message" in errorData && errorData.message) {
|
|
50
|
+
errorObj.message = String(errorData.message);
|
|
51
|
+
}
|
|
52
|
+
if ("code" in errorData) {
|
|
53
|
+
errorObj.code = String(errorData.code);
|
|
54
|
+
}
|
|
55
|
+
if ("details" in errorData) {
|
|
56
|
+
errorObj.details = errorData.details;
|
|
57
|
+
}
|
|
58
|
+
else if ("issues" in errorData) {
|
|
59
|
+
errorObj.details = errorData.issues;
|
|
60
|
+
}
|
|
61
|
+
if ("timestamp" in errorData) {
|
|
62
|
+
errorObj.timestamp = String(errorData.timestamp);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return errorObj;
|
|
66
|
+
}
|
|
67
|
+
catch (_a) {
|
|
68
|
+
// If JSON parsing fails, return the basic error object
|
|
69
|
+
return errorObj;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Enriches an error object with additional data from response
|
|
74
|
+
*/
|
|
75
|
+
async function enrichErrorWithData(errorObj, errorData, path) {
|
|
76
|
+
const data = await errorData;
|
|
77
|
+
if (data) {
|
|
78
|
+
if ("message" in data) {
|
|
79
|
+
errorObj.message = String(data.message);
|
|
80
|
+
}
|
|
81
|
+
if ("code" in data) {
|
|
82
|
+
errorObj.code = String(data.code);
|
|
83
|
+
}
|
|
84
|
+
if ("details" in data) {
|
|
85
|
+
errorObj.details = data.details;
|
|
86
|
+
}
|
|
87
|
+
else if ("issues" in data) {
|
|
88
|
+
errorObj.details = data.issues;
|
|
89
|
+
}
|
|
90
|
+
if ("timestamp" in data) {
|
|
91
|
+
errorObj.timestamp = String(data.timestamp);
|
|
92
|
+
}
|
|
93
|
+
if (path) {
|
|
94
|
+
errorObj.path = path;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return errorObj;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Determines the type of error and routes to appropriate handler
|
|
101
|
+
*/
|
|
102
|
+
async function processError(error) {
|
|
103
|
+
if (error instanceof Response) {
|
|
104
|
+
return extractResponseErrorDetails(error);
|
|
105
|
+
}
|
|
106
|
+
if (isAxiosError(error)) {
|
|
107
|
+
return extractAxiosErrorDetails(error);
|
|
108
|
+
}
|
|
109
|
+
if (error instanceof Error) {
|
|
110
|
+
return extractStandardErrorDetails(error);
|
|
111
|
+
}
|
|
112
|
+
// Handle unknown error types
|
|
113
|
+
return {
|
|
114
|
+
message: error ? JSON.stringify(error) : "Unknown error",
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Wraps API calls with standardized error handling
|
|
119
|
+
*
|
|
120
|
+
* @param apiCall - The API call function to execute
|
|
121
|
+
* @param defaultData - Optional default value to return when an error occurs
|
|
122
|
+
* @returns A standardized result object with data or formatted error message
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* // Basic usage
|
|
126
|
+
* const result = await apiWrapper(() => fetchUserData(userId));
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // With default data value
|
|
130
|
+
* const result = await apiWrapper(() => fetchPosts(), []);
|
|
131
|
+
* // result.data will be [] instead of null on error
|
|
132
|
+
*/
|
|
133
|
+
export async function apiWrapper(apiCall, defaultData) {
|
|
134
|
+
try {
|
|
135
|
+
const data = await apiCall();
|
|
136
|
+
return {
|
|
137
|
+
data,
|
|
138
|
+
error: null,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
const errorObj = await processError(error);
|
|
143
|
+
return {
|
|
144
|
+
data: (defaultData !== null && defaultData !== void 0 ? defaultData : null),
|
|
145
|
+
error: errorObj,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|