-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ZNTA-975): add middleware to include meta.timestamp in all API c…
…alls The middleware also adds some repeated defaults such as credentials and JSON headers, which avoids the need to wrap calls in a helper function. I chose a distinct type rather than reusing CALL_API so that there is a way to skip the new behaviour if that is ever needed.
- Loading branch information
1 parent
6315977
commit 1f76246
Showing
6 changed files
with
80 additions
and
58 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
61 changes: 61 additions & 0 deletions
61
server/zanata-frontend/src/frontend/app/editor/middlewares/call-api.js
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,61 @@ | ||
/* Enahanced CALL_API that sets some defaults and gives a better API */ | ||
|
||
import { CALL_API } from 'redux-api-middleware' | ||
|
||
// use this key instead of CALL_API to trigger this middleware | ||
export const CALL_API_ENHANCED = Symbol('CALL_API_ENHANCED') | ||
|
||
/* | ||
* Applies the following enhancements before passing to redux-api-middleware: | ||
* | ||
* - defaults method to 'GET' if not specified | ||
* - always include credentials | ||
* - sets JSON content type and accept headers | ||
* - adds meta.timestamp to request, success and failure actions | ||
*/ | ||
export default store => next => action => { | ||
if (action[CALL_API_ENHANCED]) { | ||
const { headers, types, ...remainingKeys } = action[CALL_API_ENHANCED] | ||
return next({ | ||
[CALL_API]: { | ||
// default, overridden if remainingKeys.method is present | ||
method: 'GET', | ||
credentials: 'include', | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
...headers | ||
}, | ||
types: typesWithTimestamp(types), | ||
// remaining keys are last to allow overriding anything above | ||
...remainingKeys | ||
} | ||
}) | ||
} else { | ||
return next(action) | ||
} | ||
} | ||
|
||
/* Add meta.timestamp to all the given methods */ | ||
function typesWithTimestamp ([request, success, failure]) { | ||
const timestamp = Date.now() | ||
return [ | ||
withTimestamp(request, timestamp), | ||
withTimestamp(success, timestamp), | ||
withTimestamp(failure, timestamp) | ||
] | ||
} | ||
|
||
/* Add meta.timestamp to a redux-api-middleware type descriptor */ | ||
// Note: may not work properly when meta is or returns a promise. If we start | ||
// using promises for meta, this should be updated. | ||
function withTimestamp (type, timestamp) { | ||
const normalType = typeof type === 'string' || typeof type === 'symbol' | ||
? { type } | ||
: type | ||
const oldMeta = normalType.meta || {} | ||
const meta = typeof oldMeta === 'function' | ||
? (...args) => ({ ...(oldMeta(...args)), timestamp }) | ||
: { ...oldMeta, timestamp } | ||
return { ...normalType, meta } | ||
} |
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
33 changes: 0 additions & 33 deletions
33
server/zanata-frontend/src/frontend/app/editor/utils/api-util.js
This file was deleted.
Oops, something went wrong.
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