mp-js-api 0.0.4 → 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.
Files changed (52) hide show
  1. package/dist/api.d.ts +74 -0
  2. package/dist/api.d.ts.map +1 -0
  3. package/dist/api.js +158 -0
  4. package/dist/index.d.ts +51 -48
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +72 -317
  7. package/dist/tables/addresses.d.ts +0 -2
  8. package/dist/tables/addresses.d.ts.map +1 -1
  9. package/dist/tables/addresses.js +0 -58
  10. package/dist/tables/contact-attributes.d.ts +0 -2
  11. package/dist/tables/contact-attributes.d.ts.map +1 -1
  12. package/dist/tables/contact-attributes.js +0 -22
  13. package/dist/tables/contacts.d.ts +0 -2
  14. package/dist/tables/contacts.d.ts.map +1 -1
  15. package/dist/tables/contacts.js +0 -112
  16. package/dist/tables/event-participants.d.ts +0 -2
  17. package/dist/tables/event-participants.d.ts.map +1 -1
  18. package/dist/tables/event-participants.js +0 -50
  19. package/dist/tables/events.d.ts +0 -2
  20. package/dist/tables/events.d.ts.map +1 -1
  21. package/dist/tables/events.js +0 -120
  22. package/dist/tables/from-response-answers.d.ts +6 -6
  23. package/dist/tables/from-response-answers.d.ts.map +1 -1
  24. package/dist/tables/group-participants.d.ts +0 -2
  25. package/dist/tables/group-participants.d.ts.map +1 -1
  26. package/dist/tables/group-participants.js +0 -64
  27. package/dist/tables/groups.d.ts +0 -2
  28. package/dist/tables/groups.d.ts.map +1 -1
  29. package/dist/tables/groups.js +0 -106
  30. package/dist/tables/households.d.ts +0 -2
  31. package/dist/tables/households.d.ts.map +1 -1
  32. package/dist/tables/households.js +0 -58
  33. package/dist/tables/participants.d.ts +0 -2
  34. package/dist/tables/participants.d.ts.map +1 -1
  35. package/dist/tables/participants.js +0 -66
  36. package/dist/utils/strings.d.ts +14 -5
  37. package/dist/utils/strings.d.ts.map +1 -1
  38. package/dist/utils/strings.js +28 -15
  39. package/package.json +1 -1
  40. package/src/api.ts +266 -0
  41. package/src/index.ts +197 -435
  42. package/src/tables/addresses.ts +1 -59
  43. package/src/tables/contact-attributes.ts +0 -23
  44. package/src/tables/contacts.ts +0 -118
  45. package/src/tables/event-participants.ts +1 -51
  46. package/src/tables/events.ts +0 -119
  47. package/src/tables/from-response-answers.ts +6 -8
  48. package/src/tables/group-participants.ts +55 -119
  49. package/src/tables/groups.ts +1 -107
  50. package/src/tables/households.ts +0 -58
  51. package/src/tables/participants.ts +1 -67
  52. package/src/utils/strings.ts +31 -17
