Last active
August 19, 2020 10:03
-
-
Save samternent/5464fc033881599902c2c9569e6f741a to your computer and use it in GitHub Desktop.
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
import axios from 'axios'; | |
let instance = axios.create(); | |
instance.defaults.baseURL = 'xxx.teamwork.com' | |
instance.defaults.headers.common['Authorization'] = 'Bearer XXX'; | |
export default instance; |
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
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); | |
}; |
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
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