4nm
Version:
TypeScript reimplementation of Telegram's official library for communicating with Telegram Web Apps.
93 lines (92 loc) • 2.18 kB
JavaScript
import { parseQueryString } from '../url';
import { extractInitData } from './utils';
/**
* Represents class which is responsible for communication with WebApps init
* data.
*/
export class InitData {
raw;
unsafe;
params;
/**
* Attempts to create InitData instance from their its raw representation.
* @param raw - init data in raw format (query string).
* @see parseInitData
*/
static fromRaw(raw) {
return new InitData(raw, parseQueryString(raw), extractInitData(raw));
}
/**
* Creates new empty instance of InitData.
*/
static empty() {
return new InitData('', {}, { authDate: new Date(0), hash: '' });
}
constructor(
/**
* Raw representation of init data (query string).
*/
raw,
/**
* Unsafe representation of init data. This field is useful in case,
* some new updates appeared in Telegram Web Apps, but this library version
* is not up-to-date.
*/
unsafe,
/**
* Raw init data parameters.
*/
params) {
this.raw = raw;
this.unsafe = unsafe;
this.params = params;
}
/**
* @see InitDataInterface.authDate
*/
get authDate() {
return this.params.authDate;
}
/**
* @see InitDataInterface.canSendAfter
*/
get canSendAfter() {
return this.params.canSendAfter;
}
/**
* @see InitDataInterface.chat
*/
get chat() {
return this.params.chat;
}
/**
* @see InitDataInterface.hash
*/
get hash() {
return this.params.hash;
}
/**
* @see InitDataInterface.queryId
*/
get queryId() {
return this.params.queryId;
}
/**
* @see InitDataInterface.receiver
*/
get receiver() {
return this.params.receiver;
}
/**
* @see InitDataInterface.startParam
*/
get startParam() {
return this.params.startParam;
}
/**
* @see InitDataInterface.user
*/
get user() {
return this.params.user;
}
}