Skip to content

Instantly share code, notes, and snippets.

@samternent
Last active August 19, 2020 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samternent/5464fc033881599902c2c9569e6f741a to your computer and use it in GitHub Desktop.
Save samternent/5464fc033881599902c2c9569e6f741a to your computer and use it in GitHub Desktop.
import axios from 'axios';
let instance = axios.create();
instance.defaults.baseURL = 'xxx.teamwork.com'
instance.defaults.headers.common['Authorization'] = 'Bearer XXX';
export default instance;
import api from './api';
const defaultParams = {
page: 1,
pageSize: 10,
};
const normalizeV1Pagination = (headers) => ({
page: parseInt(headers['x-page'], 10),
pages: parseInt(headers['x-pages'], 10),
totalRecords: parseInt(headers['x-records'], 10),
});
const normalizeV1Cache = (headers) => ({
etag: headers['etag'],
});
const normalizeV2Data = (data) => data;
const normalizeV1Data = (data) => data.map((task) => ({
title: task.content,
description: task.description,
assignee: task['responsible-party-names'] || 'Anybody',
}));
const normalizeV2Headers = (headers) => headers;
const serializeParams = (params = {}) => ({
...defaultParams,
...params,
});
const fetch = {
'v1': async (params = { id: 0 }) => {
try {
const { data, headers } = await api.get(`projects/${params.id}/tasks.json`, {
params: serializeParams(params)
});
if (data.STATUS === "OK") {
return {
data: normalizeV1Data(data['todo-items']),
paginator: normalizeV1Pagination(headers),
cache: normalizeV1Cache(headers),
};
}
} catch (err) {
throw new Error(err);
}
},
'v2': async (params = { id: 0 }) => {
try {
const { data, headers, status } = await api.get(`/projects/api/v2/projects/${params.id}/taskids.json`, {
data: serializeParams(params)
});
if (status === 200) {
return {
data: normalizeV2Data(data.project),
paginator: normalizeV2Headers(headers),
cache: normalizeV1Cache(headers),
};
}
} catch (err) {
throw new Error(err);
}
}
};
export default async (params, options = { version: 'v1' }) => {
return fetch[options.version](params);
};
import test from 'ava';
import getTasksByProject from './tasks';
test('respectful page size', async (t) => {
const tasks = await getTasksByProject({ id: 422481, pageSize: 10 });
t.is(tasks.data.length, 10);
});
test('working paginator', async (t) => {
const page1 = await getTasksByProject({ id: 422481, page: 1, pageSize: 15 });
const page2 = await getTasksByProject({ id: 422481, page: 2, pageSize: 15 });
const tasks = [
...page1.data,
...page2.data,
];
t.is(page1.paginator.page, 1);
t.is(page2.paginator.page, 2);
t.is(tasks.length, 30);
});
test('normalised data', async (t) => {
const page1 = await getTasksByProject({ id: 422481, page: 1, pageSize: 15 });
t.deepEqual(Object.keys(page1.data[0]), ['title', 'description', 'assignee']);
t.deepEqual(Object.keys(page1.paginator), ['page', 'pages', 'totalRecords']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment