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