From 6baddd92d878ea6e7c209f7f9dc6b6b5da2d5e05 Mon Sep 17 00:00:00 2001 From: JexSrs Date: Tue, 24 Oct 2023 20:09:35 +0300 Subject: [PATCH] Axios pre request options deep merge --- package.json | 2 +- src/components/Utils.ts | 3 ++- src/components/config.ts | 2 +- src/utils/deepmerge.util.ts | 9 +++++++++ test/config.test.ts | 2 ++ 5 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 src/utils/deepmerge.util.ts diff --git a/package.json b/package.json index 8376ab0..40e56ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@unistudents/saffron", - "version": "6.1.0", + "version": "6.1.1", "description": "A fairly intuitive & powerful framework that enables you to collect & save articles and news from all over the web. ", "license": "MIT", "homepage": "https://github.com/unistudents/saffron#readme", diff --git a/src/components/Utils.ts b/src/components/Utils.ts index 1c63926..47f9a46 100644 --- a/src/components/Utils.ts +++ b/src/components/Utils.ts @@ -8,6 +8,7 @@ import {Job} from "./job"; import {Worker} from "../modules/worker"; import striptags from "striptags"; import type {DynamicSourceFile} from "./DynamicSourceFile"; +import {deepmerge} from "../utils/deepmerge.util"; export class Utils { @@ -296,7 +297,7 @@ export class Utils { axiosConfig = await axiosConfig(this.source); } - options = {...options, ...axiosConfig} as AxiosRequestConfig + options = deepmerge(options, axiosConfig); } options.responseType = 'arraybuffer'; diff --git a/src/components/config.ts b/src/components/config.ts index 0beb5cf..7003ae6 100644 --- a/src/components/config.ts +++ b/src/components/config.ts @@ -265,7 +265,7 @@ export class Config { private mergeObject(src: any, original: object): any { return _.mergeWith({}, original, src, (o, s) => { - if (typeof o == 'object' && !Array.isArray(o) && typeof o != 'function' && typeof s != 'function') + if (typeof o == 'object' && !Array.isArray(o) && typeof s != 'function' && typeof s != 'function') return this.mergeObject(s, o); return s != null ? s : o; }); diff --git a/src/utils/deepmerge.util.ts b/src/utils/deepmerge.util.ts new file mode 100644 index 0000000..eaef454 --- /dev/null +++ b/src/utils/deepmerge.util.ts @@ -0,0 +1,9 @@ +import _ from "lodash"; + +export function deepmerge(a: T, b: T): T { + return _.mergeWith({}, a, b, (o, s) => { + if (typeof o == 'object' && !Array.isArray(o) && typeof o != 'function' && typeof s != 'function') + return deepmerge(s, o); + return s != null ? s : o; + }); +} \ No newline at end of file diff --git a/test/config.test.ts b/test/config.test.ts index 45c1b8c..921b882 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -56,6 +56,7 @@ describe('Configuration', function () { headers: { 'User-Agent': 'saffron-' }, + validateStatus: (_) => true, maxRedirects: 5 }, preprocessor: async (r, s) => r, @@ -98,6 +99,7 @@ describe('Configuration', function () { expect((c.workers?.axios as any)?.timeout).to.equal((ec.workers?.axios as any)?.timeout); expect((c.workers?.axios as any)?.maxRedirects).to.equal((ec.workers?.axios as any)?.maxRedirects); expect((c.workers?.axios as any)?.headers).to.deep.equal((ec.workers?.axios as any)?.headers); + expect((c.workers?.axios as any)?.validateStatus).to.be.a('function'); expect(c.workers?.preprocessor).to.equal(ec.workers?.preprocessor); expect(c.workers?.articles?.amount).to.equal(ec.workers.articles?.amount); expect(c.workers?.articles?.includeContentAttachments).to.equal(ec.workers.articles?.includeContentAttachments);