unikvs 0.0.9 → 0.0.12

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.
Files changed (63) hide show
  1. package/README.md +3 -0
  2. package/dist/src/_logger.d.ts.map +1 -1
  3. package/dist/src/_merge-variables.d.ts +13 -0
  4. package/dist/src/_merge-variables.d.ts.map +1 -0
  5. package/dist/src/_merge-variables.js +12 -0
  6. package/dist/src/_storage.d.ts +11 -11
  7. package/dist/src/_storage.d.ts.map +1 -1
  8. package/dist/src/_storage.js +31 -22
  9. package/dist/src/_streams.d.ts +20 -0
  10. package/dist/src/_streams.d.ts.map +1 -0
  11. package/dist/src/_streams.js +36 -0
  12. package/dist/src/_to-value-stream.d.ts.map +1 -1
  13. package/dist/src/_to-value-stream.js +44 -6
  14. package/dist/src/_transformer.d.ts +7 -7
  15. package/dist/src/_transformer.d.ts.map +1 -1
  16. package/dist/src/_transformer.js +12 -12
  17. package/dist/src/_valibot.d.ts +6 -7
  18. package/dist/src/_valibot.d.ts.map +1 -1
  19. package/dist/src/_valibot.js +1 -1
  20. package/dist/src/_value-schemas.d.ts +179 -0
  21. package/dist/src/_value-schemas.d.ts.map +1 -0
  22. package/dist/src/_value-schemas.js +150 -0
  23. package/dist/src/errors.d.ts +218 -27
  24. package/dist/src/errors.d.ts.map +1 -1
  25. package/dist/src/errors.js +102 -47
  26. package/dist/src/index.d.ts +3 -3
  27. package/dist/src/index.d.ts.map +1 -1
  28. package/dist/src/index.js +2 -2
  29. package/dist/src/unikvs-config.d.ts +58 -139
  30. package/dist/src/unikvs-config.d.ts.map +1 -1
  31. package/dist/src/unikvs-config.js +41 -46
  32. package/dist/src/unikvs.d.ts +86 -161
  33. package/dist/src/unikvs.d.ts.map +1 -1
  34. package/dist/src/unikvs.js +374 -171
  35. package/dist/src/utils.types.d.ts +1 -3
  36. package/dist/src/utils.types.d.ts.map +1 -1
  37. package/dist/src/value-stream.types.d.ts +5 -6
  38. package/dist/src/value-stream.types.d.ts.map +1 -1
  39. package/dist/src/variables.types.d.ts +11 -0
  40. package/dist/src/variables.types.d.ts.map +1 -0
  41. package/package.json +26 -25
  42. package/src/_merge-variables.ts +19 -0
  43. package/src/_storage.ts +47 -24
  44. package/src/_streams.ts +47 -0
  45. package/src/_to-value-stream.ts +47 -8
  46. package/src/_transformer.ts +13 -13
  47. package/src/_valibot.ts +8 -6
  48. package/src/_value-schemas.ts +362 -0
  49. package/src/errors.ts +280 -68
  50. package/src/index.ts +2 -3
  51. package/src/unikvs-config.ts +142 -167
  52. package/src/unikvs.ts +523 -285
  53. package/src/utils.types.ts +1 -3
  54. package/src/value-stream.types.ts +5 -6
  55. package/src/variables.types.ts +13 -0
  56. package/dist/src/_merge-context.d.ts +0 -13
  57. package/dist/src/_merge-context.d.ts.map +0 -1
  58. package/dist/src/_merge-context.js +0 -12
  59. package/dist/src/context.types.d.ts +0 -17
  60. package/dist/src/context.types.d.ts.map +0 -1
  61. package/src/_merge-context.ts +0 -19
  62. package/src/context.types.ts +0 -19
  63. /package/dist/src/{context.types.js → variables.types.js} +0 -0
