@openfn/language-http 5.1.1 → 6.1.0

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.
@@ -1,3 +1,15 @@
1
+ /**
2
+ * Options provided to the HTTP request
3
+ * @typedef {Object} RequestOptions
4
+ * @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).
5
+ * @property {object} errors - Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors for this code.
6
+ * @property {object} form - Pass a JSON object to be serialised into a multipart HTML form (as FormData) in the body.
7
+ * @property {object} query - An object of query parameters to be encoded into the URL.
8
+ * @property {object} headers - An object of headers to append to the request.
9
+ * @property {string} parseAs - Parse the response body as json, text or stream. By default will use the response headers.
10
+ * @property {number} timeout - Request timeout in ms. Default: 300 seconds.
11
+ * @property {object} tls - TLS/SSL authentication options. See https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions
12
+ */
1
13
  /**
2
14
  * Execute a sequence of operations.
3
15
  * Wraps `language-common/execute`, and prepends initial state for http.
@@ -11,22 +23,41 @@
11
23
  * @returns {Operation}
12
24
  */
13
25
  export function execute(...operations: Operations): Operation;
26
+ /**
27
+ * Make a HTTP request
28
+ * @public
29
+ * @example
30
+ * request(
31
+ * 'GET',
32
+ * '/myEndpoint',
33
+ * {
34
+ * query: {foo: 'bar', a: 1},
35
+ * headers: {'content-type': 'application/json'},
36
+ * }
37
+ * )
38
+ * @function
39
+ * @param {string} method - The HTTP method to use
40
+ * @param {string} path - Path to resource
41
+ * @param {RequestOptions} params - Query, Headers and Authentication parameters
42
+ * @param {function} callback - (Optional) Callback function
43
+ * @returns {Operation}
44
+ */
45
+ export function request(method: string, path: string, params: RequestOptions, callback: Function): Operation;
14
46
  /**
15
47
  * Make a GET request
16
48
  * @public
17
49
  * @example
18
- * get('/myEndpoint', {
19
- * query: {foo: 'bar', a: 1},
20
- * headers: {'content-type': 'application/json'},
21
- * authentication: {username: 'user', password: 'pass'}
22
- * })
50
+ * get('/myEndpoint', {
51
+ * query: {foo: 'bar', a: 1},
52
+ * headers: {'content-type': 'application/json'},
53
+ * })
23
54
  * @function
24
55
  * @param {string} path - Path to resource
25
- * @param {object} params - Query, Headers and Authentication parameters
56
+ * @param {RequestOptions} params - Query, Headers and Authentication parameters
26
57
  * @param {function} callback - (Optional) Callback function
27
58
  * @returns {Operation}
28
59
  */
29
- export function get(path: string, params: object, callback: Function): Operation;
60
+ export function get(path: string, params: RequestOptions, callback: Function): Operation;
30
61
  /**
31
62
  * Make a POST request
32
63
  * @public
@@ -34,15 +65,14 @@ export function get(path: string, params: object, callback: Function): Operation
34
65
  * post('/myEndpoint', {
35
66
  * body: {'foo': 'bar'},
36
67
  * headers: {'content-type': 'application/json'},
37
- * authentication: {username: 'user', password: 'pass'}
38
68
  * })
39
69
  * @function
40
70
  * @param {string} path - Path to resource
41
- * @param {object} params - Body, Query, Headers and Authentication parameters
71
+ * @param {RequestOptions} params - Body, Query, Headers and Authentication parameters
42
72
  * @param {function} callback - (Optional) Callback function
43
73
  * @returns {operation}
44
74
  */
45
- export function post(path: string, params: object, callback: Function): operation;
75
+ export function post(path: string, params: RequestOptions, callback: Function): operation;
46
76
  /**
47
77
  * Make a PUT request
48
78
  * @public
@@ -50,15 +80,14 @@ export function post(path: string, params: object, callback: Function): operatio
50
80
  * put('/myEndpoint', {
51
81
  * body: {'foo': 'bar'},
52
82
  * headers: {'content-type': 'application/json'},
53
- * authentication: {username: 'user', password: 'pass'}
54
83
  * })
55
84
  * @function
56
85
  * @param {string} path - Path to resource
57
- * @param {object} params - Body, Query, Headers and Auth parameters
86
+ * @param {RequestOptions} params - Body, Query, Headers and Auth parameters
58
87
  * @param {function} callback - (Optional) Callback function
59
88
  * @returns {Operation}
60
89
  */
61
- export function put(path: string, params: object, callback: Function): Operation;
90
+ export function put(path: string, params: RequestOptions, callback: Function): Operation;
62
91
  /**
63
92
  * Make a PATCH request
64
93
  * @public
@@ -66,15 +95,14 @@ export function put(path: string, params: object, callback: Function): Operation
66
95
  * patch('/myEndpoint', {
67
96
  * body: {'foo': 'bar'},
68
97
  * headers: {'content-type': 'application/json'},
69
- * authentication: {username: 'user', password: 'pass'}
70
98
  * })
71
99
  * @function
72
100
  * @param {string} path - Path to resource
73
- * @param {object} params - Body, Query, Headers and Auth parameters
101
+ * @param {RequestOptions} params - Body, Query, Headers and Auth parameters
74
102
  * @param {function} callback - (Optional) Callback function
75
103
  * @returns {Operation}
76
104
  */
