Comparing version 6.0.4 to 6.1.0
@@ -7,2 +7,4 @@ /// <reference types="node" /> | ||
export declare class GaxiosError<T = any> extends Error { | ||
config: GaxiosOptions; | ||
response?: GaxiosResponse<T> | undefined; | ||
error?: Error | NodeJS.ErrnoException | undefined; | ||
@@ -17,4 +19,2 @@ /** | ||
code?: string; | ||
response?: GaxiosResponse<T>; | ||
config: GaxiosOptions; | ||
/** | ||
@@ -27,4 +27,4 @@ * An HTTP Status code. | ||
*/ | ||
status: number; | ||
constructor(message: string, options: GaxiosOptions, response: GaxiosResponse<T>, error?: Error | NodeJS.ErrnoException | undefined); | ||
status?: number; | ||
constructor(message: string, config: GaxiosOptions, response?: GaxiosResponse<T> | undefined, error?: Error | NodeJS.ErrnoException | undefined); | ||
} | ||
@@ -95,4 +95,22 @@ export interface Headers { | ||
key?: string; | ||
/** | ||
* An experimental error redactor. | ||
* | ||
* @experimental | ||
*/ | ||
errorRedactor?: typeof defaultErrorRedactor | false; | ||
} | ||
/** | ||
* A partial object of `GaxiosResponse` with only redactable keys | ||
* | ||
* @experimental | ||
*/ | ||
export type RedactableGaxiosOptions = Pick<GaxiosOptions, 'body' | 'data' | 'headers' | 'url'>; | ||
/** | ||
* A partial object of `GaxiosResponse` with only redactable keys | ||
* | ||
* @experimental | ||
*/ | ||
export type RedactableGaxiosResponse<T = any> = Pick<GaxiosResponse<T>, 'config' | 'data' | 'headers'>; | ||
/** | ||
* Configuration for the Gaxios `request` method. | ||
@@ -166,1 +184,16 @@ */ | ||
} | ||
/** | ||
* An experimental error redactor. | ||
* | ||
* @param config Config to potentially redact properties of | ||
* @param response Config to potentially redact properties of | ||
* | ||
* @experimental | ||
*/ | ||
export declare function defaultErrorRedactor<T = any>(data: { | ||
config?: RedactableGaxiosOptions; | ||
response?: RedactableGaxiosResponse<T>; | ||
}): { | ||
config?: RedactableGaxiosOptions | undefined; | ||
response?: RedactableGaxiosResponse<T> | undefined; | ||
}; |
@@ -15,15 +15,33 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GaxiosError = void 0; | ||
exports.defaultErrorRedactor = exports.GaxiosError = void 0; | ||
const url_1 = require("url"); | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
class GaxiosError extends Error { | ||
constructor(message, options, response, error) { | ||
constructor(message, config, response, error) { | ||
super(message); | ||
this.config = config; | ||
this.response = response; | ||
this.error = error; | ||
this.response = response; | ||
this.config = options; | ||
this.response.data = translateData(options.responseType, response.data); | ||
if (this.response) { | ||
this.response.data = translateData(config.responseType, response === null || response === void 0 ? void 0 : response.data); | ||
this.status = this.response.status; | ||
} | ||
if (error && 'code' in error && error.code) { | ||
this.code = error.code; | ||
} | ||
this.status = response.status; | ||
if (config.errorRedactor) { | ||
const errorRedactor = (config.errorRedactor); | ||
// shallow-copy config for redaction as we do not want | ||
// future requests to have redacted information | ||
this.config = { ...config }; | ||
if (this.response) { | ||
// copy response's config, as it may be recursively redacted | ||
this.response = { ...this.response, config: { ...this.response.config } }; | ||
} | ||
const results = errorRedactor({ config, response }); | ||
this.config = { ...config, ...results.config }; | ||
if (this.response) { | ||
this.response = { ...this.response, ...results.response, config }; | ||
} | ||
} | ||
} | ||
@@ -46,2 +64,68 @@ } | ||
} | ||
/** | ||
* An experimental error redactor. | ||
* | ||
* @param config Config to potentially redact properties of | ||
* @param response Config to potentially redact properties of | ||
* | ||
* @experimental | ||
*/ | ||
function defaultErrorRedactor(data) { | ||
const REDACT = '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.'; | ||
function redactHeaders(headers) { | ||
if (!headers) | ||
return; | ||
for (const key of Object.keys(headers)) { | ||
// any casing of `Authentication` | ||
if (/^authentication$/.test(key)) { | ||
headers[key] = REDACT; | ||
} | ||
} | ||
} | ||
function redactString(obj, key) { | ||
if (typeof obj === 'object' && | ||
obj !== null && | ||
typeof obj[key] === 'string') { | ||
const text = obj[key]; | ||
if (/grant_type=/.test(text) || /assertion=/.test(text)) { | ||
obj[key] = REDACT; | ||
} | ||
} | ||
} | ||
function redactObject(obj) { | ||
if (typeof obj === 'object' && obj !== null) { | ||
if ('grant_type' in obj) { | ||
obj['grant_type'] = REDACT; | ||
} | ||
if ('assertion' in obj) { | ||
obj['assertion'] = REDACT; | ||
} | ||
} | ||
} | ||
if (data.config) { | ||
redactHeaders(data.config.headers); | ||
redactString(data.config, 'data'); | ||
redactObject(data.config.data); | ||
redactString(data.config, 'body'); | ||
redactObject(data.config.body); | ||
try { | ||
const url = new url_1.URL(data.config.url || ''); | ||
if (url.searchParams.has('token')) { | ||
url.searchParams.set('token', REDACT); | ||
} | ||
data.config.url = url.toString(); | ||
} | ||
catch (_a) { | ||
// ignore error | ||
} | ||
} | ||
if (data.response) { | ||
defaultErrorRedactor({ config: data.response.config }); | ||
redactHeaders(data.response.headers); | ||
redactString(data.response, 'data'); | ||
redactObject(data.response.data); | ||
} | ||
return data; | ||
} | ||
exports.defaultErrorRedactor = defaultErrorRedactor; | ||
//# sourceMappingURL=common.js.map |
@@ -134,6 +134,6 @@ "use strict"; | ||
await new Promise(resolve => { | ||
translatedResponse.data.on('data', chunk => { | ||
(translatedResponse === null || translatedResponse === void 0 ? void 0 : translatedResponse.data).on('data', chunk => { | ||
response += chunk; | ||
}); | ||
translatedResponse.data.on('end', resolve); | ||
(translatedResponse === null || translatedResponse === void 0 ? void 0 : translatedResponse.data).on('end', resolve); | ||
}); | ||
@@ -147,4 +147,5 @@ translatedResponse.data = response; | ||
catch (e) { | ||
const err = e; | ||
err.config = opts; | ||
const err = e instanceof common_1.GaxiosError | ||
? e | ||
: new common_1.GaxiosError(e.message, opts, undefined, e); | ||
const { shouldRetry, config } = await (0, retry_1.getRetryConfig)(err); | ||
@@ -290,2 +291,6 @@ if (shouldRetry && config) { | ||
} | ||
if (typeof opts.errorRedactor !== 'function' && | ||
opts.errorRedactor !== false) { | ||
opts.errorRedactor = common_1.defaultErrorRedactor; | ||
} | ||
return opts; | ||
@@ -292,0 +297,0 @@ } |
@@ -86,6 +86,7 @@ "use strict"; | ||
function shouldRetryRequest(err) { | ||
var _a; | ||
const config = getConfig(err); | ||
// node-fetch raises an AbortError if signaled: | ||
// https://github.com/bitinn/node-fetch#request-cancellation-with-abortsignal | ||
if (err.name === 'AbortError') { | ||
if (err.name === 'AbortError' || ((_a = err.error) === null || _a === void 0 ? void 0 : _a.name) === 'AbortError') { | ||
return false; | ||
@@ -92,0 +93,0 @@ } |
# Changelog | ||
## [6.1.0](https://github.com/googleapis/gaxios/compare/v6.0.4...v6.1.0) (2023-08-11) | ||
### Features | ||
* Prevent Auth Logging by Default ([#565](https://github.com/googleapis/gaxios/issues/565)) ([b28e562](https://github.com/googleapis/gaxios/commit/b28e5628f5964e2ecc04cc1df8a54948567a4b48)) | ||
## [6.0.4](https://github.com/googleapis/gaxios/compare/v6.0.3...v6.0.4) (2023-08-03) | ||
@@ -4,0 +11,0 @@ |
{ | ||
"name": "gaxios", | ||
"version": "6.0.4", | ||
"version": "6.1.0", | ||
"description": "A simple common HTTP client specifically for Google APIs and services.", | ||
@@ -5,0 +5,0 @@ "main": "build/src/index.js", |
@@ -10,2 +10,3 @@ # gaxios | ||
## Install | ||
```sh | ||
@@ -20,3 +21,3 @@ $ npm install gaxios | ||
const res = await request({ | ||
url: 'https://www.googleapis.com/discovery/v1/apis/' | ||
url: 'https://www.googleapis.com/discovery/v1/apis/', | ||
}); | ||
@@ -26,4 +27,5 @@ ``` | ||
## Setting Defaults | ||
Gaxios supports setting default properties both on the default instance, and on additional instances. This is often useful when making many requests to the same domain with the same base settings. For example: | ||
Gaxios supports setting default properties both on the default instance, and on additional instances. This is often useful when making many requests to the same domain with the same base settings. For example: | ||
```js | ||
@@ -92,3 +94,3 @@ const gaxios = require('gaxios'); | ||
// The timeout for the HTTP request. Defaults to 0. | ||
// The timeout for the HTTP request in milliseconds. Defaults to 0. | ||
timeout: 1000, | ||
@@ -159,2 +161,11 @@ | ||
signal?: AbortSignal | ||
/** | ||
* An experimental, customizable error redactor. | ||
* | ||
* Set `false` to disable. | ||
* | ||
* @experimental | ||
*/ | ||
errorRedactor?: typeof defaultErrorRedactor | false; | ||
} | ||
@@ -164,2 +175,3 @@ ``` | ||
## License | ||
[Apache-2.0](https://github.com/googleapis/gaxios/blob/master/LICENSE) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86797
923
172