Skip to content

Commit 59ce50b

Browse files
committed
feat(REST API): Replace my own Result<T, E> with option-t`'s
Also delete`is.ts` and use `unknownutil` `Result.value` has been changed into `Result.val`
1 parent 8cf7b83 commit 59ce50b

38 files changed

+1237
-655
lines changed

browser/dom/edit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { press } from "./press.ts";
33
import { getLineCount } from "./node.ts";
44
import { range } from "../../range.ts";
55
import { textInput } from "./dom.ts";
6-
import { isArray, isNumber, isString } from "../../is.ts";
6+
import { isArray, isNumber, isString } from "../../deps/unknownutil.ts";
77
import { sleep } from "../../sleep.ts";
88

99
export const undo = (count = 1): void => {

browser/dom/node.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference no-default-lib="true"/>
22
/// <reference lib="esnext"/>
33
/// <reference lib="dom" />
4-
import { isNone, isNumber, isString } from "../../is.ts";
4+
import { isNumber, isString, isUndefined } from "../../deps/unknownutil.ts";
55
import { ensureArray } from "../../ensure.ts";
66
import { getCachedLines } from "./getCachedLines.ts";
77
import { takeInternalLines } from "./takeInternalLines.ts";
@@ -18,7 +18,7 @@ import * as Text from "../../text.ts";
1818
export const getLineId = <T extends HTMLElement>(
1919
value?: number | string | T,
2020
): string | undefined => {
21-
if (isNone(value)) return undefined;
21+
if (isUndefined(value)) return undefined;
2222

2323
// 行番号のとき
2424
if (isNumber(value)) return getBaseLine(value)?.id;
@@ -43,7 +43,7 @@ export const getLineId = <T extends HTMLElement>(
4343
export const getLineNo = <T extends HTMLElement>(
4444
value?: number | string | T,
4545
): number | undefined => {
46-
if (isNone(value)) return undefined;
46+
if (isUndefined(value)) return undefined;
4747

4848
// 行番号のとき
4949
if (isNumber(value)) return value;
@@ -55,7 +55,7 @@ export const getLineNo = <T extends HTMLElement>(
5555
export const getLine = <T extends HTMLElement>(
5656
value?: number | string | T,
5757
): Line | undefined => {
58-
if (isNone(value)) return undefined;
58+
if (isUndefined(value)) return undefined;
5959

6060
// 行番号のとき
6161
if (isNumber(value)) return getLines()[value];
@@ -67,7 +67,7 @@ export const getLine = <T extends HTMLElement>(
6767
export const getBaseLine = <T extends HTMLElement>(
6868
value?: number | string | T,
6969
): BaseLine | undefined => {
70-
if (isNone(value)) return undefined;
70+
if (isUndefined(value)) return undefined;
7171

7272
// 行番号のとき
7373
if (isNumber(value)) return takeInternalLines()[value];
@@ -82,9 +82,9 @@ export const getLineDOM = <T extends HTMLElement>(
8282
if (isLineDOM(value)) return value;
8383

8484
const id = getLineId(value);
85-
if (isNone(id)) return id;
85+
if (isUndefined(id)) return id;
8686
const line = document.getElementById(`L${id}`);
87-
if (isNone(line)) return undefined;
87+
if (isUndefined(line)) return undefined;
8888
return line as HTMLDivElement;
8989
};
9090
export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
@@ -101,7 +101,7 @@ export const getLines = (): readonly Line[] => {
101101
export const getText = <T extends HTMLElement>(
102102
value?: number | string | T,
103103
): string | undefined => {
104-
if (isNone(value)) return undefined;
104+
if (isUndefined(value)) return undefined;
105105

106106
// 数字と文字列は行として扱う
107107
if (isNumber(value) || isString(value)) return getBaseLine(value)?.text;
@@ -121,7 +121,7 @@ export const getText = <T extends HTMLElement>(
121121
//中に含まれている文字の列番号を全て取得し、それに対応する文字列を返す
122122
const chars = [] as number[];
123123
const line = getBaseLine(value);
124-
if (isNone(line)) return;
124+
if (isUndefined(line)) return;
125125
for (const dom of getChars(value)) {
126126
chars.push(getIndex(dom));
127127
}
@@ -130,30 +130,30 @@ export const getText = <T extends HTMLElement>(
130130

131131
export const getExternalLink = (dom: HTMLElement): HTMLElement | undefined => {
132132
const link = dom.closest(".link");
133-
if (isNone(link)) return undefined;
133+
if (isUndefined(link)) return undefined;
134134
return link as HTMLElement;
135135
};
136136
export const getInternalLink = (dom: HTMLElement): HTMLElement | undefined => {
137137
const link = dom.closest(".page-link");
138-
if (isNone(link)) return undefined;
138+
if (isUndefined(link)) return undefined;
139139
return link as HTMLElement;
140140
};
141141
export const getLink = (dom: HTMLElement) => {
142142
const link = dom.closest(".link, .page-link");
143-
if (isNone(link)) return undefined;
143+
if (isUndefined(link)) return undefined;
144144
return link as HTMLElement;
145145
};
146146

147147
export const getFormula = (dom: HTMLElement): HTMLElement | undefined => {
148148
const formula = dom.closest(".formula");
149-
if (isNone(formula)) return undefined;
149+
if (isUndefined(formula)) return undefined;
150150
return formula as HTMLElement;
151151
};
152152
export const getNextLine = <T extends HTMLElement>(
153153
value?: number | string | T,
154154
): Line | undefined => {
155155
const index = getLineNo(value);
156-
if (isNone(index)) return undefined;
156+
if (isUndefined(index)) return undefined;
157157

158158
return getLine(index + 1);
159159
};
@@ -162,26 +162,26 @@ export const getPrevLine = <T extends HTMLElement>(
162162
value?: number | string | T,
163163
): Line | undefined => {
164164
const index = getLineNo(value);
165-
if (isNone(index)) return undefined;
165+
if (isUndefined(index)) return undefined;
166166

167167
return getLine(index - 1);
168168
};
169169

170170
export const getHeadLineDOM = (): HTMLDivElement | undefined => {
171171
const line = lines()?.firstElementChild;
172-
if (isNone(line)) return undefined;
172+
if (isUndefined(line)) return undefined;
173173
return line as HTMLDivElement;
174174
};
175175
export const getTailLineDOM = (): HTMLDivElement | undefined => {
176176
const line = lines()?.lastElementChild;
177-
if (isNone(line)) return undefined;
177+
if (isUndefined(line)) return undefined;
178178
return line as HTMLDivElement;
179179
};
180180
export const getIndentCount = <T extends HTMLElement>(
181181
value?: number | string | T,
182182
): number | undefined => {
183183
const text = getText(value);
184-
if (isNone(text)) return undefined;
184+
if (isUndefined(text)) return undefined;
185185
return Text.getIndentCount(text);
186186
};
187187
/** 指定した行の配下にある行の数を返す
@@ -192,7 +192,7 @@ export const getIndentLineCount = <T extends HTMLElement>(
192192
value?: number | string | T,
193193
): number | undefined => {
194194
const index = getLineNo(value);
195-
if (isNone(index)) return;
195+
if (isUndefined(index)) return;
196196
return Text.getIndentLineCount(index, getLines());
197197
};
198198

@@ -213,7 +213,7 @@ export const getIndex = (dom: HTMLSpanElement): number => {
213213
if (!isCharDOM(dom)) throw Error("A char DOM is required.");
214214

215215
const index = dom.className.match(/c-(\d+)/)?.[1];
216-
if (isNone(index)) throw Error('.char-index must have ".c-{\\d}"');
216+
if (isUndefined(index)) throw Error('.char-index must have ".c-{\\d}"');
217217
return parseInt(index);
218218
};
219219
export const getHeadCharDOM = (
@@ -245,7 +245,7 @@ export const getDOMFromPoint = (
245245
const char = targets.find((target) => isCharDOM(target));
246246
const line = targets.find((target) => isLineDOM(target));
247247
return {
248-
char: isNone(char) ? undefined : char as HTMLSpanElement,
249-
line: isNone(line) ? undefined : line as HTMLDivElement,
248+
char: isUndefined(char) ? undefined : char as HTMLSpanElement,
249+
line: isUndefined(line) ? undefined : line as HTMLDivElement,
250250
};
251251
};

browser/websocket/deletePage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { push, PushOptions, RetryError } from "./push.ts";
2-
import { Result } from "../../rest/util.ts";
1+
import { Result } from "../../deps/option-t.ts";
2+
import { push, PushError, PushOptions } from "./push.ts";
33

44
export type DeletePageOptions = PushOptions;
55

@@ -13,7 +13,7 @@ export const deletePage = (
1313
project: string,
1414
title: string,
1515
options?: DeletePageOptions,
16-
): Promise<Result<string, RetryError>> =>
16+
): Promise<Result<string, PushError>> =>
1717
push(
1818
project,
1919
title,

browser/websocket/id.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,3 @@
1-
import { getProject } from "../../rest/project.ts";
2-
import { getProfile } from "../../rest/profile.ts";
3-
4-
/** cached user ID */
5-
let userId: string | undefined;
6-
export const getUserId = async (): Promise<string> => {
7-
if (userId !== undefined) return userId;
8-
9-
const user = await getProfile();
10-
if (user.isGuest) {
11-
throw new Error("this script can only be executed by Logged in users");
12-
}
13-
userId = user.id;
14-
return userId;
15-
};
16-
17-
/** cached pairs of project name and project id */
18-
const projectMap = new Map<string, string>();
19-
export const getProjectId = async (project: string): Promise<string> => {
20-
const cachedId = projectMap.get(project);
21-
if (cachedId !== undefined) return cachedId;
22-
23-
const result = await getProject(project);
24-
if (!result.ok) {
25-
const { name, message } = result.value;
26-
throw new Error(`${name} ${message}`);
27-
}
28-
const { id } = result.value;
29-
projectMap.set(project, id);
30-
return id;
31-
};
32-
331
const zero = (n: string) => n.padStart(8, "0");
342

353
export const createNewLineId = (userId: string): string => {

browser/websocket/listen.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
import { createOk, isErr, Result, unwrapOk } from "../../deps/option-t.ts";
2+
import {
3+
NotFoundError,
4+
NotLoggedInError,
5+
NotMemberError,
6+
} from "../../deps/scrapbox-rest.ts";
17
import {
28
ProjectUpdatesStreamCommit,
39
ProjectUpdatesStreamEvent,
410
Socket,
511
socketIO,
612
wrap,
713
} from "../../deps/socket.ts";
14+
import { HTTPError } from "../../rest/responseIntoResult.ts";
15+
import { AbortError, NetworkError } from "../../rest/robustFetch.ts";
16+
import { getProjectId } from "./pull.ts";
817
import { connect, disconnect } from "./socket.ts";
9-
import { getProjectId } from "./id.ts";
1018
export type {
1119
ProjectUpdatesStreamCommit,
1220
ProjectUpdatesStreamEvent,
@@ -27,11 +35,24 @@ export async function* listenStream(
2735
events: ["commit" | "event", ...("commit" | "event")[]],
2836
options?: ListenStreamOptions,
2937
): AsyncGenerator<
30-
ProjectUpdatesStreamEvent | ProjectUpdatesStreamCommit,
38+
Result<
39+
ProjectUpdatesStreamEvent | ProjectUpdatesStreamCommit,
40+
| NotFoundError
41+
| NotLoggedInError
42+
| NotMemberError
43+
| NetworkError
44+
| AbortError
45+
| HTTPError
46+
>,
3147
void,
3248
unknown
3349
> {
34-
const projectId = await getProjectId(project);
50+
const result = await getProjectId(project);
51+
if (isErr(result)) {
52+
yield result;
53+
return;
54+
}
55+
const projectId = unwrapOk(result);
3556

3657
const injectedSocket = options?.socket;
3758
const socket = injectedSocket ?? await socketIO();
@@ -45,9 +66,13 @@ export async function* listenStream(
4566
data: { projectId, pageId: null, projectUpdatesStream: true },
4667
});
4768

48-
yield* response(
49-
...events.map((event) => `projectUpdatesStream:${event}` as const),
50-
);
69+
for await (
70+
const streamEvent of response(
71+
...events.map((event) => `projectUpdatesStream:${event}` as const),
72+
)
73+
) {
74+
yield createOk(streamEvent);
75+
}
5176
} finally {
5277
if (!injectedSocket) await disconnect(socket);
5378
}

browser/websocket/patch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Change, DeletePageChange, PinChange } from "../../deps/socket.ts";
22
import { makeChanges } from "./makeChanges.ts";
33
import { Line, Page } from "../../deps/scrapbox-rest.ts";
4-
import { push, PushOptions, RetryError } from "./push.ts";
4+
import { push, PushError, PushOptions } from "./push.ts";
55
import { suggestUnDupTitle } from "./suggestUnDupTitle.ts";
6-
import { Result } from "../../rest/util.ts";
6+
import { Result } from "../../deps/option-t.ts";
77

88
export type PatchOptions = PushOptions;
99

@@ -32,7 +32,7 @@ export const patch = (
3232
metadata: PatchMetadata,
3333
) => string[] | undefined | Promise<string[] | undefined>,
3434
options?: PatchOptions,
35-
): Promise<Result<string, RetryError>> =>
35+
): Promise<Result<string, PushError>> =>
3636
push(
3737
project,
3838
title,

browser/websocket/pin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { Result } from "../../deps/option-t.ts";
12
import { Change, Socket } from "../../deps/socket.ts";
2-
import { push, PushOptions, RetryError } from "./push.ts";
3-
import { Result } from "../../rest/util.ts";
3+
import { push, PushError, PushOptions } from "./push.ts";
44

55
export interface PinOptions extends PushOptions {
66
/** ピン留め対象のページが存在しないときの振る舞いを変えるoption
@@ -22,7 +22,7 @@ export const pin = (
2222
project: string,
2323
title: string,
2424
options?: PinOptions,
25-
): Promise<Result<string, RetryError>> =>
25+
): Promise<Result<string, PushError>> =>
2626
push(
2727
project,
2828
title,
@@ -51,7 +51,7 @@ export const unpin = (
5151
project: string,
5252
title: string,
5353
options: UnPinOptions,
54-
): Promise<Result<string, RetryError>> =>
54+
): Promise<Result<string, PushError>> =>
5555
push(
5656
project,
5757
title,

0 commit comments

Comments
 (0)