@openfn/language-http 6.4.4 → 6.4.6

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/dist/index.cjs CHANGED
@@ -100,11 +100,11 @@ var import_language_common = require("@openfn/language-common");
100
100
  var import_util = require("@openfn/language-common/util");
101
101
  var cheerio = __toESM(require("cheerio"), 1);
102
102
  var import_cheerio_tableparser = __toESM(require("cheerio-tableparser"), 1);
103
- function addAuth(configuration = {}, headers) {
103
+ function addAuth(configuration, headers) {
104
104
  if (headers.Authorization) {
105
105
  return;
106
106
  }
107
- const { username, password, access_token } = configuration;
107
+ const { username, password, access_token } = configuration ?? {};
108
108
  if (access_token) {
109
109
  Object.assign(headers, { Authorization: `Bearer ${access_token}` });
110
110
  } else if (username && password) {
@@ -118,6 +118,23 @@ function encodeFormBody(data) {
118
118
  }
119
119
  return form;
120
120
  }
121
+ var assertUrl = (pathOrUrl, baseUrl) => {
122
+ if (!baseUrl && pathOrUrl && !/^https?:\/\//.test(pathOrUrl)) {
123
+ const e = new Error("UNEXPECTED_RELATIVE_URL");
124
+ e.code = "UNEXPECTED_RELATIVE_URL";
125
+ e.description = `You passed a relative URL but didn't set baseUrl`;
126
+ e.url = pathOrUrl;
127
+ e.fix = `Set the baseUrl on state.configuration or use an absolute URL, like https://example.com/api/${pathOrUrl}`;
128
+ throw e;
129
+ }
130
+ if (!baseUrl && !pathOrUrl) {
131
+ const e = new Error("NO_URL");
132
+ e.code = "NO_URL";
133
+ e.description = `No URL provided`;
134
+ e.fix = `Make sure to pass a URL string into the request. You may need to set a baseURL on state.configuration.`;
135
+ throw e;
136
+ }
137
+ };
121
138
  function request(method, path, params, callback = (s) => s) {
122
139
  return (state) => {
123
140
  var _a;
@@ -137,6 +154,7 @@ function request(method, path, params, callback = (s) => s) {
137
154
  body = encodeFormBody(resolvedParams.form);
138
155
  }
139
156
  const baseUrl = (_a = state.configuration) == null ? void 0 : _a.baseUrl;
157
+ assertUrl(resolvedPath, baseUrl);
140
158
  if (baseUrl) {
141
159
  addAuth(state.configuration, headers);
142
160
  }
package/dist/index.js CHANGED
@@ -50,11 +50,11 @@ import {
50
50
  } from "@openfn/language-common/util";
51
51
  import * as cheerio from "cheerio";
52
52
  import cheerioTableparser from "cheerio-tableparser";
53
- function addAuth(configuration = {}, headers) {
53
+ function addAuth(configuration, headers) {
54
54
  if (headers.Authorization) {
55
55
  return;
56
56
  }
57
- const { username, password, access_token } = configuration;
57
+ const { username, password, access_token } = configuration ?? {};
58
58
  if (access_token) {
59
59
  Object.assign(headers, { Authorization: `Bearer ${access_token}` });
60
60
  } else if (username && password) {
@@ -68,6 +68,23 @@ function encodeFormBody(data) {
68
68
  }
69
69
  return form;
70
70
  }
71
+ var assertUrl = (pathOrUrl, baseUrl) => {
72
+ if (!baseUrl && pathOrUrl && !/^https?:\/\//.test(pathOrUrl)) {
73
+ const e = new Error("UNEXPECTED_RELATIVE_URL");
74
+ e.code = "UNEXPECTED_RELATIVE_URL";
75
+ e.description = `You passed a relative URL but didn't set baseUrl`;
76
+ e.url = pathOrUrl;
77
+ e.fix = `Set the baseUrl on state.configuration or use an absolute URL, like https://example.com/api/${pathOrUrl}`;
78
+ throw e;
79
+ }
80
+ if (!baseUrl && !pathOrUrl) {
81
+ const e = new Error("NO_URL");
82
+ e.code = "NO_URL";
83
+ e.description = `No URL provided`;
84
+ e.fix = `Make sure to pass a URL string into the request. You may need to set a baseURL on state.configuration.`;
85
+ throw e;
86
+ }
87
+ };
71
88
  function request(method, path, params, callback = (s) => s) {
72
89
  return (state) => {
73
90
  var _a;
@@ -87,6 +104,7 @@ function request(method, path, params, callback = (s) => s) {
87
104
  body = encodeFormBody(resolvedParams.form);
88
105
  }
89
106
  const baseUrl = (_a = state.configuration) == null ? void 0 : _a.baseUrl;
107
+ assertUrl(resolvedPath, baseUrl);
90
108
  if (baseUrl) {
91
109
  addAuth(state.configuration, headers);
92
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-http",
3
- "version": "6.4.4",
3
+ "version": "6.4.6",
4
4
  "description": "An HTTP request language package for use with Open Function",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -28,14 +28,13 @@
28
28
  "dependencies": {
29
29
  "cheerio": "1.0.0-rc.12",
30
30
  "cheerio-tableparser": "^1.0.1",
31
- "@openfn/language-common": "2.0.2"
31
+ "@openfn/language-common": "2.0.3"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@openfn/simple-ast": "0.4.1",
35
35
  "chai": "^4.3.10",
36
36
  "deep-eql": "4.1.1",
37
37
  "esno": "^0.16.3",
38
- "mocha": "9.2.2",
39
38
  "mocha-suppress-logs": "^0.4.1",
40
39
  "rimraf": "^3.0.2"
41
40
  },
package/types/Utils.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export function addAuth(configuration: {}, headers: any): void;
1
+ export function addAuth(configuration: any, headers: any): void;
2
2
  export function request(method: any, path: any, params: any, callback?: (s: any) => any): (state: any) => any;
3
3
  export function xmlParser(body: any, script: any, callback?: (s: any) => any): (state: any) => any;