-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (95 loc) · 4.05 KB
/
index.js
File metadata and controls
107 lines (95 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// @ts-check
import { Octokit } from "@octokit/core";
import { listItems } from "./api/items.list.js";
import { addDraftItem } from "./api/items.add-draft.js";
import { addItem } from "./api/items.add.js";
import { getItem } from "./api/items.get.js";
import { getItemByContentId } from "./api/items.get-by-content-id.js";
import { getItemByContentRepositoryAndNumber } from "./api/items.get-by-content-repository-and-number.js";
import { updateItem } from "./api/items.update.js";
import { updateItemByContentId } from "./api/items.update-by-content-id.js";
import { updateItemByContentRepositoryAndNumber } from "./api/items.update-by-content-repository-and-number.js";
import { archiveItem } from "./api/items.archive.js";
import { archiveItemByContentId } from "./api/items.archive-by-content-id.js";
import { archiveItemByContentRepositoryAndNumber } from "./api/items.archive-by-content-repository-and-number.js";
import { removeItem } from "./api/items.remove.js";
import { removeItemByContentId } from "./api/items.remove-by-content-id.js";
import { removeItemByContentRepositoryAndNumber } from "./api/items.remove-by-content-repository-and-name.js";
import { getProperties } from "./api/project.getProperties.js";
import { defaultMatchFunction } from "./api/lib/default-match-function.js";
import { defaultTruncateFunction } from "./api/lib/default-truncate-function.js";
/** @type {import("./").BUILT_IN_FIELDS} */
export const BUILT_IN_FIELDS = {
title: "Title",
status: "Status",
};
export * from "./api/errors.js";
export default class GitHubProject {
/**
* @param {import(".").GitHubProjectOptions} options
*/
constructor(options) {
const { owner, number, fields = {} } = options;
// set octokit either from `options.octokit` or `options.token`
const octokit =
"token" in options
? new Octokit({ auth: options.token })
: options.octokit;
/** @type {import(".").GitHubProjectState} */
const state = {
didLoadFields: false,
matchFieldName: options.matchFieldName || defaultMatchFunction,
matchFieldOptionValue:
options.matchFieldOptionValue || defaultMatchFunction,
truncate: options.truncate || defaultTruncateFunction,
};
// set API
const itemsApi = {
list: listItems.bind(null, this, state),
addDraft: addDraftItem.bind(null, this, state),
add: addItem.bind(null, this, state),
get: getItem.bind(null, this, state),
getByContentId: getItemByContentId.bind(null, this, state),
getByContentRepositoryAndNumber: getItemByContentRepositoryAndNumber.bind(
null,
this,
state,
),
update: updateItem.bind(null, this, state),
updateByContentId: updateItemByContentId.bind(null, this, state),
updateByContentRepositoryAndNumber:
updateItemByContentRepositoryAndNumber.bind(null, this, state),
archive: archiveItem.bind(null, this, state),
archiveByContentId: archiveItemByContentId.bind(null, this, state),
archiveByContentRepositoryAndNumber:
archiveItemByContentRepositoryAndNumber.bind(null, this, state),
remove: removeItem.bind(null, this, state),
removeByContentId: removeItemByContentId.bind(null, this, state),
removeByContentRepositoryAndNumber:
removeItemByContentRepositoryAndNumber.bind(null, this, state),
};
Object.defineProperties(this, {
owner: { get: () => owner },
number: { get: () => number },
fields: { get: () => ({ ...BUILT_IN_FIELDS, ...fields }) },
octokit: { get: () => octokit },
items: { get: () => itemsApi },
getProperties: { get: () => getProperties.bind(null, this, state) },
});
}
/**
* Returns a GithubProject instance and calls `getProperties()` to preload
* project level properties.
*
* @param {import(".").GitHubProjectOptions} options
*
* @return {Promise<import(".").default>}
*/
static async getInstance(options) {
const project = /** @type {import(".").default} */ (
new GitHubProject(options)
);
await project.getProperties();
return project;
}
}