package/src/index.ts CHANGED
@@ -1,75 +1,22 @@
1
- import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
2
- import { URLSearchParams } from 'url';
3
- import { Contact, mapContactToContactRecord } from './tables/contacts';
4
- import { Participant, mapParticipantToParticipantRecord, } from './tables/participants';
5
- import { EventParticipant, mapEventParticipantToEventParticipantRecord, } from './tables/event-participants';
6
- import { GroupParticipant, mapGroupParticipantToGroupParticipantRecord, } from './tables/group-participants';
7
- import { Event } from './tables/events';
8
- import { Group } from './tables/groups';
9
- import { ContactRecord } from './tables/contacts';
10
- import { mapContactRecord } from './tables/contacts';
11
- import { mapParticipantRecord } from './tables/participants';
12
- import { ParticipantRecord } from './tables/participants';
13
- import { EventRecord } from './tables/events';
14
- import { mapEventRecord } from './tables/events';
15
- import { GroupRecord } from './tables/groups';
16
- import { mapGroupRecord } from './tables/groups';
17
- import { mapEventParticipantRecord } from './tables/event-participants';
18
- import { EventParticipantRecord } from './tables/event-participants';
19
- import { GroupParticipantRecord } from './tables/group-participants';
20
- import { mapGroupParticipantRecord } from './tables/group-participants';
21
- import { Household, HouseholdRecord, mapHouseholdRecord, mapHouseholdToHouseholdRecord, } from './tables/households';
22
- import { Address, AddressRecord, mapAddressRecord, mapAddressToAddressRecord } from './tables/addresses';
23
- import { ContactAttribute, ContactAttributeRecord, mapContactAttributeRecord, mapContactAttributeToContactAttributeRecord } from './tables/contact-attributes';
24
- import { convertToCamelCase, convertToSnakeCase, escapeSql } from './utils/strings';
1
+ import { createApiBase, MPApiBase, ErrorDetails, MPGetOptions, MPCreateOptions, MPUpdateOptions } from './api';
2
+ import { convertToCamelCase, convertToSnakeCase, escapeSql, stringifyURLParams } from './utils/strings';
3
+ import { Contact, ContactRecord } from './tables/contacts';
4
+ import { Event, EventRecord } from './tables/events';
5
+ import { Group, GroupRecord } from './tables/groups';
6
+ import { Address, AddressRecord } from './tables/addresses';
7
+ import { Household, HouseholdRecord } from './tables/households';
8
+ import { Participant, ParticipantRecord } from './tables/participants';
9
+ import { EventParticipant, EventParticipantRecord } from './tables/event-participants';
10
+ import { GroupParticipant, GroupParticipantRecord } from './tables/group-participants';
11
+ import { ContactAttribute, ContactAttributeRecord } from './tables/contact-attributes';
25
12
  import { FormResponse, FormResponseRecord } from './tables/form-responses';
13
+ import { FormResponseAnswer } from './tables/from-response-answers';
14
+ import { AxiosInstance } from 'axios';
26
15
 
27
- interface TokenData {
28
- access_token: string;
29
- expires_in: number;
30
- token_type: 'Bearer';
31
- }
32
-
33
- interface AccessToken {
34
- digest: string;
35
- expiration: number;
36
- }
37
-
38
- const createTokenGetter = (auth: { username: string; password: string; }) => {
39
- let token: AccessToken | undefined;
40
-
41
- return async () => {
42
- // If the token is near expiration, get a new one.
43
- if (!token || token.expiration - 60000 < Date.now()) {
44
- const tokenRes = await axios.post<TokenData>(
45
- 'https://mp.revival.com/ministryplatformapi/oauth/connect/token',
46
- new URLSearchParams({
47
- grant_type: 'client_credentials',
48
- scope: 'http://www.thinkministry.com/dataplatform/scopes/all',
49
- }).toString(),
50
- { auth }
51
- );
52
- const [, payload] = tokenRes.data.access_token.split('.');
53
- try {
54
- const jsonPayload: { exp: number; } = JSON.parse(
55
- Buffer.from(payload, 'base64url').toString()
56
- );
57
- token = {
58
- digest: tokenRes.data.access_token,
59
- expiration: jsonPayload.exp * 1000,
60
- };
61
- return token.digest;
62
- } catch (err) {
63
- console.error(err);
64
- }
65
- } else {
66
- return token.digest;
67
- }
68
- };
69
- };
70
16
 
71
17
  type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
72
18
 
19
+
73
20
  export type CreateContactParams = WithRequired<
74
21
  Omit<Partial<Contact>, 'contactID'>,
75
22
  'company' | 'displayName'
@@ -102,480 +49,289 @@ export type CreateFormResponseParams = WithRequired<
102
49
  Omit<Partial<FormResponse>, 'formResponseID'>,
103
50
  'formID' | 'responseDate'
104
51
  >;
52
+ export type CreateFormResponseAnswerParams = WithRequired<
53
+ Omit<Partial<FormResponseAnswer>, 'formResponseAnswerID'>,
54
+ 'formFieldID' | 'formResponseID'
55
+ >;
105
56
 
106
- export type MPGetOptions = {
107
- select?: string;
108
- filter?: string;
109
- orderBy?: string;
110
- groupBy?: string;
111
- top?: number;
112
- skip?: number;
113
- distinct?: boolean;
114
- };
115
-
116
- export type MPCreateOptions = {
117
- user?: number;
118
- };
119
57
 
