@abcagency/hc-ui-components 1.0.0 → 1.0.2

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.js CHANGED
@@ -563,7 +563,25 @@ var getClientAuthKey = function getClientAuthKey() {
563
563
  return clientAuthKey;
564
564
  };
565
565
 
566
- var baseURL = "https://https://api.myhirecontrol.com";
566
+ var baseURL = "https://api.myhirecontrol.com";
567
+ var memoryStorage = {
568
+ authToken: null,
569
+ tokenExpiration: null
570
+ };
571
+ function setStorage(key, value) {
572
+ try {
573
+ sessionStorage.setItem(key, value);
574
+ } catch (error) {
575
+ memoryStorage[key] = value;
576
+ }
577
+ }
578
+ function getStorage(key) {
579
+ try {
580
+ return sessionStorage.getItem(key) || memoryStorage[key];
581
+ } catch (error) {
582
+ return memoryStorage[key];
583
+ }
584
+ }
567
585
  var login = /*#__PURE__*/function () {
568
586
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
569
587
  var clientAuthKey, response, data;
@@ -592,8 +610,8 @@ var login = /*#__PURE__*/function () {
592
610
  _context.next = 12;
593
611
  break;
594
612
  }
595
- sessionStorage.setItem('authToken', data.token);
596
- sessionStorage.setItem('tokenExpiration', data.expiration);
613
+ setStorage('authToken', data.token);
614
+ setStorage('tokenExpiration', data.expiration);
597
615
  return _context.abrupt("return", {
598
616
  token: data.token,
599
617
  expiration: data.expiration
@@ -631,8 +649,8 @@ var fetchWithAuth = /*#__PURE__*/function () {
631
649
  while (1) switch (_context2.prev = _context2.next) {
632
650
  case 0:
633
651
  options = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
634
- token = sessionStorage.getItem('authToken');
635
- expirationDateTime = sessionStorage.getItem('tokenExpiration');
652
+ token = getStorage('authToken');
653
+ expirationDateTime = getStorage('tokenExpiration');
636
654
  currentTime = new Date();
637
655
  if (!(!token || !expirationDateTime || new Date(expirationDateTime) <= currentTime)) {
638
656
  _context2.next = 9;
@@ -1,606 +1,116 @@
1
- function ownKeys(e, r) {
2
- var t = Object.keys(e);
3
- if (Object.getOwnPropertySymbols) {
4
- var o = Object.getOwnPropertySymbols(e);
5
- r && (o = o.filter(function (r) {
6
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
7
- })), t.push.apply(t, o);
8
- }
9
- return t;
10
- }
11
- function _objectSpread2(e) {
12
- for (var r = 1; r < arguments.length; r++) {
13
- var t = null != arguments[r] ? arguments[r] : {};
14
- r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
15
- _defineProperty(e, r, t[r]);
16
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
17
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
18
- });
19
- }
20
- return e;
21
- }
22
- function _regeneratorRuntime() {
23
- _regeneratorRuntime = function () {
24
- return e;
25
- };
26
- var t,
27
- e = {},
28
- r = Object.prototype,
29
- n = r.hasOwnProperty,
30
- o = Object.defineProperty || function (t, e, r) {
31
- t[e] = r.value;
32
- },
33
- i = "function" == typeof Symbol ? Symbol : {},
34
- a = i.iterator || "@@iterator",
35
- c = i.asyncIterator || "@@asyncIterator",
36
- u = i.toStringTag || "@@toStringTag";
37
- function define(t, e, r) {
38
- return Object.defineProperty(t, e, {
39
- value: r,
40
- enumerable: !0,
41
- configurable: !0,
42
- writable: !0
43
- }), t[e];
44
- }
45
- try {
46
- define({}, "");
47
- } catch (t) {
48
- define = function (t, e, r) {
49
- return t[e] = r;
50
- };
51
- }
52
- function wrap(t, e, r, n) {
53
- var i = e && e.prototype instanceof Generator ? e : Generator,
54
- a = Object.create(i.prototype),
55
- c = new Context(n || []);
56
- return o(a, "_invoke", {
57
- value: makeInvokeMethod(t, r, c)
58
- }), a;
59
- }
60
- function tryCatch(t, e, r) {
61
- try {
62
- return {
63
- type: "normal",
64
- arg: t.call(e, r)
65
- };
66
- } catch (t) {
67
- return {
68
- type: "throw",
69
- arg: t
70
- };
71
- }
72
- }
73
- e.wrap = wrap;
74
- var h = "suspendedStart",
75
- l = "suspendedYield",
76
- f = "executing",
77
- s = "completed",
78
- y = {};
79
- function Generator() {}
80
- function GeneratorFunction() {}
81
- function GeneratorFunctionPrototype() {}
82
- var p = {};
83
- define(p, a, function () {
84
- return this;
85
- });
86
- var d = Object.getPrototypeOf,
87
- v = d && d(d(values([])));
88
- v && v !== r && n.call(v, a) && (p = v);
89
- var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
90
- function defineIteratorMethods(t) {
91
- ["next", "throw", "return"].forEach(function (e) {
92
- define(t, e, function (t) {
93
- return this._invoke(e, t);
94
- });
95
- });
96
- }
97
- function AsyncIterator(t, e) {
98
- function invoke(r, o, i, a) {
99
- var c = tryCatch(t[r], t, o);
100
- if ("throw" !== c.type) {
101
- var u = c.arg,
102
- h = u.value;
103
- return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
104
- invoke("next", t, i, a);
105
- }, function (t) {
106
- invoke("throw", t, i, a);
107
- }) : e.resolve(h).then(function (t) {
108
- u.value = t, i(u);
109
- }, function (t) {
110
- return invoke("throw", t, i, a);
111
- });
112
- }
113
- a(c.arg);
114
- }
115
- var r;
116
- o(this, "_invoke", {
117
- value: function (t, n) {
118
- function callInvokeWithMethodAndArg() {
119
- return new e(function (e, r) {
120
- invoke(t, n, e, r);
121
- });
122
- }
123
- return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
124
- }
125
- });
126
- }
127
- function makeInvokeMethod(e, r, n) {
128
- var o = h;
129
- return function (i, a) {
130
- if (o === f) throw Error("Generator is already running");
131
- if (o === s) {
132
- if ("throw" === i) throw a;
133
- return {
134
- value: t,
135
- done: !0
136
- };
137
- }
138
- for (n.method = i, n.arg = a;;) {
139
- var c = n.delegate;
140
- if (c) {
141
- var u = maybeInvokeDelegate(c, n);
142
- if (u) {
143
- if (u === y) continue;
144
- return u;
145
- }
146
- }
147
- if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
148
- if (o === h) throw o = s, n.arg;
149
- n.dispatchException(n.arg);
150
- } else "return" === n.method && n.abrupt("return", n.arg);
151
- o = f;
152
- var p = tryCatch(e, r, n);
153
- if ("normal" === p.type) {
154
- if (o = n.done ? s : l, p.arg === y) continue;
155
- return {
156
- value: p.arg,
157
- done: n.done
158
- };
159
- }
160
- "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
161
- }
162
- };
163
- }
164
- function maybeInvokeDelegate(e, r) {
165
- var n = r.method,
166
- o = e.iterator[n];
167
- if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
168
- var i = tryCatch(o, e.iterator, r.arg);
169
- if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
170
- var a = i.arg;
171
- return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
172
- }
173
- function pushTryEntry(t) {
174
- var e = {
175
- tryLoc: t[0]
176
- };
177
- 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
178
- }
179
- function resetTryEntry(t) {
180
- var e = t.completion || {};
181
- e.type = "normal", delete e.arg, t.completion = e;
182
- }
183
- function Context(t) {
184
- this.tryEntries = [{
185
- tryLoc: "root"
186
- }], t.forEach(pushTryEntry, this), this.reset(!0);
187
- }
188
- function values(e) {
189
- if (e || "" === e) {
190
- var r = e[a];
191
- if (r) return r.call(e);
192
- if ("function" == typeof e.next) return e;
193
- if (!isNaN(e.length)) {
194
- var o = -1,
195
- i = function next() {
196
- for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
197
- return next.value = t, next.done = !0, next;
198
- };
199
- return i.next = i;
200
- }
201
- }
202
- throw new TypeError(typeof e + " is not iterable");
203
- }
204
- return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
205
- value: GeneratorFunctionPrototype,
206
- configurable: !0
207
- }), o(GeneratorFunctionPrototype, "constructor", {
208
- value: GeneratorFunction,
209
- configurable: !0
210
- }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
211
- var e = "function" == typeof t && t.constructor;
212
- return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
213
- }, e.mark = function (t) {
214
- return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
215
- }, e.awrap = function (t) {
216
- return {
217
- __await: t
218
- };
219
- }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
220
- return this;
221
- }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
222
- void 0 === i && (i = Promise);
223
- var a = new AsyncIterator(wrap(t, r, n, o), i);
224
- return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
225
- return t.done ? t.value : a.next();
226
- });
227
- }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
228
- return this;
229
- }), define(g, "toString", function () {
230
- return "[object Generator]";
231
- }), e.keys = function (t) {
232
- var e = Object(t),
233
- r = [];
234
- for (var n in e) r.push(n);
235
- return r.reverse(), function next() {
236
- for (; r.length;) {
237
- var t = r.pop();
238
- if (t in e) return next.value = t, next.done = !1, next;
239
- }
240
- return next.done = !0, next;
241
- };
242
- }, e.values = values, Context.prototype = {
243
- constructor: Context,
244
- reset: function (e) {
245
- if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
246
- },
247
- stop: function () {
248
- this.done = !0;
249
- var t = this.tryEntries[0].completion;
250
- if ("throw" === t.type) throw t.arg;
251
- return this.rval;
252
- },
253
- dispatchException: function (e) {
254
- if (this.done) throw e;
255
- var r = this;
256
- function handle(n, o) {
257
- return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
258
- }
259
- for (var o = this.tryEntries.length - 1; o >= 0; --o) {
260
- var i = this.tryEntries[o],
261
- a = i.completion;
262
- if ("root" === i.tryLoc) return handle("end");
263
- if (i.tryLoc <= this.prev) {
264
- var c = n.call(i, "catchLoc"),
265
- u = n.call(i, "finallyLoc");
266
- if (c && u) {
267
- if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
268
- if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
269
- } else if (c) {
270
- if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
271
- } else {
272
- if (!u) throw Error("try statement without catch or finally");
273
- if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
274
- }
275
- }
276
- }
277
- },
278
- abrupt: function (t, e) {
279
- for (var r = this.tryEntries.length - 1; r >= 0; --r) {
280
- var o = this.tryEntries[r];
281
- if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
282
- var i = o;
283
- break;
284
- }
285
- }
286
- i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
287
- var a = i ? i.completion : {};
288
- return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
289
- },
290
- complete: function (t, e) {
291
- if ("throw" === t.type) throw t.arg;
292
- return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
293
- },
294
- finish: function (t) {
295
- for (var e = this.tryEntries.length - 1; e >= 0; --e) {
296
- var r = this.tryEntries[e];
297
- if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
298
- }
299
- },
300
- catch: function (t) {
301
- for (var e = this.tryEntries.length - 1; e >= 0; --e) {
302
- var r = this.tryEntries[e];
303
- if (r.tryLoc === t) {
304
- var n = r.completion;
305
- if ("throw" === n.type) {
306
- var o = n.arg;
307
- resetTryEntry(r);
308
- }
309
- return o;
310
- }
311
- }
312
- throw Error("illegal catch attempt");
313
- },
314
- delegateYield: function (e, r, n) {
315
- return this.delegate = {
316
- iterator: values(e),
317
- resultName: r,
318
- nextLoc: n
319
- }, "next" === this.method && (this.arg = t), y;
320
- }
321
- }, e;
322
- }
323
- function _toPrimitive(t, r) {
324
- if ("object" != typeof t || !t) return t;
325
- var e = t[Symbol.toPrimitive];
326
- if (void 0 !== e) {
327
- var i = e.call(t, r || "default");
328
- if ("object" != typeof i) return i;
329
- throw new TypeError("@@toPrimitive must return a primitive value.");
330
- }
331
- return ("string" === r ? String : Number)(t);
332
- }
333
- function _toPropertyKey(t) {
334
- var i = _toPrimitive(t, "string");
335
- return "symbol" == typeof i ? i : i + "";
336
- }
337
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
338
- try {
339
- var info = gen[key](arg);
340
- var value = info.value;
341
- } catch (error) {
342
- reject(error);
343
- return;
344
- }
345
- if (info.done) {
346
- resolve(value);
347
- } else {
348
- Promise.resolve(value).then(_next, _throw);
349
- }
350
- }
351
- function _asyncToGenerator(fn) {
352
- return function () {
353
- var self = this,
354
- args = arguments;
355
- return new Promise(function (resolve, reject) {
356
- var gen = fn.apply(self, args);
357
- function _next(value) {
358
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
359
- }
360
- function _throw(err) {
361
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
362
- }
363
- _next(undefined);
364
- });
365
- };
366
- }
367
- function _defineProperty(obj, key, value) {
368
- key = _toPropertyKey(key);
369
- if (key in obj) {
370
- Object.defineProperty(obj, key, {
371
- value: value,
372
- enumerable: true,
373
- configurable: true,
374
- writable: true
375
- });
376
- } else {
377
- obj[key] = value;
378
- }
379
- return obj;
380
- }
1
+ import { getClientAuthKey } from '~/clientToken.js';
381
2
 
