@sendelius/vue 0.4.3 → 0.5.0

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/api/api.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export function initApi(): import('axios').AxiosInstance;
2
- export function getClient(): any;
1
+ export function initApi(): {};
2
+ export function getClient(name?: string): any;
@@ -1,3 +1,3 @@
1
1
  export { initApi, getClient } from './api.js';
2
- export { get, post, put, patch, remove, request } from './requests.js';
2
+ export { useApi } from './requests.js';
3
3
  export { ApiError } from './errors.js';
@@ -1,6 +1,8 @@
1
- export function get(url: any, params?: {}, config?: {}): Promise<any>;
2
- export function post(url: any, body?: {}, config?: {}): Promise<any>;
3
- export function put(url: any, body?: {}, config?: {}): Promise<any>;
4
- export function patch(url: any, body?: {}, config?: {}): Promise<any>;
5
- export function remove(url: any, body?: {}, config?: {}): Promise<any>;
6
- export function request(method: any, url: any, data?: {}, config?: {}): Promise<any>;
1
+ export function useApi(client?: string): {
2
+ get: (url: any, body?: {}, config?: {}) => Promise<any>;
3
+ post: (url: any, body?: {}, config?: {}) => Promise<any>;
4
+ put: (url: any, body?: {}, config?: {}) => Promise<any>;
5
+ patch: (url: any, body?: {}, config?: {}) => Promise<any>;
6
+ remove: (url: any, body?: {}, config?: {}) => Promise<any>;
7
+ request: (method: any, url: any, data?: {}, config?: {}) => Promise<any>;
8
+ };
@@ -1,7 +1,9 @@
1
1
  export function configure(options?: {}): void;
