Skip to content

Commit a9b926b

Browse files
committed
feat: ship duel esm & cjs
1 parent 3bf0434 commit a9b926b

File tree

10 files changed

+200
-123
lines changed

10 files changed

+200
-123
lines changed

example/index.ts

Lines changed: 0 additions & 113 deletions
This file was deleted.

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"vrchat": "^1.19.4"
1919
},
2020
"devDependencies": {
21-
"@types/prompts": "^2.4.9"
21+
"@types/prompts": "^2.4.9",
22+
"typescript": "^5.8.3"
2223
}
2324
}

example/pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/automatic-sessions.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import KeyvFile from "keyv-file";
2+
import prompts from "prompts";
3+
import { VRChat } from "vrchat";
4+
5+
const vrchat = new VRChat({
6+
application: {
7+
name: "Example",
8+
version: "1.5.1",
9+
contact: "timmy@examaple.com"
10+
},
11+
authentication: {
12+
optimistic: false,
13+
credentials: () => prompts([
14+
{
15+
name: "username",
16+
type: "text",
17+
message: "VRChat username",
18+
},
19+
{
20+
name: "password",
21+
type: "password",
22+
message: "VRChat password",
23+
},
24+
{
25+
name: "twoFactorCode",
26+
type: "text",
27+
message: "Two-factor authentication code",
28+
}
29+
]),
30+
},
31+
keyv: new KeyvFile({ filename: "./data.json" }),
32+
});
33+
34+
(async () => {
35+
await vrchat.getCurrentSubscriptions({ throwOnError: true });
36+
const { data: { displayName } } = await vrchat.getCurrentUser({ throwOnError: true });
37+
// eslint-disable-next-line no-console
38+
console.log(`Logged in as ${displayName}`);
39+
// await Promise.all(Array.from({ length: 10 }).map(() => vrchat.getCurrentOnlineUsers({ throwOnError: true })));
40+
})();

example/src/default.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* eslint-disable no-console */
2+
import KeyvFile from "keyv-file";
3+
import prompts from "prompts";
4+
import { VRChat, VRChatError } from "vrchat";
5+
6+
const vrchat = new VRChat({
7+
/**
8+
* When using the VRChat API, you must provide an application name, version, and contact information.
9+
* This is used to identify your application to VRChat, and to provide support if needed.
10+
*/
11+
application: {
12+
name: "Example",
13+
version: "1.5.1",
14+
/**
15+
* An email, or a URL to a support page.
16+
*/
17+
contact: "timmy@examaple.com"
18+
},
19+
/**
20+
* Save cookies to a file, so you can use them later.
21+
*
22+
* This is optional, but recommended for persistent sessions.
23+
* If you don't provide a `keyv` instance, it will use an in-memory store.
24+
*
25+
* You can use any Keyv-compatible adapter for this, such as `keyv-file`, `@keyv/redis`, or `@keyv/postgres`.
26+
* See https://keyv.org/docs/keyv/#official-storage-adapters for more information.
27+
*/
28+
keyv: new KeyvFile({ filename: "./data.json" }),
29+
});
30+
31+
(async () => {
32+
const { data: user } = await vrchat
33+
.getCurrentUser({ throwOnError: true })
34+
// Since we've passed `throwOnError: true`, this will throw an error if the user is not logged in.
35+
// We can catch that error and prompt the user for their credentials.
36+
.catch(async (reason) => {
37+
// Rethrow the error if it's not a 401 Unauthorized error.
38+
if (!(reason instanceof VRChatError) || reason.statusCode !== 401)
39+
throw reason;
40+
41+
/**
42+
* Ask the user for their VRChat username and password.
43+
* You can also use environment variables, or any other method to get the credentials.
44+
*/
45+
const { username, password } = await prompts([
46+
{
47+
name: "username",
48+
type: "text",
49+
message: "VRChat username",
50+
},
51+
{
52+
name: "password",
53+
type: "password",
54+
message: "VRChat password",
55+
},
56+
]);
57+
58+
return vrchat.login({
59+
username,
60+
password,
61+
/**
62+
* This function will be called when two-factor authentication is required.
63+
* Accepts any code, such as from an authenticator app or a recovery code.
64+
*/
65+
twoFactorCode: async () => {
66+
const { code } = await prompts({
67+
name: "code",
68+
type: "text",
69+
message: "Two-factor authentication code",
70+
});
71+
72+
return code;
73+
},
74+
/**
75+
* Or, automatically generate a code from a secret.
76+
* Useful for service accounts & automated workflows.
77+
*
78+
* If this is a user-initiated login, don't use this.
79+
*/
80+
// totpSecret: "",
81+
throwOnError: true,
82+
});
83+
});
84+
85+
console.log(`Logged in as ${user.displayName} (id: ${user.id}).`);
86+
87+
/**
88+
* Get 10 online friends, you can change the `n` parameter to get more or less friends.
89+
* Exercise: You can also use the `offline` query parameter to include offline friends.
90+
*/
91+
const { data: friends } = await vrchat.getFriends({ query: { n: 10 }, throwOnError: true });
92+
93+
console.log(
94+
friends.length > 0
95+
? "Your friends:"
96+
: "Aw, you don't have any online friends."
97+
);
98+
for (const friend of friends) {
99+
console.log(`- ${friend.displayName} (id: ${friend.id})`);
100+
}
101+
102+
const tupperId = "usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469";
103+
104+
await vrchat
105+
// Add Tupper as a friend, he's a nice guy.
106+
.friend({ path: { userId: tupperId }, throwOnError: true })
107+
// If we've already sent one, we'll catch the error and ignore it.
108+
.catch(() => { });
109+
110+
const { data: { outgoingRequest } } = await vrchat
111+
.getFriendStatus({ path: { userId: tupperId }, throwOnError: true });
112+
113+
// Tupper ghosted our friend request, so let's delete it.
114+
if (outgoingRequest) await vrchat.deleteFriendRequest({ path: { userId: tupperId }, throwOnError: true });
115+
})();

