mp-js-api 0.0.28 → 0.0.29

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/src/api.ts CHANGED
@@ -3,11 +3,12 @@ import { URLSearchParams } from 'url';
3
3
  import { convertToCamelCase, convertToSnakeCase, escapeApostrophes, stringifyURLParams, toCapitalSnakeCase } from './utils/converters';
4
4
 
5
5
 
6
- export type APIGetOneInstance = <T, R>({ id, path, mpOptions, config }: APIGetParameter & { id: number; }) => Promise<R | undefined | { error: ErrorDetails; }>;
7
- export type APIGetMultipleInstance = <T, R>({ path, mpOptions, config }: APIGetParameter & { mpOptions: MPGetOptions; }) => Promise<R[] | { error: ErrorDetails; }>;
8
- export type APICreateOneInstance = <T, R>({ path, mpOptions, params, config }: APICreateOneParameter<T>) => Promise<R | { error: ErrorDetails; }>;
9
- export type APICreateManyInstance = <T, R>({ path, mpOptions, params, config }: APICreateManyParameter<T>) => Promise<R[] | { error: ErrorDetails; }>;
10
- export type APIUpdateInstance = <T, R>({ path, mpOptions, params, config }: APIUpdateParameter<T>) => Promise<R[] | { error: ErrorDetails; }>;
6
+ export type APIGetOneInstance = <T extends Record<string, any>>({ id, path, mpQuery, config }: APIGetParameter & { id: number; }) => Promise<T | undefined | { error: ErrorDetails; }>;
7
+ export type APIGetMultipleInstance = <T extends Record<string, any>>({ path, mpQuery, config }: APIGetParameter & { mpQuery: MPGetQuery; }) => Promise<T[] | { error: ErrorDetails; }>;
8
+ export type APICreateOneInstance = <T extends Record<string, any>>({ path, mpQuery, data, config }: APICreateOneParameter) => Promise<T | { error: ErrorDetails; }>;
9
+ export type APICreateManyInstance = <T extends Record<string, any>>({ path, mpQuery, data, config }: APICreateManyParameter) => Promise<T[] | { error: ErrorDetails; }>;
10
+ export type APICreateFileInstance = <T extends Record<string, any>>({ path, mpQuery, data, config }: APICreateFileParameter) => Promise<T | { error: ErrorDetails; }>;
11
+ export type APIUpdateInstance = <T extends Record<string, any>>({ path, mpQuery, data, config }: APIUpdateParameter) => Promise<T[] | { error: ErrorDetails; }>;
11
12
 
12
13
 
