@k-int/bruno-shared-scripts 1.2.1 → 2.0.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## [2.0.1](https://gitlab.com/knowledge-integration/bruno/bruno-shared-scripts/compare/v2.0.0...v2.0.1) (2026-01-16)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Improvements to login and folioAxios to allow for connecting to a DIFFERENT folio within the pre request scripts ([5acea6e](https://gitlab.com/knowledge-integration/bruno/bruno-shared-scripts/commit/5acea6ec44aefa3fce84f75454908974fdb23f42))
7
+
8
+ # [2.0.0](https://gitlab.com/knowledge-integration/bruno/bruno-shared-scripts/compare/v1.2.1...v2.0.0) (2026-01-12)
9
+
10
+
11
+ * feat!: Bruno version 3 ([d028018](https://gitlab.com/knowledge-integration/bruno/bruno-shared-scripts/commit/d02801860cb1d230ea17575145c6787ab905c5bd))
12
+
13
+
14
+ ### BREAKING CHANGES
15
+
16
+ * init function is now REQUIRED, setting up the global console, bru and req instances
17
+
1
18
  ## [1.2.1](https://gitlab.com/knowledge-integration/bruno/bruno-shared-scripts/compare/v1.2.0...v1.2.1) (2025-10-30)
2
19
 
3
20
 
package/README.md CHANGED
@@ -0,0 +1,94 @@
1
+ # Bruno shared scripts
2
+
3
+ This is a public NPM module dedicated to any shared behaviour required across varying collections in Bruno. For now it
4
+ focuses mostly on connecting to a
5
+ remote FOLIO instance and managing FOLIO headers, but there is nothing tying this module to only serving those purposes.
6
+
7
+ ## Motivation
8
+
9
+ Analogous to Postman's shared scripts cloud library, Bruno instead allows ANY NPM to be imported (In developer mode, via
10
+ `require`) and used within pre/post
11
+ request scripts. This is significantly more powerful and open, and allows us to publish the module onto the public NPM
12
+ with strict versioning and be more in
13
+ control.
14
+
15
+ ## Setup
16
+
17
+ To use the shared scripts within a collection, simply import and initialise them.
18
+
19
+ ```javascript
20
+ const {
21
+ init,
22
+ } = require('@k-int/bruno-shared-scripts');
23
+
24
+ init({
25
+ bruInstance: bru,
26
+ logger: console,
27
+ requestInstance: req
28
+ })
29
+ ```
30
+
31
+ This init is a new requirement for Bruno V3, as the virtual environment the scripts are running in have now been
32
+ configured in such a way that third party modules do not get access to the globals previously available (bru, req),
33
+ and access to the console in Bruno's developer tools is ALSO siloed.
34
+
35
+ To combat this, `init` at the collection level in the pre-request script will ensure that the module is able to
36
+ access the functionality.
37
+
38
+ ## How it works
39
+ The module is developed as an ES module, but needs to be built for publishing. This happens via the `npm run build`.
40
+ This will overwrite the `es` directory, creating a single `index.js` file that can then be required by Bruno scripts.
41
+
42
+ ## Development
43
+
44
+ Since this is an NPM, and Bruno collections can be built with NPM using a package.json, this _can_ be run in a
45
+ workspace for "easy" development. However developing this module is not completely straightforward, and can take a while
46
+
47
+ ### Working in a workspace
48
+ If working in an NPM workspace, as per [bienenvolk-bruno-workspace](https://gitlab.com/knowledge-integration/bruno/bienenvolk-bruno-workspace),
49
+ then the development process is:
50
+ - Make changes to shared scripts
51
+ - Run `build`
52
+ - Restart Bruno GUI
53
+ - IMPORTANT, Bruno caches the NPMs it's using per session, so any changes will NOT be visible until a restart
54
+ - Run request
55
+ - Go back to step 1
56
+
57
+ ### Working without a workspace
58
+ The development process is largely the same as working in a workspace, with the only difference being that the
59
+ version will need to point at the newest version
60
+
61
+ - Edit Bruno collection package.json to point the bruno-shared-scripts at `file:path/to/file`
62
+ - Run `build` for shared scripts
63
+ - clean `npm install` in the collection (Delete package lock and node_modules to be sure)
64
+ - Restart Bruno GUI
65
+ - IMPORTANT, Bruno caches the NPMs it's using per session, so any changes will NOT be visible until a restart
66
+ - Run request
67
+ - Go back to step 2
68
+
69
+ And finally remembering to revert the version pointer at the end when the fixed shared-scripts have been published.
70
+
71
+ ### Bruno globals
72
+ As of Bruno V3, access to `bru`, `req` and `console` will no longer behave as expected within a shared NPM. The
73
+ solution to this is (for now) a file called `bruno-globals-provider`. This sets up a `setGlobals` method which in
74
+ turn is used by init (I wanted to keep the globals setter separate from the init in case we have side effects down
75
+ the road).
76
+
77
+ Then in each helper method, the requisite getter(s) can be used:
78
+
79
+ ```javascript
80
+ import { getBru, getConsole, getReq } from '../bruno-globals-provider.js';
81
+
82
+ const helloWorld = () => {
83
+ const bru = getBru();
84
+ const logger = getConsole();
85
+ const req = getReq();
86
+
87
+ logger.log("Hello world");
88
+ logger.log("Request: %o", req);
89
+
90
+ logger.log("Access Bruno functionality: %o", bru.getEnv("FAKE_ENVIRONMENT_VARIABLE"));
91
+ }
92
+ ```
93
+
94
+ Should post-request script helpers be needed, this will need to be extended to include `res` as well.
package/es/index.js CHANGED
@@ -19,17 +19,57 @@ var __copyProps = (to, from, except, desc) => {
19
19
  };
20
20
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
21
 
22
+ // src/bruno-globals-provider.js
23
+ var bruInstance, logger, requestInstance, setGlobals, getBru, getConsole, getReq;
24
+ var init_bruno_globals_provider = __esm({
25
+ "src/bruno-globals-provider.js"() {
26
+ bruInstance = null;
27
+ logger = console;
28
+ requestInstance = null;
29
+ setGlobals = ({
30
+ bruInstance: theBruInstance,
31
+ logger: theLogger,
32
+ requestInstance: theRequestInstance
33
+ }) => {
34
+ bruInstance = theBruInstance;
35
+ logger = theLogger;
36
+ requestInstance = theRequestInstance;
37
+ };
38
+ getBru = () => {
39
+ if (!bruInstance) {
40
+ throw new Error("Bruno instance not initialized. Call init({ bruInstance: <bruInstance> }) first.");
41
+ }
42
+ return bruInstance;
43
+ };
44
+ getConsole = () => {
45
+ if (!logger) {
46
+ throw new Error("Bruno logger not initialized. Call init({ logger: console }) first.");
47
+ }
48
+ return logger;
49
+ };
50
+ getReq = () => {
51
+ if (!requestInstance) {
52
+ throw new Error("Bruno logger not initialized. Call init({ requestInstance: <requestInstance> }) first.");
53
+ }
54
+ return requestInstance;
55
+ };
56
+ }
57
+ });
58
+
22
59
  // src/utils/baseUrl.js
23
- var okapiProtocol, okapiUrl, okapiPort, getBaseUrl, setBaseUrl;
60
+ var getBaseUrl, setBaseUrl;
24
61
  var init_baseUrl = __esm({
25
62
  "src/utils/baseUrl.js"() {
26
- okapiProtocol = bru.getEnvVar("okapiProtocol");
27
- okapiUrl = bru.getEnvVar("okapiUrl");
28
- okapiPort = bru.getEnvVar("okapiPort");
63
+ init_bruno_globals_provider();
29
64
  getBaseUrl = () => {
65
+ const bru = getBru();
30
66
  return bru.getEnvVar("baseUrl");
31
67
  };
32
68
  setBaseUrl = () => {
69
+ const bru = getBru();
70
+ const okapiProtocol = bru.getEnvVar("okapiProtocol");
71
+ const okapiUrl = bru.getEnvVar("okapiUrl");
72
+ const okapiPort = bru.getEnvVar("okapiPort");
33
73
  const baseUrl = `${okapiProtocol}://${okapiUrl}${okapiPort ? ":" + okapiPort : ""}`;
34
74
  bru.setEnvVar("baseUrl", baseUrl);
35
75
  };
@@ -40,19 +80,25 @@ var init_baseUrl = __esm({
40
80
  var getTenant, getToken, getIgnoreCreds, getUserName, getPassword, getCreds;
41
81
  var init_auth_utils = __esm({
42
82
  "src/auth/auth-utils.js"() {
83
+ init_bruno_globals_provider();
43
84
  getTenant = () => {
85
+ const bru = getBru();
44
86
  return bru.getEnvVar("x-okapi-tenant-value");
45
87
  };
46
88
  getToken = () => {
89
+ const bru = getBru();
47
90
  return bru.getVar("x-okapi-token");
48
91
  };
49
92
  getIgnoreCreds = () => {
50
- return bru.getEnvVar("ignoreCreds");
93
+ const bru = getBru();
94
+ return bru.getEnvVar("ignoreCredentials");
51
95
  };
52
96
  getUserName = () => {
97
+ const bru = getBru();
53
98
  return bru.getEnvVar("username");
54
99
  };
55
100
  getPassword = () => {
101
+ const bru = getBru();
56
102
  return bru.getEnvVar("password");
57
103
  };
58
104
  getCreds = () => {
@@ -67,12 +113,13 @@ var init_auth_utils = __esm({
67
113
  });
68
114
 
69
115
  // src/auth/login.js
70
- var axios, getLoginWithExpiryUrl, getLoginUrl, doALogin, loginFunc, login, loginWithExpiry;
116
+ import axios from "axios";
117
+ var getLoginWithExpiryUrl, getLoginUrl, doALogin, loginFunc, login, loginWithExpiry;
71
118
  var init_login = __esm({
72
119
  "src/auth/login.js"() {
73
120
  init_utils();
74
121
  init_auth_utils();
75
- axios = require("axios");
122
+ init_bruno_globals_provider();
76
123
  getLoginWithExpiryUrl = () => {
77
124
  const baseUrl = getBaseUrl();
78
125
  return `${baseUrl}/bl-users/login-with-expiry`;
@@ -84,48 +131,62 @@ var init_login = __esm({
84
131
  doALogin = async ({
85
132
  urlOverride = void 0,
86
133
  withExpiry = true,
87
- suppressConsole = true
134
+ suppressConsole = true,
135
+ credsOverride = void 0,
136
+ tenantOverride = void 0
88
137
  } = {}) => {
89
- !suppressConsole && console.log(`doALogin(${urlOverride}, ${withExpiry})`);
90
- const ignoreCreds = getIgnoreCreds();
138
+ const logger2 = getConsole();
139
+ const req = getReq();
140
+ const theTenant = tenantOverride ?? getTenant();
141
+ !suppressConsole && logger2.log(`doALogin(${urlOverride}, ${withExpiry}, ${suppressConsole})`);
142
+ const ignoreCreds = !!credsOverride ? false : getIgnoreCreds();
91
143
  const preExistingHeaders = req.getHeaders();
92
144
  const preExistingTenant = preExistingHeaders[Object.keys(preExistingHeaders).find((key) => key.toLowerCase() === "X-Okapi-Tenant".toLowerCase())];
93
145
  if (!preExistingTenant) {
94
- req.setHeader("x-okapi-tenant", getTenant());
146
+ req.setHeader("x-okapi-tenant", theTenant);
95
147
  }
96
148
  if (!ignoreCreds || ignoreCreds === false) {
97
149
  const url = urlOverride ?? (withExpiry ? getLoginWithExpiryUrl() : getLoginUrl());
98
- const creds = getCreds();
99
- const tenant = getTenant();
100
- const config = getBaseRequestConfig();
101
- !suppressConsole && console.log(`Sending login request to ${url} with creds ${JSON.stringify(creds)} for tenant: ${tenant}`);
150
+ const creds = credsOverride ?? getCreds();
151
+ const config = getBaseRequestConfig({ tenantOverride });
152
+ !suppressConsole && logger2.log(`Sending login request to ${url} with creds ${JSON.stringify(creds)} for tenant: ${theTenant}, and config: ${JSON.stringify(config)}`);
102
153
  return await axios.post(
103
154
  url,
104
155
  creds,
105
156
  config
106
157
  );
158
+ } else {
159
+ !suppressConsole && logger2.log(`Skipping login as ignoreCredentials is set to true`);
107
160
  }
108
161
  };
109
162
  loginFunc = async ({
110
163
  urlOverride = void 0,
111
164
  withExpiry = true,
112
- suppressConsole = true
165
+ suppressConsole = true,
166
+ credsOverride = void 0,
167
+ tenantOverride = void 0
113
168
  } = {}) => {
114
- !suppressConsole && console.log(`loginFunc(${urlOverride}, ${withExpiry})`);
115
- await doALogin({ urlOverride, withExpiry, suppressConsole }).then((loginResp) => {
116
- !suppressConsole && console.log("Setting request headers...");
117
- req.setHeader("Cookie", loginResp.headers["set-cookie"]);
118
- const token = loginResp.headers["x-okapi-token"];
119
- bru.setVar("x-okapi-token-value", token);
120
- if (!withExpiry) {
121
- req.setHeader("x-okapi-token", getToken());
169
+ const logger2 = getConsole();
170
+ const bru = getBru();
171
+ const req = getReq();
172
+ !suppressConsole && logger2.log(`loginFunc(${urlOverride}, ${withExpiry}, ${suppressConsole})`);
173
+ await doALogin({ req, urlOverride, withExpiry, suppressConsole, credsOverride, tenantOverride }).then((loginResp) => {
174
+ const ignoreCreds = !!credsOverride ? false : getIgnoreCreds();
175
+ if (!ignoreCreds || ignoreCreds === false) {
176
+ !suppressConsole && logger2.log("Setting request headers...");
177
+ req.setHeader("Cookie", loginResp.headers["set-cookie"]);
178
+ const token = loginResp.headers["x-okapi-token"];
179
+ bru.setVar("x-okapi-token-value", token);
180
+ if (!withExpiry) {
181
+ req.setHeader("x-okapi-token", getToken());
182
+ }
122
183
  }
123
184
  }).catch((err) => {
124
- console.error("Failed to login to folio: %o", err);
185
+ logger2.error("Failed to login to folio: %o", err);
125
186
  });
126
187
  };
127
- login = (urlOverride) => loginFunc({ urlOverride, withExpiry: false });
128
- loginWithExpiry = (urlOverride) => loginFunc({ urlOverride });
188
+ login = ({ urlOverride, suppressConsole, credsOverride, tenantOverride } = {}) => loginFunc({ urlOverride, withExpiry: false, suppressConsole, credsOverride, tenantOverride });
189
+ loginWithExpiry = ({ urlOverride, suppressConsole, credsOverride, tenantOverride } = {}) => loginFunc({ urlOverride, suppressConsole, credsOverride, tenantOverride });
129
190
  }
130
191
  });
131
192
 
@@ -156,8 +217,8 @@ var getBaseRequestConfig;
156
217
  var init_config = __esm({
157
218
  "src/utils/config.js"() {
158
219
  init_auth();
159
- getBaseRequestConfig = () => {
160
- const tenant = getTenant();
220
+ getBaseRequestConfig = ({ tenantOverride }) => {
221
+ const tenant = tenantOverride ?? getTenant();
161
222
  return {
162
223
  headers: {
163
224
  "Content-type": "application/json",
@@ -183,46 +244,70 @@ var init_utils = __esm({
183
244
  });
184
245
 
185
246
  // src/index.js
186
- var index_exports = {};
187
- __export(index_exports, {
188
- doALogin: () => doALogin,
189
- getBaseRequestConfig: () => getBaseRequestConfig,
190
- getBaseUrl: () => getBaseUrl,
191
- getCreds: () => getCreds,
192
- getFolioAxios: () => getFolioAxios,
193
- getIgnoreCreds: () => getIgnoreCreds,
194
- getLoginUrl: () => getLoginUrl,
195
- getLoginWithExpiryUrl: () => getLoginWithExpiryUrl,
196
- getPassword: () => getPassword,
197
- getTenant: () => getTenant,
198
- getToken: () => getToken,
199
- getUserName: () => getUserName,
200
- login: () => login,
201
- loginWithExpiry: () => loginWithExpiry,
202
- setBaseUrl: () => setBaseUrl
203
- });
204
- module.exports = __toCommonJS(index_exports);
247
+ init_bruno_globals_provider();
205
248
  init_utils();
206
249
  init_auth();
207
250
 
208
251
  // src/folioRequest.js
252
+ init_bruno_globals_provider();
253
+ init_auth();
254
+ import axios2 from "axios";
209
255
  var { doALogin: doALogin2 } = (init_auth(), __toCommonJS(auth_exports));
210
- var axios2 = require("axios");
211
256
  var { getBaseRequestConfig: getBaseRequestConfig2 } = (init_utils(), __toCommonJS(utils_exports));
212
257
  var getFolioAxios = async ({
213
258
  urlOverride = void 0,
214
259
  withExpiry = true,
215
- suppressConsole = true
260
+ suppressConsole = true,
261
+ credsOverride = void 0,
262
+ tenantOverride = void 0
216
263
  } = {}) => {
217
- return doALogin2({ urlOverride, withExpiry, suppressConsole }).then((loginResp) => {
218
- const config = getBaseRequestConfig2();
264
+ const logger2 = getConsole();
265
+ const config = getBaseRequestConfig2({ tenantOverride });
266
+ const ignoreCreds = !!credsOverride ? false : getIgnoreCreds();
267
+ if (ignoreCreds) {
268
+ !suppressConsole && logger2.log("Ignoring credentials so returning a folioAxios without logging in: %o", config);
269
+ return axios2.create(config);
270
+ }
271
+ return doALogin2({ urlOverride, withExpiry, suppressConsole, credsOverride, tenantOverride }).then((loginResp) => {
272
+ !suppressConsole && logger2.log("folio login succeeded with response: %o", loginResp);
219
273
  config.headers.Cookie = loginResp.headers["set-cookie"];
220
274
  if (!withExpiry) {
221
275
  config.headers["x-okapi-token"] = loginResp.headers["x-okapi-token"];
222
276
  }
223
- !suppressConsole && console.log("folioRequest ready with config: %o", config);
277
+ !suppressConsole && logger2.log("folioRequest ready with config: %o", config);
224
278
  return axios2.create(config);
225
279
  }).catch((err) => {
226
- console.error("Failed to login to folio: %o", err);
280
+ logger2.error("Failed to login to folio: %o", err);
281
+ });
282
+ };
283
+
284
+ // src/index.js
285
+ var init = ({
286
+ bruInstance: bruInstance2,
287
+ logger: logger2,
288
+ requestInstance: requestInstance2
289
+ }) => {
290
+ setGlobals({
291
+ bruInstance: bruInstance2,
292
+ logger: logger2,
293
+ requestInstance: requestInstance2
227
294
  });
228
295
  };
296
+ export {
297
+ doALogin,
298
+ getBaseRequestConfig,
299
+ getBaseUrl,
300
+ getCreds,
301
+ getFolioAxios,
302
+ getIgnoreCreds,
303
+ getLoginUrl,
304
+ getLoginWithExpiryUrl,
305
+ getPassword,
306
+ getTenant,
307
+ getToken,
308
+ getUserName,
309
+ init,
310
+ login,
311
+ loginWithExpiry,
312
+ setBaseUrl
313
+ };
package/package.json CHANGED
@@ -1,18 +1,24 @@
1
1
  {
2
2
  "name": "@k-int/bruno-shared-scripts",
3
- "version": "1.2.1",
3
+ "version": "2.0.1",
4
4
  "exports": "./es/index.js",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "test": "echo \"Error: no test specified\" && exit 1",
9
- "build": "esbuild ./src/index.js --bundle --format=cjs --outdir=es --external:axios",
9
+ "build-es": "esbuild ./src/index.js --bundle --format=esm --outdir=es --external:axios --allow-overwrite",
10
+ "build-cjs": "esbuild ./src/index.js --bundle --format=cjs --outdir=cjs --external:axios --out-extension:.js=.cjs --allow-overwrite",
11
+ "__deprecated_build": "npm run build-es && npm run build-cjs",
12
+ "build": "npm run build-es",
10
13
  "semantic-release": "semantic-release"
11
14
  },
12
15
  "devDependencies": {
13
- "esbuild": "^0.25.2",
14
16
  "@semantic-release/changelog": "^6.0.3",
15
17
  "@semantic-release/git": "^10.0.1",
16
- "@semantic-release/gitlab": "^13.2.9"
18
+ "@semantic-release/gitlab": "^13.2.9",
19
+ "esbuild": "^0.25.2"
20
+ },
21
+ "dependencies": {
22
+ "axios": "^1.13.2"
17
23
  }
18
24
  }
@@ -1,20 +1,32 @@
1
+ import { getBru } from '../bruno-globals-provider.js';
2
+
1
3
  const getTenant = () => {
4
+ const bru = getBru();
5
+
2
6
  return bru.getEnvVar("x-okapi-tenant-value")
3
7
  };
4
8
 
5
9
  const getToken = () => {
10
+ const bru = getBru();
11
+
6
12
  return bru.getVar("x-okapi-token")
7
13
  };
8
14
 
9
15
  const getIgnoreCreds = () => {
10
- return bru.getEnvVar("ignoreCreds")
16
+ const bru = getBru();
17
+
18
+ return bru.getEnvVar("ignoreCredentials")
11
19
  }
12
20
 
13
21
  const getUserName = () => {
22
+ const bru = getBru();
23
+
14
24
  return bru.getEnvVar("username")
15
25
  }
16
26
 
17
27
  const getPassword = () => {
28
+ const bru = getBru();
29
+
18
30
  return bru.getEnvVar("password")
19
31
  }
20
32
 
package/src/auth/login.js CHANGED
@@ -1,8 +1,9 @@
1
- //import axios from 'axios';
1
+ import axios from 'axios';
2
2
  import { getBaseRequestConfig, getBaseUrl } from "../utils";
3
3
  import { getCreds, getIgnoreCreds, getTenant, getToken } from './auth-utils';
4
+ import { getBru, getConsole, getReq } from '../bruno-globals-provider.js';
4
5
 
5
- const axios = require('axios'); // This comes from bruno context...
6
+ // const axios = require('axios'); // This no longer comes from bruno context...
6
7
 
7
8
  // Right now login-with-expiry won't work against localhost
8
9
  const getLoginWithExpiryUrl = () => {
@@ -18,11 +19,17 @@ const getLoginUrl = () => {
18
19
  const doALogin = async ({
19
20
  urlOverride = undefined,
20
21
  withExpiry = true,
21
- suppressConsole = true
22
+ suppressConsole = true,
23
+ credsOverride = undefined,
24
+ tenantOverride = undefined
22
25
  } = {}) => {
23
- !suppressConsole && console.log(`doALogin(${urlOverride}, ${withExpiry})`);
26
+ const logger = getConsole();
27
+ const req = getReq();
24
28
 
25
- const ignoreCreds = getIgnoreCreds();
29
+ const theTenant = tenantOverride ?? getTenant();
30
+
31
+ !suppressConsole && logger.log(`doALogin(${urlOverride}, ${withExpiry}, ${suppressConsole})`);
32
+ const ignoreCreds = !!credsOverride ? false : getIgnoreCreds();
26
33
  //console.log("URL OVERRIDE: %o", urlOverride)
27
34
 
28
35
  // Ensure that x-okapi-tenant is set if NOT set by request
@@ -32,52 +39,65 @@ const doALogin = async ({
32
39
  ] // Make sure this is case insensitive
33
40
 
34
41
  if (!preExistingTenant) {
35
- req.setHeader('x-okapi-tenant', getTenant()) // Keep an eye on this in PM we needed some funky stuff for "disabled" headers
42
+ req.setHeader('x-okapi-tenant', theTenant) // Keep an eye on this in PM we needed some funky stuff for "disabled" headers
36
43
  }
37
44
 
38
45
  // Way to ignore creds for local endpoints
39
46
  if (!ignoreCreds || ignoreCreds === false) {
40
47
  const url = urlOverride ?? (withExpiry ? getLoginWithExpiryUrl() : getLoginUrl());
41
- const creds = getCreds();
42
- const tenant = getTenant();
43
- const config = getBaseRequestConfig();
44
- !suppressConsole && console.log(`Sending login request to ${url} with creds ${JSON.stringify(creds)} for tenant: ${tenant}`);
48
+ const creds = credsOverride ?? getCreds();
49
+ const config = getBaseRequestConfig({ tenantOverride });
50
+
51
+ !suppressConsole && logger.log(`Sending login request to ${url} with creds ${JSON.stringify(creds)} for tenant: ${theTenant}, and config: ${JSON.stringify(config)}`);
45
52
 
46
53
  return await axios.post(
47
54
  url,
48
55
  creds,
49
56
  config
50
- )
57
+ );
58
+ } else {
59
+ !suppressConsole && logger.log(`Skipping login as ignoreCredentials is set to true`);
51
60
  }
52
61
  }
53
62
 
54
63
  const loginFunc = async ({
55
64
  urlOverride = undefined,
56
65
  withExpiry = true,
57
- suppressConsole = true
66
+ suppressConsole = true,
67
+ credsOverride = undefined,
68
+ tenantOverride = undefined,
58
69
  } = {}) => {
59
- !suppressConsole && console.log(`loginFunc(${urlOverride}, ${withExpiry})`);
60
- await doALogin({ urlOverride, withExpiry, suppressConsole })
70
+ const logger = getConsole();
71
+ const bru = getBru();
72
+ const req = getReq();
73
+
74
+ !suppressConsole && logger.log(`loginFunc(${urlOverride}, ${withExpiry}, ${suppressConsole})`);
75
+
76
+ await doALogin({ req, urlOverride, withExpiry, suppressConsole, credsOverride, tenantOverride })
61
77
  .then((loginResp) => {
62
- !suppressConsole && console.log("Setting request headers...")
63
- // We can't seem to set the cookie jar programatically, so directly set cookies on request instead
64
- req.setHeader('Cookie', loginResp.headers["set-cookie"])
78
+ const ignoreCreds = !!credsOverride ? false : getIgnoreCreds();
79
+
80
+ if (!ignoreCreds || ignoreCreds === false) {
81
+ !suppressConsole && logger.log("Setting request headers...")
82
+ // We can't seem to set the cookie jar programatically, so directly set cookies on request instead
83
+ req.setHeader('Cookie', loginResp.headers["set-cookie"])
65
84
 
66
- const token = loginResp.headers["x-okapi-token"]
67
- bru.setVar("x-okapi-token-value", token)
85
+ const token = loginResp.headers["x-okapi-token"]
86
+ bru.setVar("x-okapi-token-value", token)
68
87
 
69
- if (!withExpiry) {
70
- // This is only populated on login, not login with expiry
71
- req.setHeader('x-okapi-token', getToken())
88
+ if (!withExpiry) {
89
+ // This is only populated on login, not login with expiry
90
+ req.setHeader('x-okapi-token', getToken())
91
+ }
72
92
  }
73
93
  })
74
94
  .catch(err => {
75
- console.error("Failed to login to folio: %o", err)
95
+ logger.error("Failed to login to folio: %o", err)
76
96
  });
77
97
  }
78
98
 
79
- const login = (urlOverride) => loginFunc({ urlOverride, withExpiry: false });
80
- const loginWithExpiry = (urlOverride) => loginFunc({ urlOverride });
99
+ const login = ({ urlOverride, suppressConsole, credsOverride, tenantOverride } = {}) => loginFunc({ urlOverride, withExpiry: false, suppressConsole, credsOverride, tenantOverride });
100
+ const loginWithExpiry = ({ urlOverride, suppressConsole, credsOverride, tenantOverride } = {}) => loginFunc({ urlOverride, suppressConsole, credsOverride, tenantOverride });
81
101
 
82
102
  export {
83
103
  doALogin,
@@ -0,0 +1,39 @@
1
+ // Previously bruno globals were available to third party modules.
2
+ // This is no longer the case, and instead must be initialised.
3
+ // I don't love this model, but it will allow us to continue on as we have been working.
4
+
5
+ let bruInstance = null;
6
+ let logger = console; // Default to standard node console
7
+
8
+ let requestInstance = null;
9
+
10
+ export const setGlobals = ({
11
+ bruInstance: theBruInstance,
12
+ logger: theLogger,
13
+ requestInstance: theRequestInstance,
14
+ }) => {
15
+ bruInstance = theBruInstance;
16
+ logger = theLogger;
17
+ requestInstance = theRequestInstance;
18
+ };
19
+
20
+ export const getBru = () => {
21
+ if (!bruInstance) {
22
+ throw new Error("Bruno instance not initialized. Call init({ bruInstance: <bruInstance> }) first.");
23
+ }
24
+ return bruInstance;
25
+ };
26
+
27
+ export const getConsole = () => {
28
+ if (!logger) {
29
+ throw new Error("Bruno logger not initialized. Call init({ logger: console }) first.");
30
+ }
31
+ return logger;
32
+ };
33
+
34
+ export const getReq = () => {
35
+ if (!requestInstance) {
36
+ throw new Error("Bruno logger not initialized. Call init({ requestInstance: <requestInstance> }) first.");
37
+ }
38
+ return requestInstance;
39
+ };
@@ -1,26 +1,43 @@
1
+ import axios from 'axios';
2
+ import { getConsole } from './bruno-globals-provider.js';
3
+ import { getIgnoreCreds } from './auth/index.js';
1
4
  const { doALogin } = require('./auth');
2
5
 
3
- const axios = require('axios');
4
6
  const { getBaseRequestConfig } = require('./utils');
5
7
 
6
8
  export const getFolioAxios = async ({
7
9
  urlOverride = undefined,
8
10
  withExpiry = true,
9
- suppressConsole = true
11
+ suppressConsole = true,
12
+ credsOverride = undefined,
13
+ tenantOverride = undefined,
10
14
  } = {}) => {
11
- return doALogin({ urlOverride, withExpiry, suppressConsole }).then(loginResp => {
15
+ const logger = getConsole();
12
16
 
13
- const config = getBaseRequestConfig()
14
- config.headers.Cookie = loginResp.headers["set-cookie"]
17
+ const config = getBaseRequestConfig({ tenantOverride });
18
+ const ignoreCreds = !!credsOverride ? false : getIgnoreCreds();
15
19
 
16
- if (!withExpiry) {
17
- // This is only populated on login, not login with expiry
18
- config.headers['x-okapi-token'] = loginResp.headers["x-okapi-token"]
19
- }
20
+ // Bypass login/token stuff for ignored credentials
21
+ if (ignoreCreds) {
22
+ !suppressConsole && logger.log("Ignoring credentials so returning a folioAxios without logging in: %o", config);
20
23
 
21
- !suppressConsole && console.log("folioRequest ready with config: %o", config);
22
24
  return axios.create(config);
23
- }).catch(err => {
24
- console.error("Failed to login to folio: %o", err)
25
- })
25
+ }
26
+
27
+ return doALogin({ urlOverride, withExpiry, suppressConsole, credsOverride, tenantOverride })
28
+ .then(loginResp => {
29
+ !suppressConsole && logger.log("folio login succeeded with response: %o", loginResp);
30
+
31
+ config.headers.Cookie = loginResp.headers["set-cookie"]
32
+
33
+ if (!withExpiry) {
34
+ // This is only populated on login, not login with expiry
35
+ config.headers['x-okapi-token'] = loginResp.headers["x-okapi-token"]
36
+ }
37
+
38
+ !suppressConsole && logger.log("folioRequest ready with config: %o", config);
39
+ return axios.create(config);
40
+ }).catch(err => {
41
+ logger.error("Failed to login to folio: %o", err)
42
+ })
26
43
  }
package/src/index.js CHANGED
@@ -1,3 +1,18 @@
1
+ import { setGlobals } from './bruno-globals-provider';
2
+
3
+ // Initialising the bruno scope at the top of a request script
4
+ export const init = ({
5
+ bruInstance,
6
+ logger,
7
+ requestInstance,
8
+ }) => {
9
+ setGlobals({
10
+ bruInstance,
11
+ logger,
12
+ requestInstance
13
+ });
14
+ };
15
+
1
16
  export * from './utils';
2
17
  export * from './auth';
3
18
  export * from './folioRequest';
@@ -1,12 +1,18 @@
1
- const okapiProtocol = bru.getEnvVar('okapiProtocol');
2
- const okapiUrl = bru.getEnvVar('okapiUrl');
3
- const okapiPort = bru.getEnvVar('okapiPort');
1
+ import { getBru } from '../bruno-globals-provider.js';
4
2
 
5
3
  const getBaseUrl = () => {
4
+ const bru = getBru();
5
+
6
6
  return bru.getEnvVar("baseUrl");
7
7
  }
8
8
 
9
9
  const setBaseUrl = () => {
10
+ const bru = getBru();
11
+
12
+ const okapiProtocol = bru.getEnvVar('okapiProtocol');
13
+ const okapiUrl = bru.getEnvVar('okapiUrl');
14
+ const okapiPort = bru.getEnvVar('okapiPort');
15
+
10
16
  const baseUrl = `${okapiProtocol}://${okapiUrl}${okapiPort ? ':' + okapiPort : ''}`;
11
17
  //console.log("WHAT IS BASEURL: %o", baseUrl)
12
18
  bru.setEnvVar("baseUrl", baseUrl);
@@ -1,7 +1,7 @@
1
1
  import { getTenant } from '../auth';
2
2
 
3
- export const getBaseRequestConfig = () => {
4
- const tenant = getTenant();
3
+ export const getBaseRequestConfig = ({ tenantOverride }) => {
4
+ const tenant = tenantOverride ?? getTenant();
5
5
  return {
6
6
  headers: {
7
7
  "Content-type": "application/json",