-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add advanced middleware feature to inject custom network adapters This moves most of the logic from the query middleware to the new advanced middleware. The query middleware will call advanced using the default superagent adapter. * Add tests for the superagent adapter * Add advanced CommonJS entry point alias * Create separate default and advanced entries * Fix advanced invalid imports * Add test for PATCH in superagent adapter * Update README with docs about redux-query/advanced
- Loading branch information
1 parent
908a9b2
commit d922161
Showing
8 changed files
with
452 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dist/commonjs/advanced'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import superagent from 'superagent'; | ||
import * as httpMethods from '../constants/http-methods'; | ||
|
||
export const createRequest = (url, method) => { | ||
switch (method) { | ||
case httpMethods.GET: | ||
return superagent.get(url); | ||
case httpMethods.POST: | ||
return superagent.post(url); | ||
case httpMethods.PUT: | ||
return superagent.put(url); | ||
case httpMethods.PATCH: | ||
return superagent.patch(url); | ||
case httpMethods.DELETE: | ||
return superagent.del(url); | ||
default: | ||
throw new Error(`Unsupported HTTP method: ${method}`); | ||
} | ||
}; | ||
|
||
const superagentNetworkAdapter = (url, method, { body, headers, credentials } = {}) => { | ||
const request = createRequest(url, method); | ||
|
||
if (body) { | ||
request.send(body); | ||
} | ||
|
||
if (headers) { | ||
request.set(headers); | ||
} | ||
|
||
if (credentials === 'include') { | ||
request.withCredentials(); | ||
} | ||
|
||
const execute = (cb) => request.end((err, response) => { | ||
const resStatus = (response && response.status) || 0; | ||
const resBody = (response && response.body) || undefined; | ||
const resText = (response && response.text) || undefined; | ||
|
||
cb(err, resStatus, resBody, resText); | ||
}); | ||
|
||
const abort = () => request.abort(); | ||
|
||
return { execute, abort }; | ||
}; | ||
|
||
export default superagentNetworkAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as actions from './actions'; | ||
import * as actionTypes from './constants/action-types'; | ||
import * as httpMethods from './constants/http-methods'; | ||
import * as querySelectors from './selectors/query'; | ||
|
||
export { default as connectRequest } from './components/connect-request'; | ||
export { getQueryKey, reconcileQueryKey } from './lib/query-key'; | ||
export { default as queriesReducer } from './reducers/queries'; | ||
export { default as entitiesReducer } from './reducers/entities'; | ||
export { default as queryMiddlewareAdvanced } from './middleware/query-advanced'; | ||
export { cancelQuery, mutateAsync, requestAsync, removeEntities, removeEntity } from './actions'; | ||
export { actions, actionTypes, httpMethods, querySelectors }; |
Oops, something went wrong.