@metagptx/web-sdk 0.0.26 → 0.0.28

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/dist/index.d.ts CHANGED
@@ -1,6 +1,63 @@
1
1
  import * as axios0 from "axios";
2
2
  import { AxiosRequestConfig } from "axios";
3
3
 
4
+ //#region src/modules/frame/frame.d.ts
5
+
6
+ /**
7
+ * Default message type definitions
8
+ * Extend this interface to add custom message types
9
+ */
10
+ interface DefaultFrameMessageTypes {
11
+ 'mgx-appview-error': {
12
+ errMsg: string;
13
+ stack: string;
14
+ };
15
+ 'mgx-create-page': {
16
+ path: string;
17
+ };
18
+ 'mgx-token-request': {
19
+ domain: string;
20
+ };
21
+ }
22
+ /**
23
+ * Message type map interface
24
+ * Extend this interface to add custom message types and their corresponding data types
25
+ * @example
26
+ * ```typescript
27
+ * interface CustomMessageTypes extends FrameMessageTypes {
28
+ * 'custom-type': { customField: string };
29
+ * }
30
+ * ```
31
+ */
32
+ interface FrameMessageTypes extends DefaultFrameMessageTypes {}
33
+ /**
34
+ * Extract message type keys from FrameMessageTypes
35
+ */
36
+ type FrameMessageType = keyof FrameMessageTypes;
37
+ /**
38
+ * Extract data type for a specific message type
39
+ */
40
+ type FrameMessageData<T extends FrameMessageType> = FrameMessageTypes[T];
41
+ /**
42
+ * PostMessage options interface
43
+ */
44
+ interface PostMessageOptions {
45
+ /**
46
+ * Target origin for the message
47
+ * @default '*'
48
+ */
49
+ targetOrigin?: string;
50
+ /**
51
+ * Target window name
52
+ * @default window.name
53
+ */
54
+ targetName?: string;
55
+ }
56
+ /**
57
+ * Creates a frame module for frame-related operations
58
+ * @returns Object containing frame operation methods
59
+ */
60
+ //#endregion
4
61
  //#region src/utils/request.d.ts
5
62
  /**
6
63
  * Request instance configuration options
@@ -97,59 +154,6 @@ interface ApiCallParams {
97
154
  * @returns Object containing invoke method for API calls
98
155
  */
99
156
  //#endregion
100
- //#region src/modules/frame.d.ts
101
- /**
102
- * Default message type definitions
103
- * Extend this interface to add custom message types
104
- */
105
- interface DefaultFrameMessageTypes {
106
- 'mgx-appview-error': {
107
- errMsg: string;
108
- stack: string;
109
- };
110
- 'mgx-create-page': {
111
- path: string;
112
- };
113
- }
114
- /**
115
- * Message type map interface
116
- * Extend this interface to add custom message types and their corresponding data types
117
- * @example
118
- * ```typescript
119
- * interface CustomMessageTypes extends FrameMessageTypes {
120
- * 'custom-type': { customField: string };
121
- * }
122
- * ```
123
- */
124
- interface FrameMessageTypes extends DefaultFrameMessageTypes {}
125
- /**
126
- * Extract message type keys from FrameMessageTypes
127
- */
128
- type FrameMessageType = keyof FrameMessageTypes;
129
- /**
130
- * Extract data type for a specific message type
131
- */
132
- type FrameMessageData<T extends FrameMessageType> = FrameMessageTypes[T];
133
- /**
134
- * PostMessage options interface
135
- */
136
- interface PostMessageOptions {
137
- /**
138
- * Target origin for the message
139
- * @default '*'
140
- */
141
- targetOrigin?: string;
142
- /**
143
- * Target window name
144
- * @default window.name
145
- */
146
- targetName?: string;
147
- }
148
- /**
149
- * Creates a frame module for frame-related operations
150
- * @returns Object containing frame operation methods
151
- */
152
- //#endregion
153
157
  //#region src/modules/integrations.d.ts
154
158
  /**
155
159
  * Integration function parameters interface
@@ -183,6 +187,7 @@ interface PluginOptions {
183
187
  */
184
188
  declare function viteDefaultRoutes(options?: PluginOptions): {
185
189
  name: string;
190
+ enforce: string;
186
191
  resolveId(id: string): string | null;
187
192
  load(id: string): string | null;
188
193
  transform(code: string, id: string): {
@@ -191,4 +196,4 @@ declare function viteDefaultRoutes(options?: PluginOptions): {
191
196
  } | null;
192
197
  };
193
198
  //#endregion
194
- export { type AnyType, type ApiCallParams, type ClientConfig, type DefaultFrameMessageTypes, type FrameMessageData, type FrameMessageType, type FrameMessageTypes, type IntegrationFunction, type IntegrationParams, type PostMessageOptions, type RequestConfig, createClient, viteDefaultRoutes };
199
+ export { type AnyType, type ApiCallParams, type ClientConfig, type IntegrationFunction, type IntegrationParams, type RequestConfig, createClient, viteDefaultRoutes };
package/dist/index.js CHANGED
@@ -1,10 +1,12 @@
1
- import e from"axios";import t from"qs";const n=e=>{let t=e.response?.status;if(t&&t>=400&&t!==401)try{typeof window<`u`&&window.top&&window.top?.postMessage({type:`mgx-appview-error`,targetName:window.name,data:{errMsg:e.response?.data?.message||e.response?.data?.detail||`Server Error`,stack:JSON.stringify({url:e.response?.config?.url,data:e.response?.data,status:e.response?.status})}},`*`)}catch(e){console.warn(`Failed to notify parent window about API error:`,e)}},r=(r={})=>{let{onUnauthorized:i,...a}=r,o=typeof globalThis<`u`&&`localStorage`in globalThis&&typeof globalThis.localStorage?.getItem==`function`?globalThis.localStorage.getItem(`token`)??void 0:void 0,s={timeout:6e4,paramsSerializer:e=>t.stringify(e,{arrayFormat:`brackets`}),...a,headers:{"Content-Type":`application/json`,...o?{Authorization:`Bearer ${o}`}:{},...a.headers}},c=e.create(s);return c.interceptors.request.use(e=>e,e=>Promise.reject(e)),c.interceptors.response.use(e=>e,e=>{let t=e.response?.status;return t===401&&i&&i(),n(e),Promise.reject(e)}),c},i=e=>typeof e==`string`?e.startsWith(`_`)||e.startsWith(`$`)||e===`constructor`||e===`toString`||e===`valueOf`||e===`inspect`||e===`toJSON`:!0,a=()=>{let e=window.location?.href??``,t=``;e.startsWith(`/`)?t=e:e&&(t=e.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]+/i,``));let n=new URLSearchParams({from_url:t});window.location.href=`/api/v1/auth/login?${n.toString()}`},o=e=>{let{requestInstance:t}=e;return{async login(){let e=new URLSearchParams(window.location.search),t=e.get(`token`);return t&&typeof globalThis<`u`&&`localStorage`in globalThis&&(globalThis.localStorage?.setItem(`token`,t),window.location.href=`/`),t},async me(){return t.get(`/api/v1/auth/me`)},async logout(){typeof globalThis<`u`&&`localStorage`in globalThis&&globalThis.localStorage?.removeItem(`token`);let e=await t.get(`/api/v1/auth/logout`);return window.location.href=`/`,e},toLogin:a}},s=e=>{let{requestInstance:t,entityName:n}=e,r=`/api/v1/entities/${n}`,i=e=>{if(!e)return;let t={...e};return e.fields&&Array.isArray(e.fields)&&(t.fields=e.fields.join(`,`)),e.query&&typeof e.query==`object`&&(t.query=JSON.stringify(e.query)),t};return{async query(e){let n=i(e);return t.get(r,{params:n})},async queryAll(e){let n=i(e);return t.get(`${r}/all`,{params:n})},async get(e){let{id:n,...a}=e,o=i(a);return t.get(`${r}/${n}`,{params:o})},async create(e){return t.post(r,e.data)},async update(e){return t.put(`${r}/${e.id}`,e.data)},async delete(e){return t.delete(`${r}/${e.id}`)},async deleteBatch(e){return t.delete(`${r}/batch`,{data:{ids:e.ids}})},async createBatch(e){return t.post(`${r}/batch`,e.data)},async updateBatch(e){return t.put(`${r}/batch`,e.data)}}},c=e=>{let{requestInstance:t}=e,n=new Map;return new Proxy({},{get(e,r){if(!i(r))return n.has(r)||n.set(r,s({requestInstance:t,entityName:r})),n.get(r)}})},l=e=>{let{requestInstance:t}=e;return{async invoke(e){let{url:n,method:r=`GET`,data:i,options:a={}}=e,o={method:r.toUpperCase(),url:n,...a};return i&&[`POST`,`PUT`,`PATCH`].includes(o.method)?o.data=i:i&&[`GET`,`DELETE`].includes(o.method)&&(o.params=i),t.request(o)}}},u=e=>{let{requestInstance:t}=e;return new Proxy({},{get(e,n){if(!i(n))return new Proxy({},{get(e,r){if(!i(r))return(e={})=>{let i=`/api/integrations/core/${r}`;n!==`core`&&(i=`/api/integrations/providers/${n}/${r}`);let{payload:a={},option:o={}}=e,s=a instanceof FormData?{...o,headers:{...o.headers,"Content-Type":void 0}}:o;return t.post(i,a,s)}}})}})},d=()=>({postMessage(e,t,n){if(typeof window>`u`||!window.top){console.warn(`postMessage: window.top is not available`);return}try{let{targetOrigin:r=`*`,targetName:i=window.name}=n||{},a={type:e,targetName:i,data:t};window.top.postMessage(a,r)}catch(e){console.warn(`Failed to send postMessage:`,e)}},createPage(e){let t=e??window.location.pathname;this.postMessage(`mgx-create-page`,{path:t})}}),f=(e={})=>{let t=r({baseURL:`/`,...e}),n=o({requestInstance:t}),i=c({requestInstance:t}),a=l({requestInstance:t}),s=u({requestInstance:t}),f=d();return{auth:n,entities:i,apiCall:a,integrations:s,frame:f}},p=`import { Button } from '@/components/ui/button';
1
+ import e from"axios";import t from"qs";const n=e=>{let t=e.response?.status;if(t&&t>=400&&t!==401)try{typeof window<`u`&&window.top&&window.top?.postMessage({type:`mgx-appview-error`,targetName:window.name,data:{errMsg:e.response?.data?.message||e.response?.data?.detail||`Server Error`,stack:JSON.stringify({url:e.response?.config?.url,data:e.response?.data,status:e.response?.status})}},`*`)}catch(e){console.warn(`Failed to notify parent window about API error:`,e)}},r=(r={})=>{let{onUnauthorized:i,...a}=r,o=typeof globalThis<`u`&&`localStorage`in globalThis&&typeof globalThis.localStorage?.getItem==`function`?globalThis.localStorage.getItem(`token`)??void 0:void 0,s={timeout:6e4,paramsSerializer:e=>t.stringify(e,{arrayFormat:`brackets`}),...a,headers:{"Content-Type":`application/json`,...o?{Authorization:`Bearer ${o}`}:{},...a.headers}},c=e.create(s);return c.interceptors.request.use(e=>e,e=>Promise.reject(e)),c.interceptors.response.use(e=>e,e=>{let t=e.response?.status;return t===401&&i&&i(),n(e),Promise.reject(e)}),c},i=e=>typeof e==`string`?e.startsWith(`_`)||e.startsWith(`$`)||e===`constructor`||e===`toString`||e===`valueOf`||e===`inspect`||e===`toJSON`:!0,a=()=>{let e=window.location?.href??``,t=``;e.startsWith(`/`)?t=e:e&&(t=e.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]+/i,``));let n=new URLSearchParams({from_url:t});window.location.href=`/api/v1/auth/login?${n.toString()}`},o=e=>{let{requestInstance:t}=e;return{async login(){let e=new URLSearchParams(window.location.search),t=e.get(`token`);return t&&typeof globalThis<`u`&&`localStorage`in globalThis&&(globalThis.localStorage?.setItem(`token`,t),window.location.href=`/`),t},async me(){return t.get(`/api/v1/auth/me`)},async logout(){typeof globalThis<`u`&&`localStorage`in globalThis&&globalThis.localStorage?.removeItem(`token`);let e=await t.get(`/api/v1/auth/logout`);return window.location.href=`/`,e},toLogin:a}},s=e=>{let{requestInstance:t,entityName:n}=e,r=`/api/v1/entities/${n}`,i=e=>{if(!e)return;let t={...e};return e.fields&&Array.isArray(e.fields)&&(t.fields=e.fields.join(`,`)),e.query&&typeof e.query==`object`&&(t.query=JSON.stringify(e.query)),t};return{async query(e){let n=i(e);return t.get(r,{params:n})},async queryAll(e){let n=i(e);return t.get(`${r}/all`,{params:n})},async get(e){let{id:n,...a}=e,o=i(a);return t.get(`${r}/${n}`,{params:o})},async create(e){return t.post(r,e.data)},async update(e){return t.put(`${r}/${e.id}`,e.data)},async delete(e){return t.delete(`${r}/${e.id}`)},async deleteBatch(e){return t.delete(`${r}/batch`,{data:{ids:e.ids}})},async createBatch(e){return t.post(`${r}/batch`,e.data)},async updateBatch(e){return t.put(`${r}/batch`,e.data)}}},c=e=>{let{requestInstance:t}=e,n=new Map;return new Proxy({},{get(e,r){if(!i(r))return n.has(r)||n.set(r,s({requestInstance:t,entityName:r})),n.get(r)}})},l=e=>{let{requestInstance:t}=e;return{async invoke(e){let{url:n,method:r=`GET`,data:i,options:a={}}=e,o={method:r.toUpperCase(),url:n,...a};return i&&[`POST`,`PUT`,`PATCH`].includes(o.method)?o.data=i:i&&[`GET`,`DELETE`].includes(o.method)&&(o.params=i),t.request(o)}}},u=e=>{let{requestInstance:t}=e;return new Proxy({},{get(e,n){if(!i(n))return new Proxy({},{get(e,r){if(!i(r))return(e={})=>{let i=`/api/integrations/core/${r}`;n!==`core`&&(i=`/api/integrations/providers/${n}/${r}`);let{payload:a={},option:o={}}=e,s=a instanceof FormData?{...o,headers:{...o.headers,"Content-Type":void 0}}:o;return t.post(i,a,s)}}})}})},d=e=>{let{requestInstance:t}=e,n=async e=>{let n=e.token,r=await t.post(`/api/v1/auth/token/exchange`,{platform_token:n});r.status===200&&r.data.token&&(globalThis.localStorage?.setItem(`token`,n),window.location.href=`/`)},r=e=>{switch(e.data.type){case`mgx-token-saved`:n(e.data.data);break;default:break}};window.addEventListener(`message`,r)},f=e=>{d(e);let t={postMessage(e,t,n){if(typeof window>`u`||!window.top){console.warn(`postMessage: window.top is not available`);return}try{let{targetOrigin:r=`*`,targetName:i=window.name}=n||{},a={type:e,targetName:i,data:t};window.top.postMessage(a,r)}catch(e){console.warn(`Failed to send postMessage:`,e)}},createPage(e){let t=e??window.location.pathname;this.postMessage(`mgx-create-page`,{path:t})}},n=()=>{let e=localStorage.getItem(`token`);e||t.postMessage(`mgx-token-request`,{domain:window.location.href})};return n(),t},p=(e={})=>{let t=r({baseURL:`/`,...e}),n=o({requestInstance:t}),i=c({requestInstance:t}),a=l({requestInstance:t}),s=u({requestInstance:t}),d=f({requestInstance:t});return{auth:n,entities:i,apiCall:a,integrations:s,frame:d}},m=`import { Button } from '@/components/ui/button';
2
2
  import { createClient } from '@metagptx/web-sdk';
