integrate-sdk 0.3.3 → 0.3.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 (40) hide show
  1. package/dist/index.js +42 -15
  2. package/dist/server.js +42 -15
  3. package/dist/src/adapters/nextjs-callback.d.ts +44 -0
  4. package/dist/src/adapters/nextjs-callback.d.ts.map +1 -0
  5. package/dist/src/adapters/nextjs.d.ts +6 -2
  6. package/dist/src/adapters/nextjs.d.ts.map +1 -1
  7. package/dist/src/index.d.ts +1 -1
  8. package/dist/src/index.d.ts.map +1 -1
  9. package/dist/src/oauth/types.d.ts +9 -0
  10. package/dist/src/oauth/types.d.ts.map +1 -1
  11. package/dist/src/oauth/window-manager.d.ts +2 -1
  12. package/dist/src/oauth/window-manager.d.ts.map +1 -1
  13. package/package.json +15 -3
  14. package/src/adapters/auto-routes.ts +217 -0
  15. package/src/adapters/base-handler.ts +212 -0
  16. package/src/adapters/nextjs-callback.tsx +160 -0
  17. package/src/adapters/nextjs.ts +318 -0
  18. package/src/adapters/tanstack-start.ts +264 -0
  19. package/src/client.ts +952 -0
  20. package/src/config/types.ts +180 -0
  21. package/src/errors.ts +207 -0
  22. package/src/index.ts +110 -0
  23. package/src/integrations/vercel-ai.ts +104 -0
  24. package/src/oauth/manager.ts +307 -0
  25. package/src/oauth/pkce.ts +127 -0
  26. package/src/oauth/types.ts +101 -0
  27. package/src/oauth/window-manager.ts +322 -0
  28. package/src/plugins/generic.ts +119 -0
  29. package/src/plugins/github-client.ts +345 -0
  30. package/src/plugins/github.ts +122 -0
  31. package/src/plugins/gmail-client.ts +114 -0
  32. package/src/plugins/gmail.ts +108 -0
  33. package/src/plugins/server-client.ts +20 -0
  34. package/src/plugins/types.ts +89 -0
  35. package/src/protocol/jsonrpc.ts +88 -0
  36. package/src/protocol/messages.ts +145 -0
  37. package/src/server.ts +117 -0
  38. package/src/transport/http-session.ts +322 -0
  39. package/src/transport/http-stream.ts +331 -0
  40. package/src/utils/naming.ts +52 -0
