@dev-crew-berlin/enter-js-utils 0.24.0 → 0.30.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.
Files changed (44) hide show
  1. package/README.md +2 -7
  2. package/dist/api-client/api-base.d.ts +22 -7
  3. package/dist/api-client/api-base.js +30 -32
  4. package/dist/api-client/index.d.ts +27 -45
  5. package/dist/api-client/index.js +58 -46
  6. package/dist/generated/api-schema.d.ts +1662 -0
  7. package/dist/generated/api-schema.js +1 -0
  8. package/dist/lib/api-result.d.ts +1 -8
  9. package/dist/lib/api-result.js +1 -12
  10. package/dist/models/access-token.d.ts +2 -11
  11. package/dist/models/access-token.js +1 -11
  12. package/dist/models/attendee.d.ts +13 -60
  13. package/dist/models/attendee.js +41 -87
  14. package/dist/models/checkin-counts.d.ts +4 -10
  15. package/dist/models/checkin-counts.js +1 -10
  16. package/dist/models/checkin.d.ts +9 -0
  17. package/dist/models/checkin.js +16 -0
  18. package/dist/models/checkins.d.ts +4 -32
  19. package/dist/models/checkins.js +14 -42
  20. package/dist/models/event.d.ts +2 -40
  21. package/dist/models/field-group.d.ts +5 -448
  22. package/dist/models/field-group.js +4 -31
  23. package/dist/models/field-value.d.ts +0 -1
  24. package/dist/models/field-value.js +1 -3
  25. package/dist/models/field.d.ts +11 -297
  26. package/dist/models/field.js +1 -89
  27. package/dist/models/index.d.ts +3 -3
  28. package/dist/models/instance.d.ts +10 -53
  29. package/dist/models/instance.js +23 -65
  30. package/dist/models/permission.d.ts +2 -0
  31. package/dist/models/permission.js +1 -0
  32. package/dist/models/user.d.ts +2 -20
  33. package/dist/models/user.js +1 -13
  34. package/dist/models/variable.d.ts +2 -27
  35. package/dist/models/variable.js +1 -16
  36. package/dist/ui/companion-info.stories.js +1 -3
  37. package/dist/ui/guest-card.stories.js +2 -6
  38. package/package.json +10 -8
  39. package/dist/models/_helpers.d.ts +0 -23
  40. package/dist/models/_helpers.js +0 -89
  41. package/dist/models/invite.d.ts +0 -33
  42. package/dist/models/invite.js +0 -21
  43. package/dist/models/meta-field.d.ts +0 -7
  44. package/dist/models/meta-field.js +0 -9
package/README.md CHANGED
@@ -25,7 +25,7 @@ typed js client for the enter api.
25
25
  ```typescript
26
26
  import EnterAPIClient from '@dev-crew-berlin/enter-js-utils/api-client'
27
27
 
28
- const url = 'https://enter.events/api/v1'
28
+ const url = 'https://api.enter.events'
29
29
 
30
30
  async function apiExample() {
31
31
  // fetch access Token
@@ -36,14 +36,9 @@ async function apiExample() {
36
36
  deviceName: 'my-device',
37
37
  })
38
38
 
39
- const credentials = {
40
- accessToken: accessToken,
41
- url: url,
42
- }
43
-
44
39
  // create api instance
45
40
  const api = new EnterAPIClient({
46
- credentials: credentials,
41
+ credentials: { accessToken, url },
47
42
  onLogout: (reason?: string) => {
48
43
  deleteCredetials()
49
44
  },
@@ -1,9 +1,23 @@
1
- import { APIResult, APIValidator } from '../lib';
1
+ import { APIResult } from '../lib';
2
2
  import { AccessToken } from '../models/access-token';
3
+ import { paths } from '../generated/api-schema';
3
4
  export interface APICredentials {
4
- accessToken: string;
5
+ accessToken: AccessToken;
5
6
  url: string;
6
7
  }
8
+ declare type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
9
+ declare type JSONRes = {
10
+ content: {
11
+ 'application/json': unknown;
12
+ };
13
+ };
14
+ declare type ResJSON<Code extends number> = {
15
+ responses: Record<Code, JSONRes>;
16
+ };
17
+ declare type ResponseType<Path extends keyof paths, Method extends HTTPMethod> = paths[Path] extends Record<Method, ResJSON<200>> ? paths[Path][Method]['responses'][200]['content']['application/json'] : paths[Path] extends Record<Method, ResJSON<204>> ? paths[Path][Method]['responses'][204]['content']['application/json'] : never;
18
+ export declare type RequestBody<Path extends keyof paths, Method extends HTTPMethod> = paths[Path] extends Record<Method, {
19
+ requestBody: JSONRes;
20
+ }> ? paths[Path][Method]['requestBody']['content']['application/json'] : never;
7
21
  export default class APIBase {
8
22
  private credentials;
9
23
  private isLoggedIn;
@@ -19,12 +33,13 @@ export default class APIBase {
19
33
  password: string;
20
34
  deviceName: string;
21
35
  }): Promise<APIResult<AccessToken>>;
22
- static getAuthHeader(username: string, password: string): string;
23
36
  private fetchFromEnter;
24
- protected get<Model>(endpoint: string, validate: APIValidator<Model>): Promise<APIResult<Model>>;
25
- protected post<Model>(endpoint: string, data: Record<string, unknown> | Array<unknown> | null, validate: APIValidator<Model>): Promise<APIResult<Model>>;
26
- protected put<Model>(endpoint: string, data: Record<string, unknown> | Array<unknown>, validate: APIValidator<Model>): Promise<APIResult<Model>>;
27
- protected patch<Model>(endpoint: string, data: Record<string, unknown> | Array<unknown>, validate: APIValidator<Model>): Promise<APIResult<Model>>;
37
+ protected get<Path extends keyof paths>(endpoint: Path): Promise<APIResult<ResponseType<Path, 'get'>>>;
38
+ protected post<Path extends keyof paths>(endpoint: Path, data: RequestBody<Path, 'post'>): Promise<APIResult<ResponseType<Path, 'post'>>>;
39
+ protected put<Path extends keyof paths>(endpoint: Path, data: RequestBody<Path, 'put'>): Promise<APIResult<ResponseType<Path, 'put'>>>;
40
+ protected patch<Path extends keyof paths>(endpoint: Path, data: RequestBody<Path, 'patch'>): Promise<APIResult<ResponseType<Path, 'patch'>>>;
41
+ protected delete<Path extends keyof paths>(endpoint: Path): Promise<APIResult<ResponseType<Path, 'delete'>>>;
28
42
  refreshToken(): Promise<APIResult<AccessToken>>;
29
43
  logout(): Promise<void>;
30
44
  }
45
+ export {};
@@ -1,5 +1,4 @@
1
1
  import { failure, success } from '../lib';
2
- import { parseAccessToken } from '../models/access-token';
3
2
  export default class APIBase {
4
3
  constructor(args) {
5
4
  this.credentials = args.credentials;
@@ -18,17 +17,16 @@ export default class APIBase {
18
17
 
19
18
  static async fetchAccessToken(args) {
20
19
  const body = {
21
- apiKey: 'default',
22
- user: args.user,
20
+ username: args.user,
23
21
  password: args.password,
24
- device: args.deviceName
22
+ scope: args.deviceName
25
23
  };
26
- const res = await APIBase.fetchResult(`${args.url}/authenticate`, {
24
+ const res = await APIBase.fetchResult(`/${args.url}/authenticate`, {
27
25
  method: 'POST',
28
26
  headers: {
29
- 'Content-Type': 'application/json'
27
+ 'Content-Type': 'application/x-www-form-urlencoded'
30
28
  },
31
- body: JSON.stringify(body)
29
+ body: new URLSearchParams(body)
32
30
  });
33
31
 
34
32
  if (!res.success) {
@@ -59,22 +57,16 @@ export default class APIBase {
59
57
  }
60
58
 
61
59
  const payload = await res.data.json();
62
- return parseAccessToken(payload);
60
+ return success(payload);
63
61
  }
64
62
 
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) {
63
+ async fetchFromEnter(endpoint, method, data) {
71
64
  const body = data !== null ? JSON.stringify(data) : null;
72
65
  const headers = {
73
- Authorization: APIBase.getAuthHeader('default', this.credentials.accessToken),
74
- 'Content-Type': 'application/json',
75
- 'Enter-Live-Api': 'true'
66
+ Authorization: `bearer ${this.credentials.accessToken.access_token}`,
67
+ 'Content-Type': 'application/json'
76
68
  };
77
- const res = await APIBase.fetchResult(`${this.credentials.url}/${endpoint}`, {
69
+ const res = await APIBase.fetchResult(`${this.credentials.url}${endpoint}`, {
78
70
  method,
79
71
  headers,
80
72
  body
@@ -122,32 +114,38 @@ export default class APIBase {
122
114
  return success(json);
123
115
  }
124
116
 
125
- async get(endpoint, validate) {
126
- const rawResponse = await this.fetchFromEnter(endpoint, 'GET');
117
+ async get(endpoint) {
118
+ const rawResponse = await this.fetchFromEnter(endpoint, 'get', undefined);
119
+ if (!rawResponse.success) return rawResponse;
120
+ return success(rawResponse.data);
121
+ }
122
+
123
+ async post(endpoint, data) {
124
+ const rawResponse = await this.fetchFromEnter(endpoint, 'post', data);
127
125
  if (!rawResponse.success) return rawResponse;
128
- return validate(rawResponse.data);
126
+ return success(rawResponse.data);
129
127
  }
130
128
 
131
- async post(endpoint, data, validate) {
132
- const rawResponse = await this.fetchFromEnter(endpoint, 'POST', data);
129
+ async put(endpoint, data) {
130
+ const rawResponse = await this.fetchFromEnter(endpoint, 'put', data);
133
131
  if (!rawResponse.success) return rawResponse;
134
- return validate(rawResponse.data);
132
+ return success(rawResponse.data);
135
133
  }
136
134
 
137
- async put(endpoint, data, validate) {
138
- const rawResponse = await this.fetchFromEnter(endpoint, 'PUT', data);
135
+ async patch(endpoint, data) {
136
+ const rawResponse = await this.fetchFromEnter(endpoint, 'patch', data);
139
137
  if (!rawResponse.success) return rawResponse;
140
- return validate(rawResponse.data);
138
+ return success(rawResponse.data);
141
139
  }
142
140
 
143
- async patch(endpoint, data, validate) {
144
- const rawResponse = await this.fetchFromEnter(endpoint, 'PATCH', data);
141
+ async delete(endpoint) {
142
+ const rawResponse = await this.fetchFromEnter(endpoint, 'delete', undefined);
145
143
  if (!rawResponse.success) return rawResponse;
146
- return validate(rawResponse.data);
144
+ return success(rawResponse.data);
147
145
  }
148
146
 
149
147
  async refreshToken() {
150
- return this.post('authenticate/refresh', null, parseAccessToken);
148
+ return this.post('/authenticate/refresh', null);
151
149
  }
152
150
 
153
151
  async logout() {
@@ -156,7 +154,7 @@ export default class APIBase {
156
154
 
157
155
  try {
158
156
  // delete token at api backend so it becomes invalid
159
- await this.fetchFromEnter('/authenticate', 'DELETE');
157
+ await this.fetchFromEnter('/authenticate', 'delete', undefined);
160
158
  } catch (e) {
161
159
  console.warn('error deleting the token in the backend, so it is still valid', e);
162
160
  }
@@ -1,76 +1,58 @@
1
- import APIBase from './api-base';
1
+ import APIBase, { RequestBody } from './api-base';
2
2
  import { APIResult } from '../lib';
3
3
  import { Attendee } from '../models/attendee';
4
4
  import { Event } from '../models';
5
5
  import { CheckinCounts } from '../models/checkin-counts';
6
6
  import { FieldGroups } from '../models/field-group';
7
7
  import { Instance } from '../models/instance';
8
- import { Invite } from '../models/invite';
9
8
  import { User } from '../models/user';
10
9
  import { Variable } from '../models/variable';
11
- import { Dictionary } from '../models/_helpers';
12
10
  export type { APICredentials } from './api-base';
13
11
  export default class API extends APIBase {
12
+ getInstanceList(): Promise<APIResult<Instance[]>>;
14
13
  getInstance(args: {
15
- instanceId: string;
14
+ instanceName: string;
16
15
  }): Promise<APIResult<Instance>>;
17
- getInstanceList(): Promise<APIResult<Instance[]>>;
18
16
  getFields(args: {
19
- instanceId: string;
17
+ instanceName: string;
20
18
  }): Promise<APIResult<FieldGroups>>;
21
19
  getCheckinCounts(args: {
22
- instanceId: string;
20
+ instanceName: string;
23
21
  }): Promise<APIResult<CheckinCounts>>;
24
22
  getAttendeeList(args: {
25
- instanceId: string;
23
+ instanceName: string;
26
24
  includeDeleted?: boolean;
27
25
  }): Promise<APIResult<Attendee[]>>;
28
- sendEmail(args: {
29
- instanceId: string;
30
- collection: 'attendees';
31
- attendeeId: string;
32
- mailId: string;
33
- } | {
34
- instanceId: string;
35
- collection: 'invites';
36
- index: number;
37
- mailId: string;
38
- }): Promise<APIResult<null>>;
39
- getAttendeesByAuthCode(args: {
40
- instanceId: string;
41
- authCode: string;
42
- }): Promise<APIResult<Attendee[]>>;
43
- getInviteByAuthCode(args: {
44
- instanceId: string;
45
- authCode: string;
46
- }): Promise<APIResult<Invite>>;
47
- getAttendeeById(args: {
48
- instanceId: string;
26
+ getAttendee(args: {
27
+ instanceName: string;
49
28
  attendeeId: string;
50
29
  }): Promise<APIResult<Attendee>>;
51
- createEvents(args: {
52
- events: Event[];
53
- }): Promise<APIResult<null>>;
54
- getUser(): Promise<APIResult<User>>;
55
- patchUser(args: {
56
- userdata: Dictionary<unknown>;
57
- }): Promise<APIResult<null>>;
58
- getAllVariables(args: {
59
- instanceId: string;
30
+ getVariableList(args: {
31
+ instanceName: string;
60
32
  }): Promise<APIResult<Variable[]>>;
61
- getVariableByName(args: {
62
- instanceId: string;
33
+ getVariable(args: {
34
+ instanceName: string;
63
35
  variableName: string;
64
36
  }): Promise<APIResult<Variable>>;
65
37
  createVariable(args: {
66
- instanceId: string;
38
+ instanceName: string;
67
39
  variable: Variable;
68
40
  }): Promise<APIResult<Variable>>;
69
41
  updateVariable(args: {
70
- instanceId: string;
42
+ instanceName: string;
71
43
  variableName: string;
72
- update: {
73
- value: string;
74
- };
44
+ update: RequestBody<'/instances/{instance_name}/variables/{variable_name}', 'put'>;
75
45
  }): Promise<APIResult<Variable>>;
46
+ deleteVariable(args: {
47
+ instanceName: string;
48
+ variableName: string;
49
+ }): Promise<APIResult<null>>;
50
+ getUser(): Promise<APIResult<User>>;
51
+ updateUser(args: RequestBody<'/users/me', 'patch'>): Promise<APIResult<User>>;
52
+ sendEmail(args: {
53
+ instanceName: string;
54
+ attendeeId: string;
55
+ emailName: string;
56
+ }): Promise<APIResult<null>>;
57
+ createEvents(args: Event[]): Promise<APIResult<null>>;
76
58
  }
@@ -1,28 +1,32 @@
1
1
  import APIBase from './api-base';
2
2
  import { success } from '../lib';
3
- import { parseAttendee, parseAttendeeList } from '../models/attendee';
4
- import { parseCheckinCounts } from '../models/checkin-counts';
5
- import { parseFieldGroups } from '../models/field-group';
6
- import { parseInstance, parseInstanceList } from '../models/instance';
7
- import { parseInvite } from '../models/invite';
8
- import { parseUser } from '../models/user';
9
- import { parseVariable, parseVariableList } from '../models/variable';
10
- import { parseNoContent } from '../models/_helpers';
3
+ import { Attendee } from '../models/attendee';
4
+ import { FieldGroups } from '../models/field-group';
5
+ import { Instance } from '../models/instance';
11
6
  export default class API extends APIBase {
12
- async getInstance(args) {
13
- return this.get(`instances/${args.instanceId}`, parseInstance);
7
+ async getInstanceList() {
8
+ const result = await this.get('/instances');
9
+ if (!result.success) return result;
10
+ return success(result.data.map(instanceJSON => new Instance(instanceJSON)));
14
11
  }
15
12
 
16
- async getInstanceList() {
17
- return this.get(`instances`, parseInstanceList);
13
+ async getInstance(args) {
14
+ const instanceName = args.instanceName;
15
+ const result = await this.get(`/instances/${instanceName}`);
16
+ if (!result.success) return result;
17
+ return success(new Instance(result.data));
18
18
  }
19
19
 
20
20
  async getFields(args) {
21
- return this.get(`instances/${args.instanceId}/fields`, parseFieldGroups);
21
+ const instanceName = args.instanceName;
22
+ const result = await this.get(`/instances/${instanceName}/fields`);
23
+ if (!result.success) return result;
24
+ return success(new FieldGroups(result.data));
22
25
  }
23
26
 
24
27
  async getCheckinCounts(args) {
25
- return this.get(`instances/${args.instanceId}/checkins`, parseCheckinCounts);
28
+ const instanceName = args.instanceName;
29
+ return this.get(`/instances/${instanceName}/checkin_counts`);
26
30
  }
27
31
 
28
32
  async getAttendeeList(args) {
@@ -32,58 +36,66 @@ export default class API extends APIBase {
32
36
  query.append('include_deleted', 'true');
33
37
  }
34
38
 
35
- return this.get(`instances/${args.instanceId}/attendees?${query.toString()}`, parseAttendeeList);
39
+ const instanceName = args.instanceName;
40
+ const queryString = `?${query.toString()}`;
41
+ const result = await this.get(`/instances/${instanceName}/attendees${queryString}`);
42
+ if (!result.success) return result;
43
+ return success(result.data.map(attendeeJSON => new Attendee(attendeeJSON)));
36
44
  }
37
45
 
38
- async sendEmail(args) {
39
- const guestId = args.collection === 'attendees' ? args.attendeeId : args.index;
40
- return this.post(`instances/${args.instanceId}/${args.collection}/${guestId}/emails/${args.mailId}`, null, parseNoContent);
41
- }
42
-
43
- async getAttendeesByAuthCode(args) {
44
- // it is stupid to get all attendees here
45
- // but here we would need a special endpoint that could get attendees by authcode
46
- const allAttendees = await this.getAttendeeList({
47
- instanceId: args.instanceId
48
- });
49
- if (!allAttendees.success) return allAttendees;
50
- return success(allAttendees.data.filter(attendee => attendee.auth === args.authCode));
46
+ async getAttendee(args) {
47
+ const instanceName = args.instanceName;
48
+ const attendeeId = args.attendeeId;
49
+ const result = await this.get(`/instances/${instanceName}/attendees/${attendeeId}`);
50
+ if (!result.success) return result;
51
+ return success(new Attendee(result.data));
51
52
  }
52
53
 
53
- async getInviteByAuthCode(args) {
54
- return await this.get(`instances/${args.instanceId}/invites/${args.authCode}`, parseInvite);
54
+ async getVariableList(args) {
55
+ const instanceName = args.instanceName;
56
+ return this.get(`/instances/${instanceName}/variables`);
55
57
  }
56
58
 
57
- async getAttendeeById(args) {
58
- return await this.get(`instances/${args.instanceId}/attendees/${args.attendeeId}`, parseAttendee);
59
+ async getVariable(args) {
60
+ const instanceName = args.instanceName;
61
+ const variableName = args.variableName;
62
+ return this.get(`/instances/${instanceName}/variables/${variableName}`);
59
63
  }
60
64
 
61
- async createEvents(args) {
62
- return this.post(`events`, args.events, parseNoContent);
65
+ async createVariable(args) {
66
+ const instanceName = args.instanceName;
67
+ return this.post(`/instances/${instanceName}/variables`, args.variable);
63
68
  }
64
69
 
65
- async getUser() {
66
- return this.get('user', parseUser);
70
+ async updateVariable(args) {
71
+ const instanceName = args.instanceName;
72
+ const variableName = args.variableName;
73
+ return this.put(`/instances/${instanceName}/variables/${variableName}`, args.update);
67
74
  }
68
75
 
69
- async patchUser(args) {
70
- return this.patch('user', args, parseNoContent);
76
+ async deleteVariable(args) {
77
+ const instanceName = args.instanceName;
78
+ const variableName = args.variableName;
79
+ return this.delete(`/instances/${instanceName}/variables/${variableName}`);
71
80
  }
72
81
 
73
- async getAllVariables(args) {
74
- return this.get(`instances/${args.instanceId}/variables`, parseVariableList);
82
+ async getUser() {
83
+ return this.get('/users/me');
75
84
  }
76
85
 
77
- async getVariableByName(args) {
78
- return this.get(`instances/${args.instanceId}/variables/${args.variableName}`, parseVariable);
86
+ async updateUser(args) {
87
+ return this.patch('/users/me', args);
79
88
  }
80
89
 
81
- async createVariable(args) {
82
- return this.post(`instances/${args.instanceId}/variables`, args.variable, parseVariable);
90
+ async sendEmail(args) {
91
+ const instanceName = args.instanceName;
92
+ const attendeeId = args.attendeeId;
93
+ const emailName = args.emailName;
94
+ return this.post(`/instances/${instanceName}/attendees/${attendeeId}/emails/${emailName}/send`, null);
83
95
  }
84
96
 
85
- async updateVariable(args) {
86
- return this.put(`instances/${args.instanceId}/variables/${args.variableName}`, args.update, parseVariable);
97
+ async createEvents(args) {
98
+ return this.post(`/events`, args);
87
99
  }
88
100
 
89
101
  }