@dev-crew-berlin/enter-js-utils 0.5.1 → 0.5.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.
Files changed (103) hide show
  1. package/dist/api-client/api-base.js +160 -287
  2. package/dist/api-client/index.js +73 -190
  3. package/dist/lib/api-result.js +9 -13
  4. package/dist/lib/class-names.js +2 -12
  5. package/dist/lib/index.js +1 -2
  6. package/dist/lib/not-undefined.js +2 -3
  7. package/dist/lib/result.js +27 -23
  8. package/dist/lib/theme.js +17 -18
  9. package/dist/models/_helpers.js +64 -65
  10. package/dist/models/access-token.js +9 -6
  11. package/dist/models/attendee.js +109 -100
  12. package/dist/models/checkin-counts.js +7 -3
  13. package/dist/models/checkins.js +61 -69
  14. package/dist/models/event.js +1 -2
  15. package/dist/models/field-group.js +43 -43
  16. package/dist/models/field-value.js +1 -2
  17. package/dist/models/field.js +86 -41
  18. package/dist/models/index.js +1 -2
  19. package/dist/models/instance.js +109 -99
  20. package/dist/models/invite.js +18 -15
  21. package/dist/models/meta-field.js +8 -7
  22. package/dist/models/user.js +10 -9
  23. package/dist/models/variable.js +15 -14
  24. package/dist/ui/button.js +15 -29
  25. package/dist/ui/button.stories.js +17 -27
  26. package/dist/ui/checkin-count-indicator.js +67 -46
  27. package/dist/ui/checkin-count-indicator.stories.js +45 -47
  28. package/dist/ui/companion-info.js +14 -14
  29. package/dist/ui/companion-info.stories.js +59 -50
  30. package/dist/ui/fonts.js +6 -4
  31. package/dist/ui/guest-card.js +40 -31
  32. package/dist/ui/guest-card.stories.js +115 -104
  33. package/dist/ui/index.js +5 -1
  34. package/dist/ui/tag.js +9 -7
  35. package/package.json +7 -34
  36. package/dist/api-client/api-base.d.ts +0 -30
  37. package/dist/api-client/api-base.js.map +0 -1
  38. package/dist/api-client/index.d.ts +0 -75
  39. package/dist/api-client/index.js.map +0 -1
  40. package/dist/lib/api-result.d.ts +0 -25
  41. package/dist/lib/api-result.js.map +0 -1
  42. package/dist/lib/class-names.d.ts +0 -1
  43. package/dist/lib/class-names.js.map +0 -1
  44. package/dist/lib/index.d.ts +0 -5
  45. package/dist/lib/index.js.map +0 -1
  46. package/dist/lib/not-undefined.d.ts +0 -1
  47. package/dist/lib/not-undefined.js.map +0 -1
  48. package/dist/lib/result.d.ts +0 -33
  49. package/dist/lib/result.js.map +0 -1
  50. package/dist/lib/theme.d.ts +0 -18
  51. package/dist/lib/theme.js.map +0 -1
  52. package/dist/models/_helpers.d.ts +0 -23
  53. package/dist/models/_helpers.js.map +0 -1
  54. package/dist/models/access-token.d.ts +0 -11
  55. package/dist/models/access-token.js.map +0 -1
  56. package/dist/models/attendee.d.ts +0 -90
  57. package/dist/models/attendee.js.map +0 -1
  58. package/dist/models/checkin-counts.d.ts +0 -10
  59. package/dist/models/checkin-counts.js.map +0 -1
  60. package/dist/models/checkins.d.ts +0 -40
  61. package/dist/models/checkins.js.map +0 -1
  62. package/dist/models/event.d.ts +0 -40
  63. package/dist/models/event.js.map +0 -1
  64. package/dist/models/field-group.d.ts +0 -484
  65. package/dist/models/field-group.js.map +0 -1
  66. package/dist/models/field-value.d.ts +0 -2
  67. package/dist/models/field-value.js.map +0 -1
  68. package/dist/models/field.d.ts +0 -321
  69. package/dist/models/field.js.map +0 -1
  70. package/dist/models/index.d.ts +0 -14
  71. package/dist/models/index.js.map +0 -1
  72. package/dist/models/instance.d.ts +0 -78
  73. package/dist/models/instance.js.map +0 -1
  74. package/dist/models/invite.d.ts +0 -33
  75. package/dist/models/invite.js.map +0 -1
  76. package/dist/models/meta-field.d.ts +0 -7
  77. package/dist/models/meta-field.js.map +0 -1
  78. package/dist/models/user.d.ts +0 -18
  79. package/dist/models/user.js.map +0 -1
  80. package/dist/models/variable.d.ts +0 -27
  81. package/dist/models/variable.js.map +0 -1
  82. package/dist/ui/button.d.ts +0 -7
  83. package/dist/ui/button.js.map +0 -1
  84. package/dist/ui/button.stories.d.ts +0 -18
  85. package/dist/ui/button.stories.js.map +0 -1
  86. package/dist/ui/checkin-count-indicator.d.ts +0 -15
  87. package/dist/ui/checkin-count-indicator.js.map +0 -1
  88. package/dist/ui/checkin-count-indicator.stories.d.ts +0 -84
  89. package/dist/ui/checkin-count-indicator.stories.js.map +0 -1
  90. package/dist/ui/companion-info.d.ts +0 -15
  91. package/dist/ui/companion-info.js.map +0 -1
  92. package/dist/ui/companion-info.stories.d.ts +0 -44
  93. package/dist/ui/companion-info.stories.js.map +0 -1
  94. package/dist/ui/fonts.d.ts +0 -3
  95. package/dist/ui/fonts.js.map +0 -1
  96. package/dist/ui/guest-card.d.ts +0 -16
  97. package/dist/ui/guest-card.js.map +0 -1
  98. package/dist/ui/guest-card.stories.d.ts +0 -92
  99. package/dist/ui/guest-card.stories.js.map +0 -1
  100. package/dist/ui/index.d.ts +0 -1
  101. package/dist/ui/index.js.map +0 -1
  102. package/dist/ui/tag.d.ts +0 -7
  103. package/dist/ui/tag.js.map +0 -1
