Skip to content

Commit 9b0914d

Browse files
authored
Merge pull request #8 from takker99/refactor
Refactor
2 parents 6652f7c + 38f4526 commit 9b0914d

File tree

4 files changed

+174
-143
lines changed

4 files changed

+174
-143
lines changed

api/response.ts

Lines changed: 69 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { Omit } from "../utils.ts";
2-
import { Line } from "../base.ts";
3-
import { NotFoundError, NotMemberError } from "./error.ts";
2+
import {
3+
CommitId,
4+
Line,
5+
Page as PageBase,
6+
PageId,
7+
ProjectId,
8+
StringLc,
9+
UserId,
10+
} from "../base.ts";
411

512
/** 関連ページのメタデータ */
6-
export interface RelatedPage {
7-
/** ページのid */ id: string;
8-
/** ページのタイトル */ title: string;
9-
/** ページのタイトルを小文字にして、` `を`_`に変換したもの */ titleLc: string;
10-
/** ページのサムネイル画像 */ image: string;
11-
/** ページのサムネイル本文。最大5行 */ descriptions: string[];
12-
/** ページ内のリンク */ linksLc: string[];
13+
export interface RelatedPage extends PageBase {
14+
/** ページ内のリンク */ linksLc: StringLc[];
1315
/** おそらく被リンク数 */ linked: number;
14-
/** ページの最終更新日時 */ updated: number;
15-
/** おそらくページの閲覧日時 */ accessed: number;
1616
}
1717

1818
/** user information */
1919
export interface User {
20-
/** user id */ id: string;
20+
id: UserId;
2121
/** user name */ name: string;
2222
/** user display name */ displayName: string;
2323
/** profile image URL */ photo: string;
@@ -33,21 +33,12 @@ export interface UserInfo extends User {
3333
}
3434

3535
/** summary of page information */
36-
export interface PageSummary {
37-
/** ページのid */ id: string;
38-
/** ページのタイトル */ title: string;
39-
/** ページのサムネイル画像
40-
* 存在しなければ`null`
41-
*/
42-
image: string | null;
43-
/** ページのサムネイル本文。最大5行 */ descriptions: string[];
36+
export interface PageSummary extends PageBase {
4437
/** ピン留めされていたら1, されていなかったら0 */ pin: 0 | 1;
4538
/** ページの閲覧回数 */ views: number;
4639
/** おそらく被リンク数 */ linked: number;
47-
/** 最新の編集コミットid */ commitId: string;
40+
/** 最新の編集コミットid */ commitId: CommitId;
4841
/** ページの作成日時 */ created: number;
49-
/** ページの最終更新日時 */ updated: number;
50-
/** Date last visitedに使われる最終アクセス日時 */ accessed: number;
5142
/** page rank */ pageRank: number;
5243
/** Page historyの最終生成日時 */ snapshotCreated: number | null;
5344
}
@@ -74,20 +65,17 @@ export interface Page extends PageSummary {
7465
}
7566

7667
/** the response type of https://scrpabox.io/api/pages/:projectname */
77-
export type PageListResponse =
78-
| NotFoundError
79-
| NotMemberError
80-
| {
81-
/** data取得先のproject名 */ projectName: string;
82-
/** parameterに渡したskipと同じ */ skip: number;
83-
/** parameterに渡したlimitと同じ */ limit: number;
84-
/** projectの全ページ数 (中身のないページを除く) */ count: number;
85-
/** 取得できたページ情報 */ pages: PageSummary[];
86-
};
68+
export interface PageList {
69+
/** data取得先のproject名 */ projectName: string;
70+
/** parameterに渡したskipと同じ */ skip: number;
71+
/** parameterに渡したlimitと同じ */ limit: number;
72+
/** projectの全ページ数 (中身のないページを除く) */ count: number;
73+
/** 取得できたページ情報 */ pages: PageSummary[];
74+
}
8775

88-
/** project basic information */
89-
export interface Project {
90-
id: string;
76+
/** project information which isn't joined */
77+
export interface NotMemberProject {
78+
id: ProjectId;
9179
name: string;
9280
displayName: string;
9381
publicVisible: boolean;
@@ -98,83 +86,72 @@ export interface Project {
9886
image?: string;
9987
created: number;
10088
updated: number;
101-
isMember: boolean;
102-
plan?: string;
89+
isMember: false;
90+
}
91+
92+
/** project information which is joined */
93+
export interface MemberProject extends Omit<NotMemberProject, "isMember"> {
94+
isMember: true;
95+
plan?: string | null;
96+
users: UserInfo[];
97+
admins: UserId[];
98+
owner: UserId;
99+
trialing: boolean;
100+
trialMaxPages: number;
101+
skipPayment: boolean;
102+
uploadFileTo: "gcs";
103+
uploadImaegTo: "gyazo" | "gcs";
104+
emailAddressPatterns: string[];
105+
backuped: number | null;
106+
}
107+
108+
export interface GuestUser {
109+
isGuest: true;
110+
csrfToken: string;
103111
}
104112

105-
/** the response type of https://scrpabox.io/api/projects/:projectname */
106-
export type ProjectResponse =
107-
| NotFoundError
108-
| NotMemberError
109-
| (
110-
& Omit<Omit<Project, "isMember">, "plan">
111-
& ({ isMember: false } | {
112-
isMember: true;
113-
plan?: string | null;
114-
users: UserInfo[];
115-
admins: string[];
116-
owner: string;
117-
trialing: boolean;
118-
trialMaxPages: number;
119-
skipPayment: boolean;
120-
uploadFileTo: "gcs";
121-
uploadImaegTo: "gyazo" | "gcs";
122-
emailAddressPatterns: string[];
123-
backuped: number | null;
124-
})
125-
);
113+
export interface MemberUser extends UserInfo {
114+
isGuest: false;
115+
csrfToken: string;
116+
config: {
117+
userScript: boolean;
118+
emacsBinding: boolean;
119+
};
120+
}
126121

127122
/** the response type of https://scrapbox.io/api/users/me */
128-
export type UserResponse =
129-
| {
130-
isGuest: true;
131-
csrfToken: string;
132-
}
133-
| ({
134-
isGuest: false;
135-
csrfToken: string;
136-
config: {
137-
userScript: boolean;
138-
emacsBinding: boolean;
139-
};
140-
} & UserInfo);
123+
export type UserResponse = GuestUser | MemberUser;
141124

142125
/** the response type of https://scrapbox.io/api/pages/:projectname/search/titles */
143-
export type LinksResponse =
144-
| NotFoundError
145-
| NotMemberError
146-
| {
147-
message: "Invalid pageId";
148-
}
149-
| {
150-
/** page id */ id: string;
151-
/** page title */ title: string;
152-
/** 画像が存在するかどうか */ hasIcon: boolean;
153-
/** ページの更新日時 */ updated: number;
154-
/** ページ内のリンク */ links: string[];
155-
}[];
126+
export interface SearchedTitle {
127+
id: PageId;
128+
/** page title */ title: string;
129+
/** 画像が存在するかどうか */ hasIcon: boolean;
130+
/** ページの更新日時 */ updated: number;
131+
/** ページ内のリンク */ links: string[];
132+
}
156133

157-
export type ProjectBackup = {
134+
export interface ProjectBackup {
158135
name: string;
159136
displayName: string;
160137
exported: number;
161138
pages: {
162-
id: string;
139+
id: PageId;
163140
title: string;
164141
created: number;
165142
updated: number;
166143
lines: string[];
167144
};
168-
};
169-
export type ProjectBackupWithMetadata = {
145+
}
146+
export interface ProjectBackupWithMetadata {
170147
name: string;
171148
displayName: string;
172149
exported: number;
173150
pages: {
174-
id: string;
151+
id: PageId;
175152
title: string;
176153
created: number;
177154
updated: number;
178-
lines: { text: string; updated: number; created: number }[];
155+
lines: Omit<Line, "id" | "userId">[];
179156
};
180-
};
157+
}

base.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
/** scrapboxの行のメタデータ */
22
export interface Line {
3-
/** 行のid */ id: string;
3+
/** 行のid */ id: LineId;
44
/** 行のテキスト */ text: string;
5-
/** 一番最後に行を編集した人のid */ userId: string;
5+
/** 一番最後に行を編集した人のid */ userId: UserId;
66
/** 行の作成日時 */ created: number;
77
/** 行の最終更新日時 */ updated: number;
88
}
9+
10+
/** basic information about a page */
11+
export interface Page {
12+
/** the id of a page */ id: PageId;
13+
/** the title of a page */ title: string;
14+
/** the thumbnail URL of a page if exists
15+
*
16+
* set to `null` if not exists
17+
*/
18+
image: string | null;
19+
/** the thumbnail text of a page.
20+
* the maximum number of lines is 5.
21+
* */ descriptions: string[];
22+
/** ページの最終更新日時 */ updated: number;
23+
/** Date last visitedに使われる最終アクセス日時 */ accessed: number;
24+
}
25+
26+
/** the user id */
27+
export type UserId = string;
28+
/** the line id */
29+
export type LineId = string;
30+
/** the commit id */
31+
export type CommitId = string;
32+
/** the page id */
33+
export type PageId = string;
34+
/** the project id */
35+
export type ProjectId = string;
36+
/** the formatted string
37+
*
38+
* format rule:
39+
* - UPPER CASE -> upper_case
40+
*/
41+
export type StringLc = string;

userscript.ts

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ParsedLine } from "./userscript/blocks.ts";
2+
import { StringLc } from "./base.ts";
23

34
export type Layout =
45
| "list"
@@ -33,22 +34,8 @@ export type Scrapbox =
3334
addSeparator: () => void;
3435
removeAllItems: () => void;
3536
};
36-
addListener: (type: string, listener: () => void) => void;
37-
on: (type: string, listener: () => void) => void;
38-
removeListener: (type: string, listener: () => void) => void;
39-
off: (type: string, listener: () => void) => void;
40-
removeAllListeners: (type?: string) => void;
41-
once: (type: string, listener: () => void) => void;
42-
prependListener: (type: string, listener: () => void) => void;
43-
prependOnceListener: (type: string, listener: () => void) => void;
44-
listeners: (type: string) => (() => void)[];
45-
rawListeners: (type: string) => (() => void)[];
46-
listenerCount: (type: string) => number;
47-
emit: (type: string) => void;
48-
eventNames: () => string[];
49-
getMexListeners: () => number;
50-
setMexListeners: (length: number) => void;
5137
}
38+
& UserScriptEvents
5239
& ({
5340
Layout:
5441
| "list"
@@ -73,29 +60,61 @@ export type Scrapbox =
7360
};
7461
});
7562

76-
export type PageBrief = {
77-
exists: boolean;
78-
hasIcon?: boolean;
79-
id: string;
80-
title: string;
81-
titleLc: string;
82-
updated: number;
83-
};
63+
export interface UserScriptEvents {
64+
addListener: (type: string, listener: () => void) => void;
65+
on: (type: string, listener: () => void) => void;
66+
removeListener: (type: string, listener: () => void) => void;
67+
off: (type: string, listener: () => void) => void;
68+
removeAllListeners: (type?: string) => void;
69+
once: (type: string, listener: () => void) => void;
70+
prependListener: (type: string, listener: () => void) => void;
71+
prependOnceListener: (type: string, listener: () => void) => void;
72+
listeners: (type: string) => (() => void)[];
73+
rawListeners: (type: string) => (() => void)[];
74+
listenerCount: (type: string) => number;
75+
emit: (type: string) => void;
76+
eventNames: () => string[];
77+
getMexListeners: () => number;
78+
setMexListeners: (length: number) => void;
79+
}
80+
export interface PageBrief {
81+
/** true when the page has contents */ exists: boolean;
82+
/** whether the page contains any image */ hasIcon?: boolean;
83+
/** the page id */ id: string;
84+
/** the page title */ title: string;
85+
titleLc: StringLc;
86+
/** updated time */ updated: number;
87+
}
8488

85-
type TimeStamp = {
89+
export interface TimeStamp {
90+
/** Add a timestamp format to Scrapbox
91+
*
92+
* @param format a format of timestamp. this follow the moment.js format. You can set a function which returns any string
93+
*/
8694
addFormat: (format: string | (() => string)) => void;
95+
/** Remove all timestamp formats from Scrapbox
96+
*
97+
* These include default formats
98+
*/
8799
removeAllFormat: () => void;
88-
};
100+
}
89101

90-
type AddItemProps = {
91-
title: string | (() => string);
102+
export interface AddItemProps {
103+
/** the title of a menu item */ title: string | (() => string);
104+
/** the URL of an image which views on the left of the title */
92105
image?: string;
106+
/** the event listener which is executed when the menu item is clicked */
93107
onClick: () => void;
94-
};
95-
type PageMenu = {
108+
}
109+
export interface PageMenu {
110+
/** Add a menu item to a particular Page Menu button
111+
*
112+
* @param props information used for a menu item
113+
*/
96114
addItem: (
97115
props: AddItemProps,
98116
) => void;
117+
/** Add a separator to a particular Page Menu button */
99118
addSeparator: () => void;
100119
removeAllItems: () => void;
101120
menuName: string;
@@ -109,8 +128,9 @@ type PageMenu = {
109128
items: (AddItemProps & { separator: boolean })[];
110129
}
111130
>;
112-
};
131+
}
113132

133+
/** built-in UserScript events */
114134
export type eventName =
115135
| "lines:changed"
116136
| "page:changed"

0 commit comments

Comments
 (0)