1
1
import {
2
2
execute as commonExecute ,
3
3
composeNextState ,
4
- http , // Important: this is the OLD axios-based http.
5
4
} from '@openfn/language-common' ;
6
5
import { expandReferences } from '@openfn/language-common/util' ;
7
-
8
- const { axios } = http ;
9
- export { axios } ;
6
+ import { fetch } from 'undici' ;
10
7
11
8
/**
12
9
* Execute a sequence of operations.
@@ -48,31 +45,31 @@ export function execute(...operations) {
48
45
* @param {function } callback - (Optional) callback function
49
46
* @returns {Operation }
50
47
*/
51
- export function addContact ( params , callback ) {
52
- return state => {
48
+ export function addContact ( params , callback = s => s ) {
49
+ return async state => {
53
50
const [ resolvedParams ] = expandReferences ( state , params ) ;
54
51
55
52
const { host, apiVersion, token } = state . configuration ;
56
53
57
54
const url = `${ host } /api/${ apiVersion || 'v2' } /contacts.json` ;
58
55
59
- const config = {
60
- url,
61
- data : resolvedParams ,
62
- headers : { Authorization : `Token ${ token } ` } ,
63
- } ;
56
+ const response = await fetch ( url , {
57
+ method : 'POST' ,
58
+ headers : {
59
+ Authorization : `Token ${ token } ` ,
60
+ 'Content-Type' : 'application/json' ,
61
+ } ,
62
+ body : JSON . stringify ( resolvedParams ) ,
63
+ } ) ;
64
64
65
- return http
66
- . post ( config ) ( state )
67
- . then ( response => {
68
- console . log ( 'Contact added with uuid:' , response . data . uuid ) ;
69
- const nextState = {
70
- ...composeNextState ( state , response . data ) ,
71
- response,
72
- } ;
73
- if ( callback ) return callback ( nextState ) ;
74
- return nextState ;
75
- } ) ;
65
+ const result = await response . json ( ) ;
66
+
67
+ console . log ( 'Contact added with uuid:' , result . uuid ) ;
68
+
69
+ return callback ( {
70
+ ...composeNextState ( state , result ) ,
71
+ response,
72
+ } ) ;
76
73
} ;
77
74
}
78
75
@@ -90,59 +87,50 @@ export function addContact(params, callback) {
90
87
* @param {function } callback - (Optional) callback function
91
88
* @returns {Operation }
92
89
*/
93
- export function upsertContact ( params , callback ) {
94
- return state => {
90
+ export function upsertContact ( params , callback = s => s ) {
91
+ return async state => {
95
92
const [ resolvedParams ] = expandReferences ( state , params ) ;
96
93
97
94
const { host, apiVersion, token } = state . configuration ;
98
95
99
96
const url = `${ host } /api/${ apiVersion || 'v2' } /contacts.json` ;
100
97
101
- const config = {
102
- url,
103
- data : resolvedParams ,
104
- headers : { Authorization : `Token ${ token } ` } ,
105
- } ;
106
-
107
- return http
108
- . post ( config ) ( state )
109
- . then ( resp => {
110
- console . log ( 'Contact added with uuid:' , resp . data . uuid ) ;
111
- return resp ;
112
- } )
113
- . catch ( err => {
114
- const { data } = err . response ;
115
- if (
116
- data &&
117
- data . urns &&
118
- Array . isArray ( data . urns ) &&
119
- data . urns . find ( x => x . includes ( 'URN belongs to another' ) )
120
- ) {
121
- const newUrl = `${ url } ?urn=${ config . data . urns [ 0 ] } ` ;
122
- delete config . data [ 'urns' ] ;
123
- return http
124
- . post ( { ...config , url : newUrl } ) ( state )
125
- . then ( resp => {
126
- console . log ( 'Contact updated with uuid:' , resp . data . uuid ) ;
127
- return resp ;
128
- } ) ;
129
- } else {
130
- console . log ( JSON . stringify ( data , null , 2 ) ) ;
98
+ const response = await fetch ( url , {
99
+ method : 'POST' ,
100
+ headers : {
101
+ Authorization : `Token ${ token } ` ,
102
+ 'Content-Type' : 'application/json' ,
103
+ } ,
104
+ body : JSON . stringify ( resolvedParams ) ,
105
+ } ) ;
131
106
132
- delete err . response . request ;
133
- delete err . response . config ;
107
+ let result = await response . json ( ) ;
108
+ if ( result && result . urns && Array . isArray ( result . urns [ '0' ] ) ) {
109
+ const newUrl = `${ url } ?urn=${ resolvedParams . urns [ 0 ] } ` ;
110
+
111
+ delete resolvedParams [ 'urns' ] ;
112
+ result = await fetch ( newUrl , {
113
+ method : 'POST' ,
114
+ headers : {
115
+ 'Content-Type' : 'application/json' ,
116
+ Authorization : `Token ${ token } ` ,
117
+ } ,
118
+ body : JSON . stringify ( resolvedParams ) ,
119
+ } ) ;
120
+ const res = await result . json ( ) ;
134
121
135
- throw err . response ;
136
- }
137
- } )
138
- . then ( response => {
139
- const nextState = {
140
- ...composeNextState ( state , response . data ) ,
141
- response,
142
- } ;
143
- if ( callback ) return callback ( nextState ) ;
144
- return nextState ;
122
+ console . log ( 'Contact updated with uuid:' , res . uuid ) ;
123
+ return callback ( {
124
+ ...composeNextState ( state , res ) ,
125
+ response : { } ,
145
126
} ) ;
127
+ } else {
128
+ console . log ( 'Contact added with uuid:' , result . uuid ) ;
129
+ return callback ( {
130
+ ...composeNextState ( state , result ) ,
131
+ response : { } ,
132
+ } ) ;
133
+ }
146
134
} ;
147
135
}
148
136
@@ -160,38 +148,29 @@ export function upsertContact(params, callback) {
160
148
* @param {function } callback - (Optional) callback function
161
149
* @returns {Operation }
162
150
*/
163
- export function startFlow ( params , callback ) {
164
- return state => {
151
+ export function startFlow ( params , callback = s => s ) {
152
+ return async state => {
165
153
const [ resolvedParams ] = expandReferences ( state , params ) ;
166
154
167
155
const { host, apiVersion, token } = state . configuration ;
168
156
169
157
const url = `${ host } /api/${ apiVersion || 'v2' } /flow_starts.json` ;
170
158
171
- const config = {
172
- url,
173
- data : resolvedParams ,
159
+ const response = await fetch ( url , {
160
+ method : 'POST' ,
174
161
headers : {
175
162
Authorization : `Token ${ token } ` ,
176
163
'Content-Type' : 'application/json' ,
177
164
} ,
178
- } ;
165
+ body : JSON . stringify ( resolvedParams ) ,
166
+ } ) ;
179
167
180
- return http
181
- . post ( config ) ( state )
182
- . catch ( error => {
183
- console . log ( error . response ) ;
184
- throw 'That was an error from RapidPro.' ;
185
- } )
186
- . then ( response => {
187
- console . log ( 'Flow started:' , response . data ) ;
188
- const nextState = {
189
- ...composeNextState ( state , response . data ) ,
190
- response,
191
- } ;
192
- if ( callback ) return callback ( nextState ) ;
193
- return nextState ;
194
- } ) ;
168
+ const result = await response . json ( ) ;
169
+ console . log ( 'Flow started:' , result ) ;
170
+ return callback ( {
171
+ ...composeNextState ( state , result ) ,
172
+ response,
173
+ } ) ;
195
174
} ;
196
175
}
197
176
@@ -209,38 +188,28 @@ export function startFlow(params, callback) {
209
188
* @param {function } callback - (Optional) callback function
210
189
* @returns {Operation }
211
190
*/
212
- export function sendBroadcast ( params , callback ) {
213
- return state => {
191
+ export function sendBroadcast ( params , callback = s => s ) {
192
+ return async state => {
214
193
const [ resolvedParams ] = expandReferences ( state , params ) ;
215
194
216
195
const { host, apiVersion, token } = state . configuration ;
217
196
218
197
const url = `${ host } /api/${ apiVersion || 'v2' } /broadcasts.json` ;
219
198
220
- const config = {
221
- url,
222
- data : resolvedParams ,
199
+ const response = await fetch ( url , {
200
+ method : 'POST' ,
223
201
headers : {
224
202
Authorization : `Token ${ token } ` ,
225
203
'Content-Type' : 'application/json' ,
226
204
} ,
227
- } ;
228
-
229
- return http
230
- . post ( config ) ( state )
231
- . catch ( error => {
232
- console . log ( error . response ) ;
233
- throw 'That was an error from RapidPro.' ;
234
- } )
235
- . then ( response => {
236
- console . log ( 'Broadcast queued:' , response . data ) ;
237
- const nextState = {
238
- ...composeNextState ( state , response . data ) ,
239
- response,
240
- } ;
241
- if ( callback ) return callback ( nextState ) ;
242
- return nextState ;
243
- } ) ;
205
+ body : JSON . stringify ( resolvedParams ) ,
206
+ } ) ;
207
+ const result = await response . json ( ) ;
208
+ console . log ( 'Broadcast queued:' , result ) ;
209
+ return callback ( {
210
+ ...composeNextState ( state , result ) ,
211
+ response,
212
+ } ) ;
244
213
} ;
245
214
}
246
215
@@ -253,7 +222,6 @@ export {
253
222
fields ,
254
223
fn ,
255
224
fnIf ,
256
- http , // Important: this is the OLD axios-based http. Public docs will be incorrect.
257
225
lastReferenceValue ,
258
226
merge ,
259
227
sourceValue ,
0 commit comments