Skip to content

Commit

Permalink
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
Browse files Browse the repository at this point in the history
…o do-not-treat-buffer-as-json
  • Loading branch information
d-goog committed May 20, 2024
2 parents 2089d83 + 98f7009 commit e66c4ba
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,22 @@ export class Gaxios {
delete preparedOpts.data;

const res = (await fetchImpl(config.url, preparedOpts as {})) as Response;
let data = await this.getResponseData(config, res);
const data = await this.getResponseData(config, res);

// `node-fetch`'s data isn't writable. Native `fetch`'s is.
if (Object.getOwnPropertyDescriptor(res, 'data')?.configurable) {
// Keep `Response` as a class
return Object.assign(res, {config, data});
} else {
Object.assign(res, {config});

// best effort for `node-fetch`; `.data` is not writable...
return new Proxy(res, {
get: (target, prop, receiver) => {
if (prop === 'data') return data;

return Reflect.get(target, prop, receiver);
if (!Object.getOwnPropertyDescriptor(res, 'data')?.configurable) {
Object.defineProperties(res, {
data: {
configurable: true,
writable: true,
enumerable: true,
value: data,
},
set(target, prop, newValue, receiver) {
if (prop === 'data') {
data = newValue;
return true;
} else {
return Reflect.set(target, prop, newValue, receiver);
}
},
}) as GaxiosResponse<T>;
});
}

// Keep object as an instance of `Response`
return Object.assign(res, {config, data});
}

/**
Expand Down

0 comments on commit e66c4ba

Please sign in to comment.