120
- export type MPUpdateOptions = MPCreateOptions;
121
- export type MPGetInstance = <T = any, R = AxiosResponse<T, any>>(url: string, mpOptions?: MPGetOptions, config?: AxiosRequestConfig) => Promise<R>;
122
58
  export type MPInstance = {
123
- get: MPGetInstance; // - AxiosInstance['get']
124
- post: AxiosInstance['post'];
59
+
60
+ get: AxiosInstance['get'];
125
61
  put: AxiosInstance['put'];
62
+ post: AxiosInstance['post'];
63
+ create: MPApiBase['create'];
64
+ update: MPApiBase['update'];
65
+ getOne: MPApiBase['getOne'];
66
+ getMultiple: MPApiBase['getMultiple'];
67
+
126
68
  getContact(
127
69
  id: number,
128
70
  options?: MPGetOptions
129
- ): Promise<Contact | undefined | { error: any; }>;
71
+ ): Promise<Contact | undefined | { error: ErrorDetails; }>;
130
72
  getHousehold(
131
73
  id: number,
132
74
  options?: MPGetOptions
133
- ): Promise<Household | undefined | { error: any; }>;
75
+ ): Promise<Household | undefined | { error: ErrorDetails; }>;
134
76
  getAddress(
135
77
  id: number,
136
78
  options?: MPGetOptions
137
- ): Promise<Address | undefined | { error: any; }>;
79
+ ): Promise<Address | undefined | { error: ErrorDetails; }>;
138
80
  getParticipant(
139
81
  id: number,
140
82
  options?: MPGetOptions
141
- ): Promise<Participant | undefined | { error: any; }>;
83
+ ): Promise<Participant | undefined | { error: ErrorDetails; }>;
84
+ getContactAttribute(
85
+ id: number,
86
+ options?: MPGetOptions
87
+ ): Promise<ContactAttribute | undefined | { error: ErrorDetails; }>;
142
88
  getEvent(
143
89
  id: number,
144
90
  options?: MPGetOptions
145
- ): Promise<Event | undefined | { error: any; }>;
91
+ ): Promise<Event | undefined | { error: ErrorDetails; }>;
146
92
  getGroup(
147
93
  id: number,
148
94
  options?: MPGetOptions
149
- ): Promise<Group | undefined | { error: any; }>;
95
+ ): Promise<Group | undefined | { error: ErrorDetails; }>;
150
96
  getEventParticipant(
151
97
  id: number,
152
98
  options?: MPGetOptions
153
- ): Promise<EventParticipant | undefined | { error: any; }>;
99
+ ): Promise<EventParticipant | undefined | { error: ErrorDetails; }>;
154
100
  getGroupParticipant(
155
101
  id: number,
156
102
  options?: MPGetOptions
157
- ): Promise<GroupParticipant | undefined | { error: any; }>;
103
+ ): Promise<GroupParticipant | undefined | { error: ErrorDetails; }>;
104
+ getFormResponse(
105
+ id: number,
106
+ options?: MPGetOptions
107
+ ): Promise<FormResponse | undefined | { error: ErrorDetails; }>;
158
108
 
159
109
  getContacts(
160
110
  options?: MPGetOptions
161
- ): Promise<Contact[] | { error: any; }>;
111
+ ): Promise<Contact[] | { error: ErrorDetails; }>;
162
112
  getHouseholds(
163
113
  options?: MPGetOptions
164
- ): Promise<Household[] | { error: any; }>;
114
+ ): Promise<Household[] | { error: ErrorDetails; }>;
165
115
  getAddresses(
166
116
  options?: MPGetOptions
167
- ): Promise<Address[] | { error: any; }>;
117
+ ): Promise<Address[] | { error: ErrorDetails; }>;
168
118
  getParticipants(
169
119
  options?: MPGetOptions
170
- ): Promise<Participant[] | { error: any; }>;
171
- getEvents(options?: MPGetOptions): Promise<Event[] | { error: any; }>;
172
- getGroups(options?: MPGetOptions): Promise<Group[] | { error: any; }>;
120
+ ): Promise<Participant[] | { error: ErrorDetails; }>;
121
+ getEvents(options?: MPGetOptions): Promise<Event[] | { error: ErrorDetails; }>;
122
+ getGroups(options?: MPGetOptions): Promise<Group[] | { error: ErrorDetails; }>;
173
123
  getEventParticipants(
174
124
  options?: MPGetOptions
175
- ): Promise<EventParticipant[] | { error: any; }>;
125
+ ): Promise<EventParticipant[] | { error: ErrorDetails; }>;
176
126
  getGroupParticipants(
177
127
  options?: MPGetOptions
178
- ): Promise<GroupParticipant[] | { error: any; }>;
128
+ ): Promise<GroupParticipant[] | { error: ErrorDetails; }>;
179
129
 
