note-blog-kit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +225 -2
- package/dist/cli/index.js +32 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +75 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.js +1 -0
- package/dist/next/index.cjs +2 -0
- package/dist/next/index.d.cts +2 -0
- package/dist/next/index.d.ts +2 -0
- package/dist/next/index.js +1 -0
- package/dist/react/index.cjs +103 -0
- package/dist/react/index.d.cts +111 -0
- package/dist/react/index.d.ts +111 -0
- package/dist/react/index.js +99 -0
- package/dist/server/index.cjs +324 -0
- package/dist/server/index.d.cts +160 -0
- package/dist/server/index.d.ts +160 -0
- package/dist/server/index.js +313 -0
- package/dist/styles.css +218 -0
- package/package.json +109 -4
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { NoteArticlePage, NoteArticleSummary } from '../index.cjs';
|
|
2
|
+
export { NoteArticle, NoteAuthor, NoteEmbed, NoteEyecatch, NoteSource, NoteTocItem } from '../index.cjs';
|
|
3
|
+
|
|
4
|
+
/** エラーの種類を表す安定した識別子 */
|
|
5
|
+
type NoteErrorCode = 'NOTE_NOT_FOUND' | 'NOTE_HTTP_ERROR' | 'NOTE_NETWORK_ERROR' | 'NOTE_TIMEOUT' | 'NOTE_SCHEMA_ERROR';
|
|
6
|
+
/** エラーのコンストラクタに渡す共通のオプション(ライブラリ内部用) */
|
|
7
|
+
interface NoteErrorOptions {
|
|
8
|
+
/** 元になったエラー */
|
|
9
|
+
cause?: unknown;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* note-blog-kit/server が投げるエラーの基底クラス。
|
|
13
|
+
*
|
|
14
|
+
* 判定は `instanceof` ではなく {@link isNoteError} と `code` で行うことを推奨する
|
|
15
|
+
* (ESM と CJS が混在したり、パッケージが重複して読み込まれたりすると `instanceof` は一致しないことがある)。
|
|
16
|
+
* `isNoteError(error) && error.code === 'NOTE_HTTP_ERROR'` のように書くと `error.status` まで型が絞られる。
|
|
17
|
+
*
|
|
18
|
+
* コンストラクタはライブラリ内部で使うもので、利用者が作ることは想定していない。
|
|
19
|
+
*/
|
|
20
|
+
declare class NoteError extends Error {
|
|
21
|
+
name: string;
|
|
22
|
+
readonly code: NoteErrorCode;
|
|
23
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
24
|
+
code: NoteErrorCode;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** ユーザーや記事が見つからない(HTTP 404) */
|
|
28
|
+
declare class NoteNotFoundError extends NoteError {
|
|
29
|
+
name: string;
|
|
30
|
+
readonly code: "NOTE_NOT_FOUND";
|
|
31
|
+
/** リクエストした URL */
|
|
32
|
+
readonly url: string;
|
|
33
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
34
|
+
url: string;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/** 404 以外の 2xx でない HTTP ステータス */
|
|
38
|
+
declare class NoteHttpError extends NoteError {
|
|
39
|
+
name: string;
|
|
40
|
+
readonly code: "NOTE_HTTP_ERROR";
|
|
41
|
+
readonly status: number;
|
|
42
|
+
/** リクエストした URL */
|
|
43
|
+
readonly url: string;
|
|
44
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
45
|
+
status: number;
|
|
46
|
+
url: string;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** 通信そのものの失敗(DNS・接続断・リダイレクトの拒否など) */
|
|
50
|
+
declare class NoteNetworkError extends NoteError {
|
|
51
|
+
name: string;
|
|
52
|
+
readonly code: "NOTE_NETWORK_ERROR";
|
|
53
|
+
constructor(message: string, options?: NoteErrorOptions);
|
|
54
|
+
}
|
|
55
|
+
/** 応答がタイムアウトまでに返らなかった */
|
|
56
|
+
declare class NoteTimeoutError extends NoteError {
|
|
57
|
+
name: string;
|
|
58
|
+
readonly code: "NOTE_TIMEOUT";
|
|
59
|
+
constructor(message: string, options?: NoteErrorOptions);
|
|
60
|
+
}
|
|
61
|
+
/** レスポンスのどこが想定と違ったか(デバッグ用。文言は互換性の対象外) */
|
|
62
|
+
interface NoteSchemaIssue {
|
|
63
|
+
/** `data.contents.0.key` のようなドット区切りのパス */
|
|
64
|
+
readonly path: string;
|
|
65
|
+
readonly message: string;
|
|
66
|
+
}
|
|
67
|
+
/** note のレスポンスの形が想定と違う(API の仕様変更など) */
|
|
68
|
+
declare class NoteSchemaError extends NoteError {
|
|
69
|
+
name: string;
|
|
70
|
+
readonly code: "NOTE_SCHEMA_ERROR";
|
|
71
|
+
readonly issues: readonly NoteSchemaIssue[];
|
|
72
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
73
|
+
issues: readonly NoteSchemaIssue[];
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** note-blog-kit/server が投げるエラーのいずれか。`code` で判別できる */
|
|
77
|
+
type AnyNoteError = NoteNotFoundError | NoteHttpError | NoteNetworkError | NoteTimeoutError | NoteSchemaError;
|
|
78
|
+
/**
|
|
79
|
+
* note-blog-kit のエラーかどうかを `name`・`message`・`code` で判定する。
|
|
80
|
+
* パッケージの重複や別の realm(vm など)由来で `instanceof` が一致しない場合でも判定できる。
|
|
81
|
+
*/
|
|
82
|
+
declare function isNoteError(value: unknown): value is AnyNoteError;
|
|
83
|
+
|
|
84
|
+
/** {@link listArticles} のオプション */
|
|
85
|
+
interface ListArticlesOptions {
|
|
86
|
+
/** note のユーザー名(note.com/{user} の部分)。英数字・_・- のみ */
|
|
87
|
+
user: string;
|
|
88
|
+
/** ページ番号(1 始まり)。既定は 1。1ページは6件 */
|
|
89
|
+
page?: number;
|
|
90
|
+
/**
|
|
91
|
+
* fetch の差し替え(テストやキャッシュ層のため)。既定は globalThis.fetch。
|
|
92
|
+
* 差し替えた fetch でも Cookie・認証ヘッダーを付けないこと(note の公開情報だけを取得する前提)
|
|
93
|
+
*/
|
|
94
|
+
fetch?: typeof globalThis.fetch;
|
|
95
|
+
/** 中断用のシグナル。間隔の制限で待っている間も含め、中断したらすぐにその理由をそのまま投げる */
|
|
96
|
+
signal?: AbortSignal;
|
|
97
|
+
/** タイムアウト(ミリ秒)。1〜2147483647。既定は 10000 */
|
|
98
|
+
timeoutMs?: number;
|
|
99
|
+
/**
|
|
100
|
+
* 連続リクエストの最小間隔(ミリ秒)。0〜2147483647。既定は 1000。
|
|
101
|
+
* 送信順はプロセス全体(ESM・CJS の併用やパッケージの重複を含む)で1本の列にまとめられ、
|
|
102
|
+
* この呼び出しは「直前の送信から minIntervalMs 経つまで」待ってから送られる。
|
|
103
|
+
*/
|
|
104
|
+
minIntervalMs?: number;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 記事一覧の1ページを取得する(サーバー専用。note は CORS に対応していない)。
|
|
108
|
+
*
|
|
109
|
+
* @throws {TypeError} user・page が不正なとき
|
|
110
|
+
* @throws {NoteNotFoundError} ユーザーが見つからないとき
|
|
111
|
+
* @throws {NoteHttpError} その他の 2xx でないステータス
|
|
112
|
+
* @throws {NoteNetworkError} 通信に失敗したとき
|
|
113
|
+
* @throws {NoteTimeoutError} タイムアウトしたとき
|
|
114
|
+
* @throws {NoteSchemaError} レスポンスの形が想定と違うとき
|
|
115
|
+
*/
|
|
116
|
+
declare function listArticles(options: ListArticlesOptions): Promise<NoteArticlePage>;
|
|
117
|
+
|
|
118
|
+
/** {@link iterateArticles}・{@link listAllArticles} のオプション。`listArticles` のオプションから `page` を除いたもの */
|
|
119
|
+
interface IterateArticlesOptions extends Omit<ListArticlesOptions, 'page'> {
|
|
120
|
+
/** 取得する最大ページ数(1 以上の整数)。既定は 20(1ページ6件なので120件) */
|
|
121
|
+
maxPages?: number;
|
|
122
|
+
}
|
|
123
|
+
/** 全件取得の結果のうち、記事以外の情報 */
|
|
124
|
+
interface IterateArticlesResult {
|
|
125
|
+
/** maxPages に達して打ち切ったとき true(続きのページがある可能性がある) */
|
|
126
|
+
truncated: boolean;
|
|
127
|
+
/** 最後に取得したページの totalCount */
|
|
128
|
+
totalCount: number | null;
|
|
129
|
+
/** 実際に取得したページ数 */
|
|
130
|
+
pages: number;
|
|
131
|
+
}
|
|
132
|
+
/** {@link listAllArticles} の結果 */
|
|
133
|
+
interface ListAllArticlesResult extends IterateArticlesResult {
|
|
134
|
+
/** 取得した記事(新しい順、key で重複を除いたもの) */
|
|
135
|
+
articles: NoteArticleSummary[];
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 記事を新しい順に1件ずつ返す(サーバー専用)。最終ページか maxPages に達したら終わる。
|
|
139
|
+
* ページの取得には `listArticles` を使うため、取得間隔の制限・タイムアウト・中断もそのまま効く。
|
|
140
|
+
* 取得中にページがずれて同じ記事が再び出た場合は、`key` で重複を除く。
|
|
141
|
+
*
|
|
142
|
+
* 打ち切りの有無などはジェネレーターの戻り値(`IterateArticlesResult`)で返す。
|
|
143
|
+
* `for await` ではこの戻り値を受け取れないため、必要なら {@link listAllArticles} を使うか `next()` を手で回す。
|
|
144
|
+
*
|
|
145
|
+
* @throws {TypeError} maxPages などが不正なとき(呼び出した時点ではなく、最初の `next()` で投げる)
|
|
146
|
+
* @throws 途中のページの取得で起きたエラー(それまでに返した記事はそのまま)
|
|
147
|
+
*/
|
|
148
|
+
declare function iterateArticles(options: IterateArticlesOptions): AsyncGenerator<NoteArticleSummary, IterateArticlesResult, undefined>;
|
|
149
|
+
/**
|
|
150
|
+
* 全件(maxPages まで。既定は 20 ページ)を配列で返す(サーバー専用)。
|
|
151
|
+
* `truncated` が true なら maxPages で打ち切っており、続きのページがある可能性がある。
|
|
152
|
+
*
|
|
153
|
+
* @throws {TypeError} maxPages などが不正なとき
|
|
154
|
+
* @throws 途中のページの取得で起きたエラー。それまでに集めた記事は返らない
|
|
155
|
+
* (途中までの記事も使いたい場合は {@link iterateArticles} を使う)
|
|
156
|
+
*/
|
|
157
|
+
declare function listAllArticles(options: IterateArticlesOptions): Promise<ListAllArticlesResult>;
|
|
158
|
+
|
|
159
|
+
export { NoteArticlePage, NoteArticleSummary, NoteError, NoteHttpError, NoteNetworkError, NoteNotFoundError, NoteSchemaError, NoteTimeoutError, isNoteError, iterateArticles, listAllArticles, listArticles };
|
|
160
|
+
export type { AnyNoteError, IterateArticlesOptions, IterateArticlesResult, ListAllArticlesResult, ListArticlesOptions, NoteErrorCode, NoteErrorOptions, NoteSchemaIssue };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { NoteArticlePage, NoteArticleSummary } from '../index.js';
|
|
2
|
+
export { NoteArticle, NoteAuthor, NoteEmbed, NoteEyecatch, NoteSource, NoteTocItem } from '../index.js';
|
|
3
|
+
|
|
4
|
+
/** エラーの種類を表す安定した識別子 */
|
|
5
|
+
type NoteErrorCode = 'NOTE_NOT_FOUND' | 'NOTE_HTTP_ERROR' | 'NOTE_NETWORK_ERROR' | 'NOTE_TIMEOUT' | 'NOTE_SCHEMA_ERROR';
|
|
6
|
+
/** エラーのコンストラクタに渡す共通のオプション(ライブラリ内部用) */
|
|
7
|
+
interface NoteErrorOptions {
|
|
8
|
+
/** 元になったエラー */
|
|
9
|
+
cause?: unknown;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* note-blog-kit/server が投げるエラーの基底クラス。
|
|
13
|
+
*
|
|
14
|
+
* 判定は `instanceof` ではなく {@link isNoteError} と `code` で行うことを推奨する
|
|
15
|
+
* (ESM と CJS が混在したり、パッケージが重複して読み込まれたりすると `instanceof` は一致しないことがある)。
|
|
16
|
+
* `isNoteError(error) && error.code === 'NOTE_HTTP_ERROR'` のように書くと `error.status` まで型が絞られる。
|
|
17
|
+
*
|
|
18
|
+
* コンストラクタはライブラリ内部で使うもので、利用者が作ることは想定していない。
|
|
19
|
+
*/
|
|
20
|
+
declare class NoteError extends Error {
|
|
21
|
+
name: string;
|
|
22
|
+
readonly code: NoteErrorCode;
|
|
23
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
24
|
+
code: NoteErrorCode;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** ユーザーや記事が見つからない(HTTP 404) */
|
|
28
|
+
declare class NoteNotFoundError extends NoteError {
|
|
29
|
+
name: string;
|
|
30
|
+
readonly code: "NOTE_NOT_FOUND";
|
|
31
|
+
/** リクエストした URL */
|
|
32
|
+
readonly url: string;
|
|
33
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
34
|
+
url: string;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/** 404 以外の 2xx でない HTTP ステータス */
|
|
38
|
+
declare class NoteHttpError extends NoteError {
|
|
39
|
+
name: string;
|
|
40
|
+
readonly code: "NOTE_HTTP_ERROR";
|
|
41
|
+
readonly status: number;
|
|
42
|
+
/** リクエストした URL */
|
|
43
|
+
readonly url: string;
|
|
44
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
45
|
+
status: number;
|
|
46
|
+
url: string;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** 通信そのものの失敗(DNS・接続断・リダイレクトの拒否など) */
|
|
50
|
+
declare class NoteNetworkError extends NoteError {
|
|
51
|
+
name: string;
|
|
52
|
+
readonly code: "NOTE_NETWORK_ERROR";
|
|
53
|
+
constructor(message: string, options?: NoteErrorOptions);
|
|
54
|
+
}
|
|
55
|
+
/** 応答がタイムアウトまでに返らなかった */
|
|
56
|
+
declare class NoteTimeoutError extends NoteError {
|
|
57
|
+
name: string;
|
|
58
|
+
readonly code: "NOTE_TIMEOUT";
|
|
59
|
+
constructor(message: string, options?: NoteErrorOptions);
|
|
60
|
+
}
|
|
61
|
+
/** レスポンスのどこが想定と違ったか(デバッグ用。文言は互換性の対象外) */
|
|
62
|
+
interface NoteSchemaIssue {
|
|
63
|
+
/** `data.contents.0.key` のようなドット区切りのパス */
|
|
64
|
+
readonly path: string;
|
|
65
|
+
readonly message: string;
|
|
66
|
+
}
|
|
67
|
+
/** note のレスポンスの形が想定と違う(API の仕様変更など) */
|
|
68
|
+
declare class NoteSchemaError extends NoteError {
|
|
69
|
+
name: string;
|
|
70
|
+
readonly code: "NOTE_SCHEMA_ERROR";
|
|
71
|
+
readonly issues: readonly NoteSchemaIssue[];
|
|
72
|
+
constructor(message: string, options: NoteErrorOptions & {
|
|
73
|
+
issues: readonly NoteSchemaIssue[];
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** note-blog-kit/server が投げるエラーのいずれか。`code` で判別できる */
|
|
77
|
+
type AnyNoteError = NoteNotFoundError | NoteHttpError | NoteNetworkError | NoteTimeoutError | NoteSchemaError;
|
|
78
|
+
/**
|
|
79
|
+
* note-blog-kit のエラーかどうかを `name`・`message`・`code` で判定する。
|
|
80
|
+
* パッケージの重複や別の realm(vm など)由来で `instanceof` が一致しない場合でも判定できる。
|
|
81
|
+
*/
|
|
82
|
+
declare function isNoteError(value: unknown): value is AnyNoteError;
|
|
83
|
+
|
|
84
|
+
/** {@link listArticles} のオプション */
|
|
85
|
+
interface ListArticlesOptions {
|
|
86
|
+
/** note のユーザー名(note.com/{user} の部分)。英数字・_・- のみ */
|
|
87
|
+
user: string;
|
|
88
|
+
/** ページ番号(1 始まり)。既定は 1。1ページは6件 */
|
|
89
|
+
page?: number;
|
|
90
|
+
/**
|
|
91
|
+
* fetch の差し替え(テストやキャッシュ層のため)。既定は globalThis.fetch。
|
|
92
|
+
* 差し替えた fetch でも Cookie・認証ヘッダーを付けないこと(note の公開情報だけを取得する前提)
|
|
93
|
+
*/
|
|
94
|
+
fetch?: typeof globalThis.fetch;
|
|
95
|
+
/** 中断用のシグナル。間隔の制限で待っている間も含め、中断したらすぐにその理由をそのまま投げる */
|
|
96
|
+
signal?: AbortSignal;
|
|
97
|
+
/** タイムアウト(ミリ秒)。1〜2147483647。既定は 10000 */
|
|
98
|
+
timeoutMs?: number;
|
|
99
|
+
/**
|
|
100
|
+
* 連続リクエストの最小間隔(ミリ秒)。0〜2147483647。既定は 1000。
|
|
101
|
+
* 送信順はプロセス全体(ESM・CJS の併用やパッケージの重複を含む)で1本の列にまとめられ、
|
|
102
|
+
* この呼び出しは「直前の送信から minIntervalMs 経つまで」待ってから送られる。
|
|
103
|
+
*/
|
|
104
|
+
minIntervalMs?: number;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 記事一覧の1ページを取得する(サーバー専用。note は CORS に対応していない)。
|
|
108
|
+
*
|
|
109
|
+
* @throws {TypeError} user・page が不正なとき
|
|
110
|
+
* @throws {NoteNotFoundError} ユーザーが見つからないとき
|
|
111
|
+
* @throws {NoteHttpError} その他の 2xx でないステータス
|
|
112
|
+
* @throws {NoteNetworkError} 通信に失敗したとき
|
|
113
|
+
* @throws {NoteTimeoutError} タイムアウトしたとき
|
|
114
|
+
* @throws {NoteSchemaError} レスポンスの形が想定と違うとき
|
|
115
|
+
*/
|
|
116
|
+
declare function listArticles(options: ListArticlesOptions): Promise<NoteArticlePage>;
|
|
117
|
+
|
|
118
|
+
/** {@link iterateArticles}・{@link listAllArticles} のオプション。`listArticles` のオプションから `page` を除いたもの */
|
|
119
|
+
interface IterateArticlesOptions extends Omit<ListArticlesOptions, 'page'> {
|
|
120
|
+
/** 取得する最大ページ数(1 以上の整数)。既定は 20(1ページ6件なので120件) */
|
|
121
|
+
maxPages?: number;
|
|
122
|
+
}
|
|
123
|
+
/** 全件取得の結果のうち、記事以外の情報 */
|
|
124
|
+
interface IterateArticlesResult {
|
|
125
|
+
/** maxPages に達して打ち切ったとき true(続きのページがある可能性がある) */
|
|
126
|
+
truncated: boolean;
|
|
127
|
+
/** 最後に取得したページの totalCount */
|
|
128
|
+
totalCount: number | null;
|
|
129
|
+
/** 実際に取得したページ数 */
|
|
130
|
+
pages: number;
|
|
131
|
+
}
|
|
132
|
+
/** {@link listAllArticles} の結果 */
|
|
133
|
+
interface ListAllArticlesResult extends IterateArticlesResult {
|
|
134
|
+
/** 取得した記事(新しい順、key で重複を除いたもの) */
|
|
135
|
+
articles: NoteArticleSummary[];
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 記事を新しい順に1件ずつ返す(サーバー専用)。最終ページか maxPages に達したら終わる。
|
|
139
|
+
* ページの取得には `listArticles` を使うため、取得間隔の制限・タイムアウト・中断もそのまま効く。
|
|
140
|
+
* 取得中にページがずれて同じ記事が再び出た場合は、`key` で重複を除く。
|
|
141
|
+
*
|
|
142
|
+
* 打ち切りの有無などはジェネレーターの戻り値(`IterateArticlesResult`)で返す。
|
|
143
|
+
* `for await` ではこの戻り値を受け取れないため、必要なら {@link listAllArticles} を使うか `next()` を手で回す。
|
|
144
|
+
*
|
|
145
|
+
* @throws {TypeError} maxPages などが不正なとき(呼び出した時点ではなく、最初の `next()` で投げる)
|
|
146
|
+
* @throws 途中のページの取得で起きたエラー(それまでに返した記事はそのまま)
|
|
147
|
+
*/
|
|
148
|
+
declare function iterateArticles(options: IterateArticlesOptions): AsyncGenerator<NoteArticleSummary, IterateArticlesResult, undefined>;
|
|
149
|
+
/**
|
|
150
|
+
* 全件(maxPages まで。既定は 20 ページ)を配列で返す(サーバー専用)。
|
|
151
|
+
* `truncated` が true なら maxPages で打ち切っており、続きのページがある可能性がある。
|
|
152
|
+
*
|
|
153
|
+
* @throws {TypeError} maxPages などが不正なとき
|
|
154
|
+
* @throws 途中のページの取得で起きたエラー。それまでに集めた記事は返らない
|
|
155
|
+
* (途中までの記事も使いたい場合は {@link iterateArticles} を使う)
|
|
156
|
+
*/
|
|
157
|
+
declare function listAllArticles(options: IterateArticlesOptions): Promise<ListAllArticlesResult>;
|
|
158
|
+
|
|
159
|
+
export { NoteArticlePage, NoteArticleSummary, NoteError, NoteHttpError, NoteNetworkError, NoteNotFoundError, NoteSchemaError, NoteTimeoutError, isNoteError, iterateArticles, listAllArticles, listArticles };
|
|
160
|
+
export type { AnyNoteError, IterateArticlesOptions, IterateArticlesResult, ListAllArticlesResult, ListArticlesOptions, NoteErrorCode, NoteErrorOptions, NoteSchemaIssue };
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// src/server/errors.ts
|
|
4
|
+
var NOTE_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
5
|
+
"NOTE_NOT_FOUND",
|
|
6
|
+
"NOTE_HTTP_ERROR",
|
|
7
|
+
"NOTE_NETWORK_ERROR",
|
|
8
|
+
"NOTE_TIMEOUT",
|
|
9
|
+
"NOTE_SCHEMA_ERROR"
|
|
10
|
+
]);
|
|
11
|
+
function toErrorOptions(options) {
|
|
12
|
+
return options !== void 0 && "cause" in options ? { cause: options.cause } : void 0;
|
|
13
|
+
}
|
|
14
|
+
var NoteError = class extends Error {
|
|
15
|
+
// 利用者側のバンドラがクラス名を圧縮しても変わらないよう、name は文字列で持つ
|
|
16
|
+
name = "NoteError";
|
|
17
|
+
code;
|
|
18
|
+
constructor(message, options) {
|
|
19
|
+
super(message, toErrorOptions(options));
|
|
20
|
+
this.code = options.code;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var NoteNotFoundError = class extends NoteError {
|
|
24
|
+
name = "NoteNotFoundError";
|
|
25
|
+
code = "NOTE_NOT_FOUND";
|
|
26
|
+
/** リクエストした URL */
|
|
27
|
+
url;
|
|
28
|
+
constructor(message, options) {
|
|
29
|
+
super(message, { ...options, code: "NOTE_NOT_FOUND" });
|
|
30
|
+
this.url = options.url;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var NoteHttpError = class extends NoteError {
|
|
34
|
+
name = "NoteHttpError";
|
|
35
|
+
code = "NOTE_HTTP_ERROR";
|
|
36
|
+
status;
|
|
37
|
+
/** リクエストした URL */
|
|
38
|
+
url;
|
|
39
|
+
constructor(message, options) {
|
|
40
|
+
super(message, { ...options, code: "NOTE_HTTP_ERROR" });
|
|
41
|
+
this.status = options.status;
|
|
42
|
+
this.url = options.url;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var NoteNetworkError = class extends NoteError {
|
|
46
|
+
name = "NoteNetworkError";
|
|
47
|
+
code = "NOTE_NETWORK_ERROR";
|
|
48
|
+
constructor(message, options) {
|
|
49
|
+
super(message, { ...options, code: "NOTE_NETWORK_ERROR" });
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var NoteTimeoutError = class extends NoteError {
|
|
53
|
+
name = "NoteTimeoutError";
|
|
54
|
+
code = "NOTE_TIMEOUT";
|
|
55
|
+
constructor(message, options) {
|
|
56
|
+
super(message, { ...options, code: "NOTE_TIMEOUT" });
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var NoteSchemaError = class extends NoteError {
|
|
60
|
+
name = "NoteSchemaError";
|
|
61
|
+
code = "NOTE_SCHEMA_ERROR";
|
|
62
|
+
issues;
|
|
63
|
+
constructor(message, options) {
|
|
64
|
+
const detail = options.issues.slice(0, 5).map((issue) => `${issue.path || "(root)"}: ${issue.message}`).join("; ");
|
|
65
|
+
super(detail === "" ? message : `${message} (${detail})`, { ...options, code: "NOTE_SCHEMA_ERROR" });
|
|
66
|
+
this.issues = options.issues;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function isNoteError(value) {
|
|
70
|
+
if (typeof value !== "object" || value === null) return false;
|
|
71
|
+
const { name, message, code } = value;
|
|
72
|
+
return typeof name === "string" && typeof message === "string" && typeof code === "string" && NOTE_ERROR_CODES.has(code);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/server/fetch-json.ts
|
|
76
|
+
var NOTE_ORIGIN = "https://note.com";
|
|
77
|
+
async function fetchNoteJson(url, options) {
|
|
78
|
+
if (new URL(url).origin !== NOTE_ORIGIN) {
|
|
79
|
+
throw new TypeError(`Refusing to request a non-note.com URL: ${url}`);
|
|
80
|
+
}
|
|
81
|
+
options.signal?.throwIfAborted();
|
|
82
|
+
const controller = new AbortController();
|
|
83
|
+
let timedOut = false;
|
|
84
|
+
const timer = setTimeout(() => {
|
|
85
|
+
timedOut = true;
|
|
86
|
+
controller.abort(new Error(`Timed out after ${options.timeoutMs}ms`));
|
|
87
|
+
}, options.timeoutMs);
|
|
88
|
+
const onAbort = () => controller.abort(options.signal?.reason);
|
|
89
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
90
|
+
let response;
|
|
91
|
+
let body;
|
|
92
|
+
try {
|
|
93
|
+
response = await options.fetch(url, {
|
|
94
|
+
method: "GET",
|
|
95
|
+
headers: { Accept: "application/json" },
|
|
96
|
+
credentials: "omit",
|
|
97
|
+
redirect: "error",
|
|
98
|
+
signal: controller.signal
|
|
99
|
+
});
|
|
100
|
+
body = await response.text();
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (options.signal?.aborted) throw options.signal.reason;
|
|
103
|
+
if (timedOut) {
|
|
104
|
+
throw new NoteTimeoutError(`Request to note timed out after ${options.timeoutMs}ms: ${url}`, { cause: error });
|
|
105
|
+
}
|
|
106
|
+
throw new NoteNetworkError(`Request to note failed: ${url}`, { cause: error });
|
|
107
|
+
} finally {
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
110
|
+
}
|
|
111
|
+
if (response.redirected || response.url !== "" && new URL(response.url).origin !== NOTE_ORIGIN) {
|
|
112
|
+
throw new NoteNetworkError(`note redirected the request, which is not allowed: ${url}`);
|
|
113
|
+
}
|
|
114
|
+
if (response.status === 404) {
|
|
115
|
+
throw new NoteNotFoundError(`Not found on note: ${url}`, { url });
|
|
116
|
+
}
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
throw new NoteHttpError(`note responded with HTTP ${response.status}: ${url}`, { status: response.status, url });
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
return JSON.parse(body);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
throw new NoteSchemaError("note returned a response that is not valid JSON", {
|
|
124
|
+
issues: [{ path: "", message: "Invalid JSON" }],
|
|
125
|
+
cause: error
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function hasControlOrSpace(value) {
|
|
130
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
131
|
+
const code = value.charCodeAt(i);
|
|
132
|
+
if (code <= 32 || code === 127) return true;
|
|
133
|
+
}
|
|
134
|
+
return /\s/.test(value);
|
|
135
|
+
}
|
|
136
|
+
function isHttpsUrl(value) {
|
|
137
|
+
if (!value.startsWith("https://") || hasControlOrSpace(value)) return false;
|
|
138
|
+
try {
|
|
139
|
+
return new URL(value).protocol === "https:";
|
|
140
|
+
} catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
var httpsUrl = z.string().refine(isHttpsUrl, "https \u306E URL \u3067\u306F\u3042\u308A\u307E\u305B\u3093");
|
|
145
|
+
var optionalHttpsUrl = z.string().refine((value) => value.trim() === "" || isHttpsUrl(value), "https \u306E URL \u3067\u306F\u3042\u308A\u307E\u305B\u3093");
|
|
146
|
+
var contentSchema = z.object({
|
|
147
|
+
key: z.string().min(1),
|
|
148
|
+
name: z.string(),
|
|
149
|
+
noteUrl: httpsUrl,
|
|
150
|
+
publishAt: z.string().refine((value) => !Number.isNaN(Date.parse(value)), "\u65E5\u6642\u3068\u3057\u3066\u89E3\u91C8\u3067\u304D\u307E\u305B\u3093"),
|
|
151
|
+
body: z.string(),
|
|
152
|
+
eyecatch: optionalHttpsUrl,
|
|
153
|
+
hashtags: z.array(z.object({ hashtag: z.object({ name: z.string() }) })),
|
|
154
|
+
likeCount: z.number(),
|
|
155
|
+
price: z.number(),
|
|
156
|
+
user: z.object({
|
|
157
|
+
nickname: z.string(),
|
|
158
|
+
urlname: z.string(),
|
|
159
|
+
userProfileImagePath: optionalHttpsUrl
|
|
160
|
+
})
|
|
161
|
+
});
|
|
162
|
+
var contentsResponseSchema = z.object({
|
|
163
|
+
data: z.object({
|
|
164
|
+
contents: z.array(contentSchema),
|
|
165
|
+
isLastPage: z.boolean(),
|
|
166
|
+
totalCount: z.number()
|
|
167
|
+
})
|
|
168
|
+
});
|
|
169
|
+
function emptyToNull(value) {
|
|
170
|
+
return value.trim() === "" ? null : value;
|
|
171
|
+
}
|
|
172
|
+
function toSummary(content) {
|
|
173
|
+
return {
|
|
174
|
+
key: content.key,
|
|
175
|
+
title: content.name,
|
|
176
|
+
url: content.noteUrl,
|
|
177
|
+
publishedAt: new Date(content.publishAt).toISOString(),
|
|
178
|
+
excerpt: content.body.trim(),
|
|
179
|
+
eyecatchUrl: emptyToNull(content.eyecatch),
|
|
180
|
+
hashtags: content.hashtags.map(({ hashtag }) => hashtag.name.replace(/^#/, "")),
|
|
181
|
+
likeCount: content.likeCount,
|
|
182
|
+
price: content.price,
|
|
183
|
+
author: {
|
|
184
|
+
name: content.user.nickname,
|
|
185
|
+
urlname: content.user.urlname,
|
|
186
|
+
imageUrl: emptyToNull(content.user.userProfileImagePath)
|
|
187
|
+
},
|
|
188
|
+
source: "api"
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function normalizeContentsPage(json, page) {
|
|
192
|
+
const result = contentsResponseSchema.safeParse(json);
|
|
193
|
+
if (!result.success) {
|
|
194
|
+
throw new NoteSchemaError("Unexpected shape of the note article list response", {
|
|
195
|
+
issues: result.error.issues.map((issue) => ({ path: issue.path.map(String).join("."), message: issue.message }))
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
const { data } = result.data;
|
|
199
|
+
return {
|
|
200
|
+
articles: data.contents.map(toSummary),
|
|
201
|
+
page,
|
|
202
|
+
totalCount: data.totalCount,
|
|
203
|
+
isLastPage: data.isLastPage
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// src/server/throttle.ts
|
|
208
|
+
function sleep(ms, signal) {
|
|
209
|
+
return new Promise((resolve, reject) => {
|
|
210
|
+
if (signal?.aborted) {
|
|
211
|
+
reject(signal.reason);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const onAbort = () => {
|
|
215
|
+
clearTimeout(timer);
|
|
216
|
+
reject(signal?.reason);
|
|
217
|
+
};
|
|
218
|
+
const timer = setTimeout(() => {
|
|
219
|
+
signal?.removeEventListener("abort", onAbort);
|
|
220
|
+
resolve();
|
|
221
|
+
}, ms);
|
|
222
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
function createThrottle() {
|
|
226
|
+
let tail = Promise.resolve();
|
|
227
|
+
let lastSentAt;
|
|
228
|
+
return (minIntervalMs, signal) => {
|
|
229
|
+
const turn = tail.then(async () => {
|
|
230
|
+
if (lastSentAt !== void 0) {
|
|
231
|
+
const wait = lastSentAt + minIntervalMs - Date.now();
|
|
232
|
+
if (wait > 0) await sleep(wait, signal);
|
|
233
|
+
}
|
|
234
|
+
signal?.throwIfAborted();
|
|
235
|
+
lastSentAt = Date.now();
|
|
236
|
+
});
|
|
237
|
+
tail = turn.catch(() => {
|
|
238
|
+
});
|
|
239
|
+
return turn;
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
var GLOBAL_KEY = /* @__PURE__ */ Symbol.for("note-blog-kit.throttle");
|
|
243
|
+
function getDefaultThrottle() {
|
|
244
|
+
const store = globalThis;
|
|
245
|
+
store[GLOBAL_KEY] ??= createThrottle();
|
|
246
|
+
return store[GLOBAL_KEY];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/server/list-articles.ts
|
|
250
|
+
var USER_RE = /^[A-Za-z0-9_-]{1,64}$/;
|
|
251
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
252
|
+
var DEFAULT_MIN_INTERVAL_MS = 1e3;
|
|
253
|
+
var MAX_TIMER_MS = 2 ** 31 - 1;
|
|
254
|
+
function assertMilliseconds(name, value, min) {
|
|
255
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < min || value > MAX_TIMER_MS) {
|
|
256
|
+
throw new TypeError(`${name} must be a finite number between ${min} and ${MAX_TIMER_MS}: ${String(value)}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async function listArticles(options) {
|
|
260
|
+
const { user, page = 1, timeoutMs = DEFAULT_TIMEOUT_MS, minIntervalMs = DEFAULT_MIN_INTERVAL_MS, signal } = options;
|
|
261
|
+
if (typeof user !== "string" || !USER_RE.test(user)) {
|
|
262
|
+
throw new TypeError(`Invalid note user name: ${typeof user === "string" ? JSON.stringify(user) : typeof user}`);
|
|
263
|
+
}
|
|
264
|
+
if (typeof page !== "number" || !Number.isInteger(page) || page < 1) {
|
|
265
|
+
throw new TypeError(`page must be a positive integer: ${typeof page === "number" ? page : typeof page}`);
|
|
266
|
+
}
|
|
267
|
+
assertMilliseconds("timeoutMs", timeoutMs, 1);
|
|
268
|
+
assertMilliseconds("minIntervalMs", minIntervalMs, 0);
|
|
269
|
+
const url = `${NOTE_ORIGIN}/api/v2/creators/${encodeURIComponent(user)}/contents?kind=note&page=${page}`;
|
|
270
|
+
await getDefaultThrottle()(minIntervalMs, signal);
|
|
271
|
+
const json = await fetchNoteJson(url, { fetch: options.fetch ?? globalThis.fetch, timeoutMs, signal });
|
|
272
|
+
return normalizeContentsPage(json, page);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// src/server/iterate-articles.ts
|
|
276
|
+
var DEFAULT_MAX_PAGES = 20;
|
|
277
|
+
async function* iterateArticles(options) {
|
|
278
|
+
const { maxPages = DEFAULT_MAX_PAGES, ...listOptions } = options;
|
|
279
|
+
if (typeof maxPages !== "number" || !Number.isInteger(maxPages) || maxPages < 1) {
|
|
280
|
+
throw new TypeError(
|
|
281
|
+
`maxPages must be a positive integer: ${typeof maxPages === "number" ? maxPages : typeof maxPages}`
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
const seen = /* @__PURE__ */ new Set();
|
|
285
|
+
let totalCount = null;
|
|
286
|
+
let pages = 0;
|
|
287
|
+
for (let page = 1; page <= maxPages; page += 1) {
|
|
288
|
+
const result = await listArticles({ ...listOptions, page });
|
|
289
|
+
pages = page;
|
|
290
|
+
totalCount = result.totalCount;
|
|
291
|
+
for (const article of result.articles) {
|
|
292
|
+
if (seen.has(article.key)) continue;
|
|
293
|
+
seen.add(article.key);
|
|
294
|
+
yield article;
|
|
295
|
+
}
|
|
296
|
+
if (result.isLastPage || result.articles.length === 0) {
|
|
297
|
+
return { truncated: false, totalCount, pages };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return { truncated: true, totalCount, pages };
|
|
301
|
+
}
|
|
302
|
+
async function listAllArticles(options) {
|
|
303
|
+
const articles = [];
|
|
304
|
+
const iterator = iterateArticles(options);
|
|
305
|
+
let step = await iterator.next();
|
|
306
|
+
while (!step.done) {
|
|
307
|
+
articles.push(step.value);
|
|
308
|
+
step = await iterator.next();
|
|
309
|
+
}
|
|
310
|
+
return { articles, ...step.value };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export { NoteError, NoteHttpError, NoteNetworkError, NoteNotFoundError, NoteSchemaError, NoteTimeoutError, isNoteError, iterateArticles, listAllArticles, listArticles };
|