@playcademy/sdk 0.0.1-beta.2 → 0.0.1-beta.21

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/README.md CHANGED
@@ -64,7 +64,7 @@ async function initializeAndUseClient() {
64
64
 
65
65
  // If the token has appropriate permissions, you can access all namespaces:
66
66
  // client.dev.games.upsert(...)
67
- // client.admin.rewards.createReward(...)
67
+ // client.admin.items.createItem(...)
68
68
  // Calling a method without sufficient token permissions will result in a server error.
69
69
 
70
70
  // Example: Listen for auth changes (e.g., if token is refreshed or cleared by logout)
@@ -93,9 +93,9 @@ async function runGame() {
93
93
  const { sessionId } = await client.games.startSession()
94
94
  console.log('Session started:', sessionId)
95
95
 
96
- // 2) Fetch player's inventory/rewards
97
- const rewards = await client.users.inventory.get()
98
- console.log('Player inventory:', rewards)
96
+ // 2) Fetch player's inventory/items
97
+ const items = await client.users.inventory.get()
98
+ console.log('Player inventory:', items)
99
99
 
100
100
  // 3) Save game state (uses client.gameId implicitly)
101
101
  await client.games.saveState({
@@ -136,8 +136,8 @@ All methods returning data are strongly typed.
136
136
  - `me()`: Fetch current user details.
137
137
  - **`inventory`**:
138
138
  - `get()`: Get player inventory.
139
- - `add(rewardId, qty)`: Add item to player inventory.
140
- - `spend(rewardId, qty)`: Spend item from player inventory.
139
+ - `add(itemId, qty)`: Add item to player inventory.
140
+ - `spend(itemId, qty)`: Spend item from player inventory.
141
141
  - **`progress`**: Manages persistent progress data for a game (e.g., levels completed, scores, collectibles).
142
142
  - `get(gameId?)`: Get the entire progress state for a game. `gameId` is optional and defaults to the client's current game context.
143
143
  - `update(data, gameId?)`: Update the progress state for a game. `gameId` is optional. The `data` object can be structured to hold progress for various internal nodes or aspects of the game.
@@ -157,7 +157,11 @@ All methods returning data are strongly typed.
157
157
  - **`dev.games`**: Upsert, update, delete games.
158
158
  - **`dev.keys`**: Create, list, revoke API keys for games.
159
159
  - **`admin.games`**: Pause/resume games.
160
- - **`admin.rewards`**: CRUD operations for rewards.
160
+ - **`admin.items`**: CRUD operations for items.
161
+ - **`admin.currencies`**: CRUD operations for currencies.
162
+ - **`admin.shopListings`**: CRUD operations for shop listings.
163
+ - **`shop`**: Player-facing shop operations.
164
+ - `view()`: Get shop items and currency information.
161
165
  - **`telemetry`**: Push metrics.
162
166
 
163
167
  ## Contributing
@@ -1,41 +1,113 @@
1
1
  import { type Method } from './request';
2
- import { type Game, type GameWithManifest, type UpsertGameMetadataInput, type InsertReward, type UpdateReward } from '@playcademy/data/schemas';
3
- import type { GameState, InventoryItemWithReward, ClientConfig, ClientEvents, LoginResponse, GameTokenResponse, StartSessionResponse, InventoryMutationResponse, DeveloperStatusValue } from '../types';
2
+ import { init, login } from './static';
3
+ import type { ClientConfig, ClientEvents } from '../types';
4
+ /**
5
+ * Main Playcademy SDK client for interacting with the platform API.
6
+ * Provides namespaced access to all platform features including games, users, inventory, and more.
7
+ */
4
8
  export declare class PlaycademyClient {
5
9
  private baseUrl;
6
10
  private token?;
7
11
  private gameId?;
8
12
  private listeners;
9
13
  private internalClientSessionId?;
10
- constructor(config: ClientConfig);
11
- private _initializeInternalSession;
14
+ /**
15
+ * Creates a new PlaycademyClient instance.
16
+ *
17
+ * @param config - Optional configuration object
18
+ * @param config.baseUrl - Base URL for API requests (defaults to '/api')
19
+ * @param config.token - Authentication token
20
+ * @param config.gameId - Game ID for automatic session management
21
+ */
22
+ constructor(config?: Partial<ClientConfig>);
23
+ /**
24
+ * Gets the effective base URL for API requests.
25
+ * Converts relative URLs to absolute URLs in browser environments.
26
+ *
27
+ * @returns The complete base URL for API requests
28
+ */
12
29
  getBaseUrl(): string;
13
- on<E extends keyof ClientEvents>(event: E, callback: (payload: ClientEvents[E]) => void): void;
14
- private emit;
30
+ /**
31
+ * Simple ping method for testing connectivity.
32
+ *
33
+ * @returns 'pong' string response
34
+ */
35
+ ping(): string;
36
+ /**
37
+ * Sets the authentication token for API requests.
38
+ * Emits an 'authChange' event when the token changes.
39
+ *
40
+ * @param token - The authentication token, or null to clear
41
+ */
15
42
  setToken(token: string | null): void;
43
+ /**
44
+ * Registers a callback to be called when authentication state changes.
45
+ *
46
+ * @param callback - Function to call when auth state changes
47
+ */
16
48
  onAuthChange(callback: (token: string | null) => void): void;
49
+ /**
50
+ * Registers an event listener for client events.
51
+ *
52
+ * @param event - The event type to listen for
53
+ * @param callback - Function to call when the event is emitted
54
+ */
55
+ on<E extends keyof ClientEvents>(event: E, callback: (payload: ClientEvents[E]) => void): void;
56
+ /**
57
+ * Emits an event to all registered listeners.
58
+ *
59
+ * @param event - The event type to emit
60
+ * @param payload - The event payload
61
+ */
62
+ private emit;
63
+ /**
64
+ * Makes an authenticated HTTP request to the API.
65
+ *
66
+ * @param path - API endpoint path
67
+ * @param method - HTTP method
68
+ * @param body - Request body (optional)
69
+ * @param headers - Additional headers (optional)
70
+ * @returns Promise resolving to the response data
71
+ */
17
72
  protected request<T>(path: string, method: Method, body?: unknown, headers?: Record<string, string>): Promise<T>;
73
+ /**
74
+ * Ensures a gameId is available, throwing an error if not.
75
+ *
76
+ * @returns The gameId
77
+ * @throws PlaycademyError if no gameId is configured
78
+ */
18
79
  private _ensureGameId;
80
+ /**
81
+ * Initializes an internal game session for automatic session management.
82
+ * Called automatically when a gameId is provided in the constructor.
83
+ */
84
+ private _initializeInternalSession;
85
+ /** Authentication methods (logout) */
19
86
  auth: {
20
87
  logout: () => Promise<void>;
21
88
  };
89
+ /** Runtime methods (getGameToken, exit) */
22
90
  runtime: {
23
91
  getGameToken: (gameId: string, options?: {
24
92
  apply?: boolean;
25
- }) => Promise<GameTokenResponse>;
93
+ }) => Promise<import("..").GameTokenResponse>;
26
94
  exit: () => Promise<void>;
27
95
  };
96
+ /** Game management methods (fetch, list, saveState, loadState, sessions) */
28
97
  games: {
29
- fetch: (gameIdOrSlug: string) => Promise<GameWithManifest>;
30
- list: () => Promise<Array<Game>>;
98
+ fetch: (gameIdOrSlug: string) => Promise<import("@playcademy/types").GameWithManifest>;
99
+ list: () => Promise<Array<import("@playcademy/types").Game>>;
31
100
  saveState: (state: Record<string, unknown>) => Promise<void>;
32
- loadState: () => Promise<GameState>;
33
- startSession: (gameId?: string) => Promise<StartSessionResponse>;
101
+ loadState: () => Promise<import("..").GameState>;
102
+ startSession: (gameId?: string) => Promise<import("..").StartSessionResponse>;
34
103
  endSession: (sessionId: string, gameId?: string) => Promise<void>;
35
104
  };
105
+ /** User methods (me, inventory management) */
36
106
  users: {
37
107
  me: () => Promise<{
38
108
  id: string;
109
+ createdAt: Date;
110
+ updatedAt: Date;
39
111
  name: string;
40
112
  username: string | null;
41
113
  email: string;
@@ -43,23 +115,22 @@ export declare class PlaycademyClient {
43
115
  image: string | null;
44
116
  role: "admin" | "player" | "developer";
45
117
  developerStatus: "none" | "pending" | "approved";
46
- createdAt: Date;
47
- updatedAt: Date;
48
118
  }>;
49
119
  inventory: {
50
- get: () => Promise<InventoryItemWithReward[]>;
51
- add: (rewardId: string, qty: number) => Promise<InventoryMutationResponse>;
52
- spend: (rewardId: string, qty: number) => Promise<InventoryMutationResponse>;
120
+ get: () => Promise<import("@playcademy/types").InventoryItemWithItem[]>;
121
+ add: (itemId: string, qty: number) => Promise<import("..").InventoryMutationResponse>;
122
+ spend: (itemId: string, qty: number) => Promise<import("..").InventoryMutationResponse>;
53
123
  };
54
124
  };
125
+ /** Developer tools (auth, games, keys management) */
55
126
  dev: {
56
127
  auth: {
57
128
  applyForDeveloper: () => Promise<void>;
58
- getDeveloperStatus: () => Promise<DeveloperStatusValue>;
129
+ getDeveloperStatus: () => Promise<import("..").DeveloperStatusValue>;
59
130
  };
60
131
  games: {
61
- upsert: (slug: string, metadata: UpsertGameMetadataInput, file: File | Blob) => Promise<Game>;
62
- update: (gameId: string, props: Partial<Game>) => Promise<void>;
132
+ upsert: (slug: string, metadata: import("@playcademy/types").UpsertGameMetadataInput, file: File | Blob) => Promise<import("@playcademy/types").Game>;
133
+ update: (gameId: string, props: Partial<import("@playcademy/types").Game>) => Promise<void>;
63
134
  delete: (gameId: string) => Promise<void>;
64
135
  };
65
136
  keys: {
@@ -80,12 +151,10 @@ export declare class PlaycademyClient {
80
151
  revokeKey: (keyId: string) => Promise<void>;
81
152
  };
82
153
  };
154
+ /** Map methods (elements) */
83
155
  maps: {
84
156
  elements: (mapId: string) => Promise<{
85
157
  id: string;
86
- mapId: string | null;
87
- elementSlug: string;
88
- interactionType: "game_entry" | "game_registry" | "info" | "teleport" | "door_in" | "door_out" | "npc_interaction" | "quest_trigger";
89
158
  metadata: ({
90
159
  description?: string | undefined;
91
160
  sourceTiledObjects?: Record<string, unknown>[] | undefined;
@@ -93,52 +162,161 @@ export declare class PlaycademyClient {
93
162
  [k: string]: unknown;
94
163
  }) | null;
95
164
  gameId: string | null;
165
+ mapId: string | null;
166
+ elementSlug: string;
167
+ interactionType: "game_entry" | "game_registry" | "info" | "teleport" | "door_in" | "door_out" | "npc_interaction" | "quest_trigger";
96
168
  }[]>;
97
169
  };
170
+ /** Admin methods (games, items, currencies, shop listings) */
98
171
  admin: {
99
172
  games: {
100
173
  pauseGame: (gameId: string) => Promise<void>;
101
174
  resumeGame: (gameId: string) => Promise<void>;
102
175
  };
103
- rewards: {
104
- createReward: (props: InsertReward) => Promise<{
176
+ items: {
177
+ create: (props: import("@playcademy/types").InsertItem) => Promise<{
105
178
  id: string;
106
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
107
179
  displayName: string;
108
- description: string | null;
109
180
  metadata: unknown;
181
+ createdAt: Date;
110
182
  internalName: string;
183
+ description: string | null;
184
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "other";
185
+ imageUrl: string | null;
111
186
  }>;
112
- getReward: (rewardId: string) => Promise<{
187
+ get: (itemId: string) => Promise<{
113
188
  id: string;
114
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
115
189
  displayName: string;
116
- description: string | null;
117
190
  metadata: unknown;
191
+ createdAt: Date;
118
192
  internalName: string;
193
+ description: string | null;
194
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "other";
195
+ imageUrl: string | null;
119
196
  }>;
120
- listRewards: () => Promise<{
197
+ list: () => Promise<{
121
198
  id: string;
122
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
123
199
  displayName: string;
124
- description: string | null;
125
200
  metadata: unknown;
201
+ createdAt: Date;
126
202
  internalName: string;
203
+ description: string | null;
204
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "other";
205
+ imageUrl: string | null;
127
206
  }[]>;
128
- updateReward: (rewardId: string, props: UpdateReward) => Promise<{
207
+ update: (itemId: string, props: import("@playcademy/types").UpdateItem) => Promise<{
129
208
  id: string;
130
- type: "currency" | "badge" | "trophy" | "unlock" | "upgrade" | "other";
131
209
  displayName: string;
132
- description: string | null;
133
210
  metadata: unknown;
211
+ createdAt: Date;
134
212
  internalName: string;
213
+ description: string | null;
214
+ type: "currency" | "badge" | "trophy" | "collectible" | "consumable" | "unlock" | "upgrade" | "other";
215
+ imageUrl: string | null;
135
216
  }>;
136
- deleteReward: (rewardId: string) => Promise<void>;
217
+ delete: (itemId: string) => Promise<void>;
137
218
  };
219
+ currencies: {
220
+ create: (props: import("@playcademy/types").InsertCurrency) => Promise<{
221
+ symbol: string | null;
222
+ id: string;
223
+ createdAt: Date;
224
+ updatedAt: Date | null;
225
+ itemId: string;
226
+ isPrimary: boolean;
227
+ }>;
228
+ get: (currencyId: string) => Promise<{
229
+ symbol: string | null;
230
+ id: string;
231
+ createdAt: Date;
232
+ updatedAt: Date | null;
233
+ itemId: string;
234
+ isPrimary: boolean;
235
+ }>;
236
+ list: () => Promise<{
237
+ symbol: string | null;
238
+ id: string;
239
+ createdAt: Date;
240
+ updatedAt: Date | null;
241
+ itemId: string;
242
+ isPrimary: boolean;
243
+ }[]>;
244
+ update: (currencyId: string, props: import("@playcademy/types").UpdateCurrency) => Promise<{
245
+ symbol: string | null;
246
+ id: string;
247
+ createdAt: Date;
248
+ updatedAt: Date | null;
249
+ itemId: string;
250
+ isPrimary: boolean;
251
+ }>;
252
+ delete: (currencyId: string) => Promise<void>;
253
+ };
254
+ shopListings: {
255
+ create: (props: import("@playcademy/types").InsertShopListing) => Promise<{
256
+ id: string;
257
+ createdAt: Date;
258
+ updatedAt: Date | null;
259
+ itemId: string;
260
+ currencyId: string;
261
+ price: number;
262
+ sellBackPercentage: number | null;
263
+ stock: number | null;
264
+ isActive: boolean;
265
+ availableFrom: Date | null;
266
+ availableUntil: Date | null;
267
+ }>;
268
+ get: (listingId: string) => Promise<{
269
+ id: string;
270
+ createdAt: Date;
271
+ updatedAt: Date | null;
272
+ itemId: string;
273
+ currencyId: string;
274
+ price: number;
275
+ sellBackPercentage: number | null;
276
+ stock: number | null;
277
+ isActive: boolean;
278
+ availableFrom: Date | null;
279
+ availableUntil: Date | null;
280
+ }>;
281
+ list: () => Promise<{
282
+ id: string;
283
+ createdAt: Date;
284
+ updatedAt: Date | null;
285
+ itemId: string;
286
+ currencyId: string;
287
+ price: number;
288
+ sellBackPercentage: number | null;
289
+ stock: number | null;
290
+ isActive: boolean;
291
+ availableFrom: Date | null;
292
+ availableUntil: Date | null;
293
+ }[]>;
294
+ update: (listingId: string, props: import("@playcademy/types").UpdateShopListing) => Promise<{
295
+ id: string;
296
+ createdAt: Date;
297
+ updatedAt: Date | null;
298
+ itemId: string;
299
+ currencyId: string;
300
+ price: number;
301
+ sellBackPercentage: number | null;
302
+ stock: number | null;
303
+ isActive: boolean;
304
+ availableFrom: Date | null;
305
+ availableUntil: Date | null;
306
+ }>;
307
+ delete: (listingId: string) => Promise<void>;
308
+ };
309
+ };
310
+ /** Shop methods (view) */
311
+ shop: {
312
+ view: () => Promise<import("..").ShopViewResponse>;
138
313
  };
314
+ /** Telemetry methods (pushMetrics) */
139
315
  telemetry: {
140
316
  pushMetrics: (metrics: Record<string, number>) => Promise<void>;
141
317
  };
142
- ping(): string;
143
- static login(baseUrl: string, email: string, password: string): Promise<LoginResponse>;
318
+ /** Auto-initializes a PlaycademyClient with context from the environment */
319
+ static init: typeof init;
320
+ /** Authenticates a user with email and password */
321
+ static login: typeof login;
144
322
  }