@vida-global/core 2.1.0 → 2.1.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.
@@ -25,10 +25,8 @@ class MyApiClient extends HttpClient {
25
25
  return this.#dev ? 'https://staging.foo.com' : 'https://foo.com';
26
26
  }
27
27
 
28
- get defaultHeaders() {
29
- const headers = super.defaultHeaders;
30
- headers.Authorization = `Bearer ${this.#token}`;
31
- return headers;
28
+ get bearerToken() {
29
+ return this.#token;
32
30
  }
33
31
 
34
32
  async getTickets({ page, pageSize }) {
@@ -104,4 +102,6 @@ Subclasses customize behavior through getters on the class:
104
102
 
105
103
  - `urlRoot` — base URL prepended to every endpoint.
106
104
  - `defaultHeaders` — merged into every request (per-call `headers` override).
105
+ - `bearerToken` — when it returns a value, a `Authorization: Bearer <token>` header is added to `defaultHeaders`.
106
+ - `basicAuthUsername` / `basicAuthPassword` — when both return values, a `Authorization: Basic <base64>` header is added to `defaultHeaders`. `bearerToken` takes precedence if both are set.
107
107
  - `logger` — scoped logger used for the `API CALL: ...` debug line emitted on each request (defaults to `logger.http`).
@@ -178,10 +178,41 @@ class HttpClient {
178
178
 
179
179
 
180
180
  get defaultHeaders() {
181
- return {
181
+ const headers = {
182
182
  Accept: "application/json",
183
183
  "Content-Type": "application/json",
184
- }
184
+ };
185
+ if (this.authorizationHeader) headers.Authorization = this.authorizationHeader;
186
+ return headers;
187
+ }
188
+
189
+
190
+ get authorizationHeader() {
191
+ if (this.bearerToken) return `Bearer ${this.bearerToken}`;
192
+ if (this.basicAuthCredentials) return `Basic ${this.basicAuthCredentials}`;
193
+ return null;
194
+ }
195
+
196
+
197
+ get bearerToken() {
198
+ return null;
199
+ }
200
+
201
+
202
+ get basicAuthCredentials() {
203
+ if (!this.basicAuthUsername || !this.basicAuthPassword) return null;
204
+ const credentials = `${this.basicAuthUsername}:${this.basicAuthPassword}`;
205
+ return Buffer.from(credentials).toString('base64');
206
+ }
207
+
208
+
209
+ get basicAuthUsername() {
210
+ return null;
211
+ }
212
+
213
+
214
+ get basicAuthPassword() {
215
+ return null;
185
216
  }
186
217
 
187
218
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vida-global/core",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Core libraries for supporting Vida development",
5
5
  "author": "",
6
6
  "license": "ISC",
@@ -26,6 +26,40 @@ describe('HttpClient', () => {
26
26
  });
27
27
 
28
28
 
29
+ describe('HttpClient#authorizationHeader', () => {
30
+ it('omits the Authorization header by default', async () => {
31
+ const client = new HttpClient();
32
+
33
+ await client.post(Helpers.apiUrl);
34
+
35
+ const callHeaders = fetch.mock.calls[0][1].headers;
36
+ expect(callHeaders.Authorization).toBeUndefined();
37
+ });
38
+
39
+ it('sets a bearer Authorization header when bearerToken is overridden', async () => {
40
+ const client = new Helpers.ClientWithBearerToken();
41
+
42
+ await client.post(Helpers.apiUrl);
43
+
44
+ Helpers.expectRequest('POST', Helpers.apiUrl, {
45
+ headers: { Authorization: `Bearer ${Helpers.bearerToken}` },
46
+ body: {},
47
+ });
48
+ });
49
+
50
+ it('sets a basic Authorization header when username and password are overridden', async () => {
51
+ const client = new Helpers.ClientWithBasicAuth();
52
+
53
+ await client.post(Helpers.apiUrl);
54
+
55
+ Helpers.expectRequest('POST', Helpers.apiUrl, {
56
+ headers: { Authorization: `Basic ${Helpers.basicAuthCredentials}` },
57
+ body: {},
58
+ });
59
+ });
60
+ });
61
+
62
+
29
63
  const httpMethods = [
30
64
  ['post', 'POST', { supportsBody: true }],
31
65
  ['put', 'PUT', { supportsBody: true }],
@@ -26,6 +26,33 @@ class ClientWithUrlRoot extends HttpClient {
26
26
  }
27
27
 
28
28
 
29
+ const bearerToken = TestHelpers.Faker.Text.randomString();
30
+
31
+
32
+ class ClientWithBearerToken extends HttpClient {
33
+ get bearerToken() {
34
+ return bearerToken;
35
+ }
36
+ }
37
+
38
+
39
+ const basicAuthUsername = TestHelpers.Faker.Text.randomString();
40
+ const basicAuthPassword = TestHelpers.Faker.Text.randomString();
41
+ const basicAuthCredentials = Buffer.from(`${basicAuthUsername}:${basicAuthPassword}`).toString('base64');
42
+
43
+
44
+ class ClientWithBasicAuth extends HttpClient {
45
+ get basicAuthUsername() {
46
+ return basicAuthUsername;
47
+ }
48
+
49
+
50
+ get basicAuthPassword() {
51
+ return basicAuthPassword;
52
+ }
53
+ }
54
+
55
+
29
56
  /***************************************************************************************************
30
57
  * MOCKS
31
58
  ***************************************************************************************************/
@@ -80,6 +107,10 @@ function buildExpectedRequest({ method, headers, body }) {
80
107
 
81
108
  module.exports = {
82
109
  apiUrl,
110
+ basicAuthCredentials,
111
+ bearerToken,
112
+ ClientWithBasicAuth,
113
+ ClientWithBearerToken,
83
114
  ClientWithUrlRoot,
84
115
  endpoint,
85
116
  expectRequest,