ReactiveX / ReactiveX/rxjs

Ajax config & intercept

Open
#2,686 17 comments 33 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
31.7k
Forks
3k
PR merge metrics
No merged PRs in 30d

Description

Is there a way to create a pre configured ajax instance, so that I don't need to configure it over and over again ?

For example:


import ajax from 'rxjs/Observable/dom/ajax'

const myAjax = ajax.create({url: '/api/xxx',  header: {widthCredential: true, ...}})

// Now myAjax will setup widthCredential = true for all the related outgoing ajax request
// so that  I can just:
myAjax.get('/api/xxx')
myAjax.post('/api/xxx', body)

// Instead of
ajax.get({url: '/api/xxx', header: {widthCredential: true, ...}})
ajax.post({url: '/api/xxx', body: data, header: {widthCredential: true, ...}})

BTW how can I intercept all http requests or responses in a single place using rxjs' ajax api ?

Here's how the axios library intercepts http requests and responses:


// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

Thanks !

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the rxjs/Observable/dom/ajax entry point and read the existing ajax API to understand how request options are currently supplied. Compare the requested reusable configuration and request/response interception behavior with the axios examples; done should be a clearly defined API with coverage for the intended request and response flows.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
api, frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.