Skip to content

Commit fe97b6b

Browse files
committed
chore: add type definations
1 parent ae725a3 commit fe97b6b

2 files changed

Lines changed: 237 additions & 253 deletions

File tree

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/**
2+
* @file GitHub1s Type Definitions
3+
* @author netcon
4+
*/
5+
6+
/* eslint-disable prettier/prettier */
7+
8+
// prettier-ignore
9+
declare module 'github1s' {
10+
type Promisable<T> = PromiseLike<T> | T;
11+
12+
enum FileType {
13+
Directory = 'Directory',
14+
File = 'File',
15+
Link = 'Link',
16+
}
17+
18+
interface Directory {
19+
path: string;
20+
entries: {
21+
path: string;
22+
commitSha: string;
23+
type: FileType;
24+
}[];
25+
// entries doesn't contains all results if `truncated` is true
26+
truncated: boolean;
27+
}
28+
29+
interface File {
30+
path: string;
31+
commitSha: string;
32+
content: Uint8Array;
33+
}
34+
35+
interface Branch {
36+
name: string;
37+
commitSha: string;
38+
}
39+
40+
interface Tag {
41+
name: string;
42+
commitSha: string;
43+
}
44+
45+
interface Commit {
46+
sha: string;
47+
author: string;
48+
email: string;
49+
message: string;
50+
committer?: string;
51+
createTime: string;
52+
}
53+
54+
interface CodeSearchOptions {
55+
isCaseSensitive: boolean;
56+
isWordMatch: boolean;
57+
isRegExp: boolean;
58+
includes: string; // glob string
59+
excludes: string; // glob string
60+
}
61+
62+
interface Position {
63+
line: number;
64+
character: number;
65+
}
66+
67+
interface Range {
68+
start: Position;
69+
end: Position;
70+
}
71+
72+
// for cross-repository data,
73+
// reference to another repository
74+
interface ResourceScope {
75+
schema: string; // github / github / bitbucket...
76+
repo: string;
77+
ref: string;
78+
}
79+
80+
interface CodeLocation {
81+
// we can set `scope` to marked that current
82+
// result is referenced to another repository
83+
scope?: ResourceScope;
84+
path: string;
85+
ranges: Range[];
86+
}
87+
88+
enum CodeReviewStatus {
89+
Opening = 'Opening', // icon: 🟢
90+
Merged = 'Merged', // icon: 🟣
91+
Closed = 'Closed', // icon: 🔴
92+
}
93+
94+
// may be a Pull Request for GitHub,
95+
// or a Merge Request for GitLab,
96+
// or a Change Request for Gerrit
97+
interface CodeReview {
98+
id: string;
99+
status: CodeReviewStatus;
100+
creator: string;
101+
createTime: Date;
102+
approveTime: Date | null;
103+
denyTime: Date | null;
104+
mergeTime: Date | null;
105+
closeTime: Date | null;
106+
head: {
107+
label: string;
108+
commitSha: string;
109+
};
110+
base: {
111+
label: string;
112+
commitSha: string;
113+
};
114+
}
115+
116+
interface FileBlameRange {
117+
age: number;
118+
startingLine: number;
119+
endingLine: number;
120+
commit: Commit;
121+
}
122+
123+
// the string will be rendered use markdown format
124+
type MarkdownString = string;
125+
126+
interface CommonQueryOptions {
127+
offset: number;
128+
limit: number;
129+
query?: string;
130+
}
131+
132+
export interface DataSourceProvider {
133+
// if `recursive` is true, it should try to return all subtrees
134+
provideDirectory(repo: string, ref: string, path: string, recursive: boolean): Promisable<Directory[]>;
135+
136+
// the ref here may be a commitSha, branch, tag, or 'HEAD'
137+
provideFile(repo: string, ref: string, path: string): Promisable<File>;
138+
139+
provideBranches(repo: string, options: CommonQueryOptions): Promisable<Branch[]>;
140+
141+
provideBranch(repo: string, branch: string): Promisable<Branch | null>;
142+
143+
provideTags(repo: string, options: CommonQueryOptions): Promisable<Tag[]>;
144+
145+
provideTag(repo: string, tag: string): Promisable<Tag | null>;
146+
147+
provideCommits(repo: string, options: CommonQueryOptions & { from?: string; author?: string; path?: string; }): Promisable<Commit[]>;
148+
149+
// the ref here may be a commitSha, branch, tag, or 'HEAD'
150+
provideCommit(repo: string, ref: string): Promisable<Commit | null>;
151+
152+
// use `report` to populate search results gradually
153+
provideTextSearchResults(repo: string, ref: string, query: string, options: CodeSearchOptions, report: (results: CodeLocation[]) => void): Promisable<{ limitHit: boolean }>;
154+
155+
provideCodeReviews(repo: string, options: CommonQueryOptions & { state?: CodeReviewStatus, creator?: string }): Promisable<CodeReview[]>;
156+
157+
provideCodeReview(repo: string, id: string): Promisable<CodeReview | null>;
158+
159+
provideFileBlameRanges(repo: string, ref: string, path: string): Promisable<FileBlameRange[]>;
160+
161+
provideCodeDefinition(repo: string, ref: string, path: string, line: number, character: number): Promisable<CodeLocation | CodeLocation[]>;
162+
163+
provideCodeReferences(repo: string, ref: string, path: string, line: number, character: number): Promisable<CodeLocation[]>;
164+
165+
provideCodeHover(repo: string, ref: string, path: string, line: number, character: number): Promisable<MarkdownString>;
166+
167+
provideUserAvatarLink(user: string): Promisable<string>;
168+
}
169+
170+
export enum PageType {
171+
// show the structure of a dictionary
172+
// e.g. https://github.com/conwnet/github1s/tree/master/src/vs
173+
TREE = 1,
174+
175+
// show the content for a file
176+
// e.g. https://github.com/conwnet/github1s/blob/master/extensions/github1s/src/extension.ts
177+
BLOB = 2,
178+
179+
// show the commit list of a repository
180+
// e.g. https://github.com/conwnet/github1s/commits/master
181+
COMMIT_LIST = 3,
182+
183+
// show the detail of a commit
184+
// e.g. https://github.com/conwnet/github1s/commit/c1264f7338833c7aa3a502c4629df8aa6b7d6ccf
185+
COMMIT = 4,
186+
187+
// show the pull request (or merge request) list of a repository
188+
// e.g. https://github.com/conwnet/github1s/pulls
189+
CODE_REVIEW_LIST = 5,
190+
191+
// show the detail of a pull request (or merge request)
192+
// e.g. https://github.com/conwnet/github1s/pull/81
193+
CODE_REVIEW = 6,
194+
195+
// show the file blame
196+
// e.g. https://github.com/conwnet/github1s/blame/master/.gitignore
197+
FILE_BLAME = 7,
198+
199+
// branches, tags, wiki, gist should be on the way
200+
UNKNOWN = 0,
201+
}
202+
203+
export type RouterState = { repo: string; ref: string } & (
204+
| { type: PageType.TREE; filePath: string } // for tree page
205+
| { type: PageType.BLOB; filePath: string; startLine?: number; endLine?: number } // for blob page
206+
| { type: PageType.COMMIT_LIST } // for commit list page
207+
| { type: PageType.COMMIT; commitSha: string } // for commit detail page
208+
| { type: PageType.CODE_REVIEW_LIST } // for code review list page
209+
| { type: PageType.CODE_REVIEW; codeReviewId: string } // for code review detail page
210+
);
211+
212+
export interface RouterParser {
213+
// parse giving path (starts with '/', may includes search and hash) to Router state,
214+
parsePath(path: string): Promisable<RouterState>;
215+
216+
// build the tree page path
217+
buildTreePath(repo: string, ref: string, filePath: string): Promisable<string>;
218+
219+
// build the blob page path
220+
buildBlobPath(repo: string, ref: string, filePath: string, startLine?: number, endLine?: number): Promisable<string>;
221+
222+
// build the commit list page path
223+
buildCommitListPath(repo: string): Promisable<string>;
224+
225+
// build the commit page path
226+
buildCommitPath(repo: string, commitSha: string): Promisable<string>;
227+
228+
// build the code review list page path
229+
buildCodeReviewListPath(repo: string): Promisable<string>;
230+
231+
// build the code review page path
232+
buildCodeReviewPath(repo: string, codeReviewId: string): Promisable<string>;
233+
234+
// convert giving path to the external link (using for jumping back to origin platform)
235+
buildExternalLink(path: string): Promise<string | null>;
236+
}
237+
}

0 commit comments

Comments
 (0)