@playcademy/sdk 0.0.5 → 0.0.7

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.
Files changed (48) hide show
  1. package/dist/index.d.ts +4013 -7
  2. package/dist/index.js +1820 -1785
  3. package/dist/server.d.ts +53 -0
  4. package/dist/server.js +50 -0
  5. package/dist/types.d.ts +4243 -41
  6. package/dist/types.js +1 -748
  7. package/package.json +10 -3
  8. package/dist/core/auth/flows/popup.d.ts +0 -14
  9. package/dist/core/auth/flows/redirect.d.ts +0 -15
  10. package/dist/core/auth/flows/unified.d.ts +0 -11
  11. package/dist/core/auth/login.d.ts +0 -20
  12. package/dist/core/auth/oauth.d.ts +0 -115
  13. package/dist/core/auth/utils.d.ts +0 -23
  14. package/dist/core/cache/cooldown-cache.d.ts +0 -31
  15. package/dist/core/cache/index.d.ts +0 -14
  16. package/dist/core/cache/permanent-cache.d.ts +0 -39
  17. package/dist/core/cache/singleton-cache.d.ts +0 -29
  18. package/dist/core/cache/ttl-cache.d.ts +0 -54
  19. package/dist/core/cache/types.d.ts +0 -23
  20. package/dist/core/client.d.ts +0 -521
  21. package/dist/core/errors.d.ts +0 -11
  22. package/dist/core/namespaces/achievements.d.ts +0 -84
  23. package/dist/core/namespaces/admin.d.ts +0 -385
  24. package/dist/core/namespaces/auth.d.ts +0 -54
  25. package/dist/core/namespaces/character.d.ts +0 -205
  26. package/dist/core/namespaces/credits.d.ts +0 -51
  27. package/dist/core/namespaces/dev.d.ts +0 -323
  28. package/dist/core/namespaces/games.d.ts +0 -173
  29. package/dist/core/namespaces/identity.d.ts +0 -98
  30. package/dist/core/namespaces/index.d.ts +0 -19
  31. package/dist/core/namespaces/leaderboard.d.ts +0 -48
  32. package/dist/core/namespaces/levels.d.ts +0 -90
  33. package/dist/core/namespaces/maps.d.ts +0 -93
  34. package/dist/core/namespaces/realtime.client.d.ts +0 -129
  35. package/dist/core/namespaces/realtime.d.ts +0 -90
  36. package/dist/core/namespaces/runtime.d.ts +0 -222
  37. package/dist/core/namespaces/scores.d.ts +0 -55
  38. package/dist/core/namespaces/shop.d.ts +0 -25
  39. package/dist/core/namespaces/sprites.d.ts +0 -35
  40. package/dist/core/namespaces/telemetry.d.ts +0 -28
  41. package/dist/core/namespaces/timeback.d.ts +0 -111
  42. package/dist/core/namespaces/users.d.ts +0 -172
  43. package/dist/core/request.d.ts +0 -24
  44. package/dist/core/static/identity.d.ts +0 -37
  45. package/dist/core/static/index.d.ts +0 -3
  46. package/dist/core/static/init.d.ts +0 -21
  47. package/dist/core/static/login.d.ts +0 -34
  48. package/dist/messaging.d.ts +0 -544
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playcademy/sdk",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -8,6 +8,11 @@
8
8
  "require": "./dist/index.js",
9
9
  "types": "./dist/index.d.ts"
10
10
  },
11
+ "./server": {
12
+ "import": "./dist/server.js",
13
+ "require": "./dist/server.js",
14
+ "types": "./dist/server.d.ts"
15
+ },
11
16
  "./types": {
12
17
  "import": "./dist/types.js",
13
18
  "require": "./dist/types.js",
@@ -30,12 +35,14 @@
30
35
  },
31
36
  "devDependencies": {
32
37
  "@playcademy/data": "0.0.1",
38
+ "@playcademy/logger": "0.0.1",
33
39
  "@playcademy/sandbox": "0.1.0-beta.18",
34
40
  "@playcademy/test": "0.0.1",
35
41
  "@types/bun": "latest",
42
+ "rollup": "^4.50.2",
43
+ "rollup-plugin-dts": "^6.2.3",
36
44
  "typescript": "^5.7.2",
37
- "yocto-spinner": "^0.2.2",
38
- "@playcademy/logger": "0.0.1"
45
+ "yocto-spinner": "^0.2.2"
39
46
  },
