@vtecx/vtecxdocument 1.0.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.
@@ -0,0 +1,198 @@
1
+ # ユーティリティ
2
+
3
+ リクエスト処理・レスポンス生成・ログ・プロパティ取得などの汎用メソッド群。
4
+
5
+ ---
6
+
7
+ ## リクエスト処理
8
+
9
+ ### `checkXRequestedWith()`
10
+
11
+ ```typescript
12
+ checkXRequestedWith(): Response | undefined
13
+ ```
14
+
15
+ `X-Requested-With` ヘッダが正しく設定されているか検証する(CSRF 対策)。
16
+ 不正な場合は 401 Response を返す。正常の場合は `undefined`。
17
+
18
+ API ルートの先頭で必ず呼び出す。
19
+
20
+ ```typescript
21
+ const result = vtecxnext.checkXRequestedWith()
22
+ if (result) return result
23
+ ```
24
+
25
+ ---
26
+
27
+ ### `getParameter(name)`
28
+
29
+ ```typescript
30
+ getParameter(name: string): string | undefined
31
+ ```
32
+
33
+ リクエストのクエリパラメータを取得する。存在しない場合は `undefined`。
34
+
35
+ ```typescript
36
+ const rxid = vtecxnext.getParameter('_RXID') ?? ''
37
+ const page = parseInt(vtecxnext.getParameter('n') ?? '1', 10)
38
+ ```
39
+
40
+ ---
41
+
42
+ ### `hasParameter(name)`
43
+
44
+ ```typescript
45
+ hasParameter(name: string): boolean
46
+ ```
47
+
48
+ 指定したクエリパラメータが存在するかどうかを返す。値のないフラグ系パラメータの確認に使用。
49
+
50
+ ```typescript
51
+ const isDebug = vtecxnext.hasParameter('debug')
52
+ ```
53
+
54
+ ---
55
+
56
+ ### `buffer(req)`
57
+
58
+ ```typescript
59
+ buffer(readable?: Readable): Promise<Uint8Array>
60
+ ```
61
+
62
+ リクエストボディ(または指定した `Readable` ストリーム)を `Uint8Array` として取得する。`readable` を省略した場合はコンストラクタに渡したリクエストのボディを読み取る。
63
+
64
+ ---
65
+
66
+ ## レスポンス生成
67
+
68
+ ### `response(status, body?)`
69
+
70
+ ```typescript
71
+ response(status?: number, data?: any): Response
72
+ ```
73
+
74
+ `Response` オブジェクトを生成して返す。API ルートの最後に使用する。
75
+
76
+ ```typescript
77
+ return vtecxnext.response(200, { feed: { entry: entries } })
78
+ return vtecxnext.response(401, { feed: { title: 'Unauthorized.' } })
79
+ return vtecxnext.response(204) // データなし
80
+ ```
81
+
82
+ ---
83
+
84
+ ### `setResponseHeader(name, value)`
85
+
86
+ ```typescript
87
+ setResponseHeader(name: string, value: string): void
88
+ ```
89
+
90
+ レスポンスヘッダを設定する。`response()` を呼び出す前に設定する。
91
+
92
+ ```typescript
93
+ vtecxnext.setResponseHeader('Cache-Control', 'no-store')
94
+ return vtecxnext.response(200, data)
95
+ ```
96
+
97
+ ---
98
+
99
+ ## レスポンス(feed 形式)
100
+
101
+ ### `sendMessage(statusCode, message)`
102
+
103
+ ```typescript
104
+ sendMessage(statusCode: number, message: string): Response
105
+ ```
106
+
107
+ feed 形式のレスポンスを生成して返す。`response()` の代替で、ステータスコードとメッセージを渡す。
108
+
109
+ ```typescript
110
+ return vtecxnext.sendMessage(400, 'Invalid parameter.')
111
+ ```
112
+
113
+ ---
114
+
115
+ ## 設定・ログ
116
+
117
+ ### `property(key)`
118
+
119
+ ```typescript
120
+ property(key: string): Promise<string | null>
121
+ ```
122
+
123
+ `/_settings/properties` に設定されたプロパティ値を取得する。
124
+
125
+ ```typescript
126
+ const siteKey = await vtecxnext.property('_recaptcha.sitekey')
127
+ ```
128
+
129
+ ---
130
+
131
+ ### `log(message, title?, subtitle?)`
132
+
133
+ ```typescript
134
+ log(message: string, title?: string, subtitle?: string): Promise<boolean>
135
+ ```
136
+
137
+ vte.cx にログエントリを登録する。`title` のデフォルトは `'JavaScript'`。
138
+
139
+ ```typescript
140
+ await vtecxnext.log('処理完了', 'MyApp', 'INFO')
141
+ await vtecxnext.log('エラーが発生しました', 'MyApp', 'ERROR')
142
+ ```
143
+
144
+ ---
145
+
146
+ ## 型チェック
147
+
148
+ ### `isVtecxNextError(e)`
149
+
150
+ ```typescript
151
+ isVtecxNextError(e: unknown): e is VtecxNextError
152
+ ```
153
+
154
+ エラーが `VtecxNextError` かどうかを判定する型ガード。
155
+
156
+ ```typescript
157
+ import { isVtecxNextError } from '@vtecx/vtecxnext'
158
+
159
+ try {
160
+ await vtecxnext.getEntry('/crm/customer/001')
161
+ } catch (e) {
162
+ if (isVtecxNextError(e)) {
163
+ console.error(e.status, e.message)
164
+ }
165
+ }
166
+ ```
167
+
168
+ ---
169
+
170
+ ## 文字列ユーティリティ
171
+
172
+ ### `isBlank(str)`
173
+
174
+ ```typescript
175
+ isBlank(val: any): boolean
176
+ ```
177
+
178
+ 値が `null` / `undefined` / 空文字 / 空配列のいずれかかどうかを返す。
179
+
180
+ ```typescript
181
+ if (vtecxnext.isBlank(name)) {
182
+ return vtecxnext.response(400, { feed: { title: 'Name is required.' } })
183
+ }
184
+ ```
185
+
186
+ ---
187
+
188
+ ### `null2blank(str)`
189
+
190
+ ```typescript
191
+ null2blank(str: string | null | undefined): string
192
+ ```
193
+
194
+ `null` または `undefined` を空文字列に変換する。
195
+
196
+ ```typescript
197
+ const name = vtecxnext.null2blank(entry?.customer?.name)
198
+ ```