Merge pull request #98 from acelaya/feature/clean-api-client

Feature/clean api client
This commit is contained in:
Alejandro Celaya 2019-01-10 19:26:28 +01:00 committed by GitHub
commit 58d3a59e58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 93 deletions

View file

@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
#### Removed #### Removed
* [#59](https://github.com/shlinkio/shlink-web-client/issues/59) Dropped support for old browsers. Internet explorer and dead browsers are no longer supported. * [#59](https://github.com/shlinkio/shlink-web-client/issues/59) Dropped support for old browsers. Internet explorer and dead browsers are no longer supported.
* [#97](https://github.com/shlinkio/shlink-web-client/issues/97) Dropped support for authentication via `Authorization` header with Bearer type and JWT, which will make this version no longer work with shlink earlier than v1.13.0.
#### Fixed #### Fixed

View file

@ -2,7 +2,6 @@ import qs from 'qs';
import { isEmpty, isNil, reject } from 'ramda'; import { isEmpty, isNil, reject } from 'ramda';
const API_VERSION = '1'; const API_VERSION = '1';
const STATUS_UNAUTHORIZED = 401;
const buildRestUrl = (url) => url ? `${url}/rest/v${API_VERSION}` : ''; const buildRestUrl = (url) => url ? `${url}/rest/v${API_VERSION}` : '';
export default class ShlinkApiClient { export default class ShlinkApiClient {
@ -10,98 +9,54 @@ export default class ShlinkApiClient {
this.axios = axios; this.axios = axios;
this._baseUrl = buildRestUrl(baseUrl); this._baseUrl = buildRestUrl(baseUrl);
this._apiKey = apiKey || ''; this._apiKey = apiKey || '';
this._token = '';
} }
listShortUrls = (options = {}) => listShortUrls = (options = {}) =>
this._performRequest('/short-codes', 'GET', options) this._performRequest('/short-urls', 'GET', options)
.then((resp) => resp.data.shortUrls) .then((resp) => resp.data.shortUrls);
.catch((e) => this._handleAuthError(e, this.listShortUrls, [ options ]));
createShortUrl = (options) => { createShortUrl = (options) => {
const filteredOptions = reject((value) => isEmpty(value) || isNil(value), options); const filteredOptions = reject((value) => isEmpty(value) || isNil(value), options);
return this._performRequest('/short-codes', 'POST', {}, filteredOptions) return this._performRequest('/short-urls', 'POST', {}, filteredOptions)
.then((resp) => resp.data) .then((resp) => resp.data);
.catch((e) => this._handleAuthError(e, this.createShortUrl, [ filteredOptions ]));
}; };
getShortUrlVisits = (shortCode, dates) => getShortUrlVisits = (shortCode, dates) =>
this._performRequest(`/short-codes/${shortCode}/visits`, 'GET', dates) this._performRequest(`/short-urls/${shortCode}/visits`, 'GET', dates)
.then((resp) => resp.data.visits.data) .then((resp) => resp.data.visits.data);
.catch((e) => this._handleAuthError(e, this.getShortUrlVisits, [ shortCode, dates ]));
getShortUrl = (shortCode) => getShortUrl = (shortCode) =>
this._performRequest(`/short-codes/${shortCode}`, 'GET') this._performRequest(`/short-urls/${shortCode}`, 'GET')
.then((resp) => resp.data) .then((resp) => resp.data);
.catch((e) => this._handleAuthError(e, this.getShortUrl, [ shortCode ]));
deleteShortUrl = (shortCode) => deleteShortUrl = (shortCode) =>
this._performRequest(`/short-codes/${shortCode}`, 'DELETE') this._performRequest(`/short-urls/${shortCode}`, 'DELETE')
.then(() => ({})) .then(() => ({}));
.catch((e) => this._handleAuthError(e, this.deleteShortUrl, [ shortCode ]));
updateShortUrlTags = (shortCode, tags) => updateShortUrlTags = (shortCode, tags) =>
this._performRequest(`/short-codes/${shortCode}/tags`, 'PUT', {}, { tags }) this._performRequest(`/short-urls/${shortCode}/tags`, 'PUT', {}, { tags })
.then((resp) => resp.data.tags) .then((resp) => resp.data.tags);
.catch((e) => this._handleAuthError(e, this.updateShortUrlTags, [ shortCode, tags ]));
listTags = () => listTags = () =>
this._performRequest('/tags', 'GET') this._performRequest('/tags', 'GET')
.then((resp) => resp.data.tags.data) .then((resp) => resp.data.tags.data);
.catch((e) => this._handleAuthError(e, this.listTags, []));
deleteTags = (tags) => deleteTags = (tags) =>
this._performRequest('/tags', 'DELETE', { tags }) this._performRequest('/tags', 'DELETE', { tags })
.then(() => ({ tags })) .then(() => ({ tags }));
.catch((e) => this._handleAuthError(e, this.deleteTags, [ tags ]));
editTag = (oldName, newName) => editTag = (oldName, newName) =>
this._performRequest('/tags', 'PUT', {}, { oldName, newName }) this._performRequest('/tags', 'PUT', {}, { oldName, newName })
.then(() => ({ oldName, newName })) .then(() => ({ oldName, newName }));
.catch((e) => this._handleAuthError(e, this.editTag, [ oldName, newName ]));
_performRequest = async (url, method = 'GET', query = {}, body = {}) => { _performRequest = async (url, method = 'GET', query = {}, body = {}) =>
if (isEmpty(this._token)) { await this.axios({
this._token = await this._authenticate();
}
return await this.axios({
method, method,
url: `${this._baseUrl}${url}`, url: `${this._baseUrl}${url}`,
headers: { Authorization: `Bearer ${this._token}` }, headers: { 'X-Api-Key': this._apiKey },
params: query, params: query,
data: body, data: body,
paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'brackets' }), paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'brackets' }),
}).then((resp) => {
// Save new token
const { authorization = '' } = resp.headers;
this._token = authorization.substr('Bearer '.length);
return resp;
}); });
};
_authenticate = async () => {
const resp = await this.axios({
method: 'POST',
url: `${this._baseUrl}/authenticate`,
data: { apiKey: this._apiKey },
});
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 === STATUS_UNAUTHORIZED) {
this._token = '';
return method(...args);
}
// Otherwise, let caller handle the rejection
return Promise.reject(e);
};
} }

