(feat): improve ts-sdk types

This commit is contained in:
Alois 2026-07-02 23:11:42 +02:00
commit f354fe8fd6

View file

@ -20,7 +20,7 @@ export type MTPStorage = MTPCredentialStorage;
export type MTPLogEvent = export type MTPLogEvent =
| { hint: "info" | "warning"; type: string; data: unknown } | { hint: "info" | "warning"; type: string; data: unknown }
| { hint: "error"; type: string | "error"; error: string }; | { hint: "error"; type: string | "error"; error: string; data?: unknown };
export type ParsedFrame = RawBindings.ParsedFrame; export type ParsedFrame = RawBindings.ParsedFrame;
@ -73,17 +73,27 @@ export interface MTPRaw {
bindings: MTPRawBindings; bindings: MTPRawBindings;
} }
export type MTPBytesInput = Uint8Array | number[];
export interface MTPCredentials { export interface MTPCredentials {
clientId: bigint | string | number | null; clientId: bigint | string | number | null;
keyring: Uint8Array | number[]; keyring: MTPBytesInput;
/** @deprecated Use keyring. Kept as a migration alias for existing callers. */ /** @deprecated Use keyring. Kept as a migration alias for existing callers. */
keyringBytes?: Uint8Array | number[]; keyringBytes?: MTPBytesInput;
hostPublicKey?: Uint8Array | number[]; hostPublicKey?: MTPBytesInput | string;
}
export interface MTPClientCredentials {
clientId: bigint | null;
keyring: Uint8Array;
/** @deprecated Use keyring. Kept as a migration alias for existing callers. */
keyringBytes: Uint8Array;
hostPublicKey?: Uint8Array;
} }
export interface MTPClientOptions { export interface MTPClientOptions {
url: string; url: string;
hostPublicKey?: Uint8Array | string; hostPublicKey?: MTPBytesInput | string;
credentials?: MTPCredentials | string | null; credentials?: MTPCredentials | string | null;
credentialsStorageKey?: string; credentialsStorageKey?: string;
storage?: MTPCredentialStorage; storage?: MTPCredentialStorage;
@ -333,7 +343,7 @@ export class MTPClient {
this.raw = { client, bindings }; this.raw = { client, bindings };
} }
static async create(options: MTPClientOptions = {} as MTPClientOptions): Promise<MTPClient> { static async create(options: MTPClientOptions): Promise<MTPClient> {
validateOptions(options); validateOptions(options);
await MTPClient.init(options.wasm); await MTPClient.init(options.wasm);
@ -387,7 +397,7 @@ export class MTPClient {
return await initWasm(wasm); return await initWasm(wasm);
} }
get credentials(): MTPCredentials | null { get credentials(): MTPClientCredentials | null {
return publicCredentials(this.#credentials); return publicCredentials(this.#credentials);
} }
@ -545,7 +555,7 @@ export class MTPClient {
try { try {
const frame = this.raw.bindings.parse_frame(message); const frame = this.raw.bindings.parse_frame(message);
emit(this.#options.logger, isErrorType(frame.type) emit(this.#options.logger, isErrorType(frame.type)
? { hint: "error", type: frame.type, error: errorMessage(frame) } ? { hint: "error", type: frame.type, error: errorMessage(frame), data: frame.data }
: { hint: "info", type: frame.type, data: frame.data }); : { hint: "info", type: frame.type, data: frame.data });
} catch (error) { } catch (error) {
emit(this.#options.logger, { emit(this.#options.logger, {
@ -582,6 +592,7 @@ export class MTPClient {
hint: "error", hint: "error",
type: frame.type, type: frame.type,
error: errorMessage(frame), error: errorMessage(frame),
data: frame.data,
}); });
} else { } else {
emit(this.#options.logger, { emit(this.#options.logger, {