@@ -0,0 +1,362 @@
1
+ import * as v from "./_valibot.js";
2
+ import { InvalidInputError } from "./errors.js";
3
+
4
+ // -------------------------------------------------------------------------------------------------
5
+ //
6
+ // 値の型
7
+ //
8
+ // -------------------------------------------------------------------------------------------------
9
+
10
+ /**
11
+ * プレーンな値を識別するための固有のシンボルです。
12
+ */
13
+ declare const PLAIN_VALUE: unique symbol;
14
+
15
+ /**
16
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
17
+ *
18
+ * @template TData 入出力されるデータの型です。
19
+ * @template TInput `set` が受け付ける入力データの型です。
20
+ */
21
+ export type PlainValue<TData = any, TInput = TData> = [
22
+ Type: typeof PLAIN_VALUE,
23
+ Data: TData,
24
+ Input: TInput,
25
+ ];
26
+
27
+ /**
28
+ * ストリーム形式の値を識別するための固有のシンボルです。
29
+ */
30
+ declare const STREAM_VALUE: unique symbol;
31
+
32
+ /**
33
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
34
+ *
35
+ * @template TChunkData 入出力されるチャンクデータの型です。
36
+ * @template TChunkInput `set` が受け付ける入力チャンクデータの型です。
37
+ */
38
+ export type StreamValue<TChunkData = any, TChunkInput = TChunkData> = [
39
+ Type: typeof STREAM_VALUE,
40
+ ChunkData: TChunkData,
41
+ ChunkInput: TChunkInput,
42
+ ];
43
+
44
+ /**
45
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
46
+ *
47
+ * @template TDataAndChunkData データまたはチャンクデータの型です。
48
+ * @template TInput `set` が受け付ける入力データまたは入力チャンクデータの型です。
49
+ */
50
+ export type Value<TDataAndChunkData = any, TInput = TDataAndChunkData> =
51
+ | PlainValue<TDataAndChunkData, TInput>
52
+ | StreamValue<TDataAndChunkData, TInput>;
53
+
54
+ /**
55
+ * PlainValue 型から内部のデータ型を抽出します。
56
+ *
57
+ * @template TPlainValue 対象となる PlainValue 型です。
58
+ */
59
+ export type $InferPlainValueData<TPlainValue> =
60
+ TPlainValue extends PlainValue<infer TData, any> ? TData : never;
61
+
62
+ /**
63
+ * PlainValue 型から入力データの型を抽出します。
64
+ *
65
+ * @template TPlainValue 対象となる PlainValue 型です。
66
+ */
67
+ export type $InferPlainValueInput<TPlainValue> =
68
+ TPlainValue extends PlainValue<any, infer TInput> ? TInput : never;
69
+
70
+ /**
71
+ * StreamValue 型から内部のチャンクデータ型を抽出します。
72
+ *
73
+ * @template TStreamValue 対象となる StreamValue 型です。
74
+ */
75
+ export type $InferStreamValueChunkData<TStreamValue> =
76
+ TStreamValue extends StreamValue<infer TChunkData, any> ? TChunkData : never;
77
+
78
+ /**
79
+ * StreamValue 型から入力チャンクデータの型を抽出します。
80
+ *
81
+ * @template TStreamValue 対象となる StreamValue 型です。
82
+ */
83
+ export type $InferStreamValueChunkInput<TStreamValue> =
84
+ TStreamValue extends StreamValue<any, infer TChunkInput> ? TChunkInput : never;
85
+
86
+ // -------------------------------------------------------------------------------------------------
87
+ //
88
+ // 値のスキーマ
89
+ //
90
+ // -------------------------------------------------------------------------------------------------
91
+
92
+ /**
93
+ * スキーマ付きの値であることを識別するためのブランドです。
94
+ *
95
+ * 同じライブラリーが複数読み込まれた環境でも認識できるようにグローバルシンボルを使用します。
96
+ */
97
+ const VALUE_SCHEMA_BRAND: unique symbol = Symbol.for("unikvs.valueSchema");
98
+
99
+ /**
100
+ * スキーマ付きの値が扱うデータの種類です。
101
+ *
102
+ * - `"plain"`: プレーンな値のみを扱います。
103
+ * - `"stream"`: ストリーム形式の値のみを扱います。
104
+ * - `"any"`: プレーンな値とストリーム形式の値のどちらも扱います。
105
+ */
106
+ export type ValueSchemaKind = "plain" | "stream" | "any";
107
+
108
+ /**
109
+ * キーに対応する値のスキーマ情報です。
110
+ */
111
+ export type ValueSchemaInfo = {
112
+ /**
113
+ * 扱うデータの種類です。
114
+ */
115
+ readonly kind: ValueSchemaKind;
116
+
117
+ /**
118
+ * データの検証に使用するスキーマです。
119
+ */
120
+ readonly schema: v.GenericSchema;
121
+ };
122
+
123
+ /**
124
+ * スキーマ付きの値を表す内部オブジェクトです。
125
+ */
126
+ type ValueSchemaDescriptor = ValueSchemaInfo & {
127
+ readonly [VALUE_SCHEMA_BRAND]: true;
128
+ };
129
+
130
+ /**
131
+ * 値が Valibot のスキーマであるかどうかを判定します。
132
+ */
133
+ function isGenericSchema(input: unknown): input is v.GenericSchema {
134
+ return (
135
+ typeof input === "object" &&
136
+ input !== null &&
137
+ typeof (input as { readonly ["~run"]?: unknown })["~run"] === "function"
138
+ );
139
+ }
140
+
141
+ /**
142
+ * 値がスキーマ情報であるかどうかを判定します。
143
+ */
144
+ function isValueSchemaInfo(input: unknown): input is ValueSchemaInfo {
145
+ return (
146
+ typeof input === "object" &&
147
+ input !== null &&
148
+ (input as { readonly [VALUE_SCHEMA_BRAND]?: unknown })[VALUE_SCHEMA_BRAND] === true
149
+ );
150
+ }
151
+
152
+ /**
153
+ * スキーマ情報を内部オブジェクトとして生成します。
154
+ */
155
+ function createValueSchemaDescriptor(
156
+ kind: ValueSchemaKind,
157
+ schema: v.GenericSchema,
158
+ ): ValueSchemaDescriptor {
159
+ return { [VALUE_SCHEMA_BRAND]: true, kind, schema };
160
+ }
161
+
162
+ /**
163
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
164
+ *
165
+ * プレーンな値のスキーマを定義します。
166
+ *
167
+ * @template TSchema スキーマの型です。
168
+ * @param schema 値の検証に使用する Valibot スキーマです。
169
+ * @returns プレーンな値であることを示す型情報を返します。
170
+ */
171
+ export function PlainValue<const TSchema extends v.GenericSchema>(
172
+ schema: TSchema,
173
+ ): PlainValue<v.InferOutput<TSchema>, v.InferInput<TSchema>> {
174
+ return createValueSchemaDescriptor("plain", schema) as unknown as PlainValue<
175
+ v.InferOutput<TSchema>,
176
+ v.InferInput<TSchema>
177
+ >;
178
+ }
179
+
180
+ /**
181
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
182
+ *
183
+ * ストリーム形式の値のスキーマを定義します。
184
+ *
185
+ * @template TSchema スキーマの型です。
186
+ * @param schema チャンクデータの検証に使用する Valibot スキーマです。
187
+ * @returns ストリーム形式の値であることを示す型情報を返します。
188
+ */
189
+ export function StreamValue<const TSchema extends v.GenericSchema>(
190
+ schema: TSchema,
191
+ ): StreamValue<v.InferOutput<TSchema>, v.InferInput<TSchema>> {
192
+ return createValueSchemaDescriptor("stream", schema) as unknown as StreamValue<
193
+ v.InferOutput<TSchema>,
194
+ v.InferInput<TSchema>
195
+ >;
196
+ }
197
+
198
+ /**
199
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
200
+ *
201
+ * プレーンな値とストリーム形式の値の両方で使えるスキーマを定義します。
202
+ *
203
+ * @template TSchema スキーマの型です。
204
+ * @param schema データとチャンクデータの検証に使用する Valibot スキーマです。
205
+ * @returns どちらの形式でも使える値であることを示す型情報を返します。
206
+ */
207
+ export function Value<const TSchema extends v.GenericSchema>(
208
+ schema: TSchema,
209
+ ): Value<v.InferOutput<TSchema>, v.InferInput<TSchema>> {
210
+ return createValueSchemaDescriptor("any", schema) as unknown as Value<
211
+ v.InferOutput<TSchema>,
212
+ v.InferInput<TSchema>
213
+ >;
214
+ }
215
+
216
+ // -------------------------------------------------------------------------------------------------
217
+ //
218
+ // スキーマ定義
219
+ //
220
+ // -------------------------------------------------------------------------------------------------
221
+
222
+ /**
223
+ * オブジェクト形式のスキーマ定義です。キーをそのままキーとして扱います。
224
+ */
225
+ export type UniKvsSchemaRecord = { readonly [key: string]: Value<any, any> };
226
+
227
+ /**
228
+ * 配列形式のスキーマ定義です。キーのスキーマと値のスキーマの組を並べます。
229
+ */
230
+ export type UniKvsSchemaEntries = readonly (readonly [v.GenericSchema, Value<any, any>])[];
231
+
232
+ /**
233
+ * `UniKvs.config()` の `schema` オプションに指定できるスキーマ定義です。
234
+ */
235
+ export type UniKvsSchema = UniKvsSchemaRecord | UniKvsSchemaEntries;
236
+
237
+ /**
238
+ * スキーマ定義からキーと値のマッピング型を推論します。
239
+ *
240
+ * @template TSchema 対象となるスキーマ定義の型です。
241
+ */
242
+ export type $InferKeyValueMapping<TSchema extends UniKvsSchema> =
243
+ TSchema extends readonly (readonly [v.GenericSchema, infer TValue extends Value<any, any>])[]
244
+ ? { readonly [key: string]: TValue }
245
+ : TSchema extends { readonly [key: string]: Value<any, any> }
246
+ ? { readonly [TKey in keyof TSchema]: TSchema[TKey] }
247
+ : never;
248
+
249
+ /**
250
+ * 検証済みのオブジェクト形式のスキーマ定義です。
251
+ */
252
+ export type UniKvsSchemaInfoRecord = { readonly [key: string]: ValueSchemaInfo };
253
+
254
+ /**
255
+ * 検証済みの配列形式のスキーマ定義です。
256
+ */
257
+ export type UniKvsSchemaInfoEntries = readonly (readonly [v.GenericSchema, ValueSchemaInfo])[];
258
+
259
+ /**
260
+ * 検証済みのスキーマ定義です。
261
+ */
262
+ export type UniKvsSchemaInfo = UniKvsSchemaInfoRecord | UniKvsSchemaInfoEntries;
263
+
264
+ /**
265
+ * `schema` オプションの検証に使用するスキーマです。
266
+ */
267
+ export const UniKvsSchemaSchema = v.union([
268
+ v.array(
269
+ v.tuple([
270
+ v.custom<v.GenericSchema>(isGenericSchema),
271
+ v.custom<ValueSchemaInfo>(isValueSchemaInfo),
272
+ ]),
273
+ ),
274
+ v.record(v.string(), v.custom<ValueSchemaInfo>(isValueSchemaInfo)),
275
+ ]);
276
+
277
+ // -------------------------------------------------------------------------------------------------
278
+ //
279
+ // スキーマリゾルバー
280
+ //
281
+ // -------------------------------------------------------------------------------------------------
282
+
283
+ /**
284
+ * キーに対応する値のスキーマ情報を解決するインターフェースです。
285
+ */
286
+ export interface IValueSchemaResolver {
287
+ /**
288
+ * キーに対応する値のスキーマ情報を返します。
289
+ *
290
+ * @param key 解決するキーです。
291
+ * @returns 対応するスキーマ情報を返します。対応する定義がない場合は `undefined` を返します。
292
+ * @throws キーがどのキースキーマにも一致しない場合に `InvalidInputError` を投げます。
293
+ */
294
+ resolve(key: string): ValueSchemaInfo | undefined;
295
+ }
296
+
297
+ /**
298
+ * オブジェクト形式のスキーマ定義からスキーマ情報を解決します。
299
+ */
300
+ class RecordValueSchemaResolver implements IValueSchemaResolver {
301
+ readonly #entries: Map<string, ValueSchemaInfo>;
302
+
303
+ public constructor(entries: UniKvsSchemaInfoRecord) {
304
+ this.#entries = new Map(Object.entries(entries));
305
+ }
306
+
307
+ public resolve(key: string): ValueSchemaInfo | undefined {
308
+ return this.#entries.get(key);
309
+ }
310
+ }
311
+
312
+ /**
313
+ * 配列形式のスキーマ定義からスキーマ情報を解決します。
314
+ *
315
+ * キースキーマに最初に一致した定義を採用します。
316
+ */
317
+ class EntriesValueSchemaResolver implements IValueSchemaResolver {
318
+ readonly #entries: UniKvsSchemaInfoEntries;
319
+
320
+ readonly #keySchema: v.GenericSchema | undefined;
321
+
322
+ public constructor(entries: UniKvsSchemaInfoEntries) {
323
+ this.#entries = entries;
324
+ this.#keySchema =
325
+ entries.length > 0 ? v.union(entries.map(([keySchema]) => keySchema)) : undefined;
326
+ }
327
+
328
+ public resolve(key: string): ValueSchemaInfo | undefined {
329
+ for (const [keySchema, valueSchema] of this.#entries) {
330
+ const result = v.safeParse(keySchema, key);
331
+ if (result.success) {
332
+ return valueSchema;
333
+ }
334
+ }
335
+
336
+ if (this.#keySchema) {
337
+ const result = v.safeParse(this.#keySchema, key);
338
+ if (!result.success) {
339
+ throw new InvalidInputError({
340
+ value: key,
341
+ issues: result.issues,
342
+ });
343
+ }
344
+ }
345
+
346
+ return undefined;
347
+ }
348
+ }
349
+
350
+ /**
351
+ * 検証済みのスキーマ定義からスキーマリゾルバーを生成します。
352
+ *
353
+ * @param schema 検証済みのスキーマ定義です。
354
+ * @returns スキーマリゾルバーを返します。
355
+ */
356
+ export function createValueSchemaResolver(schema: UniKvsSchemaInfo): IValueSchemaResolver {
357
+ if (Array.isArray(schema)) {
358
+ return new EntriesValueSchemaResolver(schema);
359
+ }
360
+
361
+ return new RecordValueSchemaResolver(schema);
362
+ }