180
130
  createContact(
181
131
  params: CreateContactParams,
182
132
  options?: MPCreateOptions
183
- ): Promise<Contact | { error: any; }>;
133
+ ): Promise<Contact | { error: ErrorDetails; }>;
184
134
  createHousehold(
185
135
  params: CreateHouseholdParams,
186
136
  options?: MPCreateOptions
187
- ): Promise<Household | { error: any; }>;
137
+ ): Promise<Household | { error: ErrorDetails; }>;
188
138
  createAddress(
189
139
  params: CreateAddressParams,
190
140
  options?: MPCreateOptions
191
- ): Promise<Address | { error: any; }>;
141
+ ): Promise<Address | { error: ErrorDetails; }>;
192
142
  createParticipant(
193
143
  params: CreateParticipantParams,
194
144
  options?: MPCreateOptions
195
- ): Promise<Participant | { error: any; }>;
145
+ ): Promise<Participant | { error: ErrorDetails; }>;
196
146
  createEventParticipant(
197
147
  params: CreateEventParticipantParams,
198
148
  options?: MPCreateOptions
199
- ): Promise<EventParticipant | { error: any; }>;
149
+ ): Promise<EventParticipant | { error: ErrorDetails; }>;
200
150
  createGroupParticipant(
201
151
  params: CreateGroupParticipantParams,
202
152
  options?: MPCreateOptions
203
- ): Promise<GroupParticipant | { error: any; }>;
153
+ ): Promise<GroupParticipant | { error: ErrorDetails; }>;
204
154
  createContactAttribute(
205
155
  params: CreateContactAttributeParams,
206
156
  options?: MPCreateOptions
207
- ): Promise<ContactAttribute | { error: any; }>;
157
+ ): Promise<ContactAttribute | { error: ErrorDetails; }>;
208
158
  createFormResponse(
209
159
  params: CreateFormResponseParams,
210
160
  options?: MPCreateOptions
211
- ): Promise<FormResponse | { error: any; }>;
161
+ ): Promise<FormResponse | { error: ErrorDetails; }>;
162
+ createFormResponseAnswers(
163
+ params: CreateFormResponseAnswerParams,
164
+ options?: MPCreateOptions
165
+ ): Promise<FormResponseAnswer | { error: ErrorDetails; }>;
166
+
212
167
  updateContacts(
213
168
  contacts: WithRequired<Partial<Contact>, 'contactID'>[],
214
169
  options?: MPUpdateOptions
215
- ): Promise<Contact[] | { error: any; }>;
216
-
170
+ ): Promise<Contact[] | { error: ErrorDetails; }>;
217
171
  updateEventParticipants(
218
172
  participants: WithRequired<Partial<EventParticipant>, 'eventParticipantID'>[],
219
173
  options?: MPUpdateOptions
220
- ): Promise<EventParticipant[] | { error: any; }>;
174
+ ): Promise<EventParticipant[] | { error: ErrorDetails; }>;
221
175
  };
222
176
 
223
- const stringifyMPOptions = (mpOptions: MPGetOptions | MPCreateOptions | MPUpdateOptions = {}) =>
224
- escapeSql(Object.entries(mpOptions).reduce((acc, [key, value]) => {
225
- if (!acc) {
226
- acc += `?$${key}=${value}`;
227
- } else {
228
- acc += `&$${key}=${value}`;
229
- }
230
- return acc;
231
- }, ''));
232
177
 