13
14
  export interface MPApiBase {
@@ -15,7 +16,9 @@ export interface MPApiBase {
15
16
  getMany: APIGetMultipleInstance;
16
17
  createOne: APICreateOneInstance;
17
18
  createMany: APICreateManyInstance;
18
- update: APIUpdateInstance;
19
+ updateMany: APIUpdateInstance;
20
+ createFile: APICreateFileInstance;
21
+ updateFile: APICreateFileInstance;
19
22
  get: AxiosInstance['get'];
20
23
  post: AxiosInstance['post'];
21
24
  put: AxiosInstance['put'];
@@ -77,37 +80,61 @@ export const createApiBase = ({ auth }: { auth: { username: string; password: st
77
80
  baseURL: 'https://mp.revival.com/ministryplatformapi',
78
81
  });
79
82
 
80
- const getOne: APIGetOneInstance = async <T, R>({ id, path, mpOptions, config }: APIGetParameter & { id: number; }) => {
83
+ const get = async <T = any, R = AxiosResponse<T, any>>(
84
+ url: string,
85
+ config?: AxiosRequestConfig
86
+ ) =>
87
+ api.get<T, R>(url, {
88
+ ...config,
89
+ headers: {
90
+ ...config?.headers,
91
+ Authorization: `Bearer ${await getToken()}`,
92
+ },
93
+ });
94
+
95
+ const post = async <T extends Record<string, any>, R = AxiosResponse<T, any>>(
96
+ url: string,
97
+ data?: any,
98
+ config?: AxiosRequestConfig
99
+ ) =>
100
+ api.post<T, R>(url, data, {
101
+ ...config,
102
+ headers: {
103
+ ...config?.headers,
104
+ Authorization: `Bearer ${await getToken()}`,
105
+ },
106
+ });
107
+
108
+ const put = async <T extends Record<string, any>, R = AxiosResponse<T, any>>(
109
+ url: string,
110
+ data?: any,
111
+ config?: AxiosRequestConfig
112
+ ) =>
113
+ api.put<T, R>(url, data, {
114
+ ...config,
115
+ headers: {
116
+ ...config?.headers,
117
+ Authorization: `Bearer ${await getToken()}`,
118
+ },
119
+ });
120
+
121
+ const getOne: APIGetOneInstance = async <T extends Record<string, any>>({ id, path, mpQuery, config }: APIGetParameter & { id: number; }) => {
81
122
  try {
82
- const url = `${path}/${id}` + stringifyURLParams(mpOptions);
83
- const res = await api.get<T>(url, {
84
- ...config,
85
- headers: {
86
- ...config?.headers,
87
- Authorization: `Bearer ${await getToken()}`,
88
- },
89
- });
90
- return res.data[0] ? convertToCamelCase<T, R>(res.data[0]) : undefined;
123
+ const url = `${path}/${id}` + stringifyURLParams(mpQuery);
124
+ const res = await get<T>(url, config);
125
+ return res.data[0] ? convertToCamelCase<T>(res.data[0]) : undefined;
91
126
  }
92
127
  catch (err) {
93
128
  return { error: getError(err) };
94
129
  }
95
130
  };
96
131
 
97
- const getMany: APIGetMultipleInstance = async <T, R>({ path, mpOptions, config }: APIGetParameter): Promise<R[] | { error: ErrorDetails; }> => {
132
+ const getMany: APIGetMultipleInstance = async <T extends Record<string, any>>({ path, mpQuery, config }: APIGetParameter): Promise<T[] | { error: ErrorDetails; }> => {
98
133
  try {
99
- const url = path + '/get'; //+ stringifyURLParams(mpOptions);
100
- const data = mpOptions && escapeApostrophes(convertToSnakeCase<MPGetOptions>(mpOptions));
101
- const res = await api.post<T[]>(url, data, {
102
- ...config,
103
- ...{
104
- headers: {
105
- ...config?.headers,
106
- Authorization: `Bearer ${await getToken()}`,
107
- }
108
- }
109
- });
110
- return res.data.map(record => convertToCamelCase<T, R>(record));
134
+ const url = path + '/get'; //+ stringifyURLParams(mpQuery);
135
+ const data = mpQuery && escapeApostrophes(convertToSnakeCase<MPGetQuery>(mpQuery));
136
+ const res = await post<T[]>(url, data, config);
137
+ return res.data.map(record => convertToCamelCase<T>(record));
111
138
  }
112
139
  catch (err) {
113
140
  return { error: getError(err) };
@@ -115,18 +142,13 @@ export const createApiBase = ({ auth }: { auth: { username: string; password: st
115
142
  };
116
143
 
117
144
 
118
- const createOne: APICreateOneInstance = async <T, R>({ path, mpOptions, params, config }: APICreateOneParameter<T>) => {
119
- const query = stringifyURLParams(mpOptions);
120
- const data = [convertToSnakeCase<T>(params)];
145
+ const createOne: APICreateOneInstance = async <T extends Record<string, any>>({ path, mpQuery, data: payload, config }: APICreateOneParameter) => {
146
+ const query = stringifyURLParams(mpQuery);
147
+ const data = [convertToSnakeCase<Partial<T>>(payload)];
148
+ const url = path + query;
121
149
  try {
122
- const res = await api.post(path + query, data, {
123
- ...config,
124
- headers: {
125
- ...config?.headers,
126
- Authorization: `Bearer ${await getToken()}`,
127
- },
128
- });
129
- return convertToCamelCase<any, R>(res.data[0]);
150
+ const res = await post<T>(url, data, config);
151
+ return convertToCamelCase<T>(res.data[0]);
130
152
  }
131
153
  catch (err) {
132
154
  return { error: getError(err) };
@@ -135,81 +157,60 @@ export const createApiBase = ({ auth }: { auth: { username: string; password: st
135
157
  };
136
158
 
137
159
 
138
- const createMany: APICreateManyInstance = async <T, R>({ path, mpOptions, params, config }: APICreateManyParameter<T>) => {
139
- const query = stringifyURLParams(mpOptions);
140
- const data = params.map(p => convertToSnakeCase<T>(p));
160
+ const createMany: APICreateManyInstance = async <T extends Record<string, any>>({ path, mpQuery, data: payload, config }: APICreateManyParameter) => {
161
+ const query = stringifyURLParams(mpQuery);
162
+ const data = payload.map(p => convertToSnakeCase<Partial<T>>(p));
163
+ const url = path + query;
141
164
  try {
142
- const res = await api.post(path + query, data, {
143
- ...config,
144
- headers: {
145
- ...config?.headers,
146
- Authorization: `Bearer ${await getToken()}`,
147
- },
148
- });
149
- return res.data.map(record => convertToCamelCase<any, R>(record));
165
+ const res = await post(url, data, config);
166
+ return res.data.map(record => convertToCamelCase<T>(record));
150
167
  }
151
168
  catch (err) {
152
169
  return { error: getError(err) };
153
170
  }
154
171
  };
155
172
 
156
-
157
- const update: APIUpdateInstance = async <T, R>({ path, mpOptions, params, config }: APIUpdateParameter<T>) => {
158
- const query = stringifyURLParams(mpOptions);
159
- const data = params.map(r => convertToSnakeCase<T>(r));
173
+ const updateMany: APIUpdateInstance = async <T extends Record<string, any>>({ path, mpQuery, data: payload, config }: APIUpdateParameter) => {
174
+ const query = stringifyURLParams(mpQuery);
175
+ const data = payload.map(r => convertToSnakeCase<Partial<T>>(r));
176
+ const url = path + query;
160
177
  try {
161
- const res = await api.put(path + query, data, {
162
- ...config,
163
- headers: {
164
- ...config?.headers,
165
- Authorization: `Bearer ${await getToken()}`,
166
- },
167
- });
168
- return res.data.map(record => convertToCamelCase<any, R>(record));
178
+ const res = await put(url, data, config);
179
+ return res.data.map(record => convertToCamelCase<T>(record));
169
180
  }
170
181
  catch (err) {
171
182
  return { error: getError(err) };
172
183
  }
173
-
174
184
  };
175
185
 
176
- const get = async <T = any, R = AxiosResponse<T, any>>(
177
- url: string,
178
- config?: AxiosRequestConfig
179
- ) =>
180
- api.get<T, R>(url, {
181
- ...config,
182
- headers: {
183
- ...config?.headers,
184
- Authorization: `Bearer ${await getToken()}`,
185
- },
186
- });
186
+ const createFile: APICreateFileInstance = async <T extends Record<string, any>>({ path, mpQuery, data: payload, config }: APICreateFileParameter) => {
187
+ const query = stringifyURLParams(mpQuery);
188
+ // const data = [convertToSnakeCase<Partial<T>>(payload)];
189
+ const data = payload;
190
+ const url = path + query;
191
+ try {
192
+ const res = await post(url, data, config);
193
+ return convertToCamelCase<T>(res.data[0]);
194
+ }
195
+ catch (err) {
196
+ return { error: getError(err) };
197
+ }
198
+ };
187
199
 
188
- const post = async <T, R = AxiosResponse<T, any>>(
189
- url: string,
190
- data?: any,
191
- config?: AxiosRequestConfig
192
- ) =>
193
- api.post<T, R>(url, data, {
194
- ...config,
195
- headers: {
196
- ...config?.headers,
197
- Authorization: `Bearer ${await getToken()}`,
198
- },
199
- });
200
+ const updateFile: APICreateFileInstance = async <T extends Record<string, any>>({ path, mpQuery, data: payload, config }: APICreateFileParameter) => {
201
+ const query = stringifyURLParams(mpQuery);
202
+ // const data = [convertToSnakeCase<Partial<T>>(payload)];
203
+ const data = payload;
204
+ const url = path + query;
205
+ try {
206
+ const res = await put(url, data, config);
207
+ return convertToCamelCase<T>(res.data[0]);
208
+ }
209
+ catch (err) {
210
+ return { error: getError(err) };
211
+ }
212
+ };
200
213
 
201
- const put = async <T, R = AxiosResponse<T, any>>(
202
- url: string,
203
- data?: any,
204
- config?: AxiosRequestConfig
205
- ) =>
206
- api.put<T, R>(url, data, {
207
- ...config,
208
- headers: {
209
- ...config?.headers,
210
- Authorization: `Bearer ${await getToken()}`,
211
- },
212
- });
213
214
 
214
215
 
215
216
  const getError = function (error: AxiosError): ErrorDetails {
@@ -226,14 +227,16 @@ export const createApiBase = ({ auth }: { auth: { username: string; password: st
226
227
  };
227
228
 
228
229
  return {
229
- createOne,
230
- createMany,
231
- update,
232
230
  get,
233
231
  put,
234
232
  post,
235
233
  getOne,
236
234
  getMany,
235
+ createOne,
236
+ createMany,
237
+ updateMany,
238
+ createFile,
239
+ updateFile,
237
240
  getError
238
241
  };
239
242
  };
@@ -253,7 +256,7 @@ interface AccessToken {
253
256
  expiration: number;
254
257
  }
255
258
 
256
- export type MPGetOptions = {
259
+ export type MPGetQuery = {
257
260
  select?: string;
258
261
  filter?: string;
259
262
  orderBy?: string;
@@ -263,36 +266,52 @@ export type MPGetOptions = {
263
266
  distinct?: boolean;
264
267
  };
265
268
 
266
- export type MPCreateOptions = {
269
+ export type MPCreateQuery = {
267
270
  userId?: number;
268
271
  select?: string;
269
272
  };
270
273
 
271
- export type MPUpdateOptions = MPCreateOptions;
274
+ export type MPCreateFileQuery = {
275
+ description?: string;
276
+ default?: boolean;
277
+ longestDimension?: number;
278
+ userId?: number;
279
+ };
280
+
281
+ export type MPGetFileQuery = {
282
+ thumbnail?: boolean;
283
+ };
284
+
285
+ export type MPUpdateQuery = MPCreateQuery & { allowCreate: boolean; };
272
286
 
273
287
  interface APIGetParameter {
274
288
  path: string;
275
- mpOptions?: MPGetOptions;
289
+ mpQuery?: MPGetQuery;
276
290
  config?: AxiosRequestConfig;
277
291
  }
278
292
 
279
- interface APICreateOneParameter<T> {
293
+ interface APICreateOneParameter {
280
294
  path: string;
281
- params: T,
282
- mpOptions?: MPCreateOptions;
295
+ data: Record<string, any>,
296
+ mpQuery?: MPCreateQuery;
283
297
  config?: AxiosRequestConfig;
284
298
  };
285
- interface APICreateManyParameter<T> {
299
+ interface APICreateManyParameter {
286
300
  path: string;
287
- params: T[],
288
- mpOptions?: MPCreateOptions;
301
+ data: Record<string, any>[],
302
+ mpQuery?: MPCreateQuery;
289
303
  config?: AxiosRequestConfig;
290
304
  };
291
-
292
- interface APIUpdateParameter<T> {
305
+ interface APICreateFileParameter {
306
+ path: string;
307
+ data: Record<string, any>,
308
+ mpQuery?: MPCreateFileQuery;
309
+ config?: AxiosRequestConfig;
310
+ };
311
+ interface APIUpdateParameter {
293
312
  path: string;
294
- params: T[],
295
- mpOptions?: MPCreateOptions;
313
+ data: Record<string, any>[],
314
+ mpQuery?: MPUpdateQuery;
296
315
  config?: AxiosRequestConfig;
297
316
  };
298
317