40
47
  "peerDependencies": {
41
48
  "drizzle-orm": "^0.42.0",
@@ -1,14 +0,0 @@
1
- import type { AuthOptions, AuthResult } from '../../../types';
2
- /**
3
- * Popup-based OAuth flow implementation.
4
- * Used primarily for iframe contexts where redirects don't work.
5
- * With server-first architecture, the server handles token exchange.
6
- */
7
- /**
8
- * Initiates a popup-based OAuth flow.
9
- * Opens auth URL in popup and waits for server callback message.
10
- *
11
- * @param options - Authentication options
12
- * @returns Promise resolving to the authentication result
13
- */
14
- export declare function initiatePopupFlow(options: AuthOptions): Promise<AuthResult>;
@@ -1,15 +0,0 @@
1
- import type { AuthOptions, AuthResult } from '../../../types';
2
- /**
3
- * Redirect-based OAuth flow implementation.
4
- * Used for standalone applications where traditional OAuth redirects work.
5
- * With server-first architecture, the server handles token exchange.
6
- */
7
- /**
8
- * Initiates a redirect-based OAuth flow.
9
- * This will navigate away from the current page to the IdP.
10
- * The server callback will handle token exchange and redirect back.
11
- *
12
- * @param options - Authentication options
13
- * @returns Promise that never resolves (page navigates away)
14
- */
15
- export declare function initiateRedirectFlow(options: AuthOptions): Promise<AuthResult>;
@@ -1,11 +0,0 @@
1
- /**
2
- * Unified authentication flow router for server-first architecture.
3
- * Routes to appropriate flow implementation based on mode.
4
- */
5
- import type { AuthOptions, AuthResult } from '../../../types';
6
- /**
7
- * Routes authentication request to the appropriate flow.
8
- * With server-first architecture, all flows end up at the same
9
- * server callback which handles token exchange.
10
- */
11
- export declare function initiateUnifiedFlow(options: AuthOptions): Promise<AuthResult>;
@@ -1,20 +0,0 @@
1
- /**
2
- * Main login implementation for the Playcademy SDK.
3
- * Uses server-first architecture where the server handles token exchange.
4
- */
5
- import type { AuthOptions, AuthResult, PlaycademyClient } from '../../types';
6
- /**
7
- * Performs OAuth login with the specified provider.
8
- *
9
- * With server-first architecture:
10
- * 1. SDK initiates OAuth flow (popup or redirect)
11
- * 2. IdP redirects to your server callback URL
12
- * 3. Your server exchanges code for tokens
13
- * 4. Your server returns HTML that completes the flow
14
- * 5. SDK receives the user data via postMessage (popup) or cookie (redirect)
15
- *
16
- * @param client - The PlaycademyClient instance
17
- * @param options - Authentication options including callback URL
18
- * @returns Promise resolving to the authentication result
19
- */
20
- export declare function login(client: PlaycademyClient, options: AuthOptions): Promise<AuthResult>;
@@ -1,115 +0,0 @@
1
- /**
2
- * OAuth 2.0 implementation for the Playcademy SDK
3
- */
4
- import type { AuthProviderType } from '../../types';
5
- /**
6
- * OAuth provider configuration
7
- */
8
- export interface OAuthConfig {
9
- authorizationEndpoint: string;
10
- tokenEndpoint: string;
11
- clientId: string;
12
- scope?: string;
13
- }
14
- /**
15
- * OAuth configuration for different providers
16
- */
17
- export declare const OAUTH_CONFIGS: Record<string, () => Partial<OAuthConfig>>;
18
- /**
19
- * OAuth state that needs to be maintained during the flow
20
- */
21
- export interface OAuthState {
22
- /** Random state parameter for CSRF protection */
23
- state: string;
24
- /** PKCE code verifier */
25
- codeVerifier?: string;
26
- /** Timestamp when the state was created */
27
- createdAt: number;
28
- }
29
- /**
30
- * OAuth callback parameters from the authorization server
31
- */
32
- export interface OAuthCallbackParams {
33
- code?: string;
34
- state?: string;
35
- error?: string;
36
- error_description?: string;
37
- }
38
- /**
39
- * Generates a cryptographically secure random state parameter for OAuth.
40
- * Used to prevent CSRF attacks during the OAuth flow.
41
- * Can optionally include encoded data like user ID for server-side association.
42
- *
43
- * @param data - Optional data to encode (e.g., { playcademy_user_id: "123" })
44
- * @returns A random state string, optionally with encoded data
45
- */
46
- export declare function generateOAuthState(data?: Record<string, string>): Promise<string>;
47
- /**
48
- * Parses an OAuth state parameter to extract CSRF token and any encoded data.
49
- *
50
- * @param state - The OAuth state parameter to parse
51
- * @returns Object containing CSRF token and optional decoded data
52
- */
53
- export declare function parseOAuthState(state: string): {
54
- csrfToken: string;
55
- data?: Record<string, string>;
56
- };
57
- /**
58
- * Generates a code verifier for PKCE (Proof Key for Code Exchange).
59
- *
60
- * @returns A random code verifier string
61
- */
62
- export declare function generateCodeVerifier(): string;
63
- /**
64
- * Generates a code challenge from a code verifier for PKCE.
65
- *
66
- * @param verifier - The code verifier
67
- * @returns Promise resolving to the code challenge
68
- */
69
- export declare function generateCodeChallenge(verifier: string): Promise<string>;
70
- /**
71
- * Builds the OAuth authorization URL with all required parameters.
72
- *
73
- * @param params - OAuth parameters
74
- * @returns The complete authorization URL
75
- */
76
- export declare function buildAuthorizationUrl(params: {
77
- authorizationEndpoint: string;
78
- clientId: string;
79
- redirectUri: string;
80
- state: string;
81
- codeChallenge?: string;
82
- scope?: string;
83
- responseType?: string;
84
- }): string;
85
- /**
86
- * Parses OAuth callback parameters from a URL.
87
- *
88
- * @param url - The callback URL
89
- * @returns The parsed parameters
90
- */
91
- export declare function parseCallbackUrl(url: string): OAuthCallbackParams;
92
- /**
93
- * Validates OAuth callback parameters against the expected state.
94
- *
95
- * @param params - The callback parameters
96
- * @param expectedState - The expected state value
97
- * @returns True if valid, throws otherwise
98
- */
99
- export declare function validateCallbackParams(params: OAuthCallbackParams, expectedState: string): boolean;
100
- /**
101
- * Gets the default OAuth configuration for a provider.
102
- *
103
- * @param provider - The auth provider
104
- * @returns The partial OAuth configuration (may not include clientId)
105
- * @throws Error if provider is unknown
106
- */
107
- export declare function getOAuthConfig(provider: AuthProviderType): Partial<OAuthConfig>;
108
- /**
109
- * Gets the redirect URI for OAuth callback.
110
- *
111
- * @param customUri - Custom redirect URI provided by the user
112
- * @param isPopup - Whether this is for a popup flow (only used if no custom URI)
113
- * @returns The redirect URI
114
- */
115
- export declare function getRedirectUri(customUri?: string, isPopup?: boolean): string;
@@ -1,23 +0,0 @@
1
- /**
2
- * Opens a popup window with specific dimensions and features.
3
- *
4
- * @param url - The URL to open
5
- * @param name - The window name
6
- * @returns The opened window or null if blocked
7
- */
8
- export declare function openPopupWindow(url: string, name?: string, width?: number, height?: number): Window | null;
9
- /**
10
- * Detects if we're running in an iframe context.
11
- * Safe to call in any environment (browser or Node.js).
12
- *
13
- * @returns True if in iframe, false if standalone or not in browser
14
- */
15
- export declare function isInIframe(): boolean;
16
- /**
17
- * Creates a promise that resolves when a message is received from a window.
18
- *
19
- * @param targetWindow - The window to listen for messages from
20
- * @param timeout - Timeout in milliseconds
21
- * @returns Promise that resolves with the message data
22
- */
23
- export declare function waitForWindowMessage<T>(targetWindow: Window, timeout?: number): Promise<T>;
@@ -1,31 +0,0 @@
1
- import type { CooldownCacheConfig } from './types';
2
- /**
3
- * Creates a cooldown-based cache that prevents rapid repeated calls.
4
- * Unlike TTL cache, this always fetches fresh data but enforces a minimum
5
- * time between requests. Cooldown can be configured both at creation time
6
- * (default) and at runtime (per-call).
7
- *
8
- * @param defaultCooldownMs - Default minimum milliseconds between requests
9
- * @returns Cache instance with get and clear methods
10
- *
11
- * @example
12
- * ```typescript
13
- * const cache = createCooldownCache<LevelProgress>(5000) // Default: 5 second cooldown
14
- *
15
- * // Use default cooldown
16
- * const progress1 = await cache.get('user-progress', () => fetchProgress())
17
- *
18
- * // Override with custom cooldown for this call
19
- * const progress2 = await cache.get('user-progress', () => fetchProgress(), { cooldown: 1000 })
20
- *
21
- * // Force refresh bypassing cooldown
22
- * const fresh = await cache.get('user-progress', () => fetchProgress(), { force: true })
23
- *
24
- * // Disable cooldown for this call (cooldown: 0)
25
- * const immediate = await cache.get('user-progress', () => fetchProgress(), { cooldown: 0 })
26
- * ```
27
- */
28
- export declare function createCooldownCache<T>(defaultCooldownMs: number): {
29
- get: (key: string, loader: () => Promise<T>, config?: CooldownCacheConfig) => Promise<T>;
30
- clear: (key?: string) => void;
31
- };
@@ -1,14 +0,0 @@
1
- /**
2
- * Cache utilities for the Playcademy SDK
3
- *
4
- * Available cache types:
5
- * - TTL Cache: Time-based expiration with management capabilities (e.g., 60 seconds for games list)
6
- * - Cooldown Cache: Minimum time between requests (e.g., 5 seconds for level progress)
7
- * - Singleton Cache: Single value, no keys (e.g., THE credits item ID)
8
- * - Permanent Cache: Multiple values with keys, never expire (e.g., item ID resolutions)
9
- */
10
- export { createTTLCache } from './ttl-cache';
11
- export type { TTLCacheOptions } from './ttl-cache';
12
- export { createCooldownCache } from './cooldown-cache';
13
- export { createSingletonCache } from './singleton-cache';
14
- export { createPermanentCache } from './permanent-cache';
@@ -1,39 +0,0 @@
1
- /**
2
- * Creates a permanent KV cache that stores multiple values indefinitely.
3
- * Values are cached by key until explicitly cleared.
4
- *
5
- * This is a key-value cache - it can store many different values accessed by keys.
6
- * If you only need to cache ONE value total, use SingletonCache instead.
7
- *
8
- * @param keyPrefix - Optional prefix for cache keys
9
- * @returns Cache instance with get, clear, has, and size methods
10
- *
11
- * @example
12
- * ```typescript
13
- * // Perfect for caching multiple ID resolutions
14
- * const itemIdCache = createPermanentCache<string>('items')
15
- *
16
- * // Cache different item ID resolutions with different keys
17
- * const swordId = await itemIdCache.get('sword-of-power', async () => {
18
- * const item = await resolveItem('sword-of-power')
19
- * return item.id
20
- * })
21
- *
22
- * const potionId = await itemIdCache.get('health-potion', async () => {
23
- * const item = await resolveItem('health-potion')
24
- * return item.id
25
- * })
26
- *
27
- * // Check if specific item is cached
28
- * if (itemIdCache.has('sword-of-power')) {
29
- * // Use cached value
30
- * }
31
- * ```
32
- */
33
- export declare function createPermanentCache<T>(keyPrefix?: string): {
34
- get: (key: string, loader: () => Promise<T>) => Promise<T>;
35
- clear: (key?: string) => void;
36
- has: (key: string) => boolean;
37
- size: () => number;
38
- keys: () => string[];
39
- };
@@ -1,29 +0,0 @@
1
- /**
2
- * Creates a singleton cache that stores a SINGLE value permanently.
3
- * Once set, the value is cached for the lifetime of the application.
4
- *
5
- * This is NOT a key-value cache - it stores exactly one value total.
6
- * If you need to cache multiple values with keys, use PermanentCache instead.
7
- *
8
- * @returns Cache instance with get and clear methods
9
- *
10
- * @example
11
- * ```typescript
12
- * // Perfect for single values that are resolved once
13
- * const creditsIdCache = createSingletonCache<string>()
14
- *
15
- * // First call fetches and caches THE value
16
- * const id = await creditsIdCache.get(async () => {
17
- * const item = await fetchCreditsItem()
18
- * return item.id
19
- * })
20
- *
21
- * // Subsequent calls return THE cached value (no key needed)
22
- * const cachedId = await creditsIdCache.get(() => fetchCreditsItem())
23
- * ```
24
- */
25
- export declare function createSingletonCache<T>(): {
26
- get: (loader: () => Promise<T>) => Promise<T>;
27
- clear: () => void;
28
- has: () => boolean;
29
- };
@@ -1,54 +0,0 @@
1
- import type { TTLCacheConfig } from './types';
2
- export interface TTLCacheOptions {
3
- /** Default time-to-live in milliseconds */
4
- ttl: number;
5
- /** Optional key prefix for debugging */
6
- keyPrefix?: string;
7
- /** Optional callback when cache is cleared */
8
- onClear?: () => void;
9
- }
10
- export interface CachedValue<T> {
11
- value: T;
12
- expiresAt: number;
13
- }
14
- /**
15
- * Creates a TTL (Time-To-Live) cache for async operations with management capabilities.
16
- * Caches results for a specified duration and allows force refresh.
17
- * TTL can be configured both at creation time (default) and at runtime (per-call).
18
- *
19
- * @param options - Cache configuration with default TTL
20
- * @returns Cache instance with get, clear, size, prune, getKeys, and has methods
21
- *
22
- * @example
23
- * ```typescript
24
- * const cache = createTTLCache<Game[]>({
25
- * ttl: 60 * 1000, // Default: 60 seconds
26
- * keyPrefix: 'games.list'
27
- * })
28
- *
29
- * // Use default TTL
30
- * const games = await cache.get('all', () => fetchGames())
31
- *
32
- * // Override with custom TTL for this call
33
- * const shortLived = await cache.get('all', () => fetchGames(), { ttl: 5000 })
34
- *
35
- * // Force refresh regardless of TTL
36
- * const fresh = await cache.get('all', () => fetchGames(), { force: true })
37
- *
38
- * // Disable caching for this call (ttl: 0)
39
- * const uncached = await cache.get('all', () => fetchGames(), { ttl: 0 })
40
- *
41
- * // Inspect cache
42
- * const keys = cache.getKeys() // ['all']
43
- * const hasAll = cache.has('all') // true
44
- * cache.clear('all') // Clear specific key
45
- * ```
46
- */
47
- export declare function createTTLCache<T>(options: TTLCacheOptions): {
48
- get: (key: string, loader: () => Promise<T>, config?: TTLCacheConfig) => Promise<T>;
49
- clear: (key?: string) => void;
50
- size: () => number;
51
- prune: () => void;
52
- getKeys: () => string[];
53
- has: (key: string) => boolean;
54
- };
@@ -1,23 +0,0 @@
1
- /**
2
- * Cache configuration types for runtime customization
3
- */
4
- /**
5
- * Runtime configuration for TTL cache behavior
6
- */
7
- export interface TTLCacheConfig {
8
- /** Time-to-live in milliseconds. Set to 0 to disable caching for this call. */
9
- ttl?: number;
10
- /** Force refresh, bypassing cache */
11
- force?: boolean;
12
- /** Skip cache and fetch fresh data (alias for force) */
13
- skipCache?: boolean;
14
- }
15
- /**
16
- * Runtime configuration for cooldown cache behavior
17
- */
18
- export interface CooldownCacheConfig {
19
- /** Cooldown period in milliseconds. Set to 0 to disable cooldown for this call. */
20
- cooldown?: number;
21
- /** Force refresh, bypassing cooldown */
22
- force?: boolean;
23
- }