From 98f7009ad1c48dc6a3b4dcd0696c42cce5c53bd0 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Mon, 20 May 2024 15:27:50 -0700 Subject: [PATCH] refactor: streamline `.data` for `Response` --- src/gaxios.ts | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/gaxios.ts b/src/gaxios.ts index 9b59e8e5..9afb591b 100644 --- a/src/gaxios.ts +++ b/src/gaxios.ts @@ -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; + }); } + + // Keep object as an instance of `Response` + return Object.assign(res, {config, data}); } /**