382
- var clientAuthKey = null;
383
- var getClientAuthKey = function getClientAuthKey() {
384
- return clientAuthKey;
3
+ const baseURL = `${process.env.NEXT_PUBLIC_HC_API_BASE_URL}`;
4
+
5
+ const memoryStorage = {
6
+ authToken: null,
7
+ tokenExpiration: null
8
+ };
9
+
10
+ function setStorage(key, value) {
11
+ try {
12
+ sessionStorage.setItem(key, value);
13
+ } catch (error) {
14
+ memoryStorage[key] = value;
15
+ }
16
+ }
17
+
18
+ function getStorage(key) {
19
+ try {
20
+ return sessionStorage.getItem(key) || memoryStorage[key];
21
+ } catch (error) {
22
+ return memoryStorage[key];
23
+ }
24
+ }
25
+
26
+ const login = async () => {
27
+ const clientAuthKey = getClientAuthKey();
28
+ try {
29
+ const response = await fetch(`${baseURL}/auth/login`, {
30
+ method: 'POST',
31
+ headers: {
32
+ 'Content-Type': 'application/json'
33
+ },
34
+ body: JSON.stringify({
35
+ clientAuthKey: clientAuthKey
36
+ })
37
+ });
38
+ const data = await response.json();
39
+ if (data.token && data.expiration) {
40
+ setStorage('authToken', data.token);
41
+ setStorage('tokenExpiration', data.expiration);
42
+ return { token: data.token, expiration: data.expiration };
43
+ }
44
+ } catch (error) {
45
+ console.error('Login failed:', error);
46
+ throw error;
47
+ }
48
+ };
49
+
50
+ const fetchWithAuth = async (url, options = {}) => {
51
+ let token = getStorage('authToken');
52
+ const expirationDateTime = getStorage('tokenExpiration');
53
+ const currentTime = new Date();
54
+
55
+ if (!token || !expirationDateTime || new Date(expirationDateTime) <= currentTime) {
56
+ const authResponse = await login();
57
+ token = authResponse.token;
58
+ }
59
+
60
+ const headers = new Headers(options.headers || {});
61
+ headers.append('Authorization', `Bearer ${token}`);
62
+
63
+ const finalOptions = {
64
+ ...options,
65
+ headers
66
+ };
67
+ const response = await fetch(`${baseURL}${url}`, finalOptions);
68
+
69
+ if (!response.ok) throw new Error('Network response was not ok.');
70
+ return response;
71
+ };
72
+
73
+ var api = {
74
+ get: async (url) => {
75
+ const response = await fetchWithAuth(url);
76
+ return response.json();
77
+ },
78
+ post: async (url, data) => {
79
+ const response = await fetchWithAuth(url, {
80
+ method: 'POST',
81
+ headers: {
82
+ 'Content-Type': 'application/json'
83
+ },
84
+ body: JSON.stringify(data)
85
+ });
86
+ return response.json();
87
+ }
385
88
  };
386
89
 
387
- var baseURL = "https://https://api.myhirecontrol.com";
388
- var login = /*#__PURE__*/function () {
389
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
390
- var clientAuthKey, response, data;
391
- return _regeneratorRuntime().wrap(function _callee$(_context) {
392
- while (1) switch (_context.prev = _context.next) {
393
- case 0:
394
- clientAuthKey = getClientAuthKey();
395
- _context.prev = 1;
396
- _context.next = 4;
397
- return fetch("".concat(baseURL, "/auth/login"), {
398
- method: 'POST',
399
- headers: {
400
- 'Content-Type': 'application/json'
401
- },
402
- body: JSON.stringify({
403
- clientAuthKey: clientAuthKey
404
- })
405
- });
406
- case 4:
407
- response = _context.sent;
408
- _context.next = 7;
409
- return response.json();
410
- case 7:
411
- data = _context.sent;
412
- if (!(data.token && data.expiration)) {
413
- _context.next = 12;
414
- break;
415
- }
416
- sessionStorage.setItem('authToken', data.token);
417
- sessionStorage.setItem('tokenExpiration', data.expiration);
418
- return _context.abrupt("return", {
419
- token: data.token,
420
- expiration: data.expiration
421
- });
422
- case 12:
423
- _context.next = 18;
424
- break;
425
- case 14:
426
- _context.prev = 14;
427
- _context.t0 = _context["catch"](1);
428
- console.error('Login failed:', _context.t0);
429
- throw _context.t0;
430
- case 18:
431
- case "end":
432
- return _context.stop();
433
- }
434
- }, _callee, null, [[1, 14]]);
435
- }));
436
- return function login() {
437
- return _ref.apply(this, arguments);
438
- };
439
- }();
440
- var fetchWithAuth = /*#__PURE__*/function () {
441
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(url) {
442
- var options,
443
- token,
444
- expirationDateTime,
445
- currentTime,
446
- authResponse,
447
- headers,
448
- finalOptions,
449
- response,
450
- _args2 = arguments;
451
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
452
- while (1) switch (_context2.prev = _context2.next) {
453
- case 0:
454
- options = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
455
- token = sessionStorage.getItem('authToken');
456
- expirationDateTime = sessionStorage.getItem('tokenExpiration');
457
- currentTime = new Date();
458
- if (!(!token || !expirationDateTime || new Date(expirationDateTime) <= currentTime)) {
459
- _context2.next = 9;
460
- break;
461
- }
462
- _context2.next = 7;
463
- return login();
464
- case 7:
465
- authResponse = _context2.sent;
466
- token = authResponse.token;
467
- case 9:
468
- headers = new Headers(options.headers || {});
469
- headers.append('Authorization', "Bearer ".concat(token));
470
- finalOptions = _objectSpread2(_objectSpread2({}, options), {}, {
471
- headers: headers
472
- });
473
- _context2.next = 14;
474
- return fetch("".concat(baseURL).concat(url), finalOptions);
475
- case 14:
476
- response = _context2.sent;
477
- if (response.ok) {
478
- _context2.next = 17;
479
- break;
480
- }
481
- throw new Error('Network response was not ok.');
482
- case 17:
483
- return _context2.abrupt("return", response);
484
- case 18:
485
- case "end":
486
- return _context2.stop();
487
- }
488
- }, _callee2);
489
- }));
490
- return function fetchWithAuth(_x) {
491
- return _ref2.apply(this, arguments);
492
- };
493
- }();
494
- var api = {
495
- get: function () {
496
- var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(url) {
497
- var response;
498
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
499
- while (1) switch (_context3.prev = _context3.next) {
500
- case 0:
501
- _context3.next = 2;
502
- return fetchWithAuth(url);
503
- case 2:
504
- response = _context3.sent;
505
- return _context3.abrupt("return", response.json());
506
- case 4:
507
- case "end":
508
- return _context3.stop();
509
- }
510
- }, _callee3);
511
- }));
512
- function get(_x2) {
513
- return _get.apply(this, arguments);
514
- }
515
- return get;
516
- }(),
517
- post: function () {
518
- var _post = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(url, data) {
519
- var response;
520
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
521
- while (1) switch (_context4.prev = _context4.next) {
522
- case 0:
523
- _context4.next = 2;
524
- return fetchWithAuth(url, {
525
- method: 'POST',
526
- headers: {
527
- 'Content-Type': 'application/json'
528
- },
529
- body: JSON.stringify(data)
530
- });
531
- case 2:
532
- response = _context4.sent;
533
- return _context4.abrupt("return", response.json());
534
- case 4:
535
- case "end":
536
- return _context4.stop();
537
- }
538
- }, _callee4);
539
- }));
540
- function post(_x3, _x4) {
541
- return _post.apply(this, arguments);
542
- }
543
- return post;
544
- }()
545
- };
546
-
547
- var getListings = /*#__PURE__*/function () {
548
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(companyId) {
549
- var response;
550
- return _regeneratorRuntime().wrap(function _callee$(_context) {
551
- while (1) switch (_context.prev = _context.next) {
552
- case 0:
553
- _context.prev = 0;
554
- _context.next = 4;
555
- return api.get("/Listings");
556
- case 4:
557
- response = _context.sent;
558
- return _context.abrupt("return", response);
559
- case 8:
560
- _context.prev = 8;
561
- _context.t0 = _context["catch"](0);
562
- console.error(_context.t0);
563
- throw _context.t0;
564
- case 12:
565
- case "end":
566
- return _context.stop();
567
- }
568
- }, _callee, null, [[0, 8]]);
569
- }));
570
- return function getListings(_x) {
571
- return _ref.apply(this, arguments);
572
- };
573
- }();
574
- var getListingDetails = /*#__PURE__*/function () {
575
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(listingId) {
576
- var response;
577
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
578
- while (1) switch (_context2.prev = _context2.next) {
579
- case 0:
580
- _context2.prev = 0;
581
- _context2.next = 3;
582
- return api.get("/ListingDetails/".concat(listingId));
583
- case 3:
584
- response = _context2.sent;
585
- return _context2.abrupt("return", response);
586
- case 7:
587
- _context2.prev = 7;
588
- _context2.t0 = _context2["catch"](0);
589
- console.error(_context2.t0);
590
- throw _context2.t0;
591
- case 11:
592
- case "end":
593
- return _context2.stop();
594
- }
595
- }, _callee2, null, [[0, 7]]);
596
- }));
597
- return function getListingDetails(_x2) {
598
- return _ref2.apply(this, arguments);
599
- };
600
- }();
601
- var listingService = {
602
- getListings: getListings,
603
- getListingDetails: getListingDetails
90
+ const getListings = async companyId => {
91
+ try {
92
+ var x = '';
93
+ const response = await api.get(`/Listings`);
94
+ return response;
95
+ } catch (error) {
96
+ console.error(error);
97
+ throw error;
98
+ }
99
+ };
100
+
101
+ const getListingDetails = async listingId => {
102
+ try {
103
+ const response = await api.get(`/ListingDetails/${listingId}`);
104
+ return response;
105
+ } catch (error) {
106
+ console.error(error);
107
+ throw error;
108
+ }
109
+ };
110
+
111
+ var listingService = {
112
+ getListings,
113
+ getListingDetails
604
114
  };
605
115
 
606
116
  export { listingService as default, getListingDetails, getListings };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abcagency/hc-ui-components",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/rollup.config.js CHANGED
@@ -61,7 +61,6 @@ module.exports = [
61
61
  format: 'es',
62
62
  exports: 'named'
63
63
  },
64
- plugins,
65
64
  external: externalDependencies
66
65
  },