@@ -1,292 +1,165 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (_) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
1
  import { failure, success } from '../lib';
38
2
  import { parseAccessToken } from '../models/access-token';
39
- var APIBase = /** @class */ (function () {
40
- function APIBase(args) {
41
- this.credentials = args.credentials;
42
- this.onLogout = args.onLogout;
43
- this.isLoggedIn = true;
3
+ export default class APIBase {
4
+ constructor(args) {
5
+ this.credentials = args.credentials;
6
+ this.onLogout = args.onLogout;
7
+ this.isLoggedIn = true;
8
+ }
9
+
10
+ static async fetchResult(input, init) {
11
+ try {
12
+ const res = await fetch(input, init);
13
+ return success(res);
14
+ } catch (e) {
15
+ return failure(e);
44
16
  }
45
- APIBase.fetchResult = function (input, init) {
46
- return __awaiter(this, void 0, void 0, function () {
47
- var res, e_1;
48
- return __generator(this, function (_a) {
49
- switch (_a.label) {
50
- case 0:
51
- _a.trys.push([0, 2, , 3]);
52
- return [4 /*yield*/, fetch(input, init)];
53
- case 1:
54
- res = _a.sent();
55
- return [2 /*return*/, success(res)];
56
- case 2:
57
- e_1 = _a.sent();
58
- return [2 /*return*/, failure(e_1)];
59
- case 3: return [2 /*return*/];
60
- }
61
- });
62
- });
63
- };
64
- APIBase.fetchAccessToken = function (args) {
65
- return __awaiter(this, void 0, void 0, function () {
66
- var body, res, error, e_2, payload;
67
- return __generator(this, function (_a) {
68
- switch (_a.label) {
69
- case 0:
70
- body = {
71
- apiKey: 'default',
72
- user: args.user,
73
- password: args.password,
74
- device: args.deviceName,
75
- };
76
- return [4 /*yield*/, APIBase.fetchResult("".concat(args.url, "/authenticate"), {
77
- method: 'POST',
78
- headers: { 'Content-Type': 'application/json' },
79
- body: JSON.stringify(body),
80
- })];
81
- case 1:
82
- res = _a.sent();
83
- if (!res.success) {
84
- return [2 /*return*/, failure([
85
- {
86
- type: 'network-error',
87
- message: res.error.message,
88
- endpoint: 'authenticate',
89
- },
90
- ])];
91
- }
92
- if (!(res.data.status >= 300)) return [3 /*break*/, 5];
93
- _a.label = 2;
94
- case 2:
95
- _a.trys.push([2, 4, , 5]);
96
- return [4 /*yield*/, res.data.json()];
97
- case 3:
98
- error = _a.sent();
99
- return [2 /*return*/, failure([
100
- {
101
- type: 'http-error',
102
- statusCode: res.data.status,
103
- message: error.error,
104
- endpoint: 'authenticate',
105
- },
106
- ])];
107
- case 4:
108
- e_2 = _a.sent();
109
- return [2 /*return*/, failure([
110
- {
111
- type: 'http-error',
112
- statusCode: res.data.status,
113
- message: 'Could not authenticate',
114
- endpoint: 'authenticate',
115
- },
116
- ])];
117
- case 5: return [4 /*yield*/, res.data.json()];
118
- case 6:
119
- payload = _a.sent();
120
- return [2 /*return*/, parseAccessToken(payload)];
121
- }
122
- });
123
- });
124
- };
125
- APIBase.getAuthHeader = function (username, password) {
126
- var buffer = Buffer.from("".concat(username, ":").concat(password));
127
- return 'Basic ' + buffer.toString('base64');
128
- };
129
- APIBase.prototype.fetchFromEnter = function (endpoint, method, data) {
130
- if (data === void 0) { data = null; }
131
- return __awaiter(this, void 0, void 0, function () {
132
- var body, headers, res, error, e_3, json;
133
- return __generator(this, function (_a) {
134
- switch (_a.label) {
135
- case 0:
136
- body = data !== null ? JSON.stringify(data) : null;
137
- headers = {
138
- Authorization: APIBase.getAuthHeader('default', this.credentials.accessToken),
139
- 'Content-Type': 'application/json',
140
- 'Enter-Live-Api': 'true',
141
- };
142
- return [4 /*yield*/, APIBase.fetchResult("".concat(this.credentials.url, "/").concat(endpoint), { method: method, headers: headers, body: body })];
143
- case 1:
144
- res = _a.sent();
145
- if (!res.success) {
146
- return [2 /*return*/, failure([
147
- {
148
- type: 'network-error',
149
- message: res.error.message,
150
- endpoint: endpoint,
151
- },
152
- ])];
153
- }
154
- if (res.data.status === 401) {
155
- if (this.isLoggedIn)
156
- this.logout();
157
- return [2 /*return*/, failure([{ type: 'http-error', statusCode: 401, endpoint: endpoint }])];
158
- }
159
- if (!(res.data.status >= 300)) return [3 /*break*/, 5];
160
- _a.label = 2;
161
- case 2:
162
- _a.trys.push([2, 4, , 5]);
163
- return [4 /*yield*/, res.data.json()];
164
- case 3:
165
- error = _a.sent();
166
- return [2 /*return*/, failure([
167
- {
168
- type: 'http-error',
169
- statusCode: res.data.status,
170
- message: error.error,
171
- endpoint: endpoint,
172
- },
173
- ])];
174
- case 4:
175
- e_3 = _a.sent();
176
- return [2 /*return*/, failure([
177
- {
178
- type: 'http-error',
179
- statusCode: res.data.status,
180
- message: "invalid response from backend: ".concat(res.data.status, " for: ").concat(endpoint),
181
- endpoint: endpoint,
182
- },
183
- ])];
184
- case 5:
185
- // HTTP status 204 means no content so we can't parse it as json
186
- if (res.data.status === 204)
187
- return [2 /*return*/, success(null)];
188
- return [4 /*yield*/, res.data.json()];
189
- case 6:
190
- json = _a.sent();
191
- return [2 /*return*/, success(json)];
192
- }
193
- });
194
- });
195
- };
196
- APIBase.prototype.get = function (endpoint, validate) {
197
- return __awaiter(this, void 0, void 0, function () {
198
- var rawResponse;
199
- return __generator(this, function (_a) {
200
- switch (_a.label) {
201
- case 0: return [4 /*yield*/, this.fetchFromEnter(endpoint, 'GET')];
202
- case 1:
203
- rawResponse = _a.sent();
204
- if (!rawResponse.success)
205
- return [2 /*return*/, rawResponse];
206
- return [2 /*return*/, validate(rawResponse.data)];
207
- }
208
- });
209
- });
210
- };
211
- APIBase.prototype.post = function (endpoint, data, validate) {
212
- return __awaiter(this, void 0, void 0, function () {
213
- var rawResponse;
214
- return __generator(this, function (_a) {
215
- switch (_a.label) {
216
- case 0: return [4 /*yield*/, this.fetchFromEnter(endpoint, 'POST', data)];
217
- case 1:
218
- rawResponse = _a.sent();
219
- if (!rawResponse.success)
220
- return [2 /*return*/, rawResponse];
221
- return [2 /*return*/, validate(rawResponse.data)];
222
- }
223
- });
224
- });
225
- };
226
- APIBase.prototype.put = function (endpoint, data, validate) {
227
- return __awaiter(this, void 0, void 0, function () {
228
- var rawResponse;
229
- return __generator(this, function (_a) {
230
- switch (_a.label) {
231
- case 0: return [4 /*yield*/, this.fetchFromEnter(endpoint, 'PUT', data)];
232
- case 1:
233
- rawResponse = _a.sent();
234
- if (!rawResponse.success)
235
- return [2 /*return*/, rawResponse];
236
- return [2 /*return*/, validate(rawResponse.data)];
237
- }
238
- });
239
- });
240
- };
241
- APIBase.prototype.patch = function (endpoint, data, validate) {
242
- return __awaiter(this, void 0, void 0, function () {
243
- var rawResponse;
244
- return __generator(this, function (_a) {
245
- switch (_a.label) {
246
- case 0: return [4 /*yield*/, this.fetchFromEnter(endpoint, 'PATCH', data)];
247
- case 1:
248
- rawResponse = _a.sent();
249
- if (!rawResponse.success)
250
- return [2 /*return*/, rawResponse];
251
- return [2 /*return*/, validate(rawResponse.data)];
252
- }
253
- });
254
- });
17
+ }
18
+
19
+ static async fetchAccessToken(args) {
20
+ const body = {
21
+ apiKey: 'default',
22
+ user: args.user,
23
+ password: args.password,
24
+ device: args.deviceName
255
25
  };
256
- APIBase.prototype.refreshToken = function () {
257
- return __awaiter(this, void 0, void 0, function () {
258
- return __generator(this, function (_a) {
259
- return [2 /*return*/, this.post('authenticate/refresh', null, parseAccessToken)];
260
- });
261
- });
262
- };
263
- APIBase.prototype.logout = function () {
264
- return __awaiter(this, void 0, void 0, function () {
265
- var e_4;
266
- return __generator(this, function (_a) {
267
- switch (_a.label) {
268
- case 0:
269
- this.isLoggedIn = false;
270
- this.onLogout();
271
- _a.label = 1;
272
- case 1:
273
- _a.trys.push([1, 3, , 4]);
274
- // delete token at api backend so it becomes invalid
275
- return [4 /*yield*/, this.fetchFromEnter('/authenticate', 'DELETE')];
276
- case 2:
277
- // delete token at api backend so it becomes invalid
278
- _a.sent();
279
- return [3 /*break*/, 4];
280
- case 3:
281
- e_4 = _a.sent();
282
- console.warn('error deleting the token in the backend, so it is still valid', e_4);
283
- return [3 /*break*/, 4];
284
- case 4: return [2 /*return*/];
285
- }
286
- });
287
- });
26
+ const res = await APIBase.fetchResult(`${args.url}/authenticate`, {
27
+ method: 'POST',
28
+ headers: {
29
+ 'Content-Type': 'application/json'
30
+ },
31
+ body: JSON.stringify(body)
32
+ });
33
+
34
+ if (!res.success) {
35
+ return failure([{
36
+ type: 'network-error',
37
+ message: res.error.message,
38
+ endpoint: 'authenticate'
39
+ }]);
40
+ }
41
+
42
+ if (res.data.status >= 300) {
43
+ try {
44
+ const error = await res.data.json();
45
+ return failure([{
46
+ type: 'http-error',
47
+ statusCode: res.data.status,
48
+ message: error.error,
49
+ endpoint: 'authenticate'
50
+ }]);
51
+ } catch (e) {
52
+ return failure([{
53
+ type: 'http-error',
54
+ statusCode: res.data.status,
55
+ message: 'Could not authenticate',
56
+ endpoint: 'authenticate'
57
+ }]);
58
+ }
59
+ }
60
+
61
+ const payload = await res.data.json();
62
+ return parseAccessToken(payload);
63
+ }
64
+
65
+ static getAuthHeader(username, password) {
66
+ const buffer = Buffer.from(`${username}:${password}`);
67
+ return 'Basic ' + buffer.toString('base64');
68
+ }
69
+
70
+ async fetchFromEnter(endpoint, method, data = null) {
71
+ const body = data !== null ? JSON.stringify(data) : null;
72
+ const headers = {
73
+ Authorization: APIBase.getAuthHeader('default', this.credentials.accessToken),
74
+ 'Content-Type': 'application/json',
75
+ 'Enter-Live-Api': 'true'
288
76
  };
289
- return APIBase;
290
- }());
291
- export default APIBase;
292
- //# sourceMappingURL=api-base.js.map
77
+ const res = await APIBase.fetchResult(`${this.credentials.url}/${endpoint}`, {
78
+ method,
79
+ headers,
80
+ body
81
+ });
82
+
83
+ if (!res.success) {
84
+ return failure([{
85
+ type: 'network-error',
86
+ message: res.error.message,
87
+ endpoint
88
+ }]);
89
+ }
90
+
91
+ if (res.data.status === 401) {
92
+ if (this.isLoggedIn) this.logout();
93
+ return failure([{
94
+ type: 'http-error',
95
+ statusCode: 401,
96
+ endpoint
97
+ }]);
98
+ }
99
+
100
+ if (res.data.status >= 300) {
101
+ try {
102
+ const error = await res.data.json();
103
+ return failure([{
104
+ type: 'http-error',
105
+ statusCode: res.data.status,
106
+ message: error.error,
107
+ endpoint
108
+ }]);
109
+ } catch (e) {
110
+ return failure([{
111
+ type: 'http-error',
112
+ statusCode: res.data.status,
113
+ message: `invalid response from backend: ${res.data.status} for: ${endpoint}`,
114
+ endpoint
115
+ }]);
116
+ }
117
+ } // HTTP status 204 means no content so we can't parse it as json
118
+
119
+
120
+ if (res.data.status === 204) return success(null);
121
+ const json = await res.data.json();
122
+ return success(json);
123
+ }
124
+
125
+ async get(endpoint, validate) {
126
+ const rawResponse = await this.fetchFromEnter(endpoint, 'GET');
127
+ if (!rawResponse.success) return rawResponse;
128
+ return validate(rawResponse.data);
129
+ }
130
+
131
+ async post(endpoint, data, validate) {
132
+ const rawResponse = await this.fetchFromEnter(endpoint, 'POST', data);
133
+ if (!rawResponse.success) return rawResponse;
134
+ return validate(rawResponse.data);
135
+ }
136
+
137
+ async put(endpoint, data, validate) {
138
+ const rawResponse = await this.fetchFromEnter(endpoint, 'PUT', data);
139
+ if (!rawResponse.success) return rawResponse;
140
+ return validate(rawResponse.data);
141
+ }
142
+
143
+ async patch(endpoint, data, validate) {
144
+ const rawResponse = await this.fetchFromEnter(endpoint, 'PATCH', data);
145
+ if (!rawResponse.success) return rawResponse;
146
+ return validate(rawResponse.data);
147
+ }
148
+
149
+ async refreshToken() {
150
+ return this.post('authenticate/refresh', null, parseAccessToken);
151
+ }
152
+
153
+ async logout() {
154
+ this.isLoggedIn = false;
155
+ this.onLogout();
156
+
157
+ try {
158
+ // delete token at api backend so it becomes invalid
159
+ await this.fetchFromEnter('/authenticate', 'DELETE');
160
+ } catch (e) {
161
+ console.warn('error deleting the token in the backend, so it is still valid', e);
162
+ }
163
+ }
164
+
165
+ }