3
3
 
4
4
  // Create client instance
5
- export const client = createClient();
5
+ const client = createClient();
6
6
 
7
7
  export default function NotFoundPage() {
8
+ const isInMGXIframe =
9
+ window.self !== window.top && window.name?.includes('devIframe');
8
10
  return (
9
11
  <div className="min-h-screen flex flex-col items-center justify-center bg-gradient-to-br from-gray-50 to-blue-50 p-6 text-center">
10
12
  <div className="space-y-6 max-w-md">
@@ -22,17 +24,23 @@ export default function NotFoundPage() {
22
24
  <Button asChild>
23
25
  <a href="/">Return Home</a>
24
26
  </Button>
25
- <Button variant="outline" onClick={() =>
26
- client.frame.createPage()
27
- }>
28
- Create page
29
- </Button>
27
+ {isInMGXIframe ? (
28
+ <Button variant="outline" onClick={() => client.frame.createPage()}>
29
+ Create page
30
+ </Button>
31
+ ) : (
32
+ <Button
33
+ variant="outline"
34
+ onClick={() => (window.location.href = 'https://mgx.dev')}
35
+ >
36
+ Go to MGX
37
+ </Button>
38
+ )}
30
39
  </div>
31
40
  </div>
32
41
  </div>
33
42
  );
34
43
  }
35
-
36
- `,m=[{path:`*`,componentName:`NotFoundPage`,componentCode:p}];function h(e={}){let t=e.routes||m,n=new Map;return t.forEach(e=>{let t=`\0virtual:${e.componentName.toLowerCase()}.tsx`;n.set(t,e.componentCode)}),{name:`vite-default-routes`,enforce:`pre`,resolveId(e){for(let n of t){let t=`virtual:${n.componentName.toLowerCase()}`;if(e===t)return`\0${t}.tsx`}return null},load(e){return n.get(e)||null},transform(e,n){if(!n.includes(`App.tsx`)||n.includes(`node_modules`))return null;let r=t.every(t=>e.includes(`import ${t.componentName}`)&&e.includes(t.path));if(r)return null;let i=e,a=[];if(t.forEach(t=>{if(!e.includes(`import ${t.componentName}`)){let e=`virtual:${t.componentName.toLowerCase()}`;a.push(`import ${t.componentName} from '${e}';`)}}),a.length>0){let n=e.match(/(import AuthError from[^;]+;)/);n&&(i=i.replace(n[0],`${n[0]}\n${a.join(`
44
+ `,h=[{path:`*`,componentName:`NotFoundPage`,componentCode:m}];function g(e={}){let t=e.routes||h,n=new Map;return t.forEach(e=>{let t=`\0virtual:${e.componentName.toLowerCase()}.tsx`;n.set(t,e.componentCode)}),{name:`vite-default-routes`,enforce:`pre`,resolveId(e){for(let n of t){let t=`virtual:${n.componentName.toLowerCase()}`;if(e===t)return`\0${t}.tsx`}return null},load(e){return n.get(e)||null},transform(e,n){if(!n.includes(`App.tsx`)||n.includes(`node_modules`))return null;let r=t.every(t=>e.includes(`import ${t.componentName}`)&&e.includes(t.path));if(r)return null;let i=e,a=[];if(t.forEach(t=>{if(!e.includes(`import ${t.componentName}`)){let e=`virtual:${t.componentName.toLowerCase()}`;a.push(`import ${t.componentName} from '${e}';`)}}),a.length>0){let n=e.match(/(import AuthError from[^;]+;)/);n&&(i=i.replace(n[0],`${n[0]}\n${a.join(`
37
45
  `)}`));let r=t,o=r.map(e=>` <Route path="${e.path}" element={<${e.componentName} />} />`).join(`
38
- `),s=i.match(/(\s+)(<\/Routes>)/i);s&&(i=i.replace(s[0],`${s[1]}${o}\n${s[0]}`))}return i===e?null:{code:i,map:null}}}}export{f as createClient,h as viteDefaultRoutes};
46
+ `),s=i.match(/(\s+)(<\/Routes>)/i);s&&(i=i.replace(s[0],`${s[1]}${o}\n${s[0]}`))}return i===e?null:{code:i,map:null}}}}export{p as createClient,g as viteDefaultRoutes};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@metagptx/web-sdk",
3
3
  "type": "module",
4
- "version": "0.0.26",
4
+ "version": "0.0.28",
5
5
  "packageManager": "pnpm@10.15.0+sha512.486ebc259d3e999a4e8691ce03b5cac4a71cbeca39372a9b762cb500cfdf0873e2cb16abe3d951b1ee2cf012503f027b98b6584e4df22524e0c7450d9ec7aa7b",
6
6
  "description": "TypeScript SDK for interacting with FuncSea API",
7
7
  "author": "MetaGPTX",