mp-js-api 0.0.1 → 0.0.3
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/.gitattributes +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/utils/strings.d.ts +1 -1
- package/dist/utils/strings.d.ts.map +1 -1
- package/dist/utils/strings.js +4 -3
- package/package.json +4 -4
- package/src/index.ts +497 -496
- package/src/utils/strings.ts +4 -5
- package/test/index.spec.ts +0 -103
package/.gitattributes
ADDED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEhF,OAAO,EAAE,OAAO,EAA6B,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,WAAW,EAAsC,MAAM,uBAAuB,CAAC;AACxF,OAAO,EAAE,gBAAgB,EAAgD,MAAM,6BAA6B,CAAC;AAC7G,OAAO,EAAE,gBAAgB,EAAgD,MAAM,6BAA6B,CAAC;AAC7G,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAaxC,OAAO,EAAE,SAAS,EAAuE,MAAM,qBAAqB,CAAC;AACrH,OAAO,EAAE,OAAO,EAA8D,MAAM,oBAAoB,CAAC;AACzG,OAAO,EAAE,gBAAgB,EAAkG,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEhF,OAAO,EAAE,OAAO,EAA6B,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,WAAW,E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AC"}
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,7 @@ const group_participants_2 = require("./tables/group-participants");
|
|
|
19
19
|
const households_1 = require("./tables/households");
|
|
20
20
|
const addresses_1 = require("./tables/addresses");
|
|
21
21
|
const contact_attributes_1 = require("./tables/contact-attributes");
|
|
22
|
+
const strings_1 = require("./utils/strings");
|
|
22
23
|
const createTokenGetter = (auth) => {
|
|
23
24
|
let token;
|
|
24
25
|
return async () => {
|
|
@@ -46,7 +47,7 @@ const createTokenGetter = (auth) => {
|
|
|
46
47
|
}
|
|
47
48
|
};
|
|
48
49
|
};
|
|
49
|
-
const stringifyMPOptions = (mpOptions = {}) => Object.entries(mpOptions).reduce((acc, [key, value]) => {
|
|
50
|
+
const stringifyMPOptions = (mpOptions = {}) => (0, strings_1.escapeSql)(Object.entries(mpOptions).reduce((acc, [key, value]) => {
|
|
50
51
|
if (!acc) {
|
|
51
52
|
acc += `?$${key}=${value}`;
|
|
52
53
|
}
|
|
@@ -54,7 +55,7 @@ const stringifyMPOptions = (mpOptions = {}) => Object.entries(mpOptions).reduce(
|
|
|
54
55
|
acc += `&$${key}=${value}`;
|
|
55
56
|
}
|
|
56
57
|
return acc;
|
|
57
|
-
}, '')
|
|
58
|
+
}, ''));
|
|
58
59
|
const createMPInstance = ({ auth }) => {
|
|
59
60
|
/**
|
|
60
61
|
* Gets MP oauth token.
|
package/dist/utils/strings.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare function escapeSql(str: string): string;
|
|
2
2
|
//# sourceMappingURL=strings.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../src/utils/strings.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../src/utils/strings.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,UA2BpC"}
|
package/dist/utils/strings.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
exports.escapeSql = escapeSql;
|
|
4
|
+
function escapeSql(str) {
|
|
5
|
+
return str.replace(/%|(?<=\w)'(?=\w)/g, function (char) {
|
|
5
6
|
switch (char) {
|
|
6
7
|
case "\0":
|
|
7
8
|
return "\\0";
|
|
@@ -27,4 +28,4 @@ String.prototype.escapeSql = function () {
|
|
|
27
28
|
return char;
|
|
28
29
|
}
|
|
29
30
|
});
|
|
30
|
-
}
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mp-js-api",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "Node MinistryPlatform API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "mocha --require ts-node/register test/*",
|
|
8
|
-
"
|
|
8
|
+
"compile": "tsc"
|
|
9
9
|
},
|
|
10
10
|
"keywords": [],
|
|
11
11
|
"author": "",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"@types/uuid": "^9.0.7",
|
|
20
20
|
"dotenv": "^16.3.1",
|
|
21
21
|
"mocha": "^10.2.0",
|
|
22
|
-
"ts-node": "^9.
|
|
22
|
+
"ts-node": "^10.9.2",
|
|
23
23
|
"typescript": "^5.3.3",
|
|
24
24
|
"uuid": "^9.0.1"
|
|
25
25
|
}
|
package/src/index.ts
CHANGED
|
@@ -21,551 +21,552 @@ import { mapGroupParticipantRecord } from './tables/group-participants';
|
|
|
21
21
|
import { Household, HouseholdRecord, mapHouseholdRecord, mapHouseholdToHouseholdRecord, } from './tables/households';
|
|
22
22
|
import { Address, AddressRecord, mapAddressRecord, mapAddressToAddressRecord } from './tables/addresses';
|
|
23
23
|
import { ContactAttribute, ContactAttributeRecord, mapContactAttributeRecord, mapContactAttributeToContactAttributeRecord } from './tables/contact-attributes';
|
|
24
|
+
import { escapeSql } from './utils/strings';
|
|
24
25
|
|
|
25
26
|
interface TokenData {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
access_token: string;
|
|
28
|
+
expires_in: number;
|
|
29
|
+
token_type: 'Bearer';
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
interface AccessToken {
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
digest: string;
|
|
34
|
+
expiration: number;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
const createTokenGetter = (auth: { username: string; password: string; }) => {
|
|
37
|
-
|
|
38
|
+
let token: AccessToken | undefined;
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
+
};
|
|
67
68
|
};
|
|
68
69
|
|
|
69
70
|
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
70
71
|
|
|
71
72
|
export type CreateContactParams = WithRequired<
|
|
72
|
-
|
|
73
|
-
|
|
73
|
+
Partial<Contact>,
|
|
74
|
+
'company' | 'displayName'
|
|
74
75
|
>;
|
|
75
76
|
export type CreateHouseholdParams = WithRequired<
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
Partial<Household>,
|
|
78
|
+
'householdName'
|
|
78
79
|
>;
|
|
79
80
|
export type CreateAddressParams = WithRequired<
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
Partial<Address>,
|
|
82
|
+
'addressLine1'
|
|
82
83
|
>;
|
|
83
84
|
export type CreateParticipantParams = WithRequired<
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
Partial<Participant>,
|
|
86
|
+
'contactID' | 'participantTypeID' | 'participantStartDate'
|
|
86
87
|
>;
|
|
87
88
|
export type CreateEventParticipantParams = WithRequired<
|
|
88
|
-
|
|
89
|
-
|
|
89
|
+
Partial<EventParticipant>,
|
|
90
|
+
'eventID' | 'participantID' | 'participationStatusID'
|
|
90
91
|
>;
|
|
91
92
|
export type CreateGroupParticipantParams = WithRequired<
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
Partial<GroupParticipant>,
|
|
94
|
+
'groupID' | 'participantID' | 'groupRoleID' | 'startDate'
|
|
94
95
|
>;
|
|
95
96
|
export type CreateContactAttributeParams = WithRequired<
|
|
96
|
-
|
|
97
|
-
|
|
97
|
+
Partial<ContactAttribute>,
|
|
98
|
+
'attributeID' | 'contactID' | 'startDate'
|
|
98
99
|
>;
|
|
99
100
|
|
|
100
101
|
export type MPGetOptions = {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
select?: string;
|
|
103
|
+
filter?: string;
|
|
104
|
+
orderBy?: string;
|
|
105
|
+
groupBy?: string;
|
|
106
|
+
top?: number;
|
|
107
|
+
skip?: number;
|
|
108
|
+
distinct?: boolean;
|
|
108
109
|
};
|
|
109
110
|
|
|
110
111
|
export type MPCreateOptions = {
|
|
111
|
-
|
|
112
|
+
user?: number;
|
|
112
113
|
};
|
|
113
114
|
|
|
114
115
|
export type MPUpdateOptions = MPCreateOptions;
|
|
115
116
|
export type MPGetInstance = <T = any, R = AxiosResponse<T, any>>(url: string, mpOptions?: MPGetOptions, config?: AxiosRequestConfig) => Promise<R>;
|
|
116
117
|
export type MPInstance = {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
118
|
+
get: MPGetInstance; // - AxiosInstance['get']
|
|
119
|
+
post: AxiosInstance['post'];
|
|
120
|
+
put: AxiosInstance['put'];
|
|
121
|
+
getContact(
|
|
122
|
+
id: number,
|
|
123
|
+
options?: MPGetOptions
|
|
124
|
+
): Promise<Contact | undefined | { error: any; }>;
|
|
125
|
+
getHousehold(
|
|
126
|
+
id: number,
|
|
127
|
+
options?: MPGetOptions
|
|
128
|
+
): Promise<Household | undefined | { error: any; }>;
|
|
129
|
+
getAddress(
|
|
130
|
+
id: number,
|
|
131
|
+
options?: MPGetOptions
|
|
132
|
+
): Promise<Address | undefined | { error: any; }>;
|
|
133
|
+
getParticipant(
|
|
134
|
+
id: number,
|
|
135
|
+
options?: MPGetOptions
|
|
136
|
+
): Promise<Participant | undefined | { error: any; }>;
|
|
137
|
+
getEvent(
|
|
138
|
+
id: number,
|
|
139
|
+
options?: MPGetOptions
|
|
140
|
+
): Promise<Event | undefined | { error: any; }>;
|
|
141
|
+
getGroup(
|
|
142
|
+
id: number,
|
|
143
|
+
options?: MPGetOptions
|
|
144
|
+
): Promise<Group | undefined | { error: any; }>;
|
|
145
|
+
getEventParticipant(
|
|
146
|
+
id: number,
|
|
147
|
+
options?: MPGetOptions
|
|
148
|
+
): Promise<EventParticipant | undefined | { error: any; }>;
|
|
149
|
+
getGroupParticipant(
|
|
150
|
+
id: number,
|
|
151
|
+
options?: MPGetOptions
|
|
152
|
+
): Promise<GroupParticipant | undefined | { error: any; }>;
|
|
152
153
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
154
|
+
getContacts(
|
|
155
|
+
options?: MPGetOptions
|
|
156
|
+
): Promise<Contact[] | { error: any; }>;
|
|
157
|
+
getHouseholds(
|
|
158
|
+
options?: MPGetOptions
|
|
159
|
+
): Promise<Household[] | { error: any; }>;
|
|
160
|
+
getAddresses(
|
|
161
|
+
options?: MPGetOptions
|
|
162
|
+
): Promise<Address[] | { error: any; }>;
|
|
163
|
+
getParticipants(
|
|
164
|
+
options?: MPGetOptions
|
|
165
|
+
): Promise<Participant[] | { error: any; }>;
|
|
166
|
+
getEvents(options?: MPGetOptions): Promise<Event[] | { error: any; }>;
|
|
167
|
+
getGroups(options?: MPGetOptions): Promise<Group[] | { error: any; }>;
|
|
168
|
+
getEventParticipants(
|
|
169
|
+
options?: MPGetOptions
|
|
170
|
+
): Promise<EventParticipant[] | { error: any; }>;
|
|
171
|
+
getGroupParticipants(
|
|
172
|
+
options?: MPGetOptions
|
|
173
|
+
): Promise<GroupParticipant[] | { error: any; }>;
|
|
173
174
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
175
|
+
createContact(
|
|
176
|
+
params: CreateContactParams,
|
|
177
|
+
options?: MPCreateOptions
|
|
178
|
+
): Promise<Contact | { error: any; }>;
|
|
179
|
+
createHousehold(
|
|
180
|
+
params: CreateHouseholdParams,
|
|
181
|
+
options?: MPCreateOptions
|
|
182
|
+
): Promise<Household | { error: any; }>;
|
|
183
|
+
createAddress(
|
|
184
|
+
params: CreateAddressParams,
|
|
185
|
+
options?: MPCreateOptions
|
|
186
|
+
): Promise<Address | { error: any; }>;
|
|
187
|
+
createParticipant(
|
|
188
|
+
params: CreateParticipantParams,
|
|
189
|
+
options?: MPCreateOptions
|
|
190
|
+
): Promise<Participant | { error: any; }>;
|
|
191
|
+
createEventParticipant(
|
|
192
|
+
params: CreateEventParticipantParams,
|
|
193
|
+
options?: MPCreateOptions
|
|
194
|
+
): Promise<EventParticipant | { error: any; }>;
|
|
195
|
+
createGroupParticipant(
|
|
196
|
+
params: CreateGroupParticipantParams,
|
|
197
|
+
options?: MPCreateOptions
|
|
198
|
+
): Promise<GroupParticipant | { error: any; }>;
|
|
199
|
+
createContactAttribute(
|
|
200
|
+
params: CreateContactAttributeParams,
|
|
201
|
+
options?: MPCreateOptions
|
|
202
|
+
): Promise<ContactAttribute | { error: any; }>;
|
|
202
203
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
204
|
+
updateContacts(
|
|
205
|
+
contacts: WithRequired<Partial<Contact>, 'contactID'>[],
|
|
206
|
+
options?: MPUpdateOptions
|
|
207
|
+
): Promise<Contact[] | { error: any; }>;
|
|
207
208
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
209
|
+
updateEventParticipants(
|
|
210
|
+
participants: WithRequired<Partial<EventParticipant>, 'eventParticipantID'>[],
|
|
211
|
+
options?: MPUpdateOptions
|
|
212
|
+
): Promise<EventParticipant[] | { error: any; }>;
|
|
212
213
|
};
|
|
213
214
|
|
|
214
215
|
const stringifyMPOptions = (mpOptions: MPGetOptions | MPCreateOptions | MPUpdateOptions = {}) =>
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
+
}, ''));
|
|
223
224
|
|
|
224
225
|
export const createMPInstance = ({ auth }: { auth: { username: string; password: string; }; }): MPInstance => {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
+
});
|
|
233
234
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
+
});
|
|
270
271
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
272
|
+
return {
|
|
273
|
+
get,
|
|
274
|
+
post,
|
|
275
|
+
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
|
+
}
|
|
285
|
+
},
|
|
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
|
+
}
|
|
297
|
+
},
|
|
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
|
+
}
|
|
307
|
+
},
|
|
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
|
+
}
|
|
319
|
+
},
|
|
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
|
+
}
|
|
329
|
+
},
|
|
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
|
+
}
|
|
339
|
+
},
|
|
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
|
+
}
|
|
351
|
+
},
|
|
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
|
+
}
|
|
363
|
+
},
|
|
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
|
+
}
|
|
373
|
+
},
|
|
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
|
+
}
|
|
383
|
+
},
|
|
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
|
+
}
|
|
393
|
+
},
|
|
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
|
+
}
|
|
403
|
+
},
|
|
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
|
+
}
|
|
411
|
+
},
|
|
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
|
+
}
|
|
419
|
+
},
|
|
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
|
+
}
|
|
429
|
+
},
|
|
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
|
+
}
|
|
439
|
+
},
|
|
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
|
+
}
|
|
451
|
+
},
|
|
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
|
+
}
|
|
463
|
+
},
|
|
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
|
+
}
|
|
475
|
+
},
|
|
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
|
+
}
|
|
487
|
+
},
|
|
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
|
+
}
|
|
503
|
+
},
|
|
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
|
+
}
|
|
519
|
+
},
|
|
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
|
+
}
|
|
535
|
+
},
|
|
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
|
+
}
|
|
547
|
+
},
|
|
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
|
+
}
|
|
558
|
+
},
|
|
559
|
+
};
|
|
559
560
|
};
|
|
560
561
|
|
|
561
562
|
export {
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
563
|
+
Contact,
|
|
564
|
+
Participant,
|
|
565
|
+
Event,
|
|
566
|
+
Group,
|
|
567
|
+
EventParticipant,
|
|
568
|
+
GroupParticipant,
|
|
569
|
+
Household,
|
|
570
|
+
Address,
|
|
571
|
+
ContactAttribute
|
|
571
572
|
};
|
package/src/utils/strings.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return this.replace(/%|(?<=\w)'(?=\w)/g, function (char) {
|
|
1
|
+
export function escapeSql(str: string) {
|
|
2
|
+
return str.replace(/%|(?<=\w)'(?=\w)/g, function (char: string) {
|
|
4
3
|
switch (char) {
|
|
5
4
|
case "\0":
|
|
6
5
|
return "\\0";
|
|
@@ -15,9 +14,9 @@ String.prototype.escapeSql = function () {
|
|
|
15
14
|
case "\r":
|
|
16
15
|
return "\\r";
|
|
17
16
|
case "%":
|
|
18
|
-
return "%25"
|
|
17
|
+
return "%25";
|
|
19
18
|
case "'":
|
|
20
|
-
return "''"
|
|
19
|
+
return "''";
|
|
21
20
|
case "\"":
|
|
22
21
|
case "\\":
|
|
23
22
|
return "\\" + char; // prepends a backslash to backslash, percent,
|
package/test/index.spec.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { createMPInstance } from '../src/index';
|
|
2
|
-
import * as dotenv from 'dotenv';
|
|
3
|
-
import * as assert from 'assert';
|
|
4
|
-
import AxiosError from 'axios/lib/core/AxiosError';
|
|
5
|
-
import { v4 } from 'uuid';
|
|
6
|
-
|
|
7
|
-
dotenv.config();
|
|
8
|
-
|
|
9
|
-
const { MP_USERNAME, MP_PASSWORD } = process.env as {
|
|
10
|
-
MP_USERNAME: string;
|
|
11
|
-
MP_PASSWORD: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const mp = createMPInstance({
|
|
15
|
-
auth: { username: MP_USERNAME, password: MP_PASSWORD },
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('MP Instance', function () {
|
|
19
|
-
it('should find many contacts by filter', async function () {
|
|
20
|
-
const contacts = await mp.getContacts({
|
|
21
|
-
filter: 'Last_Name LIKE \'Ferreira\'',
|
|
22
|
-
});
|
|
23
|
-
if ('error' in contacts) {
|
|
24
|
-
const { error } = contacts;
|
|
25
|
-
if (
|
|
26
|
-
typeof error === 'object' &&
|
|
27
|
-
error !== null &&
|
|
28
|
-
'response' in error &&
|
|
29
|
-
typeof error.response === 'object' &&
|
|
30
|
-
error.response !== null &&
|
|
31
|
-
'data' in error.response
|
|
32
|
-
) {
|
|
33
|
-
console.error(error.response.data);
|
|
34
|
-
} else {
|
|
35
|
-
console.error(error);
|
|
36
|
-
}
|
|
37
|
-
assert.fail();
|
|
38
|
-
} else {
|
|
39
|
-
assert(contacts instanceof Array, 'response is an array');
|
|
40
|
-
assert(contacts.length > 0, 'array length is greater than 0');
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
it('should find one contact by id', async function () {
|
|
44
|
-
const contactID = 111129;
|
|
45
|
-
const contact = await mp.findOneContact(contactID);
|
|
46
|
-
if (!contact) {
|
|
47
|
-
assert.fail('no contact found');
|
|
48
|
-
} else if ('error' in contact) {
|
|
49
|
-
const { error } = contact;
|
|
50
|
-
if (error instanceof Error) {
|
|
51
|
-
assert.fail(error);
|
|
52
|
-
} else {
|
|
53
|
-
assert.fail(`Error: ${JSON.stringify(error, null, 2)}`);
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
assert.equal(contact.firstName, 'Daniel', 'contact first name is Daniel');
|
|
57
|
-
assert.equal(contact.middleName, 'Barbosa', 'contact middle name is Barbosa');
|
|
58
|
-
assert.equal(contact.lastName, 'Ferreira', 'contact last name is Ferreira');
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
it('should find many events with options: filter, select, top', async function () {
|
|
62
|
-
const events = await mp.getEvents({
|
|
63
|
-
filter: `Event_Start_Date <= '2022-12-31' AND Event_Start_Date >= '2022-01-01'`,
|
|
64
|
-
select: 'Event_ID,Event_Title,Event_Start_Date',
|
|
65
|
-
top: 10
|
|
66
|
-
});
|
|
67
|
-
if ('error' in events) {
|
|
68
|
-
const { error } = events;
|
|
69
|
-
if (error?.response?.data) {
|
|
70
|
-
assert.fail(`AxiosError: ${JSON.stringify(error.response.data, null, 2)}`);
|
|
71
|
-
} else if (error instanceof Error) {
|
|
72
|
-
assert.fail(error);
|
|
73
|
-
} else {
|
|
74
|
-
assert.fail(JSON.stringify(error, null, 2));
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
assert(events instanceof Array, 'response is an array');
|
|
78
|
-
assert.equal(events.length, 10, 'array length is 10');
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
it('should create one contact', async function () {
|
|
82
|
-
const contact = await mp.createContact({
|
|
83
|
-
firstName: 'John',
|
|
84
|
-
lastName: 'Doe',
|
|
85
|
-
emailAddress: `test${v4().replace(/-/g, '')}@revival.com`,
|
|
86
|
-
dateOfBirth: '1999-01-01',
|
|
87
|
-
company: false,
|
|
88
|
-
displayName: 'Doe, John'
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
if ('error' in contact) {
|
|
92
|
-
const { error } = contact;
|
|
93
|
-
console.log(error.config);
|
|
94
|
-
if (error?.response?.data) {
|
|
95
|
-
assert.fail(`AxiosError: ${JSON.stringify(error.response.data, null, 2)}`);
|
|
96
|
-
} else if (error instanceof Error) {
|
|
97
|
-
assert.fail(error);
|
|
98
|
-
} else {
|
|
99
|
-
assert.fail(JSON.stringify(error, null, 2));
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
});
|