zulip-plane/plane.js

102 lines
2.4 KiB
JavaScript

/** @typedef {{workspace: string, apiKey: string, baseURI: string}} Config */
/** @type {<T>(c: Config, url: string) => Promise<T>} */
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<string>,
"labels": Array<unknown>
}} 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<T>, next_page_results: boolean, next_cursor: string}}
* Page
*/
/** @type {<T>(c: Config, u: string) => Promise<Array<T>>} */
const paginated = async (config, u) => {
let get_next_page = true
let cursor = ''
const results = []
while (get_next_page) {
/** @type {Page<any>} */
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<Array<State>>} */
getAll: (config, project) =>
paginated(
config,
`/api/v1/workspaces/${config.workspace}/projects/${project}/states`,
),
},
Issue: {
/** @type {(c: Config, p: string, i: string) => Promise<Issue>} */
get: (config, project, issue) =>
get(
config,
`/api/v1/workspaces/${config.workspace}/projects/${project}/issues/${issue}`,
),
/** @type {(c: Config, p: string) => Promise<Array<Issue>>} */
getAll: async (config, project) =>
paginated(
config,
`/api/v1/workspaces/${config.workspace}/projects/${project}/issues`,
),
},
}