/** @typedef {{workspace: string, apiKey: string, baseURI: string}} Config */ /** @type {(c: Config, url: string) => Promise} */ export const get = async (config, url) => { const rep = await fetch(config.baseURI + url, { method: 'GET', headers: { 'x-api-key': config.apiKey, }, }) if (!rep.ok) { let m = `Status not OK: ${rep.status}` try { m += '\n' + (await rep.text()) } catch {} throw new Error(m) } return rep.json() } /** @typedef {{ "id": string, "estimate_point": unknown, "name": string, "description_html": string, "description_stripped": string, "priority": string, "sequence_id": number, "project": string, "workspace": string, "parent": string | null, "state": string, "assignees": Array, "labels": Array }} Issue*/ /** @typedef {"backlog" | "unstarted" | "started" | "completed" | "cancelled"} StateGroup */ /** @typedef {{ "id": string, "created_at": string, "updated_at": string, "name": string, "description": string, "color": string, "slug": string, "group": StateGroup, "default": boolean, "project": string, "workspace": string }} State */ /** * @template T * @typedef * {{results: Array, next_page_results: boolean, next_cursor: string}} * Page */ /** @type {(c: Config, u: string) => Promise>} */ const paginated = async (config, u) => { let get_next_page = true let cursor = '' const results = [] while (get_next_page) { /** @type {Page} */ const rep = await get(config, u + `?cursor=${cursor}`) get_next_page = rep.next_page_results cursor = rep.next_cursor results.push(...rep.results) } return results } export default { State: { /** @type {(c: Config, p: string) => Promise>} */ getAll: (config, project) => paginated( config, `/api/v1/workspaces/${config.workspace}/projects/${project}/states`, ), }, Issue: { /** @type {(c: Config, p: string, i: string) => Promise} */ get: (config, project, issue) => get( config, `/api/v1/workspaces/${config.workspace}/projects/${project}/issues/${issue}`, ), /** @type {(c: Config, p: string) => Promise>} */ getAll: async (config, project) => paginated( config, `/api/v1/workspaces/${config.workspace}/projects/${project}/issues`, ), }, }