example/tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"display": "Node 16",
4+
"compilerOptions": {
5+
"target": "ES2021",
6+
"lib": [
7+
"ES2021"
8+
],
9+
"module": "commonjs",
10+
"strict": true,
11+
"esModuleInterop": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"skipLibCheck": true,
14+
"outDir": "./dist"
15+
}
16+
}

openapi-ts.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default defineConfig({
5959
asClass: true,
6060
instance: true,
6161
transformer: true,
62-
classNameBuilder: () => "_VRChat",
62+
classNameBuilder: () => "VRChatInternal",
6363
},
6464
customPlugin as any
6565
],

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"name": "vrchat",
3-
"version": "2.0.0",
4-
"type": "module",
3+
"version": "0.0.0-next.0",
54
"description": "",
6-
"main": "dist/index.js",
5+
"type": "commonjs",
76
"types": "dist/index.d.ts",
87
"homepage": "https://vrchat.community/javascript",
98
"repository": "vrchatapi/vrchatapi-javascript",
@@ -13,6 +12,13 @@
1312
"files": [
1413
"dist"
1514
],
15+
"exports": {
16+
".": {
17+
"types": "./dist/index.d.ts",
18+
"import": "./dist/index.mjs",
19+
"require": "./dist/index.js"
20+
}
21+
},
1622
"scripts": {
1723
"prebuild": "rm src/generated -rf && openapi-ts",
1824
"build": "tsup"

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/* Language and Environment */
1212
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
1313
"lib": [
14-
"es2022",
14+
"esnext",
1515
"dom"
1616
],
1717
// "jsx": "preserve", /* Specify what JSX code is generated. */

tsup.config.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { defineConfig } from "tsup";
22

3-
import * as package_ from "./package.json";
3+
import { version } from "./package.json";
44

55
export default defineConfig({
66
entry: ["src/index.ts"],
7-
format: "esm",
7+
format: [
8+
"esm",
9+
"cjs"
10+
],
811
dts: true,
912
sourcemap: true,
1013
// treeshake: true,
1114
minify: true,
1215
clean: true,
1316
env: {
14-
VERSION: package_.version,
15-
ISSUE_URL: package_.bugs.url,
17+
VERSION: version
1618
}
1719
});

0 commit comments

Comments
 (0)