mp-js-api 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/api.ts +9 -8
- package/src/index.ts +26 -1
- package/src/tables/contact-email-addresses.ts +19 -0
- package/src/utils/strings.ts +2 -1
- package/src/utils/types.d.ts +1 -5
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -24,13 +24,12 @@ export interface MPApiBase {
|
|
|
24
24
|
|
|
25
25
|
export interface ErrorDetails {
|
|
26
26
|
message: string;
|
|
27
|
-
name
|
|
27
|
+
name?: string;
|
|
28
28
|
code?: string;
|
|
29
29
|
status?: number;
|
|
30
30
|
method?: string;
|
|
31
31
|
url?: string;
|
|
32
|
-
data
|
|
33
|
-
error: true;
|
|
32
|
+
data?: string;
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
|
|
@@ -77,7 +76,7 @@ export const createApiBase = ({ auth }: { auth: { username: string; password: st
|
|
|
77
76
|
baseURL: 'https://mp.revival.com/ministryplatformapi',
|
|
78
77
|
});
|
|
79
78
|
|
|
80
|
-
const getOne: APIGetOneInstance = async <T, R>({ id, path, mpOptions, config }) => {
|
|
79
|
+
const getOne: APIGetOneInstance = async <T, R>({ id, path, mpOptions, config }: APIGetParameter & { id: number; }) => {
|
|
81
80
|
try {
|
|
82
81
|
const url = `${path}/${id}` + stringifyURLParams(mpOptions);
|
|
83
82
|
const res = await api.get<T>(url, {
|
|
@@ -94,9 +93,9 @@ export const createApiBase = ({ auth }: { auth: { username: string; password: st
|
|
|
94
93
|
}
|
|
95
94
|
};
|
|
96
95
|
|
|
97
|
-
const getMany: APIGetMultipleInstance = async <T, R>({ path, mpOptions, config }: APIGetParameter) => {
|
|
96
|
+
const getMany: APIGetMultipleInstance = async <T, R>({ id, path, mpOptions, config }: APIGetParameter) => {
|
|
98
97
|
try {
|
|
99
|
-
const url = path + stringifyURLParams(mpOptions);
|
|
98
|
+
const url = id ? `${path}/${id}` : path + stringifyURLParams(mpOptions);
|
|
100
99
|
const res = await api.get<T[]>(url, {
|
|
101
100
|
...config,
|
|
102
101
|
headers: {
|
|
@@ -212,7 +211,6 @@ export const createApiBase = ({ auth }: { auth: { username: string; password: st
|
|
|
212
211
|
|
|
213
212
|
const getError = function (error: AxiosError): ErrorDetails {
|
|
214
213
|
return {
|
|
215
|
-
error: true,
|
|
216
214
|
message: error.message,
|
|
217
215
|
name: error.name,
|
|
218
216
|
code: error.code,
|
|
@@ -269,6 +267,7 @@ export type MPCreateOptions = {
|
|
|
269
267
|
export type MPUpdateOptions = MPCreateOptions;
|
|
270
268
|
|
|
271
269
|
interface APIGetParameter {
|
|
270
|
+
id?: number;
|
|
272
271
|
path: string;
|
|
273
272
|
mpOptions?: MPGetOptions;
|
|
274
273
|
config?: AxiosRequestConfig;
|
|
@@ -292,4 +291,6 @@ interface APIUpdateParameter<T> {
|
|
|
292
291
|
params: T[],
|
|
293
292
|
mpOptions?: MPCreateOptions;
|
|
294
293
|
config?: AxiosRequestConfig;
|
|
295
|
-
};
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
export type DateTimeIsoString = `${number}-${number}-${number}T${number}:${number}:${number}`;
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createApiBase, MPApiBase, ErrorDetails, MPGetOptions, MPCreateOptions, MPUpdateOptions } from './api';
|
|
1
|
+
import { createApiBase, MPApiBase, ErrorDetails, MPGetOptions, MPCreateOptions, MPUpdateOptions, DateTimeIsoString } from './api';
|
|
2
2
|
import { convertToCamelCase, convertToSnakeCase, escapeSql, stringifyURLParams } from './utils/strings';
|
|
3
3
|
import { Contact, ContactRecord } from './tables/contacts';
|
|
4
4
|
import { Event, EventRecord } from './tables/events';
|
|
@@ -12,6 +12,7 @@ import { ContactAttribute, ContactAttributeRecord } from './tables/contact-attri
|
|
|
12
12
|
import { FormResponse, FormResponseRecord } from './tables/form-responses';
|
|
13
13
|
import { FormResponseAnswer } from './tables/from-response-answers';
|
|
14
14
|
import { AxiosInstance } from 'axios';
|
|
15
|
+
import { ContactEmailAddress, ContactEmailAddressRecord } from './tables/contact-email-addresses';
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
@@ -53,6 +54,10 @@ export type CreateFormResponseAnswerParams = WithRequired<
|
|
|
53
54
|
Omit<Partial<FormResponseAnswer>, 'formResponseAnswerID'>,
|
|
54
55
|
'formFieldID' | 'formResponseID'
|
|
55
56
|
>;
|
|
57
|
+
export type CreateContactEmailAddressParams = WithRequired<
|
|
58
|
+
Omit<Partial<ContactEmailAddress>, 'emailAddressID'>,
|
|
59
|
+
'emailAddress' | 'contactID'
|
|
60
|
+
>;
|
|
56
61
|
|
|
57
62
|
|
|
58
63
|
export type MPInstance = {
|
|
@@ -110,6 +115,10 @@ export type MPInstance = {
|
|
|
110
115
|
getContacts(
|
|
111
116
|
options?: MPGetOptions
|
|
112
117
|
): Promise<Contact[] | { error: ErrorDetails; }>;
|
|
118
|
+
getContactEmailAddresses(
|
|
119
|
+
id?: number,
|
|
120
|
+
options?: MPGetOptions
|
|
121
|
+
): Promise<ContactEmailAddress[] | { error: ErrorDetails; }>;
|
|
113
122
|
getHouseholds(
|
|
114
123
|
options?: MPGetOptions
|
|
115
124
|
): Promise<Household[] | { error: ErrorDetails; }>;
|
|
@@ -164,6 +173,10 @@ export type MPInstance = {
|
|
|
164
173
|
params: CreateFormResponseAnswerParams[],
|
|
165
174
|
options?: MPCreateOptions
|
|
166
175
|
): Promise<FormResponseAnswer[] | { error: ErrorDetails; }>;
|
|
176
|
+
createContactEmailAddress(
|
|
177
|
+
params: CreateContactEmailAddressParams[],
|
|
178
|
+
options?: MPCreateOptions
|
|
179
|
+
): Promise<ContactEmailAddress[] | { error: ErrorDetails; }>;
|
|
167
180
|
|
|
168
181
|
updateContacts(
|
|
169
182
|
contacts: WithRequired<Partial<Contact>, 'contactID'>[],
|
|
@@ -254,6 +267,11 @@ export const createMPInstance = ({ auth }: { auth: { username: string; password:
|
|
|
254
267
|
{ path: `/tables/addresses`, mpOptions }
|
|
255
268
|
);
|
|
256
269
|
},
|
|
270
|
+
async getContactEmailAddresses(id, mpOptions = {}) {
|
|
271
|
+
return getMany<ContactEmailAddressRecord, ContactEmailAddress>(
|
|
272
|
+
{ path: `/tables/contact_email_addresses`, id, mpOptions }
|
|
273
|
+
);
|
|
274
|
+
},
|
|
257
275
|
async getParticipants(mpOptions = {}) {
|
|
258
276
|
return getMany<ParticipantRecord, Participant>(
|
|
259
277
|
{ path: `/tables/participants`, mpOptions }
|
|
@@ -325,6 +343,11 @@ export const createMPInstance = ({ auth }: { auth: { username: string; password:
|
|
|
325
343
|
{ path: `/tables/form_response_answers`, mpOptions, params }
|
|
326
344
|
);
|
|
327
345
|
},
|
|
346
|
+
async createContactEmailAddress(params, mpOptions) {
|
|
347
|
+
return createMany<CreateContactEmailAddressParams, ContactEmailAddress>(
|
|
348
|
+
{ path: `/tables/contact_email_addresses`, mpOptions, params }
|
|
349
|
+
);
|
|
350
|
+
},
|
|
328
351
|
async updateContacts(params, mpOptions) {
|
|
329
352
|
return update<Partial<Contact>, Contact>(
|
|
330
353
|
{ path: `/tables/contacts`, mpOptions, params }
|
|
@@ -350,7 +373,9 @@ export {
|
|
|
350
373
|
Address,
|
|
351
374
|
FormResponse,
|
|
352
375
|
FormResponseAnswer,
|
|
376
|
+
ContactEmailAddress,
|
|
353
377
|
ErrorDetails,
|
|
378
|
+
DateTimeIsoString,
|
|
354
379
|
convertToCamelCase,
|
|
355
380
|
convertToSnakeCase,
|
|
356
381
|
stringifyURLParams,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DateTimeIsoString } from "../api";
|
|
2
|
+
|
|
3
|
+
export interface ContactEmailAddressRecord {
|
|
4
|
+
Email_Address_ID: number;
|
|
5
|
+
Email_Address: string;
|
|
6
|
+
Contact_ID: number;
|
|
7
|
+
Email_Type_ID?: number;
|
|
8
|
+
End_Date?: DateTimeIsoString | null;
|
|
9
|
+
Notes?: string | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ContactEmailAddress {
|
|
13
|
+
emailAddressID: number;
|
|
14
|
+
emailAddress: string;
|
|
15
|
+
contactID: number;
|
|
16
|
+
emailTypeID?: number;
|
|
17
|
+
endDate?: DateTimeIsoString | null;
|
|
18
|
+
notes?: string | null;
|
|
19
|
+
}
|
package/src/utils/strings.ts
CHANGED
|
@@ -43,7 +43,8 @@ export function escapeSql(str: string) {
|
|
|
43
43
|
|
|
44
44
|
export function toCamelCase(str: string, { capitalIds = false }: { capitalIds?: boolean; }= {}) {
|
|
45
45
|
str = str.replace('-', '')
|
|
46
|
-
str = str.
|
|
46
|
+
str = str.toLowerCase();
|
|
47
|
+
// str = str.replace(/^_?[A-Z]{1,3}/, match => match.toLowerCase()); // Don't convert if start with ID, HS, SMS, etc
|
|
47
48
|
str = str.replace(/(?<=^_|^__)[^\W_]/g, match => match.at(-1)?.toLowerCase() || ''); // keep underscore if first char
|
|
48
49
|
str = str.replace(/(?<!^_|^)_[^\W_]/g, match => match.charAt(1).toUpperCase()); // remove underscore if not first char
|
|
49
50
|
return capitalIds ? str.replace(/id$/i, 'ID') : str;
|