2
2
  export function getConfig(): {
3
3
  router: any;
4
- baseURL: string;
4
+ api: {
5
+ default: string;
6
+ };
5
7
  baseTitle: string;
6
8
  baseTitleSeparator: string;
7
9
  routeAfterLogin: string;
package/dist/index.js CHANGED
@@ -20,10 +20,10 @@ import { ref as y } from "vue";
20
20
  // src/config/config.js
21
21
  var b = {
22
22
  router: null,
23
- baseURL: "",
23
+ api: { default: "" },
24
24
  baseTitle: "",
25
25
  baseTitleSeparator: " - ",
26
- routeAfterLogin: "/app/",
26
+ routeAfterLogin: "/",
27
27
  routeAfterLogout: "/",
28
28
  urlUserInfo: "/auth/me",
29
29
  urlLogin: "/auth/login",
@@ -35,16 +35,24 @@ var b = {
35
35
  ...b,
36
36
  ...e
37
37
  };
38
- }, S = () => b, C = null, w = () => (C = _.create({
39
- baseURL: S().baseURL,
40
- withCredentials: !0,
41
- headers: {
42
- "Content-Type": "application/json",
43
- Accept: "application/json"
44
- }
45
- }), C), T = () => {
46
- if (!C) throw Error("@sendelius/vue: initApi() должен быть запущен до getClient().");
47
- return C;
38
+ }, S = () => b, C = {}, w = () => {
39
+ let e = S(), t = {
40
+ withCredentials: !0,
41
+ headers: {
42
+ "Content-Type": "application/json",
43
+ Accept: "application/json"
44
+ }
45
+ };
46
+ return Object.entries(e.api).forEach(([e, n]) => {
47
+ C[e] = _.create({
48
+ ...t,
49
+ baseURL: n
50
+ });
51
+ }), C;
52
+ }, T = (e = "default") => {
53
+ let t = C[e] || C.default;
54
+ if (!t) throw Error("@sendelius/vue: API-клиенты не инициализированы. Вызови initApi().");
55
+ return t;
48
56
  }, E = class extends Error {
49
57
  constructor({ status: e = null, data: t = null } = {}) {
50
58
  let n = t?.error, r = {
@@ -56,46 +64,41 @@ var b = {
56
64
  }
57
65
  };
58
66
  // src/api/requests.js
59
- async function D(e, t = {}, n = {}) {
60
- return M("get", e, {
61
- params: t,
62
- ...n
63
- });
64
- }
65
- async function O(e, t = {}, n = {}) {
66
- return M("post", e, t, n);
67
- }
68
- async function k(e, t = {}, n = {}) {
69
- return M("put", e, t, n);
70
- }
71
- async function A(e, t = {}, n = {}) {
72
- return M("patch", e, t, n);
73
- }
74
- async function j(e, t = {}, n = {}) {
75
- return M("delete", e, t, n);
76
- }
77
- async function M(e, t, n = {}, r = {}) {
78
- let i;
67
+ async function D(e, t, n = {}, r = {}, i = "default") {
68
+ let a = T(i), o;
79
69
  try {
80
- i = e === "get" ? await T().get(t, n) : e === "delete" ? await T().delete(t, {
70
+ o = e === "get" ? await a.get(t, {
71
+ params: n,
72
+ ...r
73
+ }) : e === "delete" ? await a.delete(t, {
81
74
  data: n,
82
75
  ...r
83
- }) : await T()[e](t, n, r);
76
+ }) : await a[e](t, n, r);
84
77
  } catch (e) {
85
78
  throw e.response?.status === 401 && S().onUnauthorized?.(), new E({
86
79
  status: e.response?.status ?? null,
87
80
  data: e.response?.data ?? null
88
81
  });
89
82
  }
90
- let a = i.data;
91
- if (a?.status !== "success") throw new E({
92
- status: i.status,
93
- data: a
83
+ let s = o.data;
84
+ if (s?.status !== "success") throw new E({
85
+ status: o.status,
86
+ data: s
94
87
  });
95
- return a;
88
+ return s;
89
+ }
90
+ function O(e = "default") {
91
+ return {
92
+ get: (t, n = {}, r = {}) => D("get", t, n, r, e),
93
+ post: (t, n = {}, r = {}) => D("post", t, n, r, e),
94
+ put: (t, n = {}, r = {}) => D("put", t, n, r, e),
95
+ patch: (t, n = {}, r = {}) => D("patch", t, n, r, e),
96
+ remove: (t, n = {}, r = {}) => D("delete", t, n, r, e),
97
+ request: (t, n, r = {}, i = {}) => D(t, n, r, i, e)
98
+ };
96
99
  }
97
100
  // src/auth/auth.js
98
- var N = v("auth", {
101
+ var { get: k, post: A, remove: j } = O("auth"), M = v("auth", {
99
102
  state: () => ({
100
103
  user: {},
101
104
  avatar: {
@@ -110,7 +113,7 @@ var N = v("auth", {
110
113
  async init(e = !0) {
111
114
  if (this.isInit && e) return this.isAuth;
112
115
  try {
113
- let e = await D(S().urlUserInfo);
116
+ let e = await k(S().urlUserInfo);
114
117
  e?.user ? (this.user = e.user, this.avatar = e.user.avatar ?? this.avatar, this.isAuth = !0) : this.clear();
115
118
  } catch {
116
119
  this.clear();
@@ -122,7 +125,7 @@ var N = v("auth", {
122
125
  async login(e, t, n) {
123
126
  n ||= "";
124
127
  try {
125
- await O(S().urlLogin, {
128
+ await A(S().urlLogin, {
126
129
  login: e,
127
130
  password: t,
128
131
  captcha: n
@@ -151,60 +154,61 @@ var N = v("auth", {
151
154
  }
152
155
  }
153
156
  });
154
- function P() {
155
- return N();
157
+ function N() {
158
+ return M();
156
159
  }
157
160
  // src/captcha/captcha.js
158
- async function F() {
159
- let e = await I(), t = await R(e.challenge);
160
- return L(e.id, t);
161
- }
161
+ var { get: P, post: F } = O("auth");
162
162
  async function I() {
163
- return D("/captcha/challenge");
163
+ let e = await L(), t = await z(e.challenge);
164
+ return R(e.id, t);
165
+ }
166
+ async function L() {
167
+ return P("/captcha/challenge");
164
168
  }
165
- async function L(e, t) {
166
- return (await O("/captcha/verify", {
169
+ async function R(e, t) {
170
+ return (await F("/captcha/verify", {
167
171
  id: e,
168
172
  nonce: t
169
173
  }))?.token;
170
174
  }
171
- async function R(e) {
175
+ async function z(e) {
172
176
  let t = "0".repeat(4), n = 0;
173
177
  for (;;) {
174
- if ((await z(`${e}:${n}`)).startsWith(t)) return n;
178
+ if ((await B(`${e}:${n}`)).startsWith(t)) return n;
175
179
  n++;
176
180
  }
177
181
  }
178
- async function z(e) {
182
+ async function B(e) {
179
183
  let t = new TextEncoder().encode(e), n = await crypto.subtle.digest("SHA-256", t);
180
184
  return Array.from(new Uint8Array(n)).map((e) => e.toString(16).padStart(2, "0")).join("");
181
185
  }
182
186
  // src/captcha/store.js
183
- var B = v("captcha", () => {
187
+ var V = v("captcha", () => {
184
188
  let e = y(""), t = y(!1), n = y(!1), r = null;
185
189
  return {
186
190
  token: e,
187
191
  isInit: t,
188
192
  isLoading: n,
189
- init: async () => t.value ? e.value : r || (n.value = !0, r = F().then((n) => (e.value = n, t.value = !0, n)).finally(() => {
193
+ init: async () => t.value ? e.value : r || (n.value = !0, r = I().then((n) => (e.value = n, t.value = !0, n)).finally(() => {
190
194
  n.value = !1, r = null;
191
195
  }), r)
192
196
  };
193
197
  });
194
- function V() {
195
- return B();
198
+ function H() {
199
+ return V();
196
200
  }
197
201
  // src/helpers/declension.js
198
- function H(e, t, n, r) {
202
+ function U(e, t, n, r) {
199
203
  let i = Math.abs(e) % 100, a = i % 10, o;
200
204
  return o = i >= 11 && i <= 19 ? r : a === 1 ? t : a >= 2 && a <= 4 ? n : r, `${e} ${o}`;
201
205
  }
202
206
  // src/helpers/title.js
203
- function U(e, t, n) {
207
+ function W(e, t, n) {
204
208
  t ??= S().baseTitle, n ??= S().baseTitleSeparator, t || (n = ""), document.title = e ? `${e}${n}${t}` : t;
205
209
  }
206
210
  // src/helpers/objects.js
207
- function W(e) {
211
+ function G(e) {
208
212
  return Array.isArray(e) ? e.length === 0 : e && typeof e == "object" ? Object.keys(e).length === 0 : !0;
209
213
  }
210
- export { E as ApiError, i as SndAutocomplete, t as SndBtn, u as SndCheckbox, n as SndCookie, f as SndError, r as SndInput, s as SndInputCity, c as SndInputDate, l as SndInputPassword, a as SndInputPhone, h as SndPopup, e as SndProgress, o as SndSelect, p as SndSuccess, g as SndTable, d as SndWarning, x as configure, H as declension, D as get, T as getClient, S as getConfig, w as initApi, W as isEmpty, A as patch, O as post, k as put, j as remove, M as request, U as setTitle, P as useAuth, V as useCaptcha, m as usePopup };
214
+ export { E as ApiError, i as SndAutocomplete, t as SndBtn, u as SndCheckbox, n as SndCookie, f as SndError, r as SndInput, s as SndInputCity, c as SndInputDate, l as SndInputPassword, a as SndInputPhone, h as SndPopup, e as SndProgress, o as SndSelect, p as SndSuccess, g as SndTable, d as SndWarning, x as configure, U as declension, T as getClient, S as getConfig, w as initApi, G as isEmpty, W as setTitle, O as useApi, N as useAuth, H as useCaptcha, m as usePopup };
package/package.json CHANGED
@@ -48,5 +48,5 @@
48
48
  "@parcel/watcher@2.6.0": true,
49
49
  "vue-demi@0.14.10": true
50
50
  },
51
- "version": "0.4.3"
51
+ "version": "0.5.0"
52
52
  }