@kyyinfinite/lumina 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.
- package/LICENSE +21 -0
- package/README.md +629 -0
- package/examples/ai-rich.js +84 -0
- package/examples/button.js +57 -0
- package/examples/carousel.js +51 -0
- package/examples/interactive.js +102 -0
- package/examples/media.js +66 -0
- package/examples/simple-bot.js +56 -0
- package/package.json +86 -0
- package/src/builders/ai-rich.js +644 -0
- package/src/builders/base.js +109 -0
- package/src/builders/button-v2.js +159 -0
- package/src/builders/button.js +398 -0
- package/src/builders/card.js +168 -0
- package/src/builders/carousel.js +122 -0
- package/src/builders/index.d.ts +1 -0
- package/src/builders/index.js +13 -0
- package/src/client/bot.js +192 -0
- package/src/client/connection.js +180 -0
- package/src/errors.js +88 -0
- package/src/index.d.ts +458 -0
- package/src/index.js +152 -0
- package/src/media/fetch.js +67 -0
- package/src/media/image.js +86 -0
- package/src/media/index.d.ts +1 -0
- package/src/media/index.js +12 -0
- package/src/media/resolver.js +115 -0
- package/src/media/uploader.js +65 -0
- package/src/media/video.js +195 -0
- package/src/parsers/code-tokenizer-keywords.js +128 -0
- package/src/parsers/code-tokenizer.js +191 -0
- package/src/parsers/index.d.ts +1 -0
- package/src/parsers/index.js +11 -0
- package/src/parsers/inline-entity.js +231 -0
- package/src/parsers/table-metadata.js +69 -0
- package/src/proto/enums.js +170 -0
- package/src/proto/index.d.ts +1 -0
- package/src/proto/index.js +13 -0
- package/src/proto/layouts.js +89 -0
- package/src/proto/primitives.js +181 -0
- package/src/proto/relay-nodes.js +55 -0
- package/src/proto/rich-response.js +144 -0
- package/src/proto/updater.js +318 -0
- package/src/services/index.d.ts +1 -0
- package/src/services/index.js +10 -0
- package/src/services/media-service.js +184 -0
- package/src/services/message-service.js +288 -0
- package/src/services/proto-service.js +90 -0
- package/src/utils/id.js +42 -0
- package/src/utils/logger.js +65 -0
- package/src/utils/mime.js +104 -0
- package/src/utils/promise.js +52 -0
- package/src/utils/validator.js +129 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file validator.js
|
|
3
|
+
* @module lumina/utils/validator
|
|
4
|
+
*
|
|
5
|
+
* Centralised input validators. Every "must be a plain object" check that
|
|
6
|
+
* was duplicated 5x in `_build-m.js` is now `ensurePlainObject` / `ensureObjectOrArray`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { ValidationError } from '../errors.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {unknown} v
|
|
13
|
+
* @returns {boolean} True when `v` is a non-null, non-array plain object.
|
|
14
|
+
*/
|
|
15
|
+
export function isPlainObject(v) {
|
|
16
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {unknown} v
|
|
21
|
+
* @param {string} label Human-readable field name for the error message.
|
|
22
|
+
* @throws {ValidationError} When `v` is not a plain object.
|
|
23
|
+
*/
|
|
24
|
+
export function ensurePlainObject(v, label) {
|
|
25
|
+
if (!isPlainObject(v)) {
|
|
26
|
+
throw new ValidationError(`${label} must be a plain object`, {
|
|
27
|
+
code: 'LUMINA_VALIDATION_NOT_OBJECT',
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {unknown} v
|
|
34
|
+
* @param {string} label
|
|
35
|
+
* @throws {ValidationError}
|
|
36
|
+
*/
|
|
37
|
+
export function ensureString(v, label) {
|
|
38
|
+
if (typeof v !== 'string') {
|
|
39
|
+
throw new ValidationError(`${label} must be a string`, {
|
|
40
|
+
code: 'LUMINA_VALIDATION_NOT_STRING',
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {unknown} v
|
|
47
|
+
* @param {string} label
|
|
48
|
+
* @throws {ValidationError}
|
|
49
|
+
*/
|
|
50
|
+
export function ensureStringArray(v, label) {
|
|
51
|
+
if (!Array.isArray(v) || !v.every((x) => typeof x === 'string')) {
|
|
52
|
+
throw new ValidationError(`${label} must be an array of strings`, {
|
|
53
|
+
code: 'LUMINA_VALIDATION_NOT_STRING_ARRAY',
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Accept either a plain object OR an array of plain objects.
|
|
60
|
+
* Replaces the duplicated validation block in AIRich.addReels/addProduct/addPost.
|
|
61
|
+
*
|
|
62
|
+
* @param {unknown} v
|
|
63
|
+
* @param {string} label
|
|
64
|
+
* @throws {ValidationError}
|
|
65
|
+
*/
|
|
66
|
+
export function ensureObjectOrArray(v, label) {
|
|
67
|
+
const ok =
|
|
68
|
+
isPlainObject(v) ||
|
|
69
|
+
(Array.isArray(v) && v.every((x) => isPlainObject(x)))
|
|
70
|
+
if (!ok) {
|
|
71
|
+
throw new ValidationError(`${label} must be a plain object or an array of plain objects`, {
|
|
72
|
+
code: 'LUMINA_VALIDATION_NOT_OBJECT_OR_ARRAY',
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {unknown} v
|
|
79
|
+
* @param {string} label
|
|
80
|
+
* @throws {ValidationError}
|
|
81
|
+
*/
|
|
82
|
+
export function ensureBuffer(v, label) {
|
|
83
|
+
if (!Buffer.isBuffer(v)) {
|
|
84
|
+
throw new ValidationError(`${label} must be a Buffer`, {
|
|
85
|
+
code: 'LUMINA_VALIDATION_NOT_BUFFER',
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {unknown} v
|
|
92
|
+
* @param {string} label
|
|
93
|
+
* @throws {ValidationError}
|
|
94
|
+
*/
|
|
95
|
+
export function ensureStringOrBuffer(v, label) {
|
|
96
|
+
if (typeof v !== 'string' && !Buffer.isBuffer(v)) {
|
|
97
|
+
throw new ValidationError(`${label} must be a string or Buffer`, {
|
|
98
|
+
code: 'LUMINA_VALIDATION_NOT_STRING_OR_BUFFER',
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Coerce "anything that looks like media" into a uniform source descriptor.
|
|
105
|
+
* Replaces the `Buffer.isBuffer(path) ? path : { url: path }` pattern that
|
|
106
|
+
* was duplicated 5x across the legacy Button setters.
|
|
107
|
+
*
|
|
108
|
+
* @param {string | Buffer | object} source
|
|
109
|
+
* @returns {{ buffer?: Buffer, url?: string, raw: object }}
|
|
110
|
+
*/
|
|
111
|
+
export function coerceMediaSource(source) {
|
|
112
|
+
if (Buffer.isBuffer(source)) return { buffer: source, raw: source }
|
|
113
|
+
if (typeof source === 'string') return { url: source, raw: { url: source } }
|
|
114
|
+
if (isPlainObject(source)) return { raw: source }
|
|
115
|
+
throw new ValidationError('media source must be a string, Buffer, or plain object', {
|
|
116
|
+
code: 'LUMINA_VALIDATION_BAD_MEDIA_SOURCE',
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default {
|
|
121
|
+
isPlainObject,
|
|
122
|
+
ensurePlainObject,
|
|
123
|
+
ensureString,
|
|
124
|
+
ensureStringArray,
|
|
125
|
+
ensureObjectOrArray,
|
|
126
|
+
ensureBuffer,
|
|
127
|
+
ensureStringOrBuffer,
|
|
128
|
+
coerceMediaSource,
|
|
129
|
+
}
|