67
66
  ];
package/src/apis/hcApi.js CHANGED
@@ -1,68 +1,87 @@
1
+ import { getClientAuthKey } from '~/clientToken.js';
2
+ const baseURL = `${process.env.NEXT_PUBLIC_HC_API_BASE_URL}`;
1
3
 
2
- import { getClientAuthKey } from '~/clientToken.js'
3
- const baseURL = `${process.env.NEXT_PUBLIC_HC_API_BASE_URL}`
4
+ const memoryStorage = {
5
+ authToken: null,
6
+ tokenExpiration: null
7
+ };
8
+
9
+ function setStorage(key, value) {
10
+ try {
11
+ sessionStorage.setItem(key, value);
12
+ } catch (error) {
13
+ memoryStorage[key] = value;
14
+ }
15
+ }
16
+
17
+ function getStorage(key) {
18
+ try {
19
+ return sessionStorage.getItem(key) || memoryStorage[key];
20
+ } catch (error) {
21
+ return memoryStorage[key];
22
+ }
23
+ }
4
24
 
5
25
  const login = async () => {
6
- const clientAuthKey = getClientAuthKey();
7
- try {
8
-
9
- const response = await fetch(`${baseURL}/auth/login`, {
10
- method: 'POST',
11
- headers: {
12
- 'Content-Type': 'application/json'
13
- },
14
- body: JSON.stringify({
15
- clientAuthKey: clientAuthKey
16
- })
17
- });
18
- const data = await response.json();
19
- if (data.token && data.expiration) {
20
- sessionStorage.setItem('authToken', data.token);
21
- sessionStorage.setItem('tokenExpiration', data.expiration);
22
- return { token: data.token, expiration: data.expiration };
23
- }
24
- } catch (error) {
25
- console.error('Login failed:', error);
26
- throw error;
27
- }
26
+ const clientAuthKey = getClientAuthKey();
27
+ try {
28
+ const response = await fetch(`${baseURL}/auth/login`, {
29
+ method: 'POST',
30
+ headers: {
31
+ 'Content-Type': 'application/json'
32
+ },
33
+ body: JSON.stringify({
34
+ clientAuthKey: clientAuthKey
35
+ })
36
+ });
37
+ const data = await response.json();
38
+ if (data.token && data.expiration) {
39
+ setStorage('authToken', data.token);
40
+ setStorage('tokenExpiration', data.expiration);
41
+ return { token: data.token, expiration: data.expiration };
42
+ }
43
+ } catch (error) {
44
+ console.error('Login failed:', error);
45
+ throw error;
46
+ }
28
47
  };
