@inzombieland/nuxt-common 0.0.2 → 0.0.5
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/LICENSE +8 -0
- package/assets/css/layer.css +305 -0
- package/module.cjs +5 -0
- package/module.d.mts +7 -0
- package/module.d.ts +7 -0
- package/module.json +9 -0
- package/module.mjs +70 -0
- package/package.json +4 -2
- package/runtime/AppProvider.vue +129 -0
- package/runtime/SidebarProvider.vue +138 -0
- package/runtime/api/account/account.get.d.ts +2 -0
- package/runtime/api/account/account.get.mjs +19 -0
- package/runtime/api/account/authcheck.get.d.ts +8 -0
- package/runtime/api/account/authcheck.get.mjs +28 -0
- package/runtime/api/account/default-handler.d.ts +2 -0
- package/runtime/api/account/default-handler.mjs +37 -0
- package/runtime/api/account/middleware.d.ts +2 -0
- package/runtime/api/account/middleware.mjs +18 -0
- package/runtime/api/account/token.get.d.ts +2 -0
- package/runtime/api/account/token.get.mjs +31 -0
- package/runtime/api/index.d.ts +37 -0
- package/runtime/api/index.mjs +26 -0
- package/runtime/api/user-actions.d.ts +118 -0
- package/runtime/api/user-actions.mjs +165 -0
- package/runtime/composables/use-active-sessions.d.ts +5 -0
- package/runtime/composables/use-active-sessions.mjs +15 -0
- package/runtime/composables/use-api-fetch.d.ts +3 -0
- package/runtime/composables/use-api-fetch.mjs +3 -0
- package/runtime/composables/use-helpers.d.ts +16 -0
- package/runtime/composables/use-helpers.mjs +10 -0
- package/runtime/composables/use-layer-manager.d.ts +3 -0
- package/runtime/composables/use-layer-manager.mjs +3 -0
- package/runtime/composables/use-subscribe.d.ts +10 -0
- package/runtime/composables/use-subscribe.mjs +22 -0
- package/runtime/composables/use-toggle-sidebar.d.ts +3 -0
- package/runtime/composables/use-toggle-sidebar.mjs +8 -0
- package/runtime/composables/use-user-actions.d.ts +2 -0
- package/runtime/composables/use-user-actions.mjs +4 -0
- package/runtime/composables/use-user.d.ts +3 -0
- package/runtime/composables/use-user.mjs +3 -0
- package/runtime/composables/use-validators.d.ts +8 -0
- package/runtime/composables/use-validators.mjs +17 -0
- package/runtime/middleware/auth.d.ts +2 -0
- package/runtime/middleware/auth.mjs +10 -0
- package/runtime/middleware/guest.d.ts +2 -0
- package/runtime/middleware/guest.mjs +12 -0
- package/runtime/middleware/index.d.ts +4 -0
- package/runtime/middleware/index.mjs +4 -0
- package/runtime/middleware/locale.global.d.ts +2 -0
- package/runtime/middleware/locale.global.mjs +13 -0
- package/runtime/packages/api/api-client.d.ts +8 -0
- package/runtime/packages/api/api-client.mjs +150 -0
- package/runtime/packages/api/bus.d.ts +7 -0
- package/runtime/packages/api/bus.mjs +22 -0
- package/runtime/packages/api/comet-client.d.ts +11 -0
- package/runtime/packages/api/comet-client.mjs +73 -0
- package/runtime/packages/api/get-user.d.ts +8 -0
- package/runtime/packages/api/get-user.mjs +54 -0
- package/runtime/packages/api/get-visitor.d.ts +11 -0
- package/runtime/packages/api/get-visitor.mjs +32 -0
- package/runtime/packages/api/helpers/api-helper.d.ts +6 -0
- package/runtime/packages/api/helpers/api-helper.mjs +99 -0
- package/runtime/packages/api/helpers/current-device.d.ts +67 -0
- package/runtime/packages/api/helpers/current-device.mjs +368 -0
- package/runtime/packages/api/helpers/date-helper.d.ts +15 -0
- package/runtime/packages/api/helpers/date-helper.mjs +231 -0
- package/runtime/packages/api/helpers/index.d.ts +4 -0
- package/runtime/packages/api/helpers/index.mjs +15 -0
- package/runtime/packages/api/index.d.ts +26 -0
- package/runtime/packages/api/index.mjs +50 -0
- package/runtime/packages/api/package.json +20 -0
- package/runtime/packages/api/refresh-token.d.ts +2 -0
- package/runtime/packages/api/refresh-token.mjs +39 -0
- package/runtime/packages/api/types.d.ts +51 -0
- package/runtime/packages/api/types.mjs +0 -0
- package/runtime/packages/api/use-api-actions.d.ts +13 -0
- package/runtime/packages/api/use-api-actions.mjs +5 -0
- package/runtime/packages/api/use-user.d.ts +47 -0
- package/runtime/packages/api/use-user.mjs +5 -0
- package/runtime/packages/helpers/index.d.ts +3 -0
- package/runtime/packages/helpers/index.mjs +14 -0
- package/runtime/packages/helpers/package.json +11 -0
- package/runtime/packages/helpers/phone-helper.d.ts +13 -0
- package/runtime/packages/helpers/phone-helper.mjs +58 -0
- package/runtime/packages/helpers/string-helper.d.ts +4 -0
- package/runtime/packages/helpers/string-helper.mjs +8 -0
- package/runtime/packages/layer-manager/LayerComponent.vue +54 -0
- package/runtime/packages/layer-manager/LayersProvider.vue +171 -0
- package/runtime/packages/layer-manager/index.d.ts +2 -0
- package/runtime/packages/layer-manager/index.mjs +2 -0
- package/runtime/packages/layer-manager/package.json +11 -0
- package/runtime/packages/layer-manager/types.d.ts +8 -0
- package/runtime/packages/layer-manager/types.mjs +0 -0
- package/runtime/packages/layer-manager/use-layer-actions.d.ts +23 -0
- package/runtime/packages/layer-manager/use-layer-actions.mjs +5 -0
- package/runtime/plugin.d.ts +2 -0
- package/runtime/plugin.mjs +42 -0
- package/runtime/plugins/device.d.ts +136 -0
- package/runtime/plugins/device.mjs +29 -0
- package/runtime/plugins/otp-input.d.ts +2 -0
- package/runtime/plugins/otp-input.mjs +5 -0
- package/runtime/plugins/sanitize-html.d.ts +2 -0
- package/runtime/plugins/sanitize-html.mjs +90 -0
- package/runtime/plugins/sanitize-url.d.ts +2 -0
- package/runtime/plugins/sanitize-url.mjs +9 -0
- package/runtime/server/middleware/basic-auth.d.ts +2 -0
- package/runtime/server/middleware/basic-auth.mjs +34 -0
- package/runtime/thirdparty/helpers/index.d.mts +5 -0
- package/runtime/thirdparty/helpers/index.d.ts +7 -0
- package/runtime/thirdparty/helpers/index.mjs +69 -0
- package/runtime/thirdparty/helpers/index.mjs.map +1 -0
- package/runtime/thirdparty/sanitize-url/index.d.mts +2 -0
- package/runtime/thirdparty/sanitize-url/index.d.ts +1 -0
- package/runtime/thirdparty/sanitize-url/index.mjs +98 -0
- package/runtime/thirdparty/sanitize-url/index.mjs.map +1 -0
- package/runtime/ui/varlet.d.ts +7 -0
- package/runtime/ui/varlet.mjs +13 -0
- package/types.d.mts +16 -0
- package/types.d.ts +16 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
const isoExp = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)((-(\d{2}):(\d{2})|Z)?)$/;
|
|
2
|
+
export function stringToDate(dateString) {
|
|
3
|
+
if (typeof dateString === "string") {
|
|
4
|
+
if (dateString === "0001-01-01T00:00:00Z") {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
let date = new Date(Number.NaN);
|
|
8
|
+
const parts = isoExp.exec(dateString);
|
|
9
|
+
if (parts) {
|
|
10
|
+
date = parts.at(-1) === "Z" ? new Date(
|
|
11
|
+
Date.UTC(
|
|
12
|
+
Number(parts[1]),
|
|
13
|
+
Number(parts[2]) - 1,
|
|
14
|
+
Number(parts[3]),
|
|
15
|
+
Number(parts[4]),
|
|
16
|
+
Number(parts[5]),
|
|
17
|
+
Number(parts[6])
|
|
18
|
+
)
|
|
19
|
+
) : new Date(
|
|
20
|
+
Number(parts[1]),
|
|
21
|
+
Number(parts[2]) - 1,
|
|
22
|
+
Number(parts[3]),
|
|
23
|
+
Number(parts[4]),
|
|
24
|
+
Number(parts[5]),
|
|
25
|
+
Number(parts[6])
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return date;
|
|
29
|
+
}
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
const masks = {
|
|
33
|
+
default: "dd mmm yyyy",
|
|
34
|
+
"default:R": "dd mmmm:R yyyy",
|
|
35
|
+
"default:RC": "dd mmmm:R yyyy:C",
|
|
36
|
+
defaultshortday: "d mmm yyyy",
|
|
37
|
+
"defaultshortday:C": "d mmm yyyy:C",
|
|
38
|
+
"defaultshortday:R": "d mmmm:R yyyy",
|
|
39
|
+
"defaultshortday:RC": "d mmmm:R yyyy:C",
|
|
40
|
+
shortDate: "m/d/yy",
|
|
41
|
+
mediumDate: "mmm d, yyyy",
|
|
42
|
+
longDate: "mmmm d, yyyy",
|
|
43
|
+
fullDate: "dddd, mmmm d, yyyy",
|
|
44
|
+
shortTime: "h:MM TT",
|
|
45
|
+
mediumTime: "h:MM:ss TT",
|
|
46
|
+
longTime: "h:MM:ss TT Z",
|
|
47
|
+
isoDate: "yyyy-mm-dd",
|
|
48
|
+
isoTime: "HH:MM:ss",
|
|
49
|
+
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
|
|
50
|
+
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'",
|
|
51
|
+
time: "HH:MM",
|
|
52
|
+
dateTime: "dd.mm.yyyy HH:MM",
|
|
53
|
+
clever: "clever"
|
|
54
|
+
};
|
|
55
|
+
const i18n = {
|
|
56
|
+
en: {
|
|
57
|
+
dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
|
|
58
|
+
dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
59
|
+
monthName: [
|
|
60
|
+
"January",
|
|
61
|
+
"February",
|
|
62
|
+
"March",
|
|
63
|
+
"April",
|
|
64
|
+
"May",
|
|
65
|
+
"June",
|
|
66
|
+
"July",
|
|
67
|
+
"August",
|
|
68
|
+
"September",
|
|
69
|
+
"October",
|
|
70
|
+
"November",
|
|
71
|
+
"December"
|
|
72
|
+
],
|
|
73
|
+
yesterday: "'Yesterday'",
|
|
74
|
+
today: "'Today'"
|
|
75
|
+
},
|
|
76
|
+
ru: {
|
|
77
|
+
dayNames: ["\u0432\u043E\u0441\u043A\u0440\u0435\u0441\u0435\u043D\u044C\u0435", "\u043F\u043E\u043D\u0435\u0434\u0435\u043B\u044C\u043D\u0438\u043A", "\u0432\u0442\u043E\u0440\u043D\u0438\u043A", "\u0441\u0440\u0435\u0434\u0430", "\u0447\u0435\u0442\u0432\u0435\u0440\u0433", "\u043F\u044F\u0442\u043D\u0438\u0446\u0430", "\u0441\u0443\u0431\u0431\u043E\u0442\u0430"],
|
|
78
|
+
dayNamesShort: ["\u0412\u0441", "\u041F\u043D", "\u0412\u0442", "\u0421\u0440", "\u0427\u0442", "\u041F\u0442", "\u0421\u0431"],
|
|
79
|
+
monthName: [
|
|
80
|
+
"\u044F\u043D\u0432\u0430\u0440\u044C",
|
|
81
|
+
"\u0444\u0435\u0432\u0440\u0430\u043B\u044C",
|
|
82
|
+
"\u043C\u0430\u0440\u0442",
|
|
83
|
+
"\u0430\u043F\u0440\u0435\u043B\u044C",
|
|
84
|
+
"\u043C\u0430\u0439",
|
|
85
|
+
"\u0438\u044E\u043D\u044C",
|
|
86
|
+
"\u0438\u044E\u043B\u044C",
|
|
87
|
+
"\u0430\u0432\u0433\u0443\u0441\u0442",
|
|
88
|
+
"\u0441\u0435\u043D\u0442\u044F\u0431\u0440\u044C",
|
|
89
|
+
"\u043E\u043A\u0442\u044F\u0431\u0440\u044C",
|
|
90
|
+
"\u043D\u043E\u044F\u0431\u0440\u044C",
|
|
91
|
+
"\u0434\u0435\u043A\u0430\u0431\u0440\u044C"
|
|
92
|
+
],
|
|
93
|
+
yesterday: "'\u0412\u0447\u0435\u0440\u0430'",
|
|
94
|
+
today: "'\u0421\u0435\u0433\u043E\u0434\u043D\u044F'"
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const token = /d{1,4}:?C?|m{1,4}:?[RC]?|yy(?:yy|y)?:?[RC]?|([HhMsTt])\1?|[LloZ]|"[^"]*"|'[^']*'/g;
|
|
98
|
+
const timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
|
|
99
|
+
const timezoneClip = /[^-+\dA-Z]/g;
|
|
100
|
+
const pad = (value, len = 2) => {
|
|
101
|
+
let val = String(value);
|
|
102
|
+
while (val.length < len) {
|
|
103
|
+
val = `0${val}`;
|
|
104
|
+
}
|
|
105
|
+
return val;
|
|
106
|
+
};
|
|
107
|
+
const createOnlyDate = (date) => {
|
|
108
|
+
const d = new Date(date.getTime());
|
|
109
|
+
d.setHours(0, 0, 0, 0);
|
|
110
|
+
return d;
|
|
111
|
+
};
|
|
112
|
+
const countCalendarDayBetween = (date1, date2) => {
|
|
113
|
+
const day1 = createOnlyDate(date1);
|
|
114
|
+
const day2 = createOnlyDate(date2);
|
|
115
|
+
const dayCount = (day1.getTime() - day2.getTime()) / (24 * 60 * 60 * 1e3);
|
|
116
|
+
return Math.abs(dayCount);
|
|
117
|
+
};
|
|
118
|
+
const checkIsYesterday = (now, date) => {
|
|
119
|
+
return now > date && countCalendarDayBetween(now, date) === 1;
|
|
120
|
+
};
|
|
121
|
+
const getDayName = (day, locale, short = false) => {
|
|
122
|
+
return short ? i18n[locale].dayNamesShort[day] : i18n[locale].dayNames[day];
|
|
123
|
+
};
|
|
124
|
+
const monthDeclination = (month, locale) => {
|
|
125
|
+
return month === 3 || month === 8 ? `${i18n[locale].monthName[month - 1]}\u0430` : `${i18n[locale].monthName[month - 1].slice(0, -1)}\u044F`;
|
|
126
|
+
};
|
|
127
|
+
const getMonthName = (month, locale, countChar = 0, declination = false) => {
|
|
128
|
+
const monthName = locale === "ru" && declination ? monthDeclination(month, locale) : i18n[locale].monthName[month - 1];
|
|
129
|
+
if (countChar && countChar > 0) {
|
|
130
|
+
return monthName.slice(0, 3);
|
|
131
|
+
}
|
|
132
|
+
return monthName;
|
|
133
|
+
};
|
|
134
|
+
const getMask = (format, locale, isCurrentDay, isYesterday, isCurrentYear) => {
|
|
135
|
+
const defaultFormat = {
|
|
136
|
+
en: "mmmm d, yyyy",
|
|
137
|
+
ru: "d mmmm:R yyyy"
|
|
138
|
+
};
|
|
139
|
+
const formats = {
|
|
140
|
+
default: defaultFormat,
|
|
141
|
+
get clever() {
|
|
142
|
+
if (isCurrentDay) {
|
|
143
|
+
return {
|
|
144
|
+
en: `${i18n.en.today} 'at' H:MM TT`,
|
|
145
|
+
ru: `${i18n.ru.today}, HH:MM`
|
|
146
|
+
};
|
|
147
|
+
} else if (isYesterday) {
|
|
148
|
+
return {
|
|
149
|
+
en: `${i18n.en.yesterday} 'at' H:MM TT`,
|
|
150
|
+
ru: `${i18n.ru.yesterday}, HH:MM`
|
|
151
|
+
};
|
|
152
|
+
} else if (isCurrentYear) {
|
|
153
|
+
return {
|
|
154
|
+
en: "mmmm d",
|
|
155
|
+
ru: "d mmmm:R"
|
|
156
|
+
};
|
|
157
|
+
} else {
|
|
158
|
+
return defaultFormat;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
let mask = String(masks[format] ?? (format || masks.default));
|
|
163
|
+
if (mask.startsWith("UTC:")) {
|
|
164
|
+
mask = mask.slice(4);
|
|
165
|
+
}
|
|
166
|
+
if (formats[format]?.[locale]) {
|
|
167
|
+
return formats[format]?.[locale] ?? "";
|
|
168
|
+
}
|
|
169
|
+
return mask;
|
|
170
|
+
};
|
|
171
|
+
export function dateFormat(date = /* @__PURE__ */ new Date(), format = "default", locale = "en", dateTimeNow) {
|
|
172
|
+
date = date ? new Date(date) : /* @__PURE__ */ new Date();
|
|
173
|
+
if (Number.isNaN(date)) {
|
|
174
|
+
throw new SyntaxError("invalid date");
|
|
175
|
+
}
|
|
176
|
+
const utc = (masks[format] ?? format).startsWith("UTC:");
|
|
177
|
+
const now = dateTimeNow ?? /* @__PURE__ */ new Date();
|
|
178
|
+
const currentYear = utc ? now.getUTCFullYear() : now.getFullYear();
|
|
179
|
+
const currentMonth = utc ? now.getUTCMonth() : now.getMonth();
|
|
180
|
+
const currentDate = utc ? now.getUTCDate() : now.getDate();
|
|
181
|
+
const isYesterday = checkIsYesterday(now, date);
|
|
182
|
+
const d = utc ? date.getUTCDate() : date.getDate();
|
|
183
|
+
const D = utc ? date.getUTCDay() : date.getDay();
|
|
184
|
+
const m = utc ? date.getUTCMonth() : date.getMonth();
|
|
185
|
+
const y = utc ? date.getUTCFullYear() : date.getFullYear();
|
|
186
|
+
const isCurrentYear = y === currentYear;
|
|
187
|
+
const isCurrentDay = y === currentYear && m === currentMonth && d === currentDate;
|
|
188
|
+
const H = utc ? date.getUTCHours() : date.getHours();
|
|
189
|
+
const M = utc ? date.getUTCMinutes() : date.getMinutes();
|
|
190
|
+
const s = utc ? date.getUTCSeconds() : date.getSeconds();
|
|
191
|
+
const L = utc ? date.getUTCMilliseconds() : date.getMilliseconds();
|
|
192
|
+
const o = utc ? 0 : date.getTimezoneOffset();
|
|
193
|
+
const flags = {
|
|
194
|
+
d,
|
|
195
|
+
dd: pad(d),
|
|
196
|
+
ddd: getDayName(D, locale, true),
|
|
197
|
+
dddd: getDayName(D, locale),
|
|
198
|
+
m: m + 1,
|
|
199
|
+
mm: pad(m + 1),
|
|
200
|
+
mmm: getMonthName(m + 1, locale, 3),
|
|
201
|
+
mmmm: getMonthName(m + 1, locale),
|
|
202
|
+
"mmmm:R": getMonthName(m + 1, locale, 0, true),
|
|
203
|
+
"mm:C": isYesterday || isCurrentDay ? "" : pad(m + 1),
|
|
204
|
+
"yy:C": isCurrentYear ? "" : String(y).slice(2),
|
|
205
|
+
yy: String(y).slice(2),
|
|
206
|
+
"yyyy:C": isCurrentYear ? "" : y,
|
|
207
|
+
yyyy: y,
|
|
208
|
+
yyy: y,
|
|
209
|
+
h: H % 12 || 12,
|
|
210
|
+
hh: pad(H % 12 || 12),
|
|
211
|
+
H,
|
|
212
|
+
HH: pad(H),
|
|
213
|
+
M,
|
|
214
|
+
MM: pad(M),
|
|
215
|
+
s,
|
|
216
|
+
ss: pad(s),
|
|
217
|
+
l: pad(L, 3),
|
|
218
|
+
L: pad(L > 99 ? Math.round(L / 10) : L),
|
|
219
|
+
tt: H < 12 ? "am" : "pm",
|
|
220
|
+
TT: H < 12 ? "AM" : "PM",
|
|
221
|
+
Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop()?.replace(timezoneClip, ""),
|
|
222
|
+
o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4)
|
|
223
|
+
// eslint-disable-line no-mixed-operators
|
|
224
|
+
};
|
|
225
|
+
const mask = getMask(format, locale, isCurrentDay, isYesterday, isCurrentYear);
|
|
226
|
+
const parsedDate = mask.replaceAll(token, ($0) => {
|
|
227
|
+
return $0 in flags ? String(flags[$0]) : $0.slice(1, -1);
|
|
228
|
+
});
|
|
229
|
+
return parsedDate.trim();
|
|
230
|
+
}
|
|
231
|
+
export const dateHelper = { stringToDate, dateFormat };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { apiHelper } from "./api-helper.mjs";
|
|
2
|
+
export { dateHelper } from "./date-helper.mjs";
|
|
3
|
+
export { device } from "./current-device.mjs";
|
|
4
|
+
export function once(fn) {
|
|
5
|
+
let executed = false;
|
|
6
|
+
let result;
|
|
7
|
+
return function(...args) {
|
|
8
|
+
if (!executed) {
|
|
9
|
+
executed = true;
|
|
10
|
+
result = fn(...args);
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ActiveSession, Fetch, FetchConfig } from "./types";
|
|
2
|
+
export type { ActiveSession, Fetch, FetchConfig };
|
|
3
|
+
export { useUser, type User } from "./use-user";
|
|
4
|
+
export { getVisitor, getVisitorId, type Visitor } from "./get-visitor";
|
|
5
|
+
export declare function initApiFetch($fetch: Fetch, config: FetchConfig): {
|
|
6
|
+
fetch: Fetch;
|
|
7
|
+
getUser: () => Promise<{
|
|
8
|
+
token?: string | null;
|
|
9
|
+
user?: import("src/runtime/api").User | null;
|
|
10
|
+
}>;
|
|
11
|
+
getVisitorIdentifier: () => Promise<void>;
|
|
12
|
+
useApiFetch: () => Fetch;
|
|
13
|
+
userActions: {
|
|
14
|
+
signIn: (body: {
|
|
15
|
+
username: string;
|
|
16
|
+
password: string;
|
|
17
|
+
rememberMe: boolean;
|
|
18
|
+
}) => Promise<{
|
|
19
|
+
token?: string | null;
|
|
20
|
+
user?: import("src/runtime/api").User | null;
|
|
21
|
+
} | {
|
|
22
|
+
key: string;
|
|
23
|
+
}>;
|
|
24
|
+
logout: () => Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createApiFetch, flush, setToken } from "./api-client.mjs";
|
|
2
|
+
import { createApiGetUser } from "./get-user.mjs";
|
|
3
|
+
import { createApiGetVisitorIdentifier } from "./get-visitor.mjs";
|
|
4
|
+
import { createApiRefreshToken } from "./refresh-token.mjs";
|
|
5
|
+
import { useApiActions } from "./use-api-actions.mjs";
|
|
6
|
+
import { useUser } from "./use-user.mjs";
|
|
7
|
+
export { useUser } from "./use-user.mjs";
|
|
8
|
+
export { getVisitor, getVisitorId } from "./get-visitor.mjs";
|
|
9
|
+
const apiActions = useApiActions();
|
|
10
|
+
export function initApiFetch($fetch, config) {
|
|
11
|
+
const fetch = createApiFetch($fetch, config);
|
|
12
|
+
const getUser = createApiGetUser(fetch, config);
|
|
13
|
+
const getVisitorIdentifier = createApiGetVisitorIdentifier(config);
|
|
14
|
+
const refreshToken = createApiRefreshToken(fetch, config);
|
|
15
|
+
apiActions.value = {
|
|
16
|
+
initialized: true,
|
|
17
|
+
getVisitorIdentifier,
|
|
18
|
+
refreshToken
|
|
19
|
+
};
|
|
20
|
+
const useApiFetch = () => fetch;
|
|
21
|
+
const userActions = {
|
|
22
|
+
signIn: async (body) => {
|
|
23
|
+
const response = await fetch("/account/0/signin", {
|
|
24
|
+
body,
|
|
25
|
+
method: "POST",
|
|
26
|
+
...config.useRequestHeaders?.(["cookie"]) ?? {}
|
|
27
|
+
});
|
|
28
|
+
const { accessToken, key, ...rest } = response;
|
|
29
|
+
if (!accessToken) {
|
|
30
|
+
if (key) {
|
|
31
|
+
return { key };
|
|
32
|
+
}
|
|
33
|
+
throw rest;
|
|
34
|
+
}
|
|
35
|
+
setToken(accessToken);
|
|
36
|
+
return await getUser();
|
|
37
|
+
},
|
|
38
|
+
logout: async () => {
|
|
39
|
+
const user = useUser();
|
|
40
|
+
try {
|
|
41
|
+
if (user.value) {
|
|
42
|
+
await fetch("/account/1/logout", { method: "POST" });
|
|
43
|
+
}
|
|
44
|
+
} finally {
|
|
45
|
+
flush();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return { fetch, getUser, getVisitorIdentifier, useApiFetch, userActions };
|
|
50
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@inzombieland/api",
|
|
3
|
+
"version": "1.15.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"main": "./index.mjs",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@fingerprintjs/fingerprintjs": "^4.6.2",
|
|
13
|
+
"@vueuse/core": "^12.8.2",
|
|
14
|
+
"centrifuge": "^5.3.4",
|
|
15
|
+
"ofetch": "^1.4.1",
|
|
16
|
+
"rxjs": "^7.8.2",
|
|
17
|
+
"vue": "^3.5.13",
|
|
18
|
+
"zod": "^3.24.2"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { flush, setToken } from "./api-client.mjs";
|
|
2
|
+
import { once } from "./helpers/index.mjs";
|
|
3
|
+
let refreshingTokenInProgress = false;
|
|
4
|
+
let refreshingTokenPromise = null;
|
|
5
|
+
function refreshToken(fetch, config) {
|
|
6
|
+
if (refreshingTokenInProgress && refreshingTokenPromise) {
|
|
7
|
+
return refreshingTokenPromise;
|
|
8
|
+
}
|
|
9
|
+
return refreshingTokenStart(fetch, config);
|
|
10
|
+
}
|
|
11
|
+
function refreshingTokenStart(fetch, config) {
|
|
12
|
+
refreshingTokenInProgress = true;
|
|
13
|
+
refreshingTokenPromise = refreshTokenRequest(fetch, config).finally(() => {
|
|
14
|
+
refreshingTokenInProgress = false;
|
|
15
|
+
refreshingTokenPromise = null;
|
|
16
|
+
});
|
|
17
|
+
return refreshingTokenPromise;
|
|
18
|
+
}
|
|
19
|
+
async function refreshTokenRequest(fetch, config) {
|
|
20
|
+
setToken(null);
|
|
21
|
+
try {
|
|
22
|
+
const { accessToken } = await fetch(
|
|
23
|
+
"/account/0/token",
|
|
24
|
+
config.useRequestHeaders?.(["cookie"])
|
|
25
|
+
);
|
|
26
|
+
if (!accessToken) {
|
|
27
|
+
flush();
|
|
28
|
+
throw new Error("No access token");
|
|
29
|
+
}
|
|
30
|
+
setToken(accessToken);
|
|
31
|
+
return accessToken;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
flush();
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export const createApiRefreshToken = once((fetch, config) => {
|
|
38
|
+
return () => refreshToken(fetch, config);
|
|
39
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { FetchOptions, FetchRequest, FetchResponse } from "ofetch";
|
|
2
|
+
import type { ZodError } from "zod";
|
|
3
|
+
export type FetchExtraOptions = FetchOptions & {
|
|
4
|
+
checkResponse?: (data: unknown) => {
|
|
5
|
+
success: boolean;
|
|
6
|
+
error?: ZodError;
|
|
7
|
+
data?: unknown;
|
|
8
|
+
};
|
|
9
|
+
headers?: HeadersInit & {
|
|
10
|
+
event?: string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export interface ResponseMap {
|
|
14
|
+
blob: Blob;
|
|
15
|
+
text: string;
|
|
16
|
+
arrayBuffer: ArrayBuffer;
|
|
17
|
+
stream: ReadableStream<Uint8Array>;
|
|
18
|
+
}
|
|
19
|
+
export type ResponseType = keyof ResponseMap | "json";
|
|
20
|
+
export type MappedType<R extends ResponseType, JsonType = any> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType;
|
|
21
|
+
export interface Fetch {
|
|
22
|
+
<T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R> & FetchExtraOptions): Promise<MappedType<R, T>>;
|
|
23
|
+
raw: <T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R> & FetchExtraOptions) => Promise<FetchResponse<MappedType<R, T>>>;
|
|
24
|
+
native: typeof globalThis.fetch;
|
|
25
|
+
create: (defaults: FetchOptions & FetchExtraOptions) => Fetch;
|
|
26
|
+
}
|
|
27
|
+
export type FetchConfig = {
|
|
28
|
+
apiBaseURL: string;
|
|
29
|
+
cometServerURL: string;
|
|
30
|
+
appClientID: string;
|
|
31
|
+
appClientSecret: string;
|
|
32
|
+
isMobileApp: boolean;
|
|
33
|
+
isDesktopApp: boolean;
|
|
34
|
+
appName: string;
|
|
35
|
+
getHostname: () => string;
|
|
36
|
+
getDevice: () => Promise<string>;
|
|
37
|
+
useRequestHeaders?: (include: string[]) => {};
|
|
38
|
+
};
|
|
39
|
+
export type ActiveSession = {
|
|
40
|
+
id: string;
|
|
41
|
+
visitorId: string;
|
|
42
|
+
device: string;
|
|
43
|
+
appName: string;
|
|
44
|
+
location: string;
|
|
45
|
+
ipAddress: string;
|
|
46
|
+
isCurrentSession?: boolean;
|
|
47
|
+
isOnline?: boolean;
|
|
48
|
+
signedOut?: boolean;
|
|
49
|
+
dateTime: Date;
|
|
50
|
+
firstSignIn: Date;
|
|
51
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const useApiActions: () => import("vue").Ref<{
|
|
2
|
+
initialized: boolean;
|
|
3
|
+
getVisitorIdentifier?: (() => Promise<void>) | undefined;
|
|
4
|
+
refreshToken?: (() => Promise<string>) | undefined;
|
|
5
|
+
}, {
|
|
6
|
+
initialized: boolean;
|
|
7
|
+
getVisitorIdentifier?: () => Promise<void>;
|
|
8
|
+
refreshToken?: () => Promise<string>;
|
|
9
|
+
} | {
|
|
10
|
+
initialized: boolean;
|
|
11
|
+
getVisitorIdentifier?: (() => Promise<void>) | undefined;
|
|
12
|
+
refreshToken?: (() => Promise<string>) | undefined;
|
|
13
|
+
}>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export declare type User = {
|
|
2
|
+
id: string;
|
|
3
|
+
firstName: string;
|
|
4
|
+
lastName: string;
|
|
5
|
+
middleName: string;
|
|
6
|
+
email: string;
|
|
7
|
+
emailVerified: boolean;
|
|
8
|
+
phone: string;
|
|
9
|
+
phoneVerified: boolean;
|
|
10
|
+
gender: "" | "male" | "female";
|
|
11
|
+
birthday: Date;
|
|
12
|
+
passwordLastSet: Date;
|
|
13
|
+
theme: "light" | "dark";
|
|
14
|
+
locale: string;
|
|
15
|
+
avatar: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function useUser(): import("vue").Ref<{
|
|
18
|
+
id: string;
|
|
19
|
+
firstName: string;
|
|
20
|
+
lastName: string;
|
|
21
|
+
middleName: string;
|
|
22
|
+
email: string;
|
|
23
|
+
emailVerified: boolean;
|
|
24
|
+
phone: string;
|
|
25
|
+
phoneVerified: boolean;
|
|
26
|
+
gender: "" | "male" | "female";
|
|
27
|
+
birthday: Date;
|
|
28
|
+
passwordLastSet: Date;
|
|
29
|
+
theme: "light" | "dark";
|
|
30
|
+
locale: string;
|
|
31
|
+
avatar: string;
|
|
32
|
+
} | null | undefined, User | {
|
|
33
|
+
id: string;
|
|
34
|
+
firstName: string;
|
|
35
|
+
lastName: string;
|
|
36
|
+
middleName: string;
|
|
37
|
+
email: string;
|
|
38
|
+
emailVerified: boolean;
|
|
39
|
+
phone: string;
|
|
40
|
+
phoneVerified: boolean;
|
|
41
|
+
gender: "" | "male" | "female";
|
|
42
|
+
birthday: Date;
|
|
43
|
+
passwordLastSet: Date;
|
|
44
|
+
theme: "light" | "dark";
|
|
45
|
+
locale: string;
|
|
46
|
+
avatar: string;
|
|
47
|
+
} | null | undefined>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { phoneHelper } from "./phone-helper.mjs";
|
|
2
|
+
export { stringHelper } from "./string-helper.mjs";
|
|
3
|
+
export function once(fn) {
|
|
4
|
+
let executed = false;
|
|
5
|
+
let result;
|
|
6
|
+
return function(...args) {
|
|
7
|
+
if (!executed) {
|
|
8
|
+
executed = true;
|
|
9
|
+
result = fn(...args);
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare function getPlainPhoneNumber(value: string): string;
|
|
2
|
+
/**
|
|
3
|
+
* Преобразование номера телефона в формат: +7 (987) 777-88-99
|
|
4
|
+
* @param value - номер телефона
|
|
5
|
+
* @param addLastSymbol -
|
|
6
|
+
*/
|
|
7
|
+
export declare function convertToPhone(value: string, addLastSymbol?: boolean): string;
|
|
8
|
+
export declare function maskPhone(phone?: string): string;
|
|
9
|
+
export declare const phoneHelper: {
|
|
10
|
+
getPlainPhoneNumber: typeof getPlainPhoneNumber;
|
|
11
|
+
convertToPhone: typeof convertToPhone;
|
|
12
|
+
maskPhone: typeof maskPhone;
|
|
13
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export function getPlainPhoneNumber(value) {
|
|
2
|
+
const symbol = "+ ";
|
|
3
|
+
const tempNumber = value.replace(symbol, "").replace(symbol.trim(), "");
|
|
4
|
+
return tempNumber.replaceAll(/\D+/g, "");
|
|
5
|
+
}
|
|
6
|
+
export function convertToPhone(value, addLastSymbol = true) {
|
|
7
|
+
const plainNumber = getPlainPhoneNumber(value);
|
|
8
|
+
if (plainNumber.length === 0) {
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
const mask = getCountryMask(plainNumber);
|
|
12
|
+
return getFormattedByMask(plainNumber, mask, addLastSymbol);
|
|
13
|
+
}
|
|
14
|
+
function getCountryMask(value = "") {
|
|
15
|
+
if (value.length === 0) {
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
if (value.startsWith("7")) {
|
|
19
|
+
return "+# (###) ###-##-###########";
|
|
20
|
+
}
|
|
21
|
+
if (value.startsWith("8")) {
|
|
22
|
+
return "# (###) ###-##-###########";
|
|
23
|
+
}
|
|
24
|
+
if (value.startsWith("1")) {
|
|
25
|
+
return "+# ### ### ## ###########";
|
|
26
|
+
}
|
|
27
|
+
if (value.startsWith("375")) {
|
|
28
|
+
return "+### ## ### ## ##########";
|
|
29
|
+
}
|
|
30
|
+
if (value.startsWith("380")) {
|
|
31
|
+
return "+### ## ### ## ##########";
|
|
32
|
+
}
|
|
33
|
+
return "+####################";
|
|
34
|
+
}
|
|
35
|
+
function getFormattedByMask(plainNumber, mask, addLastSymbol) {
|
|
36
|
+
let number = "";
|
|
37
|
+
let symbolIterator = 0;
|
|
38
|
+
[...mask].forEach((value) => {
|
|
39
|
+
if (plainNumber.length >= symbolIterator) {
|
|
40
|
+
if (value === "#") {
|
|
41
|
+
if (plainNumber.length > symbolIterator) {
|
|
42
|
+
number += plainNumber[symbolIterator];
|
|
43
|
+
}
|
|
44
|
+
symbolIterator++;
|
|
45
|
+
} else {
|
|
46
|
+
const notPasteSymbol = symbolIterator === plainNumber.length && !addLastSymbol;
|
|
47
|
+
if (!notPasteSymbol) {
|
|
48
|
+
number += value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return number;
|
|
54
|
+
}
|
|
55
|
+
export function maskPhone(phone = "") {
|
|
56
|
+
return `+${phone.slice(0, 1)} *****${phone.slice(-4)}`;
|
|
57
|
+
}
|
|
58
|
+
export const phoneHelper = { getPlainPhoneNumber, convertToPhone, maskPhone };
|