Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
apiRequest(fetchArgs, action) {
const meta = action.meta || {};
const completeApiRequest = this.completeApiRequest.bind(this, action.type);
const catchApiRequestError = this.catchApiRequestError.bind(this, action.type);
const preserveHeaderValues = this.preserveHeaderValues.bind(this, this.meta);
const addStatusCodeToMeta = this.addStatusCodeToMeta.bind(this, this.meta);
return fetch.apply(this, fetchArgs)
.then(this.checkResponseIsOk)
.then(preserveHeaderValues)
.then(addStatusCodeToMeta)
.then(responseToCompletion)
.then(createResponseHandlerWithMeta(meta))
.then(completeApiRequest)
.catch(catchApiRequestError);
}
async call() {
const token = await this.getToken();
if (await this.shouldRequestNewToken()) {
const refreshToken = await this.getRefreshToken();
const refreshAction = this.refreshAction(refreshToken);
const refreshApiAction = refreshAction[CALL_TOKEN_API];
const refreshApiActionMeta = refreshApiAction.meta || {};
const refreshArgs = this.getApiFetchArgsFromActionPayload(
refreshApiAction.payload,
token,
refreshApiActionMeta.authenticate
);
return fetch.apply(null, refreshArgs)
.then(this.checkResponseIsOk)
.then(responseToCompletion)
.then(createResponseHandlerWithMeta(refreshApiActionMeta))
.then(this.curriedApiCallMethod)
.catch(error => {
this.dispatch(createFailureAction(this.apiAction.type, error, this.meta));
});
} else {
return this.curriedApiCallMethod(token);
}
}
}
return () => {
return fetch.apply(null, fetchArgs)
.then(checkResponseIsOk)
.then(responseToCompletion);
};
}