-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathAdaptor.js
More file actions
190 lines (171 loc) · 4.88 KB
/
Adaptor.js
File metadata and controls
190 lines (171 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { expandReferences } from '@openfn/language-common/util';
import * as util from './Utils.js';
/**
* State object
* @typedef {Object} HttpState
* @property data - the parsed response body
* @property response - the response from the HTTP server, including headers, statusCode, body, etc
* @property references - an array of all previous data objects used in the Job
**/
/**
* Query option for the `list` helper function
* @typedef {Object} ListQueryOptions
* @property {Number} pageNo - The page number. Please note that Monnify pagination starts at 0 not 1. (Default: 0)
* @property {Number} pageSize - The page size. (Default: 100)
* @property {Date} startDate - The lower date range
* @property {Date} endDate - The higher date range
* @property {Record<string, any>} [otherOptions] - Additional options.
**/
/**
* Options provided to the HTTP request
* @typedef {Object} RequestOptions
* @public
* @property {object|string} body - body data to append to the request. JSON will be converted to a string (but a content-type header will not be attached to the request).
* @property {object} query - An object of query parameters to be encoded into the URL.
*/
/**
* Make a GET request
* @example <caption>Get all transactions</caption>
* get('/api/v1/transactions/search');
* @function
* @public
* @param {string} path - Path to resource
* @param {RequestOptions} options - Optional request options
* @returns {Operation}
* @state {HttpState}
*/
export function get(path, options = {}) {
return async state => {
const [resolvedPath, resolvedoptions] =
expandReferences(state, path, options);
const response = await util.request(
state.configuration,
'GET',
resolvedPath,
resolvedoptions
);
return util.prepareNextState(state, response);
};
}
/**
* Fetch a list of items.
* @example <caption>Get transactions within a date range. (Default range is the current date)</caption>
* list('/api/v2/disbursements/search-transactions', {
* sourceAccountNumber: 4864192954,
* startDate: new Date('2025-11-15'),
* endDate: Date.now()
* });
*
* @example <caption>Get all transactions for a specific page and page number.</caption>
* list('/api/v2/disbursements/search-transactions', {
* sourceAccountNumber: 4864192954,
* pageNo: 0,
* pageSize: 10
* });
* @function
* @public
* @param {string} path - Path to resource
* @param {ListQueryOptions} query - Query options.
* @returns {Operation}
* @state {HttpState}
*/
export function list(path, query = {}) {
return async state => {
const [resolvedPath, resolvedQuery] =
expandReferences(state, path, query);
const response = await util.requestWithPagination(
state.configuration,
'GET',
resolvedPath,
{
query: resolvedQuery
},
);
return util.prepareNextState(state, response);
};
}
/**
* Make a POST request
* @example <caption>Resend all failed notifications over a time period</caption>
* post("/api/v1/transaction-notification/resend-failed-notifications", {
"startDate": "2021-01-16T13:56:39.492",
"endDate": "2021-10-16T13:56:39.492"
});
* @function
* @public
* @param {string} path - Path to resource
* @param {object} body - The Post request body
* @param {RequestOptions} options - Optional request options
* @returns {Operation}
* @state {HttpState}
*/
export function post(path, body, options = {}) {
return async state => {
const [resolvedPath, resolvedBody, resolvedOptions] = expandReferences(state, path, body, options);
const response = await util.request(
state.configuration,
'POST',
resolvedPath,
{
body: resolvedBody,
...resolvedOptions
}
);
return util.prepareNextState(state, response);
}
}
/**
* Make a generic request
* @example <caption>Get disbursements from a wallet</caption>
* request(
* 'GET',
* '/api/v2/disbursements/search-transactions',
* {
* query: {
* sourceAccountNumber: 4864192954,
* pageNo: 0,
* pageSize: 10
* }
* }
* );
* @function
* @public
* @param {string} method - HTTP method to use
* @param {string} path - Path to resource
* @param {RequestOptions} options - Optional request options
* @returns {Operation}
* @state {HttpState}
*/
export function request(method, path, options = {}) {
return async state => {
const [resolvedMethod, resolvedPath, resolvedoptions] =
expandReferences(state, method, path, options);
const response = await util.request(
state.configuration,
resolvedMethod,
resolvedPath,
resolvedoptions,
);
return util.prepareNextState(state, response);
};
}
export {
as,
combine,
cursor,
dataPath,
dataValue,
dateFns,
each,
field,
fields,
fn,
fnIf,
group,
lastReferenceValue,
map,
merge,
scrubEmojis,
sourceValue,
util,
} from '@openfn/language-common';