@hyperyai/sdk 1.0.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.
Files changed (102) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +391 -0
  3. package/dist/components/AuthButton.d.ts +51 -0
  4. package/dist/components/AuthButton.js +60 -0
  5. package/dist/components/AuthModal.d.ts +20 -0
  6. package/dist/components/AuthModal.js +62 -0
  7. package/dist/components/BuyButton.d.ts +47 -0
  8. package/dist/components/BuyButton.js +74 -0
  9. package/dist/components/ErrorBoundary.d.ts +13 -0
  10. package/dist/components/ErrorBoundary.js +24 -0
  11. package/dist/components/HyperyModals.d.ts +30 -0
  12. package/dist/components/HyperyModals.js +30 -0
  13. package/dist/components/InsufficientCreditsAlert.d.ts +11 -0
  14. package/dist/components/InsufficientCreditsAlert.js +12 -0
  15. package/dist/components/ModernAuthForm.d.ts +52 -0
  16. package/dist/components/ModernAuthForm.js +83 -0
  17. package/dist/components/RestrictionModal.d.ts +43 -0
  18. package/dist/components/RestrictionModal.js +167 -0
  19. package/dist/components/SignIn.d.ts +26 -0
  20. package/dist/components/SignIn.js +47 -0
  21. package/dist/components/SignInForm.d.ts +41 -0
  22. package/dist/components/SignInForm.js +86 -0
  23. package/dist/components/SignUp.d.ts +26 -0
  24. package/dist/components/SignUp.js +52 -0
  25. package/dist/components/SpendingLimitAlert.d.ts +12 -0
  26. package/dist/components/SpendingLimitAlert.js +14 -0
  27. package/dist/components/UserButton.d.ts +26 -0
  28. package/dist/components/UserButton.js +35 -0
  29. package/dist/components/UserProfile.d.ts +22 -0
  30. package/dist/components/UserProfile.js +26 -0
  31. package/dist/components/WorkspaceSwitcher.d.ts +29 -0
  32. package/dist/components/WorkspaceSwitcher.js +66 -0
  33. package/dist/components/control.d.ts +64 -0
  34. package/dist/components/control.js +89 -0
  35. package/dist/components/ui/dialog.d.ts +19 -0
  36. package/dist/components/ui/dialog.js +53 -0
  37. package/dist/hooks/index.d.ts +22 -0
  38. package/dist/hooks/index.js +23 -0
  39. package/dist/hooks/useBuyerWallet.d.ts +37 -0
  40. package/dist/hooks/useBuyerWallet.js +66 -0
  41. package/dist/hooks/useCheckout.d.ts +27 -0
  42. package/dist/hooks/useCheckout.js +246 -0
  43. package/dist/hooks/useError.d.ts +19 -0
  44. package/dist/hooks/useError.js +36 -0
  45. package/dist/hooks/useMarketplace.d.ts +50 -0
  46. package/dist/hooks/useMarketplace.js +69 -0
  47. package/dist/hooks/useMemberships.d.ts +71 -0
  48. package/dist/hooks/useMemberships.js +138 -0
  49. package/dist/hooks/useWallet.d.ts +62 -0
  50. package/dist/hooks/useWallet.js +123 -0
  51. package/dist/index.d.ts +55 -0
  52. package/dist/index.js +52 -0
  53. package/dist/lib/context.d.ts +50 -0
  54. package/dist/lib/context.js +409 -0
  55. package/dist/lib/oauth.d.ts +49 -0
  56. package/dist/lib/oauth.js +149 -0
  57. package/dist/lib/parse-error.d.ts +45 -0
  58. package/dist/lib/parse-error.js +185 -0
  59. package/dist/lib/parse-stream.d.ts +43 -0
  60. package/dist/lib/parse-stream.js +89 -0
  61. package/dist/lib/popup.d.ts +42 -0
  62. package/dist/lib/popup.js +80 -0
  63. package/dist/lib/storage.d.ts +43 -0
  64. package/dist/lib/storage.js +125 -0
  65. package/dist/lib/utils.d.ts +8 -0
  66. package/dist/lib/utils.js +11 -0
  67. package/dist/types/index.d.ts +191 -0
  68. package/dist/types/index.js +4 -0
  69. package/package.json +78 -0
  70. package/src/components/AuthButton.tsx +123 -0
  71. package/src/components/AuthModal.tsx +240 -0
  72. package/src/components/BuyButton.tsx +150 -0
  73. package/src/components/ErrorBoundary.tsx +97 -0
  74. package/src/components/HyperyModals.tsx +83 -0
  75. package/src/components/InsufficientCreditsAlert.tsx +73 -0
  76. package/src/components/ModernAuthForm.tsx +322 -0
  77. package/src/components/RestrictionModal.tsx +373 -0
  78. package/src/components/SignIn.tsx +84 -0
  79. package/src/components/SignInForm.tsx +256 -0
  80. package/src/components/SignUp.tsx +86 -0
  81. package/src/components/SpendingLimitAlert.tsx +91 -0
  82. package/src/components/UserButton.tsx +106 -0
  83. package/src/components/UserProfile.tsx +106 -0
  84. package/src/components/WorkspaceSwitcher.tsx +199 -0
  85. package/src/components/control.tsx +133 -0
  86. package/src/components/ui/dialog.tsx +151 -0
  87. package/src/hooks/index.ts +65 -0
  88. package/src/hooks/useBuyerWallet.ts +117 -0
  89. package/src/hooks/useCheckout.ts +312 -0
  90. package/src/hooks/useError.ts +60 -0
  91. package/src/hooks/useMarketplace.ts +129 -0
  92. package/src/hooks/useMemberships.ts +203 -0
  93. package/src/hooks/useWallet.ts +170 -0
  94. package/src/index.ts +147 -0
  95. package/src/lib/context.tsx +478 -0
  96. package/src/lib/oauth.ts +215 -0
  97. package/src/lib/parse-error.ts +224 -0
  98. package/src/lib/parse-stream.ts +121 -0
  99. package/src/lib/popup.ts +98 -0
  100. package/src/lib/storage.ts +140 -0
  101. package/src/lib/utils.ts +14 -0
  102. package/src/types/index.ts +216 -0
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Hypery Auth Types
3
+ */
4
+
5
+ /**
6
+ * How interactive auth / card-entry steps are presented.
7
+ * - `popup` — a centered popup (seamless; stays on your page). PayPal/Firebase-style.
8
+ * - `redirect` — a full-page redirect that resumes the flow on return. Robust everywhere.
9
+ * - `auto` — popup on desktop, redirect on mobile / when a popup is blocked.
10
+ */
11
+ export type InteractionMode = 'auto' | 'popup' | 'redirect';
12
+ /** The concrete mode after resolving `auto` for the current environment. */
13
+ export type ResolvedMode = 'popup' | 'redirect';
14
+
15
+ /** Result of a popup-based login attempt. */
16
+ export interface PopupAuthResult {
17
+ /** Authentication succeeded and the user is now signed in. */
18
+ ok: boolean;
19
+ /** The browser blocked the popup — caller should fall back to redirect. */
20
+ blocked: boolean;
21
+ /** The user closed the popup without completing auth. */
22
+ cancelled: boolean;
23
+ }
24
+
25
+ /**
26
+ * Custom branding shared by AuthButton / AuthModal / ModernAuthForm.
27
+ */
28
+ export interface BrandingConfig {
29
+ /** URL to logo image */
30
+ logo?: string;
31
+ /** Your app name */
32
+ appName?: string;
33
+ /** Hex color (e.g., '#8b5cf6') */
34
+ primaryColor?: string;
35
+ }
36
+
37
+ export interface HyperyAuthConfig {
38
+ /** OAuth Client ID */
39
+ clientId: string;
40
+ /** Redirect URI after auth */
41
+ redirectUri: string;
42
+ /** Hypery base URL */
43
+ gatewayUrl: string;
44
+ /** OAuth scopes to request */
45
+ scopes?: string[];
46
+ /** Storage type for tokens */
47
+ storage?: 'localStorage' | 'sessionStorage' | 'memory';
48
+ /**
49
+ * How interactive auth / card-entry steps in the auth+charge flow are shown.
50
+ * Defaults to `auto` (popup on desktop, redirect on mobile / blocked popups).
51
+ * For popup mode, `redirectUri` must be same-origin as your app.
52
+ */
53
+ interactionMode?: InteractionMode;
54
+ /**
55
+ * Called when an {@link AuthContextValue.authenticatedFetch} request stays 401
56
+ * after a single silent-refresh retry (server-revoked token / suspended app).
57
+ * Use for custom re-auth handling; if omitted, `authRequired` is set so
58
+ * `<HyperyModals>` can auto-open the auth modal.
59
+ */
60
+ onUnauthorized?: () => void;
61
+ /**
62
+ * Called when an {@link AuthContextValue.authenticatedFetch} request returns a
63
+ * billing restriction (402/429). Receives the classified error; if omitted,
64
+ * `restriction` is set so `<HyperyModals>` can auto-open the funds modal.
65
+ */
66
+ onRestricted?: (error: ParsedError) => void;
67
+ }
68
+
69
+ export interface User {
70
+ id: string;
71
+ email: string;
72
+ name: string;
73
+ image?: string;
74
+ }
75
+
76
+ export interface AuthTokens {
77
+ accessToken: string;
78
+ refreshToken: string;
79
+ expiresIn: number;
80
+ tokenType: string;
81
+ }
82
+
83
+ export interface AuthState {
84
+ user: User | null;
85
+ isAuthenticated: boolean;
86
+ isLoading: boolean;
87
+ error: string | null;
88
+ isLoggingOut?: boolean;
89
+ }
90
+
91
+ export interface AuthContextValue extends AuthState {
92
+ login: () => Promise<void>;
93
+ /**
94
+ * Log in via a centered popup (no full-page redirect). Resolves once the popup
95
+ * completes: `ok` on success, `blocked` if the browser blocked the popup (fall
96
+ * back to `login()`), `cancelled` if the user closed it. Used by the seamless
97
+ * auth+charge flow (see `useCheckout`).
98
+ */
99
+ loginPopup: () => Promise<PopupAuthResult>;
100
+ /** The interaction mode resolved for this environment (popup vs redirect). */
101
+ interactionMode: ResolvedMode;
102
+ /** The redirectUri this provider was configured with. */
103
+ redirectUri: string;
104
+ signUp?: () => Promise<void>;
105
+ logout: () => Promise<void>;
106
+ refreshAuth: () => Promise<void>;
107
+ /**
108
+ * Get a valid access token, refreshing if the local expiry clock says so.
109
+ * Pass `forceRefresh` to refresh even when the clock says valid (used by the
110
+ * 401 retry path). Concurrent calls share ONE in-flight refresh (single-flight).
111
+ */
112
+ getAccessToken: (forceRefresh?: boolean) => Promise<string | null>;
113
+ /** OAuth clientId this provider was configured with. */
114
+ clientId: string;
115
+ /** Gateway base URL this provider was configured with. */
116
+ gatewayUrl: string;
117
+ /**
118
+ * fetch() that injects the bearer token and drives the turnkey modal UX:
119
+ * 401 → one silent-refresh retry, then `authRequired` (+ `onUnauthorized`);
120
+ * 402/429 → `restriction` is set (+ `onRestricted`). Always returns the
121
+ * Response so the caller can still read the body.
122
+ */
123
+ authenticatedFetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
124
+ /** Current billing restriction from the last authenticatedFetch, if any. */
125
+ restriction: ParsedError | null;
126
+ /** Clear the current restriction (e.g. after the funds modal closes). */
127
+ clearRestriction: () => void;
128
+ /** True when re-auth is required (401 survived a refresh retry). */
129
+ authRequired: boolean;
130
+ /** Clear the re-auth requirement (e.g. after the auth modal closes). */
131
+ clearAuthRequired: () => void;
132
+ }
133
+
134
+ /**
135
+ * Error response types from Hypery API
136
+ */
137
+
138
+ export interface SpendingLimitErrorData {
139
+ code: 'SPENDING_LIMIT_EXCEEDED';
140
+ message: string;
141
+ type: 'spending_limit_error';
142
+ limitType: 'daily' | 'monthly' | 'total';
143
+ limit: number;
144
+ current: number;
145
+ requested: number;
146
+ resetsAt?: string;
147
+ }
148
+
149
+ export interface InsufficientCreditsErrorData {
150
+ code: 'INSUFFICIENT_CREDITS';
151
+ message: string;
152
+ type: 'insufficient_credits_error';
153
+ available: number;
154
+ required: number;
155
+ }
156
+
157
+ export interface PaymentMethodRequiredErrorData {
158
+ code: 'PAYMENT_METHOD_REQUIRED';
159
+ message: string;
160
+ type: 'payment_method_required_error';
161
+ }
162
+
163
+ export interface PaymentDeclinedErrorData {
164
+ code: 'PAYMENT_DECLINED';
165
+ message: string;
166
+ type: 'payment_declined_error';
167
+ reason?: string;
168
+ }
169
+
170
+ export interface AuthenticationErrorData {
171
+ code: 'UNAUTHENTICATED';
172
+ message: string;
173
+ type: 'authentication_error';
174
+ }
175
+
176
+ export interface GenericErrorData {
177
+ code: string;
178
+ message: string;
179
+ type?: string;
180
+ [key: string]: any;
181
+ }
182
+
183
+ export type ErrorData =
184
+ | SpendingLimitErrorData
185
+ | InsufficientCreditsErrorData
186
+ | PaymentMethodRequiredErrorData
187
+ | PaymentDeclinedErrorData
188
+ | AuthenticationErrorData
189
+ | GenericErrorData;
190
+
191
+ export interface ErrorResponse {
192
+ error: ErrorData;
193
+ }
194
+
195
+ /**
196
+ * Parsed error information
197
+ */
198
+ export interface ParsedError {
199
+ code: string;
200
+ message: string;
201
+ type?: string;
202
+ /** HTTP status, when the caller attaches it (used as a classification fallback). */
203
+ status?: number;
204
+ isSpendingLimit: boolean;
205
+ isInsufficientCredits: boolean;
206
+ isPaymentMethodRequired: boolean;
207
+ isPaymentDeclined: boolean;
208
+ /** True for an authentication failure (401 / UNAUTHENTICATED) — drive re-auth. */
209
+ isAuth: boolean;
210
+ /** True for a scope/permission denial (403 / PERMISSION_DENIED / INSUFFICIENT_SCOPE). */
211
+ isPermissionDenied: boolean;
212
+ /** True for a rate-limit denial (429 / RATE_LIMITED). */
213
+ isRateLimit: boolean;
214
+ data: ErrorData;
215
+ }
216
+