diff --git a/package.json b/package.json index 4862304..7a66134 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ }, "scripts": { "build": "tsc -p tsconfig.prod.json", - "check": "yarn compile && yarn lint && yarn test", + "check": "yarn compile && yarn lint && yarn test --forbid-only", "compile": "tsc", "lint": "tslint -p tsconfig.json -c tslint.json \"**/*.ts\"", "release": "semantic-release", @@ -46,6 +46,7 @@ "semantic-release": "^19.0.5", "sinon": "^14.0.2", "ts-node": "^10.9.1", + "tslib": "^2.4.1", "tslint": "^6.1.3", "tslint-eslint-rules": "^5.4.0", "typescript": "^4.8.4" diff --git a/src/lib/RxjsAxios.ts b/src/lib/RxjsAxios.ts index bd9d553..1316590 100644 --- a/src/lib/RxjsAxios.ts +++ b/src/lib/RxjsAxios.ts @@ -73,10 +73,11 @@ export class RxjsAxios { public request = AxiosResponse, D = unknown>( config: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.request({ ...config, signal }), + () => this.axios.request({ ...reqConfig, signal }), controller, ); } @@ -85,10 +86,11 @@ export class RxjsAxios { url: string, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.get(url, { ...config, signal }), + () => this.axios.get(url, { ...reqConfig, signal }), controller, ); } @@ -97,10 +99,11 @@ export class RxjsAxios { url: string, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.delete(url, { ...config, signal }), + () => this.axios.delete(url, { ...reqConfig, signal }), controller, ); } @@ -109,10 +112,11 @@ export class RxjsAxios { url: string, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.head(url, { ...config, signal }), + () => this.axios.head(url, { ...reqConfig, signal }), controller, ); } @@ -121,10 +125,11 @@ export class RxjsAxios { url: string, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.options(url, { ...config, signal }), + () => this.axios.options(url, { ...reqConfig, signal }), controller, ); } @@ -134,10 +139,11 @@ export class RxjsAxios { data?: D, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.post(url, data, { ...config, signal }), + () => this.axios.post(url, data, { ...reqConfig, signal }), controller, ); } @@ -147,10 +153,11 @@ export class RxjsAxios { data?: D, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.put(url, data, { ...config, signal }), + () => this.axios.put(url, data, { ...reqConfig, signal }), controller, ); } @@ -160,10 +167,11 @@ export class RxjsAxios { data?: D, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.patch(url, data, { ...config, signal }), + () => this.axios.patch(url, data, { ...reqConfig, signal }), controller, ); } @@ -173,10 +181,11 @@ export class RxjsAxios { data?: D, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.postForm(url, data, { ...config, signal }), + () => this.axios.postForm(url, data, { ...reqConfig, signal }), controller, ); } @@ -186,10 +195,11 @@ export class RxjsAxios { data?: D, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.putForm(url, data, { ...config, signal }), + () => this.axios.putForm(url, data, { ...reqConfig, signal }), controller, ); } @@ -199,14 +209,34 @@ export class RxjsAxios { data?: D, config?: AxiosRequestConfig, ): Observable { + const reqConfig = this.validateConfig(config); const { controller, signal } = this.makeCancellable(); return observify( - () => this.axios.patchForm(url, data, { ...config, signal }), + () => this.axios.patchForm(url, data, { ...reqConfig, signal }), controller, ); } + private validateConfig(config?: AxiosRequestConfig): AxiosRequestConfig | undefined { + if (config !== undefined) { + const insteadMsg = "Instead, unsubscribe from the observable to cancel the request."; + const { cancelToken, signal, ...rest } = config; + + if (cancelToken !== undefined) { + console.warn(`Use of "cancelToken" is deprecated by Axios and has no effect on rxjs-axios. ${insteadMsg}`); + } + + if (signal !== undefined) { + console.warn(`Use of "signal" has no effect on rxjs-axios. ${insteadMsg}`); + } + + return rest; + } + + return config; + } + private makeCancellable(): Abortable { const controller = new AbortController(); const signal = controller.signal; diff --git a/test/lib/RxjsAxios.test.ts b/test/lib/RxjsAxios.test.ts index 617106d..c45e7ff 100644 --- a/test/lib/RxjsAxios.test.ts +++ b/test/lib/RxjsAxios.test.ts @@ -1,5 +1,5 @@ import { expect, TypeFactories } from "@stackbuilders/assertive-ts"; -import otherAxios from "axios"; +import originalAxios from "axios"; import FormData from "form-data"; import { Observable } from "rxjs"; import Sinon from "sinon"; @@ -10,11 +10,11 @@ import { RxjsAxios } from "../../src/lib/RxjsAxios"; describe("[Unit] RxjsAxios.test.ts", () => { describe(".of", () => { it("creates an RxjsAxios instance from another Axios instance", () => { - const rxjsAxios = RxjsAxios.of(otherAxios); + const rxjsAxios = RxjsAxios.of(originalAxios); expect(Object(rxjsAxios)) .asType(TypeFactories.object()) - .toContainEntry(["axios", otherAxios]); + .toContainEntry(["axios", originalAxios]); }); }); @@ -28,7 +28,7 @@ describe("[Unit] RxjsAxios.test.ts", () => { describe(".isAxiosError", () => { it("calls the same Axios method", () => { - const spy = Sinon.spy(otherAxios, "isAxiosError"); + const spy = Sinon.spy(originalAxios, "isAxiosError"); RxjsAxios.isAxiosError("foo"); @@ -38,7 +38,7 @@ describe("[Unit] RxjsAxios.test.ts", () => { describe(".isCancel", () => { it("calls the same Axios method", () => { - const spy = Sinon.spy(otherAxios, "isCancel"); + const spy = Sinon.spy(originalAxios, "isCancel"); RxjsAxios.isCancel("foo"); @@ -48,7 +48,7 @@ describe("[Unit] RxjsAxios.test.ts", () => { describe(".toFormData", () => { it("calls the same Axios method", () => { - const spy = Sinon.spy(otherAxios, "toFormData"); + const spy = Sinon.spy(originalAxios, "toFormData"); RxjsAxios.toFormData({ }); @@ -58,7 +58,7 @@ describe("[Unit] RxjsAxios.test.ts", () => { describe(".formToJSON", () => { it("calls the same Axios method", () => { - const spy = Sinon.spy(otherAxios, "formToJSON"); + const spy = Sinon.spy(originalAxios, "formToJSON"); const formData = new FormData(); RxjsAxios.formToJSON(formData); @@ -77,9 +77,9 @@ describe("[Unit] RxjsAxios.test.ts", () => { describe("#interceptors", () => { it("returns the instance interceptors", () => { - const rxjsAxios = RxjsAxios.of(otherAxios); + const rxjsAxios = RxjsAxios.of(originalAxios); - expect(rxjsAxios.interceptors).toBeSame(otherAxios.interceptors); + expect(rxjsAxios.interceptors).toBeSame(originalAxios.interceptors); }); }); diff --git a/tslint.json b/tslint.json index 620a894..fa5dea3 100644 --- a/tslint.json +++ b/tslint.json @@ -23,7 +23,7 @@ "linebreak-style": [true, "LF"], "max-line-length": [true, 120], "member-access": [true, "check-constructor"], - "no-console": true, + "no-console": [true, "log"], "no-duplicate-imports": true, "no-namespace": false, "no-trailing-whitespace": true, diff --git a/yarn.lock b/yarn.lock index 1159855..d7e5ffd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4742,6 +4742,7 @@ __metadata: semantic-release: ^19.0.5 sinon: ^14.0.2 ts-node: ^10.9.1 + tslib: ^2.4.1 tslint: ^6.1.3 tslint-eslint-rules: ^5.4.0 typescript: ^4.8.4 @@ -5390,7 +5391,7 @@ __metadata: languageName: node linkType: hard -"tslib@npm:^2.1.0": +"tslib@npm:^2.1.0, tslib@npm:^2.4.1": version: 2.4.1 resolution: "tslib@npm:2.4.1" checksum: 19480d6e0313292bd6505d4efe096a6b31c70e21cf08b5febf4da62e95c265c8f571f7b36fcc3d1a17e068032f59c269fab3459d6cd3ed6949eafecf64315fca