77
- export function patch(path: string, params: object, callback: Function): Operation;
105
+ export function patch(path: string, params: RequestOptions, callback: Function): Operation;
78
106
  /**
79
107
  * Make a DELETE request
80
108
  * @public
@@ -84,32 +112,71 @@ export function patch(path: string, params: object, callback: Function): Operati
84
112
  * })
85
113
  * @function
86
114
  * @param {string} path - Path to resource
87
- * @param {object} params - Body, Query, Headers and Auth parameters
115
+ * @param {RequestOptions} params - Body, Query, Headers and Auth parameters
88
116
  * @param {function} callback - (Optional) Callback function
89
117
  * @returns {Operation}
90
118
  */
91
- export function del(path: string, params: object, callback: Function): Operation;
119
+ export function del(path: string, params: RequestOptions, callback: Function): Operation;
92
120
  /**
93
121
  * Parse XML with the Cheerio parser
94
122
  * @public
95
123
  * @example
96
- * parseXML(body, function($){
97
- * return $("table[class=your_table]").parsetable(true, true, true);
98
- * })
124
+ * parseXML(
125
+ * (state) => state.response,
126
+ * ($) => {
127
+ * return $("table[class=your_table]").parsetable(true, true, true);
128
+ * }
129
+ * );
130
+ * @example <caption>Using parseXML with a callback</caption>
131
+ * parseXML(
132
+ * (state) => state.response,
133
+ * ($) => {
134
+ * return $("table[class=your_table]").parsetable(true, true, true);
135
+ * },
136
+ * (next) => ({ ...next, results: next.data.body })
137
+ * );
99
138
  * @function
100
139
  * @param {String} body - data string to be parsed
101
140
  * @param {function} script - script for extracting data
141
+ * @param {function} callback - (Optional) Callback function
102
142
  * @returns {Operation}
103
143
  */
104
- export function parseXML(body: string, script: Function): Operation;
144
+ export function parseXML(body: string, script: Function, callback: Function): Operation;
105
145
  /**
106
- * Make a request using the 'request' node module. This module is deprecated.
107
- * @example
108
- * request(params);
109
- * @function
110
- * @param {object} params - Query, Headers and Authentication parameters
111
- * @returns {Operation}
146
+ * Options provided to the HTTP request
112
147
  */
113
- export function request(params: object): Operation;
114
- export const axios: import("../../common/node_modules/axios").AxiosStatic;
115
- export { alterState, arrayToString, combine, dataPath, dataValue, dateFns, each, field, fields, fn, http, humanProper, lastReferenceValue, merge, scrubEmojis, sourceValue, splitKeys, toArray, parseCsv } from "@openfn/language-common";
148
+ export type RequestOptions = {
149
+ /**
150
+ * - 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).
151
+ */
152
+ body: object | string;
153
+ /**
154
+ * - Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors for this code.
155
+ */
156
+ errors: object;
157
+ /**
158
+ * - Pass a JSON object to be serialised into a multipart HTML form (as FormData) in the body.
159
+ */
160
+ form: object;
161
+ /**
162
+ * - An object of query parameters to be encoded into the URL.
163
+ */
164
+ query: object;
165
+ /**
166
+ * - An object of headers to append to the request.
167
+ */
168
+ headers: object;
169
+ /**
170
+ * - Parse the response body as json, text or stream. By default will use the response headers.
171
+ */
172
+ parseAs: string;
173
+ /**
174
+ * - Request timeout in ms. Default: 300 seconds.
175
+ */
176
+ timeout: number;
177
+ /**
178
+ * - TLS/SSL authentication options. See https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions
179
+ */
180
+ tls: object;
181
+ };
182
+ export { alterState, arrayToString, combine, dataPath, dataValue, dateFns, each, field, fields, fn, chunk, humanProper, lastReferenceValue, merge, scrubEmojis, sourceValue, splitKeys, toArray, parseCsv } from "@openfn/language-common";
package/types/Utils.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- export function setUrl(configuration: any, path: any): any;
2
- export function setAuth(configuration: any, manualAuth: any): any;
3
- export function assembleError({ response, error, params }: {
4
- response: any;
5
- error: any;
6
- params: any;
7
- }): any;
8
- export function tryJson(data: any): any;
9
- export function mapToAxiosConfig(requestConfig: any): any;
1
+ export function addBasicAuth(configuration: {}, headers: any): void;
2
+ export function request(method: any, path: any, params: any, callback?: (s: any) => any): (state: any) => any;
3
+ export function xmlParser(body: any, script: any, callback?: (s: any) => any): (state: any) => any;