-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from Telegram-Mini-Apps/feature/new-theme-par…
…ameters New theme parameters, new TMA methods and events, safer `postEvent`
- Loading branch information
Showing
39 changed files
with
735 additions
and
607 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@tma.js/bridge": minor | ||
--- | ||
|
||
- Support `reload_iframe` event, `iframe_will_reload` method and `iframe_ready` `reload_supported` parameter | ||
- Implement `createPostEvent` method | ||
- Add error classes | ||
- Add new theme parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Version } from '@tma.js/utils'; | ||
|
||
import type { MethodName } from '../methods/index.js'; | ||
|
||
/** | ||
* Error thrown in case, unsupported method was called. | ||
*/ | ||
export class MethodUnsupportedError extends Error { | ||
constructor(method: MethodName, version: Version) { | ||
super(`Method "${method}" is unsupported in the Mini Apps version ${version}.`); | ||
Object.setPrototypeOf(this, MethodUnsupportedError.prototype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Version } from '@tma.js/utils'; | ||
|
||
import type { MethodName } from '../methods/index.js'; | ||
|
||
/** | ||
* Error thrown in case, unsupported parameter was used. | ||
*/ | ||
export class ParameterUnsupportedError extends Error { | ||
constructor(method: MethodName, param: string, version: Version) { | ||
super(`Parameter "${param}" in method "${method}" is unsupported in the Mini Apps version ${version}.`); | ||
Object.setPrototypeOf(this, ParameterUnsupportedError.prototype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './MethodUnsupportedError.js'; | ||
export * from './ParameterUnsupportedError.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { isRecord, type Version } from '@tma.js/utils'; | ||
|
||
import { supports } from '../supports.js'; | ||
import { MethodUnsupportedError, ParameterUnsupportedError } from '../errors/index.js'; | ||
import { postEvent, type PostEvent } from './index.js'; | ||
|
||
/** | ||
* Creates function which checks if specified method and parameters are supported. In case, | ||
* method or parameters are unsupported, an error will be thrown. | ||
* @param version - Telegram Mini Apps version. | ||
* @throws {MethodUnsupportedError} Method is unsupported. | ||
* @throws {ParameterUnsupportedError} Method parameter is unsupported. | ||
*/ | ||
export function createPostEvent(version: Version): PostEvent { | ||
return (method: any, params: any) => { | ||
// Firstly, check if method itself is supported. | ||
if (!supports(method, version)) { | ||
throw new MethodUnsupportedError(method, version); | ||
} | ||
|
||
// Method could use parameters, which are supported only in specific versions of Telegram | ||
// Mini Apps. | ||
if (isRecord(params)) { | ||
let validateParam: string | undefined; | ||
|
||
if (method === 'web_app_open_link' && 'try_instant_view' in params) { | ||
validateParam = 'try_instant_view'; | ||
} else if (method === 'web_app_set_header_color' && 'color' in params) { | ||
validateParam = 'color'; | ||
} | ||
|
||
if (validateParam && !supports(method, validateParam, version)) { | ||
throw new ParameterUnsupportedError(method, validateParam, version); | ||
} | ||
} | ||
|
||
return postEvent(method, params); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export type * from './haptic.js'; | ||
export type * from './invoke-custom-method.js'; | ||
export type * from './params.js'; | ||
export type * from './popup.js'; | ||
export * from './createPostEvent.js'; | ||
export * from './haptic.js'; | ||
export * from './invoke-custom-method.js'; | ||
export * from './methods.js'; | ||
export * from './popup.js'; | ||
export * from './postEvent.js'; |
Oops, something went wrong.