mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-03 22:31:52 +00:00
Moved management of filtering options to own reducer
This commit is contained in:
@@ -25,30 +25,21 @@ export class ShlinkApiClient {
|
||||
* @returns {Promise<Array>}
|
||||
*/
|
||||
listShortUrls = (params = {}) => {
|
||||
const { page = 1 } = params;
|
||||
return this._performRequest(`/rest/short-codes?page=${page}`)
|
||||
return this._performRequest('/rest/short-codes', 'GET', params)
|
||||
.then(resp => resp.data.shortUrls.data)
|
||||
.catch(e => {
|
||||
// If auth failed, reset token to force it to be regenerated, and perform a new request
|
||||
if (e.response.status === 401) {
|
||||
this._token = '';
|
||||
return this.listShortUrls(params);
|
||||
}
|
||||
|
||||
// Otherwise, let caller handle the rejection
|
||||
return Promise.reject(e);
|
||||
});
|
||||
.catch(e => this._handleAuthError(e, this.listShortUrls, [params]));
|
||||
};
|
||||
|
||||
_performRequest = async (url, method = 'GET') => {
|
||||
_performRequest = async (url, method = 'GET', params = {}) => {
|
||||
if (isEmpty(this._token)) {
|
||||
this._token = await this._handleAuth();
|
||||
this._token = await this._authenticate();
|
||||
}
|
||||
|
||||
return await this.axios({
|
||||
method,
|
||||
url: `${this._baseUrl}${url}`,
|
||||
headers: { 'Authorization': `Bearer ${this._token}` }
|
||||
headers: { 'Authorization': `Bearer ${this._token}` },
|
||||
params
|
||||
}).then(resp => {
|
||||
// Save new token
|
||||
const { authorization = '' } = resp.headers;
|
||||
@@ -57,7 +48,7 @@ export class ShlinkApiClient {
|
||||
});
|
||||
};
|
||||
|
||||
_handleAuth = async () => {
|
||||
_authenticate = async () => {
|
||||
const resp = await this.axios({
|
||||
method: 'POST',
|
||||
url: `${this._baseUrl}/rest/authenticate`,
|
||||
@@ -65,6 +56,17 @@ export class ShlinkApiClient {
|
||||
});
|
||||
return resp.data.token;
|
||||
};
|
||||
|
||||
_handleAuthError = (e, method, args) => {
|
||||
// If auth failed, reset token to force it to be regenerated, and perform a new request
|
||||
if (e.response.status === 401) {
|
||||
this._token = '';
|
||||
return method(...args);
|
||||
}
|
||||
|
||||
// Otherwise, let caller handle the rejection
|
||||
return Promise.reject(e);
|
||||
};
|
||||
}
|
||||
|
||||
export default new ShlinkApiClient(axios);
|
||||
|
||||
Reference in New Issue
Block a user