@adlad/plugin-viverse 0.4.0 → 0.6.0

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.
@@ -1,13 +1,9 @@
1
1
  /**
2
2
  * @param {Object} options
3
3
  * @param {string} options.appId
4
- * @param {string | undefined} [options.domain]
5
- * @param {string | undefined} [options.cookieDomain]
6
4
  */
7
- export function viversePlugin({ appId, domain, cookieDomain, }: {
5
+ export function viversePlugin({ appId, }: {
8
6
  appId: string;
9
- domain?: string | undefined;
10
- cookieDomain?: string | undefined;
11
7
  }): {
12
8
  readonly name: "viverse";
13
9
  readonly initialize: (ctx: any) => Promise<void>;
@@ -23,5 +19,7 @@ export function viversePlugin({ appId, domain, cookieDomain, }: {
23
19
  */
24
20
  readonly getStorageItem: (key: string) => Promise<unknown>;
25
21
  readonly showAuthPrompt: () => void;
22
+ readonly getIsSignedIn: () => boolean;
23
+ readonly getProfile: () => Promise<any>;
26
24
  };
27
25
  };
@@ -1 +1 @@
1
- function viversePlugin({appId:e,domain:t="account.htcvive.com",cookieDomain:i}){let a=!1;if(!e)throw new Error("No app id was provided");const o={viverse:"viverse",client:"client",clientId:"clientId",domain:"domain",cookieDomain:"cookieDomain",checkAuth:"checkAuth",loginWithWorlds:"loginWithWorlds",storage:"storage",newCloudSaveClient:"newCloudSaveClient",setPlayerData:"setPlayerData",getPlayerData:"getPlayerData"};let n,s;o>0&&console.log(o);let l=null;const r=new Map,c="adlad_viverse_";return{name:"viverse",async initialize(r){if(a)throw new Error("Viverse plugin is being initialized more than once");if(a=!0,await r.loadScriptTag("https://www.viverse.com/static-assets/viverse-sdk/index.umd.cjs"),n=new globalThis[o.viverse][o.client]({[o.clientId]:e,[o.domain]:t,[o.cookieDomain]:i}),window.parent==window)throw new Error("The Viverse SDK is not supported unless the game is embedded on the viverse.com.");const c=await n[o.checkAuth]();l=c?.access_token||null;const d=new globalThis[o.viverse][o.storage];s=await d[o.newCloudSaveClient](e)},customRequests:{async setStorageItem(e,t){const i=JSON.stringify(t);if(l)await s[o.setPlayerData](e,{s:i},l);else try{localStorage.setItem(c+e,i)}catch{r.set(e,i)}},async getStorageItem(e){let t;if(l){const i=await s[o.getPlayerData](e,l);t="object"==typeof i&&null!=i?i.s:null}else try{t=localStorage.getItem(c+e)}catch{t=r.get(e)}return t&&"string"==typeof t?JSON.parse(t):null},showAuthPrompt(){n[o.loginWithWorlds]()}}}}export{viversePlugin};
1
+ function viversePlugin({appId:e}){let t=!1;if(!e)throw new Error("No app id was provided");const a={viverse:"viverse",client:"client",clientId:"clientId",domain:"domain",cookieDomain:"cookieDomain",checkAuth:"checkAuth",loginWithWorlds:"loginWithWorlds",storage:"storage",newCloudSaveClient:"newCloudSaveClient",setPlayerData:"setPlayerData",getPlayerData:"getPlayerData",avatar:"avatar",baseURL:"baseURL",token:"token"};let i,o;a>0&&console.log(a);let n=null,s=null;const l=new Map,r="adlad_viverse_";return{name:"viverse",async initialize(s){if(t)throw new Error("Viverse plugin is being initialized more than once");if(t=!0,await s.loadScriptTag("https://www.viverse.com/static-assets/viverse-sdk/index.umd.cjs"),i=new globalThis[a.viverse][a.client]({[a.clientId]:e,[a.domain]:"account.htcvive.com",[a.cookieDomain]:window.location.hostname}),window.parent==window)throw new Error("The Viverse SDK is not supported unless the game is embedded on viverse.com.");const l=await i[a.checkAuth]();n=l?.access_token||null;const r=new globalThis[a.viverse][a.storage];o=await r[a.newCloudSaveClient](e)},customRequests:{async setStorageItem(e,t){const i=JSON.stringify(t);if(n)await o[a.setPlayerData](e,{s:i},n);else{await new Promise((e=>setTimeout(e,1e3)));try{localStorage.setItem(r+e,i)}catch{l.set(e,i)}}},async getStorageItem(e){let t;if(n){const i=await o[a.getPlayerData](e,n);t="object"==typeof i&&null!=i?i.s:null}else{await new Promise((e=>setTimeout(e,1e3)));try{t=localStorage.getItem(r+e)}catch{t=l.get(e)}}return t&&"string"==typeof t?JSON.parse(t):null},showAuthPrompt(){i[a.loginWithWorlds]()},getIsSignedIn:()=>Boolean(n),async getProfile(){const e=n?(s||(s=new viverse[a.avatar]({[a.baseURL]:"https://sdk-api.viverse.com/",[a.token]:n})),s):null;return e?await e.getProfile():null}}}}export{viversePlugin};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adlad/plugin-viverse",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "AdLad plugin for the Viverse sdk.",
5
5
  "main": "mod.js",
6
6
  "types": "dist/adlad-plugin-viverse.d.ts",
package/src/mod.js CHANGED
@@ -1,13 +1,9 @@
1
1
  /**
2
2
  * @param {Object} options
3
3
  * @param {string} options.appId
4
- * @param {string | undefined} [options.domain]
5
- * @param {string | undefined} [options.cookieDomain]
6
4
  */
7
5
  export function viversePlugin({
8
6
  appId,
9
- domain = "account.htcvive.com",
10
- cookieDomain = undefined,
11
7
  }) {
12
8
  let initializeCalled = false;
13
9
 
@@ -27,6 +23,9 @@ export function viversePlugin({
27
23
  newCloudSaveClient: "newCloudSaveClient",
28
24
  setPlayerData: "setPlayerData",
29
25
  getPlayerData: "getPlayerData",
26
+ avatar: "avatar",
27
+ baseURL: "baseURL",
28
+ token: "token",
30
29
  });
31
30
 
32
31
  // @ts-ignore We want to make sure that `props` remains an object.
@@ -46,6 +45,20 @@ export function viversePlugin({
46
45
  /** @type {string?} */
47
46
  let accessToken = null;
48
47
 
48
+ /** @type {import("./types.d.ts").ViverseAvatarClient?} */
49
+ let avatarClient = null;
50
+
51
+ function getAvatarClient() {
52
+ if (!accessToken) return null;
53
+ if (!avatarClient) {
54
+ avatarClient = new viverse[props.avatar]({
55
+ [props.baseURL]: "https://sdk-api.viverse.com/",
56
+ [props.token]: accessToken,
57
+ });
58
+ }
59
+ return avatarClient;
60
+ }
61
+
49
62
  /** @type {Map<string, string>} */
50
63
  const sessionStorage = new Map();
51
64
 
@@ -64,15 +77,15 @@ export function viversePlugin({
64
77
 
65
78
  client = new globalThis[props.viverse][props.client]({
66
79
  [props.clientId]: appId,
67
- [props.domain]: domain,
68
- [props.cookieDomain]: cookieDomain,
80
+ [props.domain]: "account.htcvive.com",
81
+ [props.cookieDomain]: window.location.hostname,
69
82
  });
70
83
 
71
84
  // If we're not running in an iframe, the client.checkAuth() promise stays pending forever.
72
85
  // But since it is pretty common to run games in local development without an iframe, we'll also make this check here and throw an error.
73
86
  // That way at least some of the api calls stay working.
74
87
  if (window.parent == window) {
75
- throw new Error("The Viverse SDK is not supported unless the game is embedded on the viverse.com.");
88
+ throw new Error("The Viverse SDK is not supported unless the game is embedded on viverse.com.");
76
89
  }
77
90
 
78
91
  const authResult = await client[props.checkAuth]();
@@ -98,6 +111,9 @@ export function viversePlugin({
98
111
  // So in order to prevent the sdk from parsing our strings, we'll just provide an object.
99
112
  await cloudSaveClient[props.setPlayerData](key, { s: stringifiedValue }, accessToken);
100
113
  } else {
114
+ // Calls to the viverse sdk normally do not resolve instantly, which might cause bugs in the application.
115
+ // Let's simulate this behavior so it's easier to reproduce these issues in local development.
116
+ await new Promise((resolve) => setTimeout(resolve, 1000));
101
117
  try {
102
118
  localStorage.setItem(localStoragePrefix + key, stringifiedValue);
103
119
  } catch {
@@ -121,6 +137,9 @@ export function viversePlugin({
121
137
  stringifiedValue = null;
122
138
  }
123
139
  } else {
140
+ // Calls to the viverse sdk normally do not resolve instantly, which might cause bugs in the application.
141
+ // Let's simulate this behavior so it's easier to reproduce these issues in local development.
142
+ await new Promise((resolve) => setTimeout(resolve, 1000));
124
143
  try {
125
144
  stringifiedValue = localStorage.getItem(localStoragePrefix + key);
126
145
  } catch {
@@ -133,6 +152,14 @@ export function viversePlugin({
133
152
  showAuthPrompt() {
134
153
  client[props.loginWithWorlds]();
135
154
  },
155
+ getIsSignedIn() {
156
+ return Boolean(accessToken);
157
+ },
158
+ async getProfile() {
159
+ const client = getAvatarClient();
160
+ if (!client) return null;
161
+ return await client.getProfile();
162
+ },
136
163
  },
137
164
  });
138
165
 
package/src/types.d.ts CHANGED
@@ -2,6 +2,7 @@ declare global {
2
2
  var viverse: {
3
3
  client: typeof ViverseClient;
4
4
  storage: typeof ViverseStorage;
5
+ avatar: typeof ViverseAvatarClient;
5
6
  };
6
7
  }
7
8
 
@@ -34,4 +35,29 @@ export class CloudSaveClient {
34
35
  getPlayerData(key: string, accessToken: string): Promise<unknown>;
35
36
  }
36
37
 
38
+ interface ViverseAvatarClientOptions {
39
+ baseURL: string;
40
+ token: string;
41
+ }
42
+
43
+ export class ViverseAvatarClient {
44
+ constructor(options: ViverseAvatarClientOptions);
45
+ getProfile(): Promise<ViverseAvatar>;
46
+ }
47
+
48
+ type ViverseProfile = {
49
+ name: string;
50
+ activeAvatar: ViverseAvatar | null;
51
+ };
52
+
53
+ type ViverseAvatar = {
54
+ id: string | number;
55
+ isPrivate: boolean;
56
+ vrmUrl: string;
57
+ headIconUrl: string;
58
+ snapshot: string;
59
+ createTime: number;
60
+ updateTime: number;
61
+ };
62
+
37
63
  export {};