* feat: moved tl-runtime to esm and native ArrayBuffers * feat: migration to esm * fix(core): web-related fixes * test: finally, some good fucking e2e * chore: fixed linters etc * ci: added e2e to ci * build(tl): fixed gen-code on node 20 * fix: codegen Uint8Array, not Buffer never `git reset --hard` kids * build: only do type-aware linting for `packages/*` * build: ignore no-unresolved in ci for e2e * fix: node 16 doesn't have subtle crypto apparently? * fix(tests): use Uint8Array for gods sake please can i just merge this already * ci: don't parallel tasks in ci because machines are utter garbage and it may just randomly break * ci: pass secrets to e2e tests * ci: separate cli command for ci apparently im retarded * fix: run codegen in e2e im actually retarded * ci: more fixes for e2e * ci: debugging stuff * ci: still debugging * ci: hopefully fix ci???
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { tl } from '@mtcute/tl'
|
|
|
|
import { MtArgumentError } from '../types/errors.js'
|
|
|
|
/**
|
|
* Convert a JS object to TL JSON
|
|
*
|
|
* @param obj Object to be converted
|
|
*/
|
|
|
|
export function jsonToTlJson(obj: unknown): tl.TypeJSONValue {
|
|
if (obj === null || obj === undefined) return { _: 'jsonNull' }
|
|
if (typeof obj === 'boolean') return { _: 'jsonBool', value: obj }
|
|
if (typeof obj === 'number') return { _: 'jsonNumber', value: obj }
|
|
if (typeof obj === 'string') return { _: 'jsonString', value: obj }
|
|
|
|
if (Array.isArray(obj)) {
|
|
return { _: 'jsonArray', value: obj.map(jsonToTlJson) }
|
|
}
|
|
|
|
if (typeof obj !== 'object') {
|
|
throw new MtArgumentError(`Unsupported type: ${typeof obj}`)
|
|
}
|
|
|
|
const items: tl.TypeJSONObjectValue[] = []
|
|
|
|
Object.entries(obj).forEach(([key, value]) => {
|
|
items.push({
|
|
_: 'jsonObjectValue',
|
|
key,
|
|
value: jsonToTlJson(value),
|
|
})
|
|
})
|
|
|
|
return {
|
|
_: 'jsonObject',
|
|
value: items,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Convert TL JSON object to plain JS object
|
|
*
|
|
* @param obj TL JSON object to convert
|
|
*/
|
|
export function tlJsonToJson(obj: tl.TypeJSONValue): unknown {
|
|
switch (obj._) {
|
|
case 'jsonNull':
|
|
return null
|
|
case 'jsonBool':
|
|
case 'jsonNumber':
|
|
case 'jsonString':
|
|
return obj.value
|
|
case 'jsonArray':
|
|
return obj.value.map(tlJsonToJson)
|
|
}
|
|
|
|
const ret: Record<string, unknown> = {}
|
|
|
|
obj.value.forEach((item) => {
|
|
ret[item.key] = tlJsonToJson(item.value)
|
|
})
|
|
|
|
return ret
|
|
}
|