@@ -0,0 +1,264 @@
1
+ /**
2
+ * TanStack Start OAuth Route Adapter
3
+ * Provides OAuth route handlers for TanStack Start
4
+ */
5
+
6
+ import { OAuthHandler, type OAuthHandlerConfig } from './base-handler.js';
7
+
8
+ /**
9
+ * Create TanStack Start OAuth route handlers
10
+ *
11
+ * Use this to create secure OAuth API routes in your TanStack Start application
12
+ * that handle authorization with server-side secrets.
13
+ *
14
+ * @param config - OAuth handler configuration with MCP server URL and provider credentials
15
+ * @returns Object with authorize, callback, and status route handlers
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // app/routes/api/integrate/oauth/authorize.ts
20
+ * import { createTanStackOAuthHandler } from 'integrate-sdk';
21
+ * import { json } from '@tanstack/start';
22
+ *
23
+ * const handler = createTanStackOAuthHandler({
24
+ * providers: {
25
+ * github: {
26
+ * clientId: process.env.GITHUB_CLIENT_ID!,
27
+ * clientSecret: process.env.GITHUB_CLIENT_SECRET!,
28
+ * },
29
+ * gmail: {
30
+ * clientId: process.env.GMAIL_CLIENT_ID!,
31
+ * clientSecret: process.env.GMAIL_CLIENT_SECRET!,
32
+ * },
33
+ * },
34
+ * });
35
+ *
36
+ * export const POST = handler.authorize;
37
+ * ```
38
+ */
39
+ export function createTanStackOAuthHandler(config: OAuthHandlerConfig) {
40
+ const handler = new OAuthHandler(config);
41
+
42
+ return {
43
+ /**
44
+ * POST /api/integrate/oauth/authorize
45
+ *
46
+ * Request authorization URL from MCP server with server-side OAuth credentials
47
+ *
48
+ * Request body:
49
+ * ```json
50
+ * {
51
+ * "provider": "github",
52
+ * "scopes": ["repo", "user"],
53
+ * "state": "random-state-string",
54
+ * "codeChallenge": "pkce-code-challenge",
55
+ * "codeChallengeMethod": "S256",
56
+ * "redirectUri": "https://yourapp.com/oauth/callback"
57
+ * }
58
+ * ```
59
+ *
60
+ * Response:
61
+ * ```json
62
+ * {
63
+ * "authorizationUrl": "https://github.com/login/oauth/authorize?..."
64
+ * }
65
+ * ```
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * // app/routes/api/integrate/oauth/authorize.ts
70
+ * import { createTanStackOAuthHandler } from 'integrate-sdk';
71
+ *
72
+ * const handler = createTanStackOAuthHandler({
73
+ * * providers: {
74
+ * github: {
75
+ * clientId: process.env.GITHUB_CLIENT_ID!,
76
+ * clientSecret: process.env.GITHUB_CLIENT_SECRET!,
77
+ * },
78
+ * },
79
+ * });
80
+ *
81
+ * export const POST = handler.authorize;
82
+ * ```
83
+ */
84
+ async authorize({ request }: { request: Request }): Promise<Response> {
85
+ try {
86
+ const body = await request.json();
87
+ const result = await handler.handleAuthorize(body);
88
+ return new Response(JSON.stringify(result), {
89
+ status: 200,
90
+ headers: {
91
+ 'Content-Type': 'application/json',
92
+ },
93
+ });
94
+ } catch (error: any) {
95
+ console.error('[OAuth Authorize] Error:', error);
96
+ return new Response(
97
+ JSON.stringify({ error: error.message || 'Failed to get authorization URL' }),
98
+ {
99
+ status: 500,
100
+ headers: {
101
+ 'Content-Type': 'application/json',
102
+ },
103
+ }
104
+ );
105
+ }
106
+ },
107
+
108
+ /**
109
+ * POST /api/integrate/oauth/callback
110
+ *
111
+ * Exchange authorization code for session token
112
+ *
113
+ * Request body:
114
+ * ```json
115
+ * {
116
+ * "provider": "github",
117
+ * "code": "authorization-code",
118
+ * "codeVerifier": "pkce-code-verifier",
119
+ * "state": "state-from-authorize"
120
+ * }
121
+ * ```
122
+ *
123
+ * Response:
124
+ * ```json
125
+ * {
126
+ * "sessionToken": "session-token-123",
127
+ * "provider": "github",
128
+ * "scopes": ["repo", "user"],
129
+ * "expiresAt": 1234567890
130
+ * }
131
+ * ```
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * // app/routes/api/integrate/oauth/callback.ts
136
+ * import { createTanStackOAuthHandler } from 'integrate-sdk';
137
+ *
138
+ * const handler = createTanStackOAuthHandler({
139
+ * * providers: {
140
+ * github: {
141
+ * clientId: process.env.GITHUB_CLIENT_ID!,
142
+ * clientSecret: process.env.GITHUB_CLIENT_SECRET!,
143
+ * },
144
+ * },
145
+ * });
146
+ *
147
+ * export const POST = handler.callback;
148
+ * ```
149
+ */
150
+ async callback({ request }: { request: Request }): Promise<Response> {
151
+ try {
152
+ const body = await request.json();
153
+ const result = await handler.handleCallback(body);
154
+ return new Response(JSON.stringify(result), {
155
+ status: 200,
156
+ headers: {
157
+ 'Content-Type': 'application/json',
158
+ },
159
+ });
160
+ } catch (error: any) {
161
+ console.error('[OAuth Callback] Error:', error);
162
+ return new Response(
163
+ JSON.stringify({ error: error.message || 'Failed to exchange authorization code' }),
164
+ {
165
+ status: 500,
166
+ headers: {
167
+ 'Content-Type': 'application/json',
168
+ },
169
+ }
170
+ );
171
+ }
172
+ },
173
+
174
+ /**
175
+ * GET /api/integrate/oauth/status?provider=github
176
+ *
177
+ * Check if a provider is currently authorized
178
+ *
179
+ * Query parameters:
180
+ * - provider: Provider to check (e.g., "github")
181
+ *
182
+ * Headers:
183
+ * - X-Session-Token: Session token from previous authorization
184
+ *
185
+ * Response:
186
+ * ```json
187
+ * {
188
+ * "authorized": true,
189
+ * "provider": "github",
190
+ * "scopes": ["repo", "user"],
191
+ * "expiresAt": 1234567890
192
+ * }
193
+ * ```
194
+ *
195
+ * @example
196
+ * ```typescript
197
+ * // app/routes/api/integrate/oauth/status.ts
198
+ * import { createTanStackOAuthHandler } from 'integrate-sdk';
199
+ *
200
+ * const handler = createTanStackOAuthHandler({
201
+ * * providers: {
202
+ * github: {
203
+ * clientId: process.env.GITHUB_CLIENT_ID!,
204
+ * clientSecret: process.env.GITHUB_CLIENT_SECRET!,
205
+ * },
206
+ * },
207
+ * });
208
+ *
209
+ * export const GET = handler.status;
210
+ * ```
211
+ */
212
+ async status({ request }: { request: Request }): Promise<Response> {
213
+ try {
214
+ const url = new URL(request.url);
215
+ const provider = url.searchParams.get('provider');
216
+ const sessionToken = request.headers.get('x-session-token');
217
+
218
+ if (!provider) {
219
+ return new Response(
220
+ JSON.stringify({ error: 'Missing provider query parameter' }),
221
+ {
222
+ status: 400,
223
+ headers: {
224
+ 'Content-Type': 'application/json',
225
+ },
226
+ }
227
+ );
228
+ }
229
+
230
+ if (!sessionToken) {
231
+ return new Response(
232
+ JSON.stringify({ error: 'Missing X-Session-Token header' }),
233
+ {
234
+ status: 400,
235
+ headers: {
236
+ 'Content-Type': 'application/json',
237
+ },
238
+ }
239
+ );
240
+ }
241
+
242
+ const result = await handler.handleStatus(provider, sessionToken);
243
+ return new Response(JSON.stringify(result), {
244
+ status: 200,
245
+ headers: {
246
+ 'Content-Type': 'application/json',
247
+ },
248
+ });
249
+ } catch (error: any) {
250
+ console.error('[OAuth Status] Error:', error);
251
+ return new Response(
252
+ JSON.stringify({ error: error.message || 'Failed to check authorization status' }),
253
+ {
254
+ status: 500,
255
+ headers: {
256
+ 'Content-Type': 'application/json',
257
+ },
258
+ }
259
+ );
260
+ }
261
+ },
262
+ };
263
+ }
264
+