feat: Create Instance, Auto Instance Region

This commit is contained in:
2026-06-28 23:31:02 +07:00
parent 23748b15ae
commit d9ed86fe77
24 changed files with 865 additions and 19 deletions
+46 -1
View File
@@ -1,6 +1,12 @@
import type { Instance } from "../../shared/types/instance";
import type {
Instance,
CreateInstanceInput,
CreateInstanceType,
} from "../../shared/types/instance";
import { toInstance } from "./mappers";
import { cachedRead } from "./cachedRead";
import { requireActiveClient } from "./client";
import { currentUser } from "./userService";
import { cacheKeys, policies } from "../cache/policies";
export async function getInstance(worldId: string, instanceId: string): Promise<Instance> {
@@ -13,3 +19,42 @@ export async function getInstance(worldId: string, instanceId: string): Promise<
},
);
}
type SdkType = "public" | "friends" | "hidden" | "private";
function mapCreateType(type: CreateInstanceType): { type: SdkType; canRequestInvite?: boolean } {
switch (type) {
case "public":
return { type: "public" };
case "friends+":
return { type: "hidden" };
case "friends":
return { type: "friends" };
case "invite":
return { type: "private" };
case "invite+":
return { type: "private", canRequestInvite: true };
}
}
export async function createInstance(input: CreateInstanceInput): Promise<Instance> {
const vrc = requireActiveClient();
const sdk = mapCreateType(input.type);
const ownerId = sdk.type === "public" ? undefined : (await currentUser()).id;
const { data } = await vrc.createInstance({
body: {
worldId: input.worldId,
region: input.region,
type: sdk.type,
...(ownerId ? { ownerId } : {}),
...(sdk.canRequestInvite ? { canRequestInvite: true } : {}),
},
throwOnError: true,
});
return toInstance(data);
}
export async function inviteSelf(worldId: string, instanceId: string): Promise<void> {
const vrc = requireActiveClient();
await vrc.inviteMyselfTo({ path: { worldId, instanceId }, throwOnError: true });
}
+2
View File
@@ -211,6 +211,8 @@ export function toInstance(raw: SdkInstance): Instance {
full: raw.full ?? false,
queueEnabled: raw.queueEnabled ?? false,
queueSize: raw.queueSize ?? 0,
secureName: raw.secureName ?? undefined,
shortName: raw.shortName ?? undefined,
};
}