@tma.js/sdk-solid 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/source/SDKProvider.jsx +15 -0
- package/dist/source/context.js +2 -0
- package/dist/source/hooks.js +12 -0
- package/dist/source/index.js +6 -0
- package/dist/source/types.js +1 -0
- package/dist/source/useSDK.js +61 -0
- package/dist/types/SDKProvider.d.ts +6 -0
- package/dist/types/context.d.ts +2 -0
- package/dist/types/hooks.d.ts +5 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/types.d.ts +11 -0
- package/dist/types/useSDK.d.ts +9 -0
- package/package.json +68 -0
- package/src/SDKProvider.tsx +24 -0
- package/src/context.ts +5 -0
- package/src/hooks.ts +16 -0
- package/src/index.ts +6 -0
- package/src/types.ts +13 -0
- package/src/useSDK.ts +97 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Telegram Web Apps
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @tma.js/sdk-solid
|
|
2
|
+
|
|
3
|
+
[code-badge]: https://img.shields.io/badge/source-black?logo=github
|
|
4
|
+
|
|
5
|
+
[code-link]: https://github.com/Telegram-Mini-Apps/tma.js/tree/master/packages/sdk-solid
|
|
6
|
+
|
|
7
|
+
[docs-link]: https://docs.telegram-mini-apps.com/docs/libraries/tma-js-sdk-solid
|
|
8
|
+
|
|
9
|
+
[docs-badge]: https://img.shields.io/badge/documentation-blue?logo=gitbook&logoColor=white
|
|
10
|
+
|
|
11
|
+
[npm-link]: https://npmjs.com/package/@tma.js/sdk-solid
|
|
12
|
+
|
|
13
|
+
[npm-badge]: https://img.shields.io/npm/v/@tma.js/sdk-solid?logo=npm
|
|
14
|
+
|
|
15
|
+
[size-badge]: https://img.shields.io/bundlephobia/minzip/@tma.js/sdk-solid
|
|
16
|
+
|
|
17
|
+
[![NPM][npm-badge]][npm-link]
|
|
18
|
+
![Size][size-badge]
|
|
19
|
+
[![docs-badge]][docs-link]
|
|
20
|
+
[![code-badge]][code-link]
|
|
21
|
+
|
|
22
|
+
Solid JS bindings for client SDK. Contains hooks, components and other useful tools which allow usage of Solid JS along with Web Apps client SDK. Tracks SDK components changes out of box.
|
|
23
|
+
|
|
24
|
+
This library is a part of TypeScript packages ecosystem around Telegram Web Apps. You can learn more about this package in this [documentation](https://docs.telegram-mini-apps.com/docs/libraries/tma-js-sdk-solid).
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var e=require("solid-js"),t=require("solid-js/web"),n=require("@tma.js/sdk"),r=require("@tma.js/utils");const o=e.createContext(void 0,{name:"SDKContext"});function a(){const t=e.useContext(o);if(void 0===t)throw new Error("useSDKContext hook was used outside of SDKProvider.");return t}function i(t,n,r){const[o,a]=e.createSignal(t()[n],{equals:!1});return e.createEffect((()=>{const t=o();r.forEach((e=>{t.on(e,(()=>a((()=>t))))})),e.onCleanup((()=>{r.forEach((e=>{t.off(e,(()=>a((()=>t))))}))}))})),o}function s(t,n){return e.createMemo((()=>t()[n]))}Object.defineProperty(exports,"classNames",{enumerable:!0,get:function(){return r.classNames}}),Object.defineProperty(exports,"mergeClassNames",{enumerable:!0,get:function(){return r.mergeClassNames}}),exports.SDKContext=o,exports.SDKProvider=function(r){const a=e.createMemo((()=>r.initOptions||{})),[i]=e.createResource(a,n.init),s=e.createMemo((()=>"ready"===i.state?i():null)),u=e.createMemo((()=>i.loading)),c=e.createMemo((()=>void 0===i.error?null:i.error));return t.createComponent(o.Provider,{value:{initResult:s,loading:u,error:c},get children(){return r.children}})},exports.useSDK=function(){const{initResult:t}=a(),n=e.createMemo((()=>{const e=t();if(null===e)throw new Error("Unable to use SDK as it is not ready.");return e}));return{backButton:i(n,"backButton",["isVisibleChanged"]),closingBehavior:i(n,"closingBehavior",["isConfirmationNeededChanged"]),cloudStorage:s(n,"cloudStorage"),haptic:s(n,"haptic"),initData:s(n,"initData"),initDataRaw:s(n,"initDataRaw"),mainButton:i(n,"mainButton",["backgroundColorChanged","isVisibleChanged","isProgressVisibleChanged","isEnabledChanged","textChanged","textColorChanged"]),popup:i(n,"popup",["isOpenedChanged"]),postEvent:s(n,"postEvent"),qrScanner:i(n,"qrScanner",["isOpenedChanged"]),themeParams:i(n,"themeParams",["changed"]),viewport:i(n,"viewport",["heightChanged","isExpandedChanged","stableHeightChanged","widthChanged"]),webApp:i(n,"webApp",["backgroundColorChanged","headerColorChanged"])}},exports.useSDKContext=a;
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/context.ts","../../src/hooks.ts","../../src/useSDK.ts","../../src/SDKProvider.tsx"],"sourcesContent":["import { createContext } from 'solid-js';\n\nimport type { SDKContextType } from './types.js';\n\nexport const SDKContext = createContext<SDKContextType>(undefined, { name: 'SDKContext' });\n","import { useContext } from 'solid-js';\n\nimport { SDKContext } from './context.js';\nimport type { SDKContextType } from './types.js';\n\n/**\n * Uses SDKContext context.\n */\nexport function useSDKContext(): SDKContextType {\n const context = useContext(SDKContext);\n\n if (context === undefined) {\n throw new Error('useSDKContext hook was used outside of SDKProvider.');\n }\n return context;\n}\n","import { type Accessor, createEffect, createMemo, createSignal, onCleanup } from 'solid-js';\n\nimport type { SDKInitResult, SDKInitResultKey, SDKInitResultValue } from './types.js';\nimport { useSDKContext } from './hooks.js';\n\ninterface Trackable {\n on: (event: any, ...args: any[]) => void;\n off: (event: any, ...args: any[]) => void;\n}\n\ntype EventName<T extends Trackable> = T extends {\n on(event: infer E, ...args: any[]): any\n} ? E : never;\n\ntype DynamicComponentKey = {\n [K in SDKInitResultKey]: SDKInitResultValue<K> extends Trackable\n ? K\n : never;\n}[SDKInitResultKey];\n\nexport type SDK = {\n [K in SDKInitResultKey]: Accessor<SDKInitResultValue<K>>\n};\n\nfunction useDynamicComponent<K extends DynamicComponentKey>(\n initResult: Accessor<SDKInitResult>,\n key: K,\n events: EventName<SDKInitResultValue<K>>[],\n): Accessor<SDKInitResultValue<K>> {\n const [component, setComponent] = createSignal(initResult()[key], { equals: false });\n\n createEffect(() => {\n const obj = component();\n\n events.forEach(event => {\n (obj as any).on(event, () => setComponent(() => obj));\n });\n\n onCleanup(() => {\n events.forEach(event => {\n (obj as any).off(event, () => setComponent(() => obj));\n });\n });\n });\n\n return component;\n}\n\nfunction useInitResultValue<K extends SDKInitResultKey>(initResult: Accessor<SDKInitResult>, key: K) {\n const value = createMemo<SDKInitResultValue<K>>(() => initResult()[key]);\n\n return value;\n}\n\n/**\n * Returns ready to use SDK components.\n */\nexport function useSDK(): SDK {\n const { initResult } = useSDKContext();\n\n const sdk = createMemo(() => {\n const result = initResult();\n\n if (result === null) {\n throw new Error('Unable to use SDK as it is not ready.');\n }\n return result;\n });\n\n return {\n backButton: useDynamicComponent(sdk, 'backButton', ['isVisibleChanged']),\n closingBehavior: useDynamicComponent(sdk, 'closingBehavior', ['isConfirmationNeededChanged']),\n cloudStorage: useInitResultValue(sdk, 'cloudStorage'),\n haptic: useInitResultValue(sdk, 'haptic'),\n initData: useInitResultValue(sdk, 'initData'),\n initDataRaw: useInitResultValue(sdk, 'initDataRaw'),\n mainButton: useDynamicComponent(sdk, 'mainButton', [\n 'backgroundColorChanged',\n 'isVisibleChanged',\n 'isProgressVisibleChanged',\n 'isEnabledChanged',\n 'textChanged',\n 'textColorChanged',\n ]),\n popup: useDynamicComponent(sdk, 'popup', ['isOpenedChanged']),\n postEvent: useInitResultValue(sdk, 'postEvent'),\n qrScanner: useDynamicComponent(sdk, 'qrScanner', ['isOpenedChanged']),\n themeParams: useDynamicComponent(sdk, 'themeParams', ['changed']),\n viewport: useDynamicComponent(sdk, 'viewport', [\n 'heightChanged',\n 'isExpandedChanged',\n 'stableHeightChanged',\n 'widthChanged',\n ]),\n webApp: useDynamicComponent(sdk, 'webApp', ['backgroundColorChanged', 'headerColorChanged']),\n };\n}","import { createMemo, createResource, ParentProps } from 'solid-js';\nimport { init } from '@tma.js/sdk';\n\nimport { SDKContext } from './context.js';\nimport type { SDKInitOptions } from './types.js';\n\nexport type SDKProviderProps = ParentProps<{ initOptions?: SDKInitOptions }>;\n\nexport function SDKProvider(props: SDKProviderProps) {\n const initOptions = createMemo(() => props.initOptions || {});\n const [data] = createResource(initOptions, init);\n\n const initResult = createMemo(() => {\n return data.state === 'ready' ? data() : null;\n });\n const loading = createMemo(() => data.loading);\n const error = createMemo(() => data.error === undefined ? null : data.error);\n\n return (\n <SDKContext.Provider value={{ initResult, loading, error }}>\n {props.children}\n </SDKContext.Provider>\n );\n}\n"],"names":["SDKContext","createContext","undefined","name","useSDKContext","context","useContext","Error","useDynamicComponent","initResult","key","events","component","setComponent","createSignal","equals","createEffect","obj","forEach","event","on","onCleanup","off","useInitResultValue","createMemo","props","initOptions","data","createResource","init","state","loading","error","_$createComponent","createComponent","Provider","value","children","sdk","result","backButton","closingBehavior","cloudStorage","haptic","initData","initDataRaw","mainButton","popup","postEvent","qrScanner","themeParams","viewport","webApp"],"mappings":"2HAIaA,EAAaC,EAAaA,mBAAiBC,EAAW,CAAEC,KAAM,eCIpE,SAASC,IACd,MAAMC,EAAUC,aAAWN,GAE3B,QAAgBE,IAAZG,EACF,MAAM,IAAIE,MAAM,uDAElB,OAAOF,CACT,CCSA,SAASG,EACPC,EACAC,EACAC,GAEA,MAAOC,EAAWC,GAAgBC,EAAYA,aAACL,IAAaC,GAAM,CAAEK,QAAQ,IAgB5E,OAdAC,EAAAA,cAAa,KACX,MAAMC,EAAML,IAEZD,EAAOO,SAAQC,IACZF,EAAYG,GAAGD,GAAO,IAAMN,GAAa,IAAMI,KAAK,IAGvDI,EAAAA,WAAU,KACRV,EAAOO,SAAQC,IACZF,EAAYK,IAAIH,GAAO,IAAMN,GAAa,IAAMI,KAAK,GACtD,GACF,IAGGL,CACT,CAEA,SAASW,EAA+Cd,EAAqCC,GAG3F,OAFcc,EAAAA,YAAkC,IAAMf,IAAaC,IAGrE,oPC5CO,SAAqBe,GAC1B,MAAMC,EAAcF,EAAAA,YAAW,IAAMC,EAAMC,aAAe,CAAE,KACrDC,GAAQC,EAAcA,eAACF,EAAaG,EAAIA,MAEzCpB,EAAae,EAAAA,YAAW,IACN,UAAfG,EAAKG,MAAoBH,IAAS,OAErCI,EAAUP,EAAUA,YAAC,IAAMG,EAAKI,UAChCC,EAAQR,cAAW,SAAqBtB,IAAfyB,EAAKK,MAAsB,KAAOL,EAAKK,QAEtE,OAAAC,EAAAC,gBACGlC,EAAWmC,SAAQ,CAACC,MAAO,CAAE3B,aAAYsB,UAASC,SAAOK,eAAA,OACvDZ,EAAMY,QAAQ,GAGrB,iBDkCO,WACL,MAAM5B,WAAEA,GAAeL,IAEjBkC,EAAMd,EAAAA,YAAW,KACrB,MAAMe,EAAS9B,IAEf,GAAe,OAAX8B,EACF,MAAM,IAAIhC,MAAM,yCAElB,OAAOgC,CAAM,IAGf,MAAO,CACLC,WAAYhC,EAAoB8B,EAAK,aAAc,CAAC,qBACpDG,gBAAiBjC,EAAoB8B,EAAK,kBAAmB,CAAC,gCAC9DI,aAAcnB,EAAmBe,EAAK,gBACtCK,OAAQpB,EAAmBe,EAAK,UAChCM,SAAUrB,EAAmBe,EAAK,YAClCO,YAAatB,EAAmBe,EAAK,eACrCQ,WAAYtC,EAAoB8B,EAAK,aAAc,CACjD,yBACA,mBACA,2BACA,mBACA,cACA,qBAEFS,MAAOvC,EAAoB8B,EAAK,QAAS,CAAC,oBAC1CU,UAAWzB,EAAmBe,EAAK,aACnCW,UAAWzC,EAAoB8B,EAAK,YAAa,CAAC,oBAClDY,YAAa1C,EAAoB8B,EAAK,cAAe,CAAC,YACtDa,SAAU3C,EAAoB8B,EAAK,WAAY,CAC7C,gBACA,oBACA,sBACA,iBAEFc,OAAQ5C,EAAoB8B,EAAK,SAAU,CAAC,yBAA0B,uBAE1E"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{createContext as e,useContext as n,createMemo as t,createResource as o,createSignal as r,createEffect as a,onCleanup as i}from"solid-js";import{createComponent as s}from"solid-js/web";import{init as d}from"@tma.js/sdk";export{classNames,mergeClassNames}from"@tma.js/utils";const h=e(void 0,{name:"SDKContext"});function u(){const e=n(h);if(void 0===e)throw new Error("useSDKContext hook was used outside of SDKProvider.");return e}function l(e){const n=t((()=>e.initOptions||{})),[r]=o(n,d),a=t((()=>"ready"===r.state?r():null)),i=t((()=>r.loading)),u=t((()=>void 0===r.error?null:r.error));return s(h.Provider,{value:{initResult:a,loading:i,error:u},get children(){return e.children}})}function c(e,n,t){const[o,s]=r(e()[n],{equals:!1});return a((()=>{const e=o();t.forEach((n=>{e.on(n,(()=>s((()=>e))))})),i((()=>{t.forEach((n=>{e.off(n,(()=>s((()=>e))))}))}))})),o}function g(e,n){return t((()=>e()[n]))}function p(){const{initResult:e}=u(),n=t((()=>{const n=e();if(null===n)throw new Error("Unable to use SDK as it is not ready.");return n}));return{backButton:c(n,"backButton",["isVisibleChanged"]),closingBehavior:c(n,"closingBehavior",["isConfirmationNeededChanged"]),cloudStorage:g(n,"cloudStorage"),haptic:g(n,"haptic"),initData:g(n,"initData"),initDataRaw:g(n,"initDataRaw"),mainButton:c(n,"mainButton",["backgroundColorChanged","isVisibleChanged","isProgressVisibleChanged","isEnabledChanged","textChanged","textColorChanged"]),popup:c(n,"popup",["isOpenedChanged"]),postEvent:g(n,"postEvent"),qrScanner:c(n,"qrScanner",["isOpenedChanged"]),themeParams:c(n,"themeParams",["changed"]),viewport:c(n,"viewport",["heightChanged","isExpandedChanged","stableHeightChanged","widthChanged"]),webApp:c(n,"webApp",["backgroundColorChanged","headerColorChanged"])}}export{h as SDKContext,l as SDKProvider,p as useSDK,u as useSDKContext};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/context.ts","../../src/hooks.ts","../../src/SDKProvider.tsx","../../src/useSDK.ts"],"sourcesContent":["import { createContext } from 'solid-js';\n\nimport type { SDKContextType } from './types.js';\n\nexport const SDKContext = createContext<SDKContextType>(undefined, { name: 'SDKContext' });\n","import { useContext } from 'solid-js';\n\nimport { SDKContext } from './context.js';\nimport type { SDKContextType } from './types.js';\n\n/**\n * Uses SDKContext context.\n */\nexport function useSDKContext(): SDKContextType {\n const context = useContext(SDKContext);\n\n if (context === undefined) {\n throw new Error('useSDKContext hook was used outside of SDKProvider.');\n }\n return context;\n}\n","import { createMemo, createResource, ParentProps } from 'solid-js';\nimport { init } from '@tma.js/sdk';\n\nimport { SDKContext } from './context.js';\nimport type { SDKInitOptions } from './types.js';\n\nexport type SDKProviderProps = ParentProps<{ initOptions?: SDKInitOptions }>;\n\nexport function SDKProvider(props: SDKProviderProps) {\n const initOptions = createMemo(() => props.initOptions || {});\n const [data] = createResource(initOptions, init);\n\n const initResult = createMemo(() => {\n return data.state === 'ready' ? data() : null;\n });\n const loading = createMemo(() => data.loading);\n const error = createMemo(() => data.error === undefined ? null : data.error);\n\n return (\n <SDKContext.Provider value={{ initResult, loading, error }}>\n {props.children}\n </SDKContext.Provider>\n );\n}\n","import { type Accessor, createEffect, createMemo, createSignal, onCleanup } from 'solid-js';\n\nimport type { SDKInitResult, SDKInitResultKey, SDKInitResultValue } from './types.js';\nimport { useSDKContext } from './hooks.js';\n\ninterface Trackable {\n on: (event: any, ...args: any[]) => void;\n off: (event: any, ...args: any[]) => void;\n}\n\ntype EventName<T extends Trackable> = T extends {\n on(event: infer E, ...args: any[]): any\n} ? E : never;\n\ntype DynamicComponentKey = {\n [K in SDKInitResultKey]: SDKInitResultValue<K> extends Trackable\n ? K\n : never;\n}[SDKInitResultKey];\n\nexport type SDK = {\n [K in SDKInitResultKey]: Accessor<SDKInitResultValue<K>>\n};\n\nfunction useDynamicComponent<K extends DynamicComponentKey>(\n initResult: Accessor<SDKInitResult>,\n key: K,\n events: EventName<SDKInitResultValue<K>>[],\n): Accessor<SDKInitResultValue<K>> {\n const [component, setComponent] = createSignal(initResult()[key], { equals: false });\n\n createEffect(() => {\n const obj = component();\n\n events.forEach(event => {\n (obj as any).on(event, () => setComponent(() => obj));\n });\n\n onCleanup(() => {\n events.forEach(event => {\n (obj as any).off(event, () => setComponent(() => obj));\n });\n });\n });\n\n return component;\n}\n\nfunction useInitResultValue<K extends SDKInitResultKey>(initResult: Accessor<SDKInitResult>, key: K) {\n const value = createMemo<SDKInitResultValue<K>>(() => initResult()[key]);\n\n return value;\n}\n\n/**\n * Returns ready to use SDK components.\n */\nexport function useSDK(): SDK {\n const { initResult } = useSDKContext();\n\n const sdk = createMemo(() => {\n const result = initResult();\n\n if (result === null) {\n throw new Error('Unable to use SDK as it is not ready.');\n }\n return result;\n });\n\n return {\n backButton: useDynamicComponent(sdk, 'backButton', ['isVisibleChanged']),\n closingBehavior: useDynamicComponent(sdk, 'closingBehavior', ['isConfirmationNeededChanged']),\n cloudStorage: useInitResultValue(sdk, 'cloudStorage'),\n haptic: useInitResultValue(sdk, 'haptic'),\n initData: useInitResultValue(sdk, 'initData'),\n initDataRaw: useInitResultValue(sdk, 'initDataRaw'),\n mainButton: useDynamicComponent(sdk, 'mainButton', [\n 'backgroundColorChanged',\n 'isVisibleChanged',\n 'isProgressVisibleChanged',\n 'isEnabledChanged',\n 'textChanged',\n 'textColorChanged',\n ]),\n popup: useDynamicComponent(sdk, 'popup', ['isOpenedChanged']),\n postEvent: useInitResultValue(sdk, 'postEvent'),\n qrScanner: useDynamicComponent(sdk, 'qrScanner', ['isOpenedChanged']),\n themeParams: useDynamicComponent(sdk, 'themeParams', ['changed']),\n viewport: useDynamicComponent(sdk, 'viewport', [\n 'heightChanged',\n 'isExpandedChanged',\n 'stableHeightChanged',\n 'widthChanged',\n ]),\n webApp: useDynamicComponent(sdk, 'webApp', ['backgroundColorChanged', 'headerColorChanged']),\n };\n}"],"names":["SDKContext","createContext","undefined","name","useSDKContext","context","useContext","Error","SDKProvider","props","initOptions","createMemo","data","createResource","init","initResult","state","loading","error","_$createComponent","Provider","value","children","useDynamicComponent","key","events","component","setComponent","createSignal","equals","createEffect","obj","forEach","event","on","onCleanup","off","useInitResultValue","useSDK","sdk","result","backButton","closingBehavior","cloudStorage","haptic","initData","initDataRaw","mainButton","popup","postEvent","qrScanner","themeParams","viewport","webApp"],"mappings":"8RAIaA,EAAaC,OAA8BC,EAAW,CAAEC,KAAM,eCIpE,SAASC,IACd,MAAMC,EAAUC,EAAWN,GAE3B,QAAgBE,IAAZG,EACF,MAAM,IAAIE,MAAM,uDAElB,OAAOF,CACT,CCPO,SAASG,EAAYC,GAC1B,MAAMC,EAAcC,GAAW,IAAMF,EAAMC,aAAe,CAAE,KACrDE,GAAQC,EAAeH,EAAaI,GAErCC,EAAaJ,GAAW,IACN,UAAfC,EAAKI,MAAoBJ,IAAS,OAErCK,EAAUN,GAAW,IAAMC,EAAKK,UAChCC,EAAQP,GAAW,SAAqBT,IAAfU,EAAKM,MAAsB,KAAON,EAAKM,QAEtE,OAAAC,EACGnB,EAAWoB,SAAQ,CAACC,MAAO,CAAEN,aAAYE,UAASC,SAAOI,eAAA,OACvDb,EAAMa,QAAQ,GAGrB,CCCA,SAASC,EACPR,EACAS,EACAC,GAEA,MAAOC,EAAWC,GAAgBC,EAAab,IAAaS,GAAM,CAAEK,QAAQ,IAgB5E,OAdAC,GAAa,KACX,MAAMC,EAAML,IAEZD,EAAOO,SAAQC,IACZF,EAAYG,GAAGD,GAAO,IAAMN,GAAa,IAAMI,KAAK,IAGvDI,GAAU,KACRV,EAAOO,SAAQC,IACZF,EAAYK,IAAIH,GAAO,IAAMN,GAAa,IAAMI,KAAK,GACtD,GACF,IAGGL,CACT,CAEA,SAASW,EAA+CtB,EAAqCS,GAG3F,OAFcb,GAAkC,IAAMI,IAAaS,IAGrE,CAKO,SAASc,IACd,MAAMvB,WAAEA,GAAeX,IAEjBmC,EAAM5B,GAAW,KACrB,MAAM6B,EAASzB,IAEf,GAAe,OAAXyB,EACF,MAAM,IAAIjC,MAAM,yCAElB,OAAOiC,CAAM,IAGf,MAAO,CACLC,WAAYlB,EAAoBgB,EAAK,aAAc,CAAC,qBACpDG,gBAAiBnB,EAAoBgB,EAAK,kBAAmB,CAAC,gCAC9DI,aAAcN,EAAmBE,EAAK,gBACtCK,OAAQP,EAAmBE,EAAK,UAChCM,SAAUR,EAAmBE,EAAK,YAClCO,YAAaT,EAAmBE,EAAK,eACrCQ,WAAYxB,EAAoBgB,EAAK,aAAc,CACjD,yBACA,mBACA,2BACA,mBACA,cACA,qBAEFS,MAAOzB,EAAoBgB,EAAK,QAAS,CAAC,oBAC1CU,UAAWZ,EAAmBE,EAAK,aACnCW,UAAW3B,EAAoBgB,EAAK,YAAa,CAAC,oBAClDY,YAAa5B,EAAoBgB,EAAK,cAAe,CAAC,YACtDa,SAAU7B,EAAoBgB,EAAK,WAAY,CAC7C,gBACA,oBACA,sBACA,iBAEFc,OAAQ9B,EAAoBgB,EAAK,SAAU,CAAC,yBAA0B,uBAE1E"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createMemo, createResource } from 'solid-js';
|
|
2
|
+
import { init } from '@tma.js/sdk';
|
|
3
|
+
import { SDKContext } from './context.js';
|
|
4
|
+
export function SDKProvider(props) {
|
|
5
|
+
const initOptions = createMemo(() => props.initOptions || {});
|
|
6
|
+
const [data] = createResource(initOptions, init);
|
|
7
|
+
const initResult = createMemo(() => {
|
|
8
|
+
return data.state === 'ready' ? data() : null;
|
|
9
|
+
});
|
|
10
|
+
const loading = createMemo(() => data.loading);
|
|
11
|
+
const error = createMemo(() => data.error === undefined ? null : data.error);
|
|
12
|
+
return (<SDKContext.Provider value={{ initResult, loading, error }}>
|
|
13
|
+
{props.children}
|
|
14
|
+
</SDKContext.Provider>);
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from 'solid-js';
|
|
2
|
+
import { SDKContext } from './context.js';
|
|
3
|
+
/**
|
|
4
|
+
* Uses SDKContext context.
|
|
5
|
+
*/
|
|
6
|
+
export function useSDKContext() {
|
|
7
|
+
const context = useContext(SDKContext);
|
|
8
|
+
if (context === undefined) {
|
|
9
|
+
throw new Error('useSDKContext hook was used outside of SDKProvider.');
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createEffect, createMemo, createSignal, onCleanup } from 'solid-js';
|
|
2
|
+
import { useSDKContext } from './hooks.js';
|
|
3
|
+
function useDynamicComponent(initResult, key, events) {
|
|
4
|
+
const [component, setComponent] = createSignal(initResult()[key], { equals: false });
|
|
5
|
+
createEffect(() => {
|
|
6
|
+
const obj = component();
|
|
7
|
+
events.forEach(event => {
|
|
8
|
+
obj.on(event, () => setComponent(() => obj));
|
|
9
|
+
});
|
|
10
|
+
onCleanup(() => {
|
|
11
|
+
events.forEach(event => {
|
|
12
|
+
obj.off(event, () => setComponent(() => obj));
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
return component;
|
|
17
|
+
}
|
|
18
|
+
function useInitResultValue(initResult, key) {
|
|
19
|
+
const value = createMemo(() => initResult()[key]);
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns ready to use SDK components.
|
|
24
|
+
*/
|
|
25
|
+
export function useSDK() {
|
|
26
|
+
const { initResult } = useSDKContext();
|
|
27
|
+
const sdk = createMemo(() => {
|
|
28
|
+
const result = initResult();
|
|
29
|
+
if (result === null) {
|
|
30
|
+
throw new Error('Unable to use SDK as it is not ready.');
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
backButton: useDynamicComponent(sdk, 'backButton', ['isVisibleChanged']),
|
|
36
|
+
closingBehavior: useDynamicComponent(sdk, 'closingBehavior', ['isConfirmationNeededChanged']),
|
|
37
|
+
cloudStorage: useInitResultValue(sdk, 'cloudStorage'),
|
|
38
|
+
haptic: useInitResultValue(sdk, 'haptic'),
|
|
39
|
+
initData: useInitResultValue(sdk, 'initData'),
|
|
40
|
+
initDataRaw: useInitResultValue(sdk, 'initDataRaw'),
|
|
41
|
+
mainButton: useDynamicComponent(sdk, 'mainButton', [
|
|
42
|
+
'backgroundColorChanged',
|
|
43
|
+
'isVisibleChanged',
|
|
44
|
+
'isProgressVisibleChanged',
|
|
45
|
+
'isEnabledChanged',
|
|
46
|
+
'textChanged',
|
|
47
|
+
'textColorChanged',
|
|
48
|
+
]),
|
|
49
|
+
popup: useDynamicComponent(sdk, 'popup', ['isOpenedChanged']),
|
|
50
|
+
postEvent: useInitResultValue(sdk, 'postEvent'),
|
|
51
|
+
qrScanner: useDynamicComponent(sdk, 'qrScanner', ['isOpenedChanged']),
|
|
52
|
+
themeParams: useDynamicComponent(sdk, 'themeParams', ['changed']),
|
|
53
|
+
viewport: useDynamicComponent(sdk, 'viewport', [
|
|
54
|
+
'heightChanged',
|
|
55
|
+
'isExpandedChanged',
|
|
56
|
+
'stableHeightChanged',
|
|
57
|
+
'widthChanged',
|
|
58
|
+
]),
|
|
59
|
+
webApp: useDynamicComponent(sdk, 'webApp', ['backgroundColorChanged', 'headerColorChanged']),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ParentProps } from 'solid-js';
|
|
2
|
+
import type { SDKInitOptions } from './types.js';
|
|
3
|
+
export type SDKProviderProps = ParentProps<{
|
|
4
|
+
initOptions?: SDKInitOptions;
|
|
5
|
+
}>;
|
|
6
|
+
export declare function SDKProvider(props: SDKProviderProps): import("solid-js").JSX.Element;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { InitResult, InitOptions } from '@tma.js/sdk';
|
|
2
|
+
import { Accessor } from 'solid-js';
|
|
3
|
+
export type SDKInitOptions = InitOptions;
|
|
4
|
+
export type SDKInitResult = InitResult;
|
|
5
|
+
export type SDKInitResultKey = keyof SDKInitResult;
|
|
6
|
+
export type SDKInitResultValue<K extends SDKInitResultKey> = SDKInitResult[K];
|
|
7
|
+
export interface SDKContextType {
|
|
8
|
+
initResult: Accessor<SDKInitResult | null>;
|
|
9
|
+
loading: Accessor<boolean>;
|
|
10
|
+
error: Accessor<unknown | null>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Accessor } from 'solid-js';
|
|
2
|
+
import type { SDKInitResultKey, SDKInitResultValue } from './types.js';
|
|
3
|
+
export type SDK = {
|
|
4
|
+
[K in SDKInitResultKey]: Accessor<SDKInitResultValue<K>>;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Returns ready to use SDK components.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useSDK(): SDK;
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tma.js/sdk-solid",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Solid JS bindings for Web Apps client SDK.",
|
|
5
|
+
"author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
|
|
6
|
+
"homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git@github.com:Telegram-Mini-Apps/tma.js.git",
|
|
10
|
+
"directory": "packages/sdk-solid"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Telegram-Mini-Apps/tma.js/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"telegram-mini-apps",
|
|
17
|
+
"typescript",
|
|
18
|
+
"sdk",
|
|
19
|
+
"solidjs"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src"
|
|
27
|
+
],
|
|
28
|
+
"source": "src/index.ts",
|
|
29
|
+
"main": "dist/cjs/index.js",
|
|
30
|
+
"module": "dist/esm/index.js",
|
|
31
|
+
"types": "dist/types/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/types/index.d.ts",
|
|
35
|
+
"solid": "./dist/source/index.js",
|
|
36
|
+
"import": "./dist/esm/index.js",
|
|
37
|
+
"require": "./dist/cjs/index.js",
|
|
38
|
+
"node": "./dist/cjs/index.js",
|
|
39
|
+
"default": "./dist/cjs/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./*": {
|
|
42
|
+
"types": "./dist/types/*.d.ts",
|
|
43
|
+
"solid": "./dist/source/*.js",
|
|
44
|
+
"import": "./dist/esm/*.js",
|
|
45
|
+
"require": "./dist/cjs/*.js",
|
|
46
|
+
"node": "./dist/cjs/*.js",
|
|
47
|
+
"default": "./dist/cjs/*.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@tma.js/utils": "0.5.2",
|
|
52
|
+
"@tma.js/sdk": "0.11.3"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"solid-js": "^1.0.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"eslint-config-custom": "0.1.0",
|
|
59
|
+
"tsconfig": "0.0.2"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"lint": "eslint -c .eslintrc.cjs src/**/*",
|
|
66
|
+
"build": "rimraf dist && rollup --config rollup.config.js"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createMemo, createResource, ParentProps } from 'solid-js';
|
|
2
|
+
import { init } from '@tma.js/sdk';
|
|
3
|
+
|
|
4
|
+
import { SDKContext } from './context.js';
|
|
5
|
+
import type { SDKInitOptions } from './types.js';
|
|
6
|
+
|
|
7
|
+
export type SDKProviderProps = ParentProps<{ initOptions?: SDKInitOptions }>;
|
|
8
|
+
|
|
9
|
+
export function SDKProvider(props: SDKProviderProps) {
|
|
10
|
+
const initOptions = createMemo(() => props.initOptions || {});
|
|
11
|
+
const [data] = createResource(initOptions, init);
|
|
12
|
+
|
|
13
|
+
const initResult = createMemo(() => {
|
|
14
|
+
return data.state === 'ready' ? data() : null;
|
|
15
|
+
});
|
|
16
|
+
const loading = createMemo(() => data.loading);
|
|
17
|
+
const error = createMemo(() => data.error === undefined ? null : data.error);
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<SDKContext.Provider value={{ initResult, loading, error }}>
|
|
21
|
+
{props.children}
|
|
22
|
+
</SDKContext.Provider>
|
|
23
|
+
);
|
|
24
|
+
}
|
package/src/context.ts
ADDED
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useContext } from 'solid-js';
|
|
2
|
+
|
|
3
|
+
import { SDKContext } from './context.js';
|
|
4
|
+
import type { SDKContextType } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Uses SDKContext context.
|
|
8
|
+
*/
|
|
9
|
+
export function useSDKContext(): SDKContextType {
|
|
10
|
+
const context = useContext(SDKContext);
|
|
11
|
+
|
|
12
|
+
if (context === undefined) {
|
|
13
|
+
throw new Error('useSDKContext hook was used outside of SDKProvider.');
|
|
14
|
+
}
|
|
15
|
+
return context;
|
|
16
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { InitResult, InitOptions } from '@tma.js/sdk';
|
|
2
|
+
import { Accessor } from 'solid-js';
|
|
3
|
+
|
|
4
|
+
export type SDKInitOptions = InitOptions;
|
|
5
|
+
export type SDKInitResult = InitResult;
|
|
6
|
+
export type SDKInitResultKey = keyof SDKInitResult;
|
|
7
|
+
export type SDKInitResultValue<K extends SDKInitResultKey> = SDKInitResult[K];
|
|
8
|
+
|
|
9
|
+
export interface SDKContextType {
|
|
10
|
+
initResult: Accessor<SDKInitResult | null>;
|
|
11
|
+
loading: Accessor<boolean>;
|
|
12
|
+
error: Accessor<unknown | null>;
|
|
13
|
+
}
|
package/src/useSDK.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type Accessor, createEffect, createMemo, createSignal, onCleanup } from 'solid-js';
|
|
2
|
+
|
|
3
|
+
import type { SDKInitResult, SDKInitResultKey, SDKInitResultValue } from './types.js';
|
|
4
|
+
import { useSDKContext } from './hooks.js';
|
|
5
|
+
|
|
6
|
+
interface Trackable {
|
|
7
|
+
on: (event: any, ...args: any[]) => void;
|
|
8
|
+
off: (event: any, ...args: any[]) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type EventName<T extends Trackable> = T extends {
|
|
12
|
+
on(event: infer E, ...args: any[]): any
|
|
13
|
+
} ? E : never;
|
|
14
|
+
|
|
15
|
+
type DynamicComponentKey = {
|
|
16
|
+
[K in SDKInitResultKey]: SDKInitResultValue<K> extends Trackable
|
|
17
|
+
? K
|
|
18
|
+
: never;
|
|
19
|
+
}[SDKInitResultKey];
|
|
20
|
+
|
|
21
|
+
export type SDK = {
|
|
22
|
+
[K in SDKInitResultKey]: Accessor<SDKInitResultValue<K>>
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function useDynamicComponent<K extends DynamicComponentKey>(
|
|
26
|
+
initResult: Accessor<SDKInitResult>,
|
|
27
|
+
key: K,
|
|
28
|
+
events: EventName<SDKInitResultValue<K>>[],
|
|
29
|
+
): Accessor<SDKInitResultValue<K>> {
|
|
30
|
+
const [component, setComponent] = createSignal(initResult()[key], { equals: false });
|
|
31
|
+
|
|
32
|
+
createEffect(() => {
|
|
33
|
+
const obj = component();
|
|
34
|
+
|
|
35
|
+
events.forEach(event => {
|
|
36
|
+
(obj as any).on(event, () => setComponent(() => obj));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
onCleanup(() => {
|
|
40
|
+
events.forEach(event => {
|
|
41
|
+
(obj as any).off(event, () => setComponent(() => obj));
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return component;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function useInitResultValue<K extends SDKInitResultKey>(initResult: Accessor<SDKInitResult>, key: K) {
|
|
50
|
+
const value = createMemo<SDKInitResultValue<K>>(() => initResult()[key]);
|
|
51
|
+
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns ready to use SDK components.
|
|
57
|
+
*/
|
|
58
|
+
export function useSDK(): SDK {
|
|
59
|
+
const { initResult } = useSDKContext();
|
|
60
|
+
|
|
61
|
+
const sdk = createMemo(() => {
|
|
62
|
+
const result = initResult();
|
|
63
|
+
|
|
64
|
+
if (result === null) {
|
|
65
|
+
throw new Error('Unable to use SDK as it is not ready.');
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
backButton: useDynamicComponent(sdk, 'backButton', ['isVisibleChanged']),
|
|
72
|
+
closingBehavior: useDynamicComponent(sdk, 'closingBehavior', ['isConfirmationNeededChanged']),
|
|
73
|
+
cloudStorage: useInitResultValue(sdk, 'cloudStorage'),
|
|
74
|
+
haptic: useInitResultValue(sdk, 'haptic'),
|
|
75
|
+
initData: useInitResultValue(sdk, 'initData'),
|
|
76
|
+
initDataRaw: useInitResultValue(sdk, 'initDataRaw'),
|
|
77
|
+
mainButton: useDynamicComponent(sdk, 'mainButton', [
|
|
78
|
+
'backgroundColorChanged',
|
|
79
|
+
'isVisibleChanged',
|
|
80
|
+
'isProgressVisibleChanged',
|
|
81
|
+
'isEnabledChanged',
|
|
82
|
+
'textChanged',
|
|
83
|
+
'textColorChanged',
|
|
84
|
+
]),
|
|
85
|
+
popup: useDynamicComponent(sdk, 'popup', ['isOpenedChanged']),
|
|
86
|
+
postEvent: useInitResultValue(sdk, 'postEvent'),
|
|
87
|
+
qrScanner: useDynamicComponent(sdk, 'qrScanner', ['isOpenedChanged']),
|
|
88
|
+
themeParams: useDynamicComponent(sdk, 'themeParams', ['changed']),
|
|
89
|
+
viewport: useDynamicComponent(sdk, 'viewport', [
|
|
90
|
+
'heightChanged',
|
|
91
|
+
'isExpandedChanged',
|
|
92
|
+
'stableHeightChanged',
|
|
93
|
+
'widthChanged',
|
|
94
|
+
]),
|
|
95
|
+
webApp: useDynamicComponent(sdk, 'webApp', ['backgroundColorChanged', 'headerColorChanged']),
|
|
96
|
+
};
|
|
97
|
+
}
|