@adlad/plugin-viverse 0.5.0 → 0.7.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.
- package/deno.lock +2 -2
- package/dist/adlad-plugin-viverse.d.ts +35 -5
- package/dist/adlad-plugin-viverse.js +1 -1
- package/mod.js +1 -0
- package/package.json +1 -1
- package/src/mod.js +50 -7
- package/src/types.d.ts +13 -0
package/deno.lock
CHANGED
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"@jridgewell/sourcemap-codec"
|
|
34
34
|
]
|
|
35
35
|
},
|
|
36
|
-
"acorn@8.
|
|
37
|
-
"integrity": "sha512-
|
|
36
|
+
"acorn@8.16.0": {
|
|
37
|
+
"integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==",
|
|
38
38
|
"bin": true
|
|
39
39
|
},
|
|
40
40
|
"buffer-from@1.1.2": {
|
|
@@ -1,13 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef ViverseProfile
|
|
3
|
+
* @property {string} name
|
|
4
|
+
* @property {ViverseAvatar | null} activeAvatar
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* @typedef ViverseAvatar
|
|
8
|
+
* @property {string | number} id
|
|
9
|
+
* @property {boolean} isPrivate
|
|
10
|
+
* @property {string} vrmUrl
|
|
11
|
+
* @property {string} headIconUrl
|
|
12
|
+
* @property {string} snapshot
|
|
13
|
+
* @property {number} createTime
|
|
14
|
+
* @property {number} updateTime
|
|
15
|
+
*/
|
|
1
16
|
/**
|
|
2
17
|
* @param {Object} options
|
|
3
18
|
* @param {string} options.appId
|
|
4
|
-
* @param {string | undefined} [options.domain]
|
|
5
|
-
* @param {string | undefined} [options.cookieDomain]
|
|
6
19
|
*/
|
|
7
|
-
export function viversePlugin({ appId,
|
|
20
|
+
export function viversePlugin({ appId, }: {
|
|
8
21
|
appId: string;
|
|
9
|
-
domain?: string | undefined;
|
|
10
|
-
cookieDomain?: string | undefined;
|
|
11
22
|
}): {
|
|
12
23
|
readonly name: "viverse";
|
|
13
24
|
readonly initialize: (ctx: any) => Promise<void>;
|
|
@@ -23,5 +34,24 @@ export function viversePlugin({ appId, domain, cookieDomain, }: {
|
|
|
23
34
|
*/
|
|
24
35
|
readonly getStorageItem: (key: string) => Promise<unknown>;
|
|
25
36
|
readonly showAuthPrompt: () => void;
|
|
37
|
+
readonly getIsSignedIn: () => boolean;
|
|
38
|
+
/**
|
|
39
|
+
* @returns {Promise<ViverseProfile?>}
|
|
40
|
+
*/
|
|
41
|
+
readonly getProfile: () => Promise<ViverseProfile | null>;
|
|
26
42
|
};
|
|
27
43
|
};
|
|
44
|
+
export default viversePlugin;
|
|
45
|
+
export type ViverseProfile = {
|
|
46
|
+
name: string;
|
|
47
|
+
activeAvatar: ViverseAvatar | null;
|
|
48
|
+
};
|
|
49
|
+
export type ViverseAvatar = {
|
|
50
|
+
id: string | number;
|
|
51
|
+
isPrivate: boolean;
|
|
52
|
+
vrmUrl: string;
|
|
53
|
+
headIconUrl: string;
|
|
54
|
+
snapshot: string;
|
|
55
|
+
createTime: number;
|
|
56
|
+
updateTime: number;
|
|
57
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function viversePlugin({appId:e
|
|
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 as default,viversePlugin};
|
package/mod.js
CHANGED
package/package.json
CHANGED
package/src/mod.js
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef ViverseProfile
|
|
3
|
+
* @property {string} name
|
|
4
|
+
* @property {ViverseAvatar | null} activeAvatar
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef ViverseAvatar
|
|
9
|
+
* @property {string | number} id
|
|
10
|
+
* @property {boolean} isPrivate
|
|
11
|
+
* @property {string} vrmUrl
|
|
12
|
+
* @property {string} headIconUrl
|
|
13
|
+
* @property {string} snapshot
|
|
14
|
+
* @property {number} createTime
|
|
15
|
+
* @property {number} updateTime
|
|
16
|
+
*/
|
|
17
|
+
|
|
1
18
|
/**
|
|
2
19
|
* @param {Object} options
|
|
3
20
|
* @param {string} options.appId
|
|
4
|
-
* @param {string | undefined} [options.domain]
|
|
5
|
-
* @param {string | undefined} [options.cookieDomain]
|
|
6
21
|
*/
|
|
7
22
|
export function viversePlugin({
|
|
8
23
|
appId,
|
|
9
|
-
domain = "account.htcvive.com",
|
|
10
|
-
cookieDomain = undefined,
|
|
11
24
|
}) {
|
|
12
25
|
let initializeCalled = false;
|
|
13
26
|
|
|
@@ -27,6 +40,9 @@ export function viversePlugin({
|
|
|
27
40
|
newCloudSaveClient: "newCloudSaveClient",
|
|
28
41
|
setPlayerData: "setPlayerData",
|
|
29
42
|
getPlayerData: "getPlayerData",
|
|
43
|
+
avatar: "avatar",
|
|
44
|
+
baseURL: "baseURL",
|
|
45
|
+
token: "token",
|
|
30
46
|
});
|
|
31
47
|
|
|
32
48
|
// @ts-ignore We want to make sure that `props` remains an object.
|
|
@@ -46,6 +62,20 @@ export function viversePlugin({
|
|
|
46
62
|
/** @type {string?} */
|
|
47
63
|
let accessToken = null;
|
|
48
64
|
|
|
65
|
+
/** @type {import("./types.d.ts").ViverseAvatarClient?} */
|
|
66
|
+
let avatarClient = null;
|
|
67
|
+
|
|
68
|
+
function getAvatarClient() {
|
|
69
|
+
if (!accessToken) return null;
|
|
70
|
+
if (!avatarClient) {
|
|
71
|
+
avatarClient = new viverse[props.avatar]({
|
|
72
|
+
[props.baseURL]: "https://sdk-api.viverse.com/",
|
|
73
|
+
[props.token]: accessToken,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return avatarClient;
|
|
77
|
+
}
|
|
78
|
+
|
|
49
79
|
/** @type {Map<string, string>} */
|
|
50
80
|
const sessionStorage = new Map();
|
|
51
81
|
|
|
@@ -64,15 +94,15 @@ export function viversePlugin({
|
|
|
64
94
|
|
|
65
95
|
client = new globalThis[props.viverse][props.client]({
|
|
66
96
|
[props.clientId]: appId,
|
|
67
|
-
[props.domain]:
|
|
68
|
-
[props.cookieDomain]:
|
|
97
|
+
[props.domain]: "account.htcvive.com",
|
|
98
|
+
[props.cookieDomain]: window.location.hostname,
|
|
69
99
|
});
|
|
70
100
|
|
|
71
101
|
// If we're not running in an iframe, the client.checkAuth() promise stays pending forever.
|
|
72
102
|
// 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
103
|
// That way at least some of the api calls stay working.
|
|
74
104
|
if (window.parent == window) {
|
|
75
|
-
throw new Error("The Viverse SDK is not supported unless the game is embedded on
|
|
105
|
+
throw new Error("The Viverse SDK is not supported unless the game is embedded on viverse.com.");
|
|
76
106
|
}
|
|
77
107
|
|
|
78
108
|
const authResult = await client[props.checkAuth]();
|
|
@@ -139,8 +169,21 @@ export function viversePlugin({
|
|
|
139
169
|
showAuthPrompt() {
|
|
140
170
|
client[props.loginWithWorlds]();
|
|
141
171
|
},
|
|
172
|
+
getIsSignedIn() {
|
|
173
|
+
return Boolean(accessToken);
|
|
174
|
+
},
|
|
175
|
+
/**
|
|
176
|
+
* @returns {Promise<ViverseProfile?>}
|
|
177
|
+
*/
|
|
178
|
+
async getProfile() {
|
|
179
|
+
const client = getAvatarClient();
|
|
180
|
+
if (!client) return null;
|
|
181
|
+
return await client.getProfile();
|
|
182
|
+
},
|
|
142
183
|
},
|
|
143
184
|
});
|
|
144
185
|
|
|
145
186
|
return plugin;
|
|
146
187
|
}
|
|
188
|
+
|
|
189
|
+
export default viversePlugin;
|
package/src/types.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import type { ViverseProfile } from "./mod.js";
|
|
2
|
+
|
|
1
3
|
declare global {
|
|
2
4
|
var viverse: {
|
|
3
5
|
client: typeof ViverseClient;
|
|
4
6
|
storage: typeof ViverseStorage;
|
|
7
|
+
avatar: typeof ViverseAvatarClient;
|
|
5
8
|
};
|
|
6
9
|
}
|
|
7
10
|
|
|
@@ -34,4 +37,14 @@ export class CloudSaveClient {
|
|
|
34
37
|
getPlayerData(key: string, accessToken: string): Promise<unknown>;
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
interface ViverseAvatarClientOptions {
|
|
41
|
+
baseURL: string;
|
|
42
|
+
token: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class ViverseAvatarClient {
|
|
46
|
+
constructor(options: ViverseAvatarClientOptions);
|
|
47
|
+
getProfile(): Promise<ViverseProfile>;
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
export {};
|