-
Notifications
You must be signed in to change notification settings - Fork 233
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 #227 from Telegram-Mini-Apps/feature/parse-message
Feature/parse message
- Loading branch information
Showing
7 changed files
with
61 additions
and
11 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,5 @@ | ||
--- | ||
"@tma.js/sdk": patch | ||
--- | ||
|
||
Implement parseMessage method |
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,23 @@ | ||
import { it, expect } from 'vitest'; | ||
|
||
import { parseMessage } from '../parseMessage'; | ||
|
||
it('should parse value as JSON with properties { eventType: string; eventData?: unknown }', () => { | ||
expect(parseMessage({ eventType: 1 })).toEqual({ eventType: '1' }); | ||
expect(parseMessage({ eventType: 'test' })).toEqual({ eventType: 'test' }); | ||
expect(parseMessage({ eventType: 'test', eventData: 123 })).toEqual({ | ||
eventType: 'test', | ||
eventData: 123, | ||
}); | ||
|
||
expect(parseMessage('{"eventType":1}')).toEqual({ eventType: '1' }); | ||
expect(parseMessage('{"eventType":"test"}')).toEqual({ eventType: 'test' }); | ||
expect(parseMessage('{"eventType":"test","eventData":123}')).toEqual({ | ||
eventType: 'test', | ||
eventData: 123, | ||
}); | ||
}); | ||
|
||
it('should throw if eventType property is missing', () => { | ||
expect(() => parseMessage({})).toThrow(); | ||
}); |
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
File renamed without changes.
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,28 @@ | ||
import { json, string } from '~/parsing/index.js'; | ||
|
||
/** | ||
* Message format used in communication between client and Telegram applications. | ||
*/ | ||
export interface MiniAppsMessage { | ||
/** | ||
* Event name. | ||
*/ | ||
eventType: string; | ||
/** | ||
* Event parameters. | ||
*/ | ||
eventData?: unknown; | ||
} | ||
|
||
const parser = json<MiniAppsMessage>({ | ||
eventType: string(), | ||
eventData: (value) => value, | ||
}); | ||
|
||
/** | ||
* Parses value as a message between client and Telegram applications. | ||
* @param value - value to parse. | ||
*/ | ||
export function parseMessage(value: unknown): MiniAppsMessage { | ||
return parser.parse(value); | ||
} |
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ export { | |
on, | ||
off, | ||
once, | ||
parseMessage, | ||
postEvent, | ||
request, | ||
subscribe, | ||
|