29
48
 
30
49
  const fetchWithAuth = async (url, options = {}) => {
31
- let token = sessionStorage.getItem('authToken');
32
- const expirationDateTime = sessionStorage.getItem('tokenExpiration');
33
- const currentTime = new Date();
50
+ let token = getStorage('authToken');
51
+ const expirationDateTime = getStorage('tokenExpiration');
52
+ const currentTime = new Date();
34
53
 
35
- if (!token || !expirationDateTime || new Date(expirationDateTime) <= currentTime) {
36
- const authResponse = await login();
37
- token = authResponse.token;
38
- }
54
+ if (!token || !expirationDateTime || new Date(expirationDateTime) <= currentTime) {
55
+ const authResponse = await login();
56
+ token = authResponse.token;
57
+ }
39
58
 
40
- const headers = new Headers(options.headers || {});
41
- headers.append('Authorization', `Bearer ${token}`);
59
+ const headers = new Headers(options.headers || {});
60
+ headers.append('Authorization', `Bearer ${token}`);
42
61
 
43
- const finalOptions = {
44
- ...options,
45
- headers
46
- };
47
- const response = await fetch(`${baseURL}${url}`, finalOptions);
62
+ const finalOptions = {
63
+ ...options,
64
+ headers
65
+ };
66
+ const response = await fetch(`${baseURL}${url}`, finalOptions);
48
67
 
49
- if (!response.ok) throw new Error('Network response was not ok.');
50
- return response;
68
+ if (!response.ok) throw new Error('Network response was not ok.');
69
+ return response;
51
70
  };
52
71
 
53
72
  export default {
54
- get: async (url) => {
55
- const response = await fetchWithAuth(url);
56
- return response.json();
57
- },
58
- post: async (url, data) => {
59
- const response = await fetchWithAuth(url, {
60
- method: 'POST',
61
- headers: {
62
- 'Content-Type': 'application/json'
63
- },
64
- body: JSON.stringify(data)
65
- });
66
- return response.json();
67
- }
73
+ get: async (url) => {
74
+ const response = await fetchWithAuth(url);
75
+ return response.json();
76
+ },
77
+ post: async (url, data) => {
78
+ const response = await fetchWithAuth(url, {
79
+ method: 'POST',
80
+ headers: {
81
+ 'Content-Type': 'application/json'
82
+ },
83
+ body: JSON.stringify(data)
84
+ });
85
+ return response.json();
86
+ }
68
87
  };