zhaosiyang / axios-observable Goto Github PK
View Code? Open in Web Editor NEWUse axios in a rxjs way. use Observable instead of Promise
License: MIT License
Use axios in a rxjs way. use Observable instead of Promise
License: MIT License
I get the error: "No need to use cancel token, just unsubscribe the subscription would cancel the http request automatically"
I'm just using Axios.get(url, { httpsAgent: new https.Agent({ rejectUnauthorized: false } })
, no cancel tokens.
I got a build error because Axios that this package use is still using Axios 0.19, which has an SSRF vulnerability.
This security hole is fixed in the latest version of Axios: axios/axios#3410
Do you mind to update the Axios version to 0.21.1?
Hi there,
Is it possible to re-export all the typings, such as AxiosResponse, that come from the axios inside axios-observable so we can use it like this:
import Axios, { AxiosResponse} from 'axios-observable';
Axios.get<string>(`some-url`).pipe(map((res: AxiosResponse) => res?.data));
Currently you can do it like the following:
import { AxiosResponse } from 'axios';
import Axios from 'axios-observable';
Axios.get<string>(`some-url`).pipe(map((res: AxiosResponse) => res?.data));
But that requires the extra axios package to be installed like this:
"axios": "^1.2.0",
"axios-observable": "^2.0.0",
Deploying the application will fail due to missing typings if the axios is not installed seperatly.
Anyway, great tool and thanks for all the work!
I'm building a library that uses axios-observable using rollup, and make rxjs an external dependency.
However, the built library contains a line like this:
import require$$0$1, { of, BehaviorSubject, Subject, timer, merge as merge$1, throwError, forkJoin } from 'rxjs';
When my library gets built into an Angular application, the build fails with an error like so:
./build/live-agent-core/live-agent-core.js:3655:15-27 - Error: export 'default' (imported as 'require$$0$1') was not found in 'rxjs' (possible exports: ArgumentOutOfRangeError, AsyncSubject, BehaviorSubject, ConnectableObservable, EMPTY, EmptyError, GroupedObservable, NEVER, Notification, NotificationKind, ObjectUnsubscribedError, Observable, ReplaySubject, Scheduler, Subject, Subscriber, Subscription, TimeoutError, UnsubscriptionError, VirtualAction, VirtualTimeScheduler, animationFrame, animationFrameScheduler, asap, asapScheduler, async, asyncScheduler, bindCallback, bindNodeCallback, combineLatest, concat, config, defer, empty, forkJoin, from, fromEvent, fromEventPattern, generate, identity, iif, interval, isObservable, merge, never, noop, observable, of, onErrorResumeNext, pairs, partition, pipe, queue, queueScheduler, race, range, scheduled, throwError, timer, using, zip)
I believe this is because there is a line:
const axios_1 = require("axios");
in node_modules/axios-observable/dist/create-observable.js
Would it be possible to build axios-observable for two different targets so that the require statement is replaced with an es6 import statement?
Hello!
Could it be possible to have an Axios Observable based on redaxios that has the same API as Axios?
Hello,
This is not actually an issue but a question on how to properly manage multiple requests that should be resolved at once (like with promise.all()).
I've something like this:
import Axios from 'axios-observable';
import { AxiosObservable } from 'axios-observable/dist/axios-observable.interface';
import {forkJoin, from, Observable} from 'rxjs';
import { AxiosResponse } from 'axios';
export interface IRequestOptions {
skipAuthHeader?: boolean;
}
const get = <T>(url: string, opts?: IRequestOptions): AxiosObservable<T> => {
return Axios.get<T>(url, getRequestConfig(opts));
};
const getAll = <T>(urls: string[], opts?: IRequestOptions): Observable<AxiosResponse[]> => {
return forkJoin(
urls.map(url => from(Axios.get<T>(url, getRequestConfig(opts))))
);
}
While this is working fine, I don't like to have different returned types. I actually want to wrap get
and getAll
methods together, although maybe I've not think on this properly.
On the other hand, I'm not sure if with this approach I'm losing the advantages of AxiosObservable like cancel the pending requests when all subscriptions have been cancelled.
So, the questions here is, do you have some advice or workaround on how to manage this situations?
Thanks!
Thank you very much for the excellent library!
Errors occurred while migrating to Axios 1.2.1 with axios-observable 2.0.0
Stackblitz Case (Bug Reports)
https://stackblitz.com/github/dbelob/axios-observable-cancel-token-error
Current behavior
Error when trying to make HTTP request and error message in console.
Expected behavior
Successful completion of the HTTP request, displaying a message in the browser, no error messages in the console:
See example with Axios 0.27.2 https://stackblitz.com/github/dbelob/axios-observable-cancel-token-error/tree/axios-0.27.2
axios-observable version: 2.0.0
Axios version: 1.2.1
Node.js version: 18.12.1
OS version: Windows 8.1, Windows 10
Browser: Firefox 107.0.1, Chrome 108.0.5359.99, Edge 108.0.1462.46
I have issues using axios-observable
with latest Axios installed. To reproduce:
$ mkdir /tmp/deptest
$ cd /tmp/deptest
$ yarn add axios axios-observable rxjs
Which results in:
yarn add v1.22.17
info No lockfile found.
[1/4] ๐ Resolving packages...
[2/4] ๐ Fetching packages...
[3/4] ๐ Linking dependencies...
warning " > [email protected]" has incorrect peer dependency "axios@^0.21.1".
[4/4] ๐จ Building fresh packages...
success Saved lockfile.
success Saved 4 new dependencies.
info Direct dependencies
โโ [email protected]
โโ [email protected]
โโ [email protected]
info All dependencies
โโ [email protected]
โโ [email protected]
โโ [email protected]
โโ [email protected]
โจ Done in 0.80
You can see the warning:
warning " > [email protected]" has incorrect peer dependency "axios@^0.21.1".
Which is odd to me because the version range is setup correctly using ^
. Not quite sure why it is still failing.
Hi,
I am trying to integrate this library with react-query. I know that react-query only works with promises and axios-observable returns an observable. Is there a way for me to get it working?
In the example, isn't the following:
Axios.get('/user?ID=12345'), {
params: {
ID: 12345
}
})
[...]
supposed to be:
Axios.get('/user'), {
params: {
ID: 12345
}
})
[...]
I'm got this error when trying to apply interceptors to a custom Axios instance - Property 'interceptors' is a static member of type 'Axios'.
const instance = Axios.create();
instance.interceptors.request.use(function () {/*...*/});```
I think Cancellation isn't working well. because it's not cancell te request to the server or I'm not right?
axios <= 0.18.0 has a high severity security vulnerability, please update to 0.19.0
axios/axios#2164
Hi, thanks for making this wonderful module.
What I'm currently doing is like below:
jobs$
.pipe(
mergeMap<string, AxiosObservable<string>>(
(url = 'https://www.baidu.com') => {
return Axios.get(url);
},
),
map((result) => result.data),
tap((html) => {
console.log(html);
}),
)
So I need to specify the input and output data type of mergeMap
function in order to get it work. In that case I need to import AxiosObservable
. Is this the best way to use it? If AxiosObservable
is needed, can you please export it in the main project file, instead of 'axios-observable/dist/axios-observable.interface'?
Something like this would be fine:
import { AxiosObservable } from 'axios-observable';
Hi! Seems axios-observable wants a very old version of axios (0.27.0) -- could this be updated?
node: v18.4.0
npm:8.19.2
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: @xxx/xxx:
npm ERR! Found: [email protected]
npm ERR! node_modules/axios
npm ERR! axios@"^1.1.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer axios@"^0.27.0" from [email protected]
npm ERR! node_modules/axios-observable
npm ERR! axios-observable@"*" from the root project
when i am returning type from interceptor it seems axios response type is not changed , can you fix it ?
I am doing a POC install of axios-observable in a Vue based application and I'm getting the following error:
45:13 Property 'observable' does not exist on type 'SymbolConstructor'.
43 | export declare type ObservableLike<T> = InteropObservable<T>;
44 | export declare type InteropObservable<T> = {
45 | [Symbol.observable]: () => Subscribable<T>;
46 | };
47 | /** OBSERVER INTERFACES */
48 | export interface NextObserver<T> {
Version: typescript 3.9.6
After a quick search I can see that this is an error introduced in Typescript that affected a lot of packages.
I would like to know if this is a known error for 'axios-observable' and is there a patch/fix coming?
Thanks
Hy @zhaosiyang
I'm working on implementing a httpClient similar to the one from Angular and found this library.
The httpClient is a service that will be injected in other services and basically acts like a facade for the axios lib.
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { Axios } from 'axios-observable';
// httpClient Class
export class HttpClient {
protected readonly instance: AxiosInstance;
constructor(
@inject('ApiBaseUrl') baseURL: string
) {
this.instance = Axios.create({
baseURL
})
this.get = this.instance.get;
// other methods go here
}
}
I also have some interceptors inside the class.
It works fine with plain axios but as I want to make all the requests observable and not promises I'm using the lib you created.
There are several errors that I'm encountering:
Now, this is simple to fix, just remove the type of the instance
protected readonly instance;
Looking at the compiled code:
Axios.prototype.get = function (url, config) {
return create_observable_1.createObservable(this.axiosInstance.get, url, config);
};
I can see that axiosInstance is not available. I will proceed in investigating more and try to find a fix.
I'm using the following versions:
axios: 0.20.0
axios-observable: 1.1.3
Lambda execution failed with status 200 due to customer function error: Error: Cannot find module 'axios-observable' Require stack: - /var/task/src/handlers/handler.js - /var/runtime/UserFunction.js - /var/runtime/index.js. Lambda request id: cac503cb-a8eb-4031-b472-3df40dc53857
@zhaosiyang Hi is it possible to add LICENSE file inside the repo and release new patch version with the license?
MIT License requires to distribute full license text along the source code so right now this is actually proprietary code and cannot be legally used by anyone.
So if you would be able to add LICENSE.md file with this text https://opensource.org/licenses/MIT it would be great.
Thanks a lot
I'm trying to intercept 401 errors, but interceptors doesn't work.
My code:
Axios.interceptors.response.use((response) => response, (error) => console.log('>>>', error));
this.instance = Axios.create({
baseURL: BASE_URL,
timeout: 10000,
headers: {
Authorization: `Bearer ${token}`,
'Access-Control-Allow-Headers': 'Authorization'
}
});
I have an issue when i try to mock axios-observable with jest.
jest.mock("axios-observable", () => Object.assign( jest.fn(), { get: jest.fn() } ));
Here's how i try to mock it.
But i got an error when executing my test saying :
'TypeError: _axiosObservable.default.get(...).pipe is not a function'
Any idea of what going wrong or any exemple of mocking exemple implementation ?
Thanks by advance,
Best Regards
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.