View file

@ -3,26 +3,20 @@ import { head, last } from 'ramda';
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient'; import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
describe('ShlinkApiClient', () => { describe('ShlinkApiClient', () => {
const createAxiosMock = (extraData) => () => const createAxiosMock = (data) => () => Promise.resolve(data);
Promise.resolve({ const createApiClient = (data) => new ShlinkApiClient(createAxiosMock(data));
headers: { authorization: 'Bearer abc123' },
data: { token: 'abc123' },
...extraData,
});
const createApiClient = (extraData) =>
new ShlinkApiClient(createAxiosMock(extraData));
describe('listShortUrls', () => { describe('listShortUrls', () => {
it('properly returns short URLs list', async () => { it('properly returns short URLs list', async () => {
const expectedList = [ 'foo', 'bar' ]; const expectedList = [ 'foo', 'bar' ];
const apiClient = createApiClient({ const { listShortUrls } = createApiClient({
data: { data: {
shortUrls: expectedList, shortUrls: expectedList,
}, },
}); });
const actualList = await apiClient.listShortUrls(); const actualList = await listShortUrls();
expect(expectedList).toEqual(actualList); expect(expectedList).toEqual(actualList);
}); });
@ -34,17 +28,17 @@ describe('ShlinkApiClient', () => {
}; };
it('returns create short URL', async () => { it('returns create short URL', async () => {
const apiClient = createApiClient({ data: shortUrl }); const { createShortUrl } = createApiClient({ data: shortUrl });
const result = await apiClient.createShortUrl({}); const result = await createShortUrl({});
expect(result).toEqual(shortUrl); expect(result).toEqual(shortUrl);
}); });
it('removes all empty options', async () => { it('removes all empty options', async () => {
const axiosSpy = sinon.spy(createAxiosMock({ data: shortUrl })); const axiosSpy = sinon.spy(createAxiosMock({ data: shortUrl }));
const apiClient = new ShlinkApiClient(axiosSpy); const { createShortUrl } = new ShlinkApiClient(axiosSpy);
await apiClient.createShortUrl( await createShortUrl(
{ foo: 'bar', empty: undefined, anotherEmpty: null } { foo: 'bar', empty: undefined, anotherEmpty: null }
); );
const lastAxiosCall = last(axiosSpy.getCalls()); const lastAxiosCall = last(axiosSpy.getCalls());
@ -64,14 +58,14 @@ describe('ShlinkApiClient', () => {
}, },
}, },
})); }));
const apiClient = new ShlinkApiClient(axiosSpy); const { getShortUrlVisits } = new ShlinkApiClient(axiosSpy);
const actualVisits = await apiClient.getShortUrlVisits('abc123', {}); const actualVisits = await getShortUrlVisits('abc123', {});
const lastAxiosCall = last(axiosSpy.getCalls()); const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args); const axiosArgs = head(lastAxiosCall.args);
expect(expectedVisits).toEqual(actualVisits); expect(expectedVisits).toEqual(actualVisits);
expect(axiosArgs.url).toContain('/short-codes/abc123/visits'); expect(axiosArgs.url).toContain('/short-urls/abc123/visits');
expect(axiosArgs.method).toEqual('GET'); expect(axiosArgs.method).toEqual('GET');
}); });
}); });
@ -82,14 +76,14 @@ describe('ShlinkApiClient', () => {
const axiosSpy = sinon.spy(createAxiosMock({ const axiosSpy = sinon.spy(createAxiosMock({
data: expectedShortUrl, data: expectedShortUrl,
})); }));
const apiClient = new ShlinkApiClient(axiosSpy); const { getShortUrl } = new ShlinkApiClient(axiosSpy);
const result = await apiClient.getShortUrl('abc123'); const result = await getShortUrl('abc123');
const lastAxiosCall = last(axiosSpy.getCalls()); const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args); const axiosArgs = head(lastAxiosCall.args);
expect(expectedShortUrl).toEqual(result); expect(expectedShortUrl).toEqual(result);
expect(axiosArgs.url).toContain('/short-codes/abc123'); expect(axiosArgs.url).toContain('/short-urls/abc123');
expect(axiosArgs.method).toEqual('GET'); expect(axiosArgs.method).toEqual('GET');
}); });
}); });
@ -100,14 +94,14 @@ describe('ShlinkApiClient', () => {
const axiosSpy = sinon.spy(createAxiosMock({ const axiosSpy = sinon.spy(createAxiosMock({
data: { tags: expectedTags }, data: { tags: expectedTags },
})); }));
const apiClient = new ShlinkApiClient(axiosSpy); const { updateShortUrlTags } = new ShlinkApiClient(axiosSpy);
const result = await apiClient.updateShortUrlTags('abc123', expectedTags); const result = await updateShortUrlTags('abc123', expectedTags);
const lastAxiosCall = last(axiosSpy.getCalls()); const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args); const axiosArgs = head(lastAxiosCall.args);
expect(expectedTags).toEqual(result); expect(expectedTags).toEqual(result);
expect(axiosArgs.url).toContain('/short-codes/abc123/tags'); expect(axiosArgs.url).toContain('/short-urls/abc123/tags');
expect(axiosArgs.method).toEqual('PUT'); expect(axiosArgs.method).toEqual('PUT');
}); });
}); });
@ -120,9 +114,9 @@ describe('ShlinkApiClient', () => {
tags: { data: expectedTags }, tags: { data: expectedTags },
}, },
})); }));
const apiClient = new ShlinkApiClient(axiosSpy); const { listTags } = new ShlinkApiClient(axiosSpy);
const result = await apiClient.listTags(); const result = await listTags();
const lastAxiosCall = last(axiosSpy.getCalls()); const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args); const axiosArgs = head(lastAxiosCall.args);
@ -136,9 +130,9 @@ describe('ShlinkApiClient', () => {
it('properly deletes provided tags', async () => { it('properly deletes provided tags', async () => {
const tags = [ 'foo', 'bar' ]; const tags = [ 'foo', 'bar' ];
const axiosSpy = sinon.spy(createAxiosMock({})); const axiosSpy = sinon.spy(createAxiosMock({}));
const apiClient = new ShlinkApiClient(axiosSpy); const { deleteTags } = new ShlinkApiClient(axiosSpy);
await apiClient.deleteTags(tags); await deleteTags(tags);
const lastAxiosCall = last(axiosSpy.getCalls()); const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args); const axiosArgs = head(lastAxiosCall.args);
@ -149,13 +143,13 @@ describe('ShlinkApiClient', () => {
}); });
describe('editTag', () => { describe('editTag', () => {
it('properly deletes provided tags', async () => { it('properly edits provided tag', async () => {
const oldName = 'foo'; const oldName = 'foo';
const newName = 'bar'; const newName = 'bar';
const axiosSpy = sinon.spy(createAxiosMock({})); const axiosSpy = sinon.spy(createAxiosMock({}));
const apiClient = new ShlinkApiClient(axiosSpy); const { editTag } = new ShlinkApiClient(axiosSpy);
await apiClient.editTag(oldName, newName); await editTag(oldName, newName);
const lastAxiosCall = last(axiosSpy.getCalls()); const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args); const axiosArgs = head(lastAxiosCall.args);
@ -164,4 +158,18 @@ describe('ShlinkApiClient', () => {
expect(axiosArgs.data).toEqual({ oldName, newName }); expect(axiosArgs.data).toEqual({ oldName, newName });
}); });
}); });
describe('deleteShortUrl', () => {
it('properly deletes provided short URL', async () => {
const axiosSpy = sinon.spy(createAxiosMock({}));
const { deleteShortUrl } = new ShlinkApiClient(axiosSpy);
await deleteShortUrl('abc123');
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(axiosArgs.url).toContain('/short-urls/abc123');
expect(axiosArgs.method).toEqual('DELETE');
});
});
}); });