Skip to content

Commit

Permalink
Axios pre request options deep merge
Browse files Browse the repository at this point in the history
  • Loading branch information
JexSrs committed Oct 24, 2023
1 parent 55df144 commit 6baddd9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/components/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -296,7 +297,7 @@ export class Utils {
axiosConfig = await axiosConfig(this.source);
}

options = {...options, ...axiosConfig} as AxiosRequestConfig
options = deepmerge(options, axiosConfig);
}

options.responseType = 'arraybuffer';
Expand Down
2 changes: 1 addition & 1 deletion src/components/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
9 changes: 9 additions & 0 deletions src/utils/deepmerge.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import _ from "lodash";

export function deepmerge<T>(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;
});
}
2 changes: 2 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('Configuration', function () {
headers: {
'User-Agent': 'saffron-'
},
validateStatus: (_) => true,
maxRedirects: 5
},
preprocessor: async (r, s) => r,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6baddd9

Please sign in to comment.