oauth2-cli 2.0.1 → 2.0.2

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
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
+ ## [2.0.2](https://github.com/battis/oauth2-cli/compare/oauth2-cli/2.0.1...oauth2-cli/2.0.2) (2026-03-27)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * catch bad response earlier ([a47d977](https://github.com/battis/oauth2-cli/commit/a47d9774194c5cdda1c03972c7183837c88d20c1))
11
+
5
12
  ## [2.0.1](https://github.com/battis/oauth2-cli/compare/oauth2-cli/2.0.0...oauth2-cli/2.0.1) (2026-03-27)
6
13
 
7
14
 
package/dist/Client.d.ts CHANGED
@@ -124,6 +124,7 @@ export declare class Client<C extends Credentials = Credentials> extends EventEm
124
124
  * server
125
125
  */
126
126
  protected prepareRequest(...args: PreparedRequest): Promise<PreparedRequest>;
127
+ private throwNotOkResponse;
127
128
  /**
128
129
  * Available hook to manipulate the complete response from the server before
129
130
  * processing it
package/dist/Client.js CHANGED
@@ -256,17 +256,31 @@ export class Client extends EventEmitter {
256
256
  async requestRaw(url, method = 'GET', body, headers = {}, dPoPOptions) {
257
257
  url = new URL(url, this.base_url || this.credentials.issuer);
258
258
  url = requestish.URL.from(requestish.URLSearchParams.appendTo(url, requestish.URLSearchParams.merge(this.inject?.search, url.searchParams)));
259
- const request = async () => await OpenIDClient.fetchProtectedResource(...(await this.prepareRequest(await this.getConfiguration(), (await this.getToken()).access_token, url, method, await requestish.Body.from(body), requestish.Headers.merge(this.inject?.headers, headers), dPoPOptions)));
259
+ headers = requestish.Headers.merge(this.inject?.headers, headers);
260
+ body = await requestish.Body.from(body);
261
+ const request = async () => await OpenIDClient.fetchProtectedResource(...(await this.prepareRequest(await this.getConfiguration(), (await this.getToken()).access_token, url, method, body, headers, dPoPOptions)));
260
262
  try {
261
263
  return this.prepareResponse(await request());
262
264
  }
263
- catch (cause) {
264
- if (Error.isError(cause) && 'status' in cause && cause.status === 401) {
265
+ catch (error) {
266
+ if (Error.isError(error) && 'status' in error && error.status === 401) {
265
267
  await this.authorize();
266
268
  return this.prepareResponse(await request());
267
269
  }
268
270
  else {
269
- throw new Error(`${this.name} request failed`, { cause });
271
+ throw new Error(`${this.name} request failed`, {
272
+ cause: {
273
+ request: {
274
+ method,
275
+ url,
276
+ headers: headers
277
+ ? Object.fromEntries(headers.entries())
278
+ : undefined,
279
+ body
280
+ },
281
+ error
282
+ }
283
+ });
270
284
  }
271
285
  }
272
286
  }
@@ -277,11 +291,27 @@ export class Client extends EventEmitter {
277
291
  async prepareRequest(...args) {
278
292
  return args;
279
293
  }
294
+ async throwNotOkResponse(response) {
295
+ throw new Error(`${this.name} response status not ok`, {
296
+ cause: {
297
+ response: {
298
+ ok: response.ok,
299
+ status: response.status,
300
+ statusText: response.statusText,
301
+ headers: Object.fromEntries(response.headers.entries()),
302
+ body: response.body ? `${await text(response.body)}` : undefined
303
+ }
304
+ }
305
+ });
306
+ }
280
307
  /**
281
308
  * Available hook to manipulate the complete response from the server before
282
309
  * processing it
283
310
  */
284
311
  async prepareResponse(response) {
312
+ if (!response.ok) {
313
+ await this.throwNotOkResponse(response);
314
+ }
285
315
  return response;
286
316
  }
287
317
  /**
@@ -310,17 +340,7 @@ export class Client extends EventEmitter {
310
340
  }
311
341
  }
312
342
  else {
313
- throw new Error(`${this.name} response status not ok`, {
314
- cause: {
315
- response: {
316
- ok: response.ok,
317
- status: response.status,
318
- statusText: response.statusText,
319
- headers: Object.fromEntries(response.headers.entries()),
320
- body: response.body ? `${await text(response.body)}` : undefined
321
- }
322
- }
323
- });
343
+ await this.throwNotOkResponse(response);
324
344
  }
325
345
  }
326
346
  /**
@@ -331,23 +351,8 @@ export class Client extends EventEmitter {
331
351
  * `PaginatedCollection<T>`
332
352
  */
333
353
  async request(url, method = 'GET', body, headers = {}, dPoPOptions) {
334
- try {
335
- const response = await this.requestRaw(url, method, body, headers, dPoPOptions);
336
- return await this.processResponse(response);
337
- }
338
- catch (error) {
339
- throw new Error(`Bad response from ${this.name}`, {
340
- cause: {
341
- request: {
342
- method,
343
- url: requestish.URL.toString(url),
344
- headers: Object.fromEntries(requestish.Headers.from(headers).entries()),
345
- body
346
- },
347
- error
348
- }
349
- });
350
- }
354
+ const response = await this.requestRaw(url, method, body, headers, dPoPOptions);
355
+ return await this.processResponse(response);
351
356
  }
352
357
  /**
353
358
  * Request a protected resource using the client's access token.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oauth2-cli",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Acquire API access tokens via OAuth 2.0 / OpenID Connect within CLI tools",
5
5
  "homepage": "https://github.com/battis/oauth2-cli/tree/main/packages/oauth2-cli#readme",
6
6
  "repository": {
@@ -23,8 +23,8 @@
23
23
  "open": "^11.0.0",
24
24
  "openid-client": "^6.8.2",
25
25
  "ora": "^9.3.0",
26
- "gcrtl": "0.1.12",
27
- "requestish": "0.1.12"
26
+ "requestish": "0.1.12",
27
+ "gcrtl": "0.1.12"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@battis/typescript-tricks": "^0.8.1",