233
178
  export const createMPInstance = ({ auth }: { auth: { username: string; password: string; }; }): MPInstance => {
234
- /**
235
- * Gets MP oauth token.
236
- * @returns token
237
- */
238
- const getToken = createTokenGetter(auth);
239
- const api = axios.create({
240
- baseURL: 'https://mp.revival.com/ministryplatformapi',
241
- });
242
179
 
243
- const get = async <T = any, R = AxiosResponse<T, any>>(
244
- url: string,
245
- mpOptions: MPGetOptions,
246
- config?: AxiosRequestConfig
247
- ) =>
248
- api.get<T, R>(url + stringifyMPOptions(mpOptions), {
249
- ...config,
250
- headers: {
251
- ...config?.headers,
252
- Authorization: `Bearer ${await getToken()}`,
253
- },
254
- });
255
- const post = async <T, R = AxiosResponse<T, any>>(
256
- url: string,
257
- data?: any,
258
- config?: AxiosRequestConfig
259
- ) =>
260
- api.post<T, R>(url, data, {
261
- ...config,
262
- headers: {
263
- ...config?.headers,
264
- Authorization: `Bearer ${await getToken()}`,
265
- },
266
- });
267
- const put = async <T, R = AxiosResponse<T, any>>(
268
- url: string,
269
- data?: any,
270
- config?: AxiosRequestConfig
271
- ) =>
272
- api.put<T, R>(url, data, {
273
- ...config,
274
- headers: {
275
- ...config?.headers,
276
- Authorization: `Bearer ${await getToken()}`,
277
- },
278
- });
180
+ const { getOne, getMultiple, create, update, get, post, put, getError } = createApiBase({ auth });
279
181
 
280
182
  return {
183
+ getOne,
184
+ getMultiple,
185
+ create,
186
+ update,
281
187
  get,
282
188
  post,
283
189
  put,
284
- async getContact(id, options = {}) {
285
- try {
286
- const res = await get<[ContactRecord?]>(
287
- `/tables/contacts/${id}`, options
288
- );
289
- return res.data[0] ? mapContactRecord(res.data[0]) : undefined;
290
- } catch (err) {
291
- return { error: err };
292
- }
190
+ async getContact(id, mpOptions = {}) {
191
+ return getOne<ContactRecord, Contact>(
192
+ { path: `/tables/contacts`, id, mpOptions }
193
+ );
293
194
  },
294
- async getHousehold(id, options = {}) {
295
- try {
296
- const res = await get<[HouseholdRecord?]>(
297
- `/tables/households/${id}`, options
298
- );
299
- return res.data[0]
300
- ? mapHouseholdRecord(res.data[0])
301
- : undefined;
302
- } catch (err) {
303
- return { error: err };
304
- }
195
+ async getHousehold(id, mpOptions = {}) {
196
+ return getOne<HouseholdRecord, Household>(
197
+ { path: `/tables/households`, id, mpOptions }
198
+ );
305
199
  },
306
- async getAddress(id, options = {}) {
307
- try {
308
- const res = await get<[AddressRecord?]>(
309
- `/tables/addresses/${id}`, options
310
- );
311
- return res.data[0] ? convertToCamelCase(res.data[0]) : undefined;
312
- } catch (err) {
313
- return { error: err };
314
- }
200
+ async getAddress(id, mpOptions = {}) {
201
+ return getOne<AddressRecord, Address>(
202
+ { path: `/tables/addresses`, id, mpOptions }
203
+ );
315
204
  },
316
- async getParticipant(id, options = {}) {
317
- try {
318
- const res = await get<[ParticipantRecord?]>(
319
- `/tables/participants/${id}`, options
320
- );
321
- return res.data[0]
322
- ? mapParticipantRecord(res.data[0])
323
- : undefined;
324
- } catch (err) {
325
- return { error: err };
326
- }
205
+ async getParticipant(id, mpOptions = {}) {
206
+ return getOne<ParticipantRecord, Participant>(
207
+ { path: `/tables/participants`, id, mpOptions }
208
+ );
327
209
  },
328
- async getEvent(id, options = {}) {
329
- try {
330
- const res = await get<[EventRecord?]>(
331
- `/tables/events/${id}`, options
332
- );
333
- return res.data[0] ? convertToCamelCase(res.data[0]) : undefined;
334
- } catch (err) {
335
- return { error: err };
336
- }
210
+ async getContactAttribute(id, mpOptions = {}) {
211
+ return getOne<ContactAttributeRecord, ContactAttribute>(
212
+ { path: `/tables/participants`, id, mpOptions }
213
+ );
337
214
  },
338
- async getGroup(id, options = {}) {
339
- try {
340
- const res = await get<[GroupRecord?]>(
341
- `/tables/groups/${id}`, options
342
- );
343
- return res.data[0] ? mapGroupRecord(res.data[0]) : undefined;
344
- } catch (err) {
345
- return { error: err };
346
- }
215
+ async getEvent(id, mpOptions = {}) {
216
+ return getOne<EventRecord, Event>(
217
+ { path: `/tables/events`, id, mpOptions }
218
+ );
347
219
  },
348
- async getEventParticipant(id, options = {}) {
349
- try {
350
- const res = await get<[EventParticipantRecord?]>(
351
- `/tables/event_participants/${id}`, options
352
- );
353
- return res.data[0]
354
- ? mapEventParticipantRecord(res.data[0])
355
- : undefined;
356
- } catch (err) {
357
- return { error: err };
358
- }
220
+ async getGroup(id, mpOptions = {}) {
221
+ return getOne<GroupRecord, Group>(
222
+ { path: `/tables/groups`, id, mpOptions }
223
+ );
359
224
  },
360
- async getGroupParticipant(id, options = {}) {
361
- try {
362
- const res = await get<[GroupParticipantRecord?]>(
363
- `/tables/group_participants/${id}`, options
364
- );
365
- return res.data[0]
366
- ? mapGroupParticipantRecord(res.data[0])
367
- : undefined;
368
- } catch (err) {
369
- return { error: err };
370
- }
225
+ async getEventParticipant(id, mpOptions = {}) {
226
+ return getOne<EventParticipantRecord, EventParticipant>(
227
+ { path: `/tables/event_participants`, id, mpOptions }
228
+ );
371
229
  },
372
- async getContacts(options = {}) {
373
- try {
374
- const res = await get<ContactRecord[]>(
375
- `/tables/contacts`, options
376
- );
377
- return res.data.map(mapContactRecord);
378
- } catch (err) {
379
- return { error: err };
380
- }
230
+ async getGroupParticipant(id, mpOptions = {}) {
231
+ return getOne<GroupParticipantRecord, GroupParticipant>(
232
+ { path: `/tables/group_participants`, id, mpOptions }
233
+ );
381
234
  },
382
- async getHouseholds(options = {}) {
383
- try {
384
- const res = await get<HouseholdRecord[]>(
385
- `/tables/households`, options
386
- );
387
- return res.data.map(mapHouseholdRecord);
388
- } catch (err) {
389
- return { error: err };
390
- }
235
+ async getFormResponse(id, mpOptions = {}) {
236
+ return getOne<FormResponseRecord, FormResponse>(
237
+ { path: `/tables/form_responses`, id, mpOptions }
238
+ );
391
239
  },
392
- async getAddresses(options = {}) {
393
- try {
394
- const res = await get<AddressRecord[]>(
395
- `/tables/addresses`, options
396
- );
397
- return res.data.map(mapAddressRecord);
398
- } catch (err) {
399
- return { error: err };
400
- }
240
+ async getContacts(mpOptions = {}) {
241
+ return getMultiple<ContactRecord, Contact>(
242
+ { path: `/tables/contacts`, mpOptions }
243
+ );
401
244
  },
402
- async getParticipants(options = {}) {
403
- try {
404
- const res = await get<ParticipantRecord[]>(
405
- `/tables/participants`, options
406
- );
407
- return res.data.map(mapParticipantRecord);
408
- } catch (err) {
409
- return { error: err };
410
- }
245
+ async getHouseholds(mpOptions = {}) {
246
+ return getMultiple<HouseholdRecord, Household>(
247
+ { path: `/tables/households`, mpOptions }
248
+ );
411
249
  },
412
- async getEvents(options = {}) {
413
- try {
414
- const res = await get<EventRecord[]>(`/tables/events`, options);
415
- return res.data.map(mapEventRecord);
416
- } catch (err) {
417
- return { error: err };
418
- }
250
+ async getAddresses(mpOptions = {}) {
251
+ return getMultiple<AddressRecord, Address>(
252
+ { path: `/tables/addresses`, mpOptions }
253
+ );
419
254
  },
420
- async getGroups(options = {}) {
421
- try {
422
- const res = await get<GroupRecord[]>(`/tables/groups`, options);
423
- return res.data.map(mapGroupRecord);
424
- } catch (err) {
425
- return { error: err };
426
- }
255
+ async getParticipants(mpOptions = {}) {
256
+ return getMultiple<ParticipantRecord, Participant>(
257
+ { path: `/tables/participants`, mpOptions }
258
+ );
427
259
  },
428
- async getEventParticipants(options = {}) {
429
- try {
430
- const res = await get<EventParticipantRecord[]>(
431
- `/tables/event_participants`, options
432
- );
433
- return res.data.map(mapEventParticipantRecord);
434
- } catch (err) {
435
- return { error: err };
436
- }
260
+ async getEvents(mpOptions = {}) {
261
+ return getMultiple<EventRecord, Event>(
262
+ { path: `/tables/events`, mpOptions }
263
+ );
437
264
  },
438
- async getGroupParticipants(options = {}) {
439
- try {
440
- const res = await get<GroupParticipantRecord[]>(
441
- `/tables/group_participants`, options
442
- );
443
- return res.data.map(mapGroupParticipantRecord);
444
- } catch (err) {
445
- return { error: err };
446
- }
265
+ async getGroups(mpOptions = {}) {
266
+ return getMultiple<GroupRecord, Group>(
267
+ { path: `/tables/groups`, mpOptions }
268
+ );
447
269
  },
448
- async createContact(params, options = {}) {
449
- const query = stringifyMPOptions(options);
450
- try {
451
- const res = await post<[ContactRecord]>(
452
- `/tables/contacts${query}`,
453
- [mapContactToContactRecord(params as Contact)]
454
- );
455
- return mapContactRecord(res.data[0]);
456
- } catch (err) {
457
- return { error: err };
458
- }
270
+ async getEventParticipants(mpOptions = {}) {
271
+ return getMultiple<EventParticipantRecord, EventParticipant>(
272
+ { path: `/tables/event_participants`, mpOptions }
273
+ );
459
274
  },
460
- async createHousehold(params, options = {}) {
461
- const query = stringifyMPOptions(options);
462
- try {
463
- const res = await post<[HouseholdRecord]>(
464
- `/tables/households${query}`,
465
- [mapHouseholdToHouseholdRecord(params as Household)]
466
- );
467
- return mapHouseholdRecord(res.data[0]);
468
- } catch (err) {
469
- return { error: err };
470
- }
275
+ async getGroupParticipants(mpOptions = {}) {
276
+ return getMultiple<GroupParticipantRecord, GroupParticipant>(
277
+ { path: `/tables/group_participants`, mpOptions }
278
+ );
471
279
  },
472
- async createAddress(params, options = {}) {
473
- const query = stringifyMPOptions(options);
474
- try {
475
- const res = await post<[AddressRecord]>(
476
- `/tables/addresses${query}`,
477
- [mapAddressToAddressRecord(params as Address)]
478
- );
479
- return mapAddressRecord(res.data[0]);
480
- } catch (err) {
481
- return { error: err };
482
- }
280
+
281
+ async createContact(params, mpOptions = {}) {
282
+ return create<CreateContactParams, Contact>(
283
+ { path: `/tables/contacts`, mpOptions, params }
284
+ );
483
285
  },
484
- async createParticipant(params, options = {}) {
485
- const query = stringifyMPOptions(options);
486
- try {
487
- const res = await post<[ParticipantRecord]>(
488
- `/tables/participants${query}`,
489
- [mapParticipantToParticipantRecord(params as Participant)]
490
- );
491
- return mapParticipantRecord(res.data[0]);
492
- } catch (err) {
493
- return { error: err };
494
- }
286
+ async createHousehold(params, mpOptions) {
287
+ return create<CreateHouseholdParams, Household>(
288
+ { path: `/tables/households`, mpOptions, params }
289
+ );
495
290
  },
496
- async createEventParticipant(params, options = {}) {
497
- const query = stringifyMPOptions(options);
498
- try {
499
- const res = await post<[EventParticipantRecord]>(
500
- `/tables/event_participants${query}`,
501
- [
502
- mapEventParticipantToEventParticipantRecord(
503
- params as EventParticipant
504
- ),
505
- ]
506
- );
507
- return mapEventParticipantRecord(res.data[0]);
508
- } catch (err) {
509
- return { error: err };
510
- }
291
+ async createAddress(params, mpOptions) {
292
+ return create<CreateAddressParams, Address>(
293
+ { path: `/tables/addresses`, mpOptions, params }
294
+ );
511
295
  },
512
- async createGroupParticipant(params, options = {}) {
513
- const query = stringifyMPOptions(options);
514
- try {
515
- const res = await post<[GroupParticipantRecord]>(
516
- `/tables/group_participants${query}`,
517
- [
518
- mapGroupParticipantToGroupParticipantRecord(
519
- params as GroupParticipant
520
- ),
521
- ]
522
- );
523
- return mapGroupParticipantRecord(res.data[0]);
524
- } catch (err) {
525
- return { error: err };
526
- }
296
+ async createParticipant(params, mpOptions) {
297
+ return create<CreateParticipantParams, Participant>(
298
+ { path: `/tables/participants`, mpOptions, params }
299
+ );
527
300
  },
528
- async createContactAttribute(params, options = {}) {
529
- const query = stringifyMPOptions(options);
530
- try {
531
- const res = await post<[ContactAttributeRecord]>(
532
- `/tables/contact_attributes${query}`,
533
- [
534
- mapContactAttributeToContactAttributeRecord(
535
- params as ContactAttribute
536
- ),
537
- ]
538
- );
539
- return mapContactAttributeRecord(res.data[0]);
540
- } catch (err) {
541
- return { error: err };
542
- }
301
+ async createEventParticipant(params, mpOptions) {
302
+ return create<CreateEventParticipantParams, EventParticipant>(
303
+ { path: `/tables/event_participants`, mpOptions, params }
304
+ );
543
305
  },
544
-
545
- async createFormResponse(params: CreateFormResponseParams, options = {}) {
546
- const query = stringifyMPOptions(options);
547
- try {
548
- const res = await post<[FormResponseRecord]>(
549
- `/tables/form_responses${query}`, [
550
- convertToSnakeCase<CreateFormResponseParams, FormResponseRecord>(params)
551
- ]);
552
- return convertToCamelCase(res.data[0]);
553
- } catch (err: any) {
554
- return { error: err };
555
- }
306
+ async createGroupParticipant(params, mpOptions) {
307
+ return create<CreateGroupParticipantParams, GroupParticipant>(
308
+ { path: `/tables/group_participants`, mpOptions, params }
309
+ );
556
310
  },
557
- async updateContacts(contacts, options = {}) {
558
- const query = stringifyMPOptions(options);
559
- try {
560
- const res = await put<ContactRecord[]>(
561
- `/tables/contacts${query}`,
562
- contacts.map(mapContactToContactRecord)
563
- );
564
- return res.data.map(mapContactRecord);
565
- } catch (err) {
566
- return { error: err };
567
- }
311
+ async createContactAttribute(params, mpOptions) {
312
+ return create<CreateContactAttributeParams, ContactAttribute>(
313
+ { path: `/tables/contact_attributes`, mpOptions, params }
314
+ );
568
315
  },
569
- async updateEventParticipants(eventParticipants, options = {}) {
570
- try {
571
- const res = await put<EventParticipantRecord[]>(
572
- `/tables/event_participants`,
573
- eventParticipants.map(mapEventParticipantToEventParticipantRecord)
574
- );
575
- return res.data.map(mapEventParticipantRecord);
576
- } catch (err) {
577
- return { error: err };
578
- }
316
+ async createFormResponse(params: CreateFormResponseParams, mpOptions) {
317
+ return create<CreateFormResponseParams, FormResponse>(
318
+ { path: `/tables/form_responses`, mpOptions, params }
319
+ );
320
+ },
321
+ async createFormResponseAnswers(params, mpOptions) {
322
+ return create<CreateFormResponseAnswerParams, FormResponseAnswer>(
323
+ { path: `/tables/form_response_answers`, mpOptions, params }
324
+ );
325
+ },
326
+ async updateContacts(params, mpOptions) {
327
+ return update<Partial<Contact>, Contact>(
328
+ { path: `/tables/contacts`, mpOptions, params }
329
+ );
330
+ },
331
+ async updateEventParticipants(params, mpOptions) {
332
+ return update<Partial<EventParticipant>, EventParticipant>(
333
+ { path: `/tables/event_participants`, mpOptions, params }
334
+ );
579
335
  },
580
336
  };
581
337
  };
@@ -587,7 +343,13 @@ export {
587
343
  Group,
588
344
  EventParticipant,
589
345
  GroupParticipant,
346
+ ContactAttribute,
590
347
  Household,
591
348
  Address,
592
- ContactAttribute
349
+ FormResponse,
350
+ ErrorDetails,
351
+ convertToCamelCase,
352
+ convertToSnakeCase,
353
+ stringifyURLParams,
354
+ escapeSql
593
355
  };