integrate-sdk 0.6.7 → 0.6.9

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.
@@ -8,6 +8,7 @@ var __export = (target, all) => {
8
8
  set: (newValue) => all[name] = () => newValue
9
9
  });
10
10
  };
11
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
11
12
 
12
13
  // src/adapters/tanstack-start.ts
13
14
  function toTanStackStartHandler(handler) {
package/dist/index.js CHANGED
@@ -2025,52 +2025,25 @@ function toNodeHandler(config) {
2025
2025
  }
2026
2026
  };
2027
2027
  }
2028
- // src/adapters/solid-start.ts
2029
- function toSolidStartHandler(baseHandler) {
2030
- const handler = async (event) => {
2031
- return baseHandler(event.request);
2032
- };
2033
- return {
2034
- GET: handler,
2035
- POST: handler,
2036
- PATCH: handler,
2037
- PUT: handler,
2038
- DELETE: handler
2039
- };
2040
- }
2041
- // src/adapters/svelte-kit.ts
2042
- async function svelteKitHandler({
2043
- authConfig,
2044
- event,
2045
- resolve,
2046
- basePath = "/api/auth"
2047
- }) {
2048
- const { url } = event;
2049
- const baseUrl = new URL(basePath, url.origin);
2050
- if (!url.pathname.startsWith(baseUrl.pathname)) {
2051
- return resolve(event);
2028
+ // src/utils/env.ts
2029
+ function getEnv(key) {
2030
+ try {
2031
+ const metaEnv = import.meta.env;
2032
+ if (metaEnv && typeof metaEnv === "object" && metaEnv !== null) {
2033
+ const value = metaEnv[key];
2034
+ if (value !== undefined && value !== null && value !== "") {
2035
+ return String(value);
2036
+ }
2037
+ }
2038
+ } catch {}
2039
+ if (typeof process !== "undefined" && process.env) {
2040
+ const value = process.env[key];
2041
+ if (value !== undefined && value !== null && value !== "") {
2042
+ return value;
2043
+ }
2052
2044
  }
2053
- return authConfig(event.request);
2054
- }
2055
- function toSvelteKitHandler(baseHandler) {
2056
- return async (event) => {
2057
- return baseHandler(event.request);
2058
- };
2045
+ return;
2059
2046
  }
2060
- // src/adapters/tanstack-start.ts
2061
- function toTanStackStartHandler(handler) {
2062
- const baseHandler = async ({ request }) => {
2063
- return handler(request);
2064
- };
2065
- return {
2066
- GET: baseHandler,
2067
- POST: baseHandler
2068
- };
2069
- }
2070
- var createTanStackOAuthHandler = toTanStackStartHandler;
2071
-
2072
- // src/index.ts
2073
- init_errors();
2074
2047
 
2075
2048
  // src/plugins/github.ts
2076
2049
  var GITHUB_TOOLS = [
@@ -2096,8 +2069,8 @@ var GITHUB_TOOLS = [
2096
2069
  function githubPlugin(config = {}) {
2097
2070
  const oauth = {
2098
2071
  provider: "github",
2099
- clientId: config.clientId ?? process.env.GITHUB_CLIENT_ID,
2100
- clientSecret: config.clientSecret ?? process.env.GITHUB_CLIENT_SECRET,
2072
+ clientId: config.clientId ?? getEnv("GITHUB_CLIENT_ID"),
2073
+ clientSecret: config.clientSecret ?? getEnv("GITHUB_CLIENT_SECRET"),
2101
2074
  scopes: config.scopes || ["repo", "user"],
2102
2075
  redirectUri: config.redirectUri,
2103
2076
  config: {
@@ -2127,8 +2100,8 @@ var GMAIL_TOOLS = [
2127
2100
  function gmailPlugin(config = {}) {
2128
2101
  const oauth = {
2129
2102
  provider: "gmail",
2130
- clientId: config.clientId ?? process.env.GMAIL_CLIENT_ID,
2131
- clientSecret: config.clientSecret ?? process.env.GMAIL_CLIENT_SECRET,
2103
+ clientId: config.clientId ?? getEnv("GMAIL_CLIENT_ID"),
2104
+ clientSecret: config.clientSecret ?? getEnv("GMAIL_CLIENT_SECRET"),
2132
2105
  scopes: config.scopes || [
2133
2106
  "https://www.googleapis.com/auth/gmail.send",
2134
2107
  "https://www.googleapis.com/auth/gmail.readonly",
@@ -2155,8 +2128,8 @@ function genericOAuthPlugin(config) {
2155
2128
  const providerUpper = config.provider.toUpperCase().replace(/[^A-Z0-9]/g, "_");
2156
2129
  const oauth = {
2157
2130
  provider: config.provider,
2158
- clientId: config.clientId ?? process.env[`${providerUpper}_CLIENT_ID`],
2159
- clientSecret: config.clientSecret ?? process.env[`${providerUpper}_CLIENT_SECRET`],
2131
+ clientId: config.clientId ?? getEnv(`${providerUpper}_CLIENT_ID`),
2132
+ clientSecret: config.clientSecret ?? getEnv(`${providerUpper}_CLIENT_SECRET`),
2160
2133
  scopes: config.scopes,
2161
2134
  redirectUri: config.redirectUri,
2162
2135
  config
@@ -2179,6 +2152,319 @@ function createSimplePlugin(config) {
2179
2152
  onDisconnect: config.onDisconnect
2180
2153
  };
2181
2154
  }
2155
+
2156
+ // src/server.ts
2157
+ var globalServerConfig = null;
2158
+ function getDefaultRedirectUri() {
2159
+ if (typeof window !== "undefined") {
2160
+ return `${window.location.origin}/api/integrate/oauth/callback`;
2161
+ }
2162
+ const integrateUrl = getEnv("INTEGRATE_URL");
2163
+ if (integrateUrl) {
2164
+ return `${integrateUrl}/api/integrate/oauth/callback`;
2165
+ }
2166
+ const vercelUrl = getEnv("VERCEL_URL");
2167
+ if (vercelUrl) {
2168
+ return `https://${vercelUrl}/api/integrate/oauth/callback`;
2169
+ }
2170
+ return "http://localhost:3000/api/integrate/oauth/callback";
2171
+ }
2172
+ function createMCPServer(config) {
2173
+ if (typeof window !== "undefined") {
2174
+ throw new Error("createMCPServer() should only be called on the server-side. " + "Use createMCPClient() for client-side code.");
2175
+ }
2176
+ const providers = {};
2177
+ const updatedPlugins = config.plugins.map((plugin) => {
2178
+ if (plugin.oauth) {
2179
+ const { clientId, clientSecret, redirectUri: pluginRedirectUri } = plugin.oauth;
2180
+ if (!clientId || !clientSecret) {
2181
+ console.warn(`Warning: Plugin "${plugin.id}" is missing OAuth credentials. ` + `Provide clientId and clientSecret in the plugin configuration.`);
2182
+ return plugin;
2183
+ }
2184
+ const redirectUri = pluginRedirectUri || config.redirectUri || getDefaultRedirectUri();
2185
+ providers[plugin.id] = {
2186
+ clientId,
2187
+ clientSecret,
2188
+ redirectUri
2189
+ };
2190
+ return {
2191
+ ...plugin,
2192
+ oauth: {
2193
+ ...plugin.oauth,
2194
+ redirectUri
2195
+ }
2196
+ };
2197
+ }
2198
+ return plugin;
2199
+ });
2200
+ globalServerConfig = {
2201
+ providers,
2202
+ serverUrl: config.serverUrl,
2203
+ apiKey: config.apiKey
2204
+ };
2205
+ const clientConfig = {
2206
+ ...config,
2207
+ plugins: updatedPlugins,
2208
+ connectionMode: config.connectionMode || "lazy",
2209
+ singleton: config.singleton ?? true
2210
+ };
2211
+ const client = new MCPClient(clientConfig);
2212
+ if (config.apiKey) {
2213
+ client.setRequestHeader("X-API-KEY", config.apiKey);
2214
+ }
2215
+ client.__oauthConfig = {
2216
+ providers,
2217
+ serverUrl: config.serverUrl,
2218
+ apiKey: config.apiKey
2219
+ };
2220
+ const { POST, GET } = createOAuthRouteHandlers({
2221
+ providers,
2222
+ serverUrl: config.serverUrl,
2223
+ apiKey: config.apiKey
2224
+ });
2225
+ const handler = async (request, context) => {
2226
+ const method = request.method.toUpperCase();
2227
+ let action;
2228
+ let segments = [];
2229
+ if (context?.params?.action) {
2230
+ action = context.params.action;
2231
+ } else if (context?.params?.all) {
2232
+ const all = context.params.all;
2233
+ if (Array.isArray(all)) {
2234
+ segments = all;
2235
+ } else if (typeof all === "string") {
2236
+ segments = all.split("/").filter(Boolean);
2237
+ }
2238
+ if (segments.length === 2 && segments[0] === "oauth") {
2239
+ action = segments[1];
2240
+ } else if (segments.length === 1) {
2241
+ action = segments[0];
2242
+ } else if (segments.length > 0) {
2243
+ action = segments[segments.length - 1];
2244
+ }
2245
+ } else {
2246
+ const url = new URL(request.url);
2247
+ const pathParts = url.pathname.split("/").filter(Boolean);
2248
+ segments = pathParts;
2249
+ const oauthIndex = pathParts.indexOf("oauth");
2250
+ if (oauthIndex >= 0 && oauthIndex < pathParts.length - 1) {
2251
+ action = pathParts[oauthIndex + 1];
2252
+ } else if (pathParts.length > 0) {
2253
+ action = pathParts[pathParts.length - 1];
2254
+ } else {
2255
+ action = "callback";
2256
+ }
2257
+ }
2258
+ if (segments.length > 0) {
2259
+ if (segments.length === 2 && segments[0] !== "oauth") {
2260
+ return Response.json({ error: `Invalid route: /${segments.join("/")}` }, { status: 404 });
2261
+ }
2262
+ }
2263
+ if (method === "GET" && action === "callback") {
2264
+ const url = new URL(request.url);
2265
+ const searchParams = url.searchParams;
2266
+ const code = searchParams.get("code");
2267
+ const state = searchParams.get("state");
2268
+ const error = searchParams.get("error");
2269
+ const errorDescription = searchParams.get("error_description");
2270
+ const defaultRedirectUrl = "/";
2271
+ const errorRedirectUrl = "/auth-error";
2272
+ if (error) {
2273
+ const errorMsg = errorDescription || error;
2274
+ console.error("[OAuth Redirect] Error:", errorMsg);
2275
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent(errorMsg)}`, request.url));
2276
+ }
2277
+ if (!code || !state) {
2278
+ console.error("[OAuth Redirect] Missing code or state parameter");
2279
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent("Invalid OAuth callback")}`, request.url));
2280
+ }
2281
+ let returnUrl = defaultRedirectUrl;
2282
+ try {
2283
+ const { parseState: parseState2 } = await Promise.resolve().then(() => exports_pkce);
2284
+ const stateData = parseState2(state);
2285
+ if (stateData.returnUrl) {
2286
+ returnUrl = stateData.returnUrl;
2287
+ }
2288
+ } catch (e) {
2289
+ try {
2290
+ const referrer = request.headers.get("referer") || request.headers.get("referrer");
2291
+ if (referrer) {
2292
+ const referrerUrl = new URL(referrer);
2293
+ const currentUrl = new URL(request.url);
2294
+ if (referrerUrl.origin === currentUrl.origin) {
2295
+ returnUrl = referrerUrl.pathname + referrerUrl.search;
2296
+ }
2297
+ }
2298
+ } catch {}
2299
+ }
2300
+ const targetUrl = new URL(returnUrl, request.url);
2301
+ targetUrl.hash = `oauth_callback=${encodeURIComponent(JSON.stringify({ code, state }))}`;
2302
+ return Response.redirect(targetUrl);
2303
+ }
2304
+ const handlerContext = { params: { action: action || "callback" } };
2305
+ if (method === "POST") {
2306
+ return POST(request, handlerContext);
2307
+ } else if (method === "GET") {
2308
+ return GET(request, handlerContext);
2309
+ } else {
2310
+ return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
2311
+ }
2312
+ };
2313
+ return {
2314
+ client,
2315
+ POST,
2316
+ GET,
2317
+ handler
2318
+ };
2319
+ }
2320
+ function createOAuthRouteHandlers(config) {
2321
+ const handler = createNextOAuthHandler(config);
2322
+ return handler.createRoutes();
2323
+ }
2324
+ var POST = async (req, context) => {
2325
+ if (!globalServerConfig) {
2326
+ return Response.json({ error: "OAuth not configured. Call createMCPServer() in your server initialization file first." }, { status: 500 });
2327
+ }
2328
+ const handler = createNextOAuthHandler(globalServerConfig);
2329
+ const routes = handler.createRoutes();
2330
+ return routes.POST(req, context);
2331
+ };
2332
+ var GET = async (req, context) => {
2333
+ if (!globalServerConfig) {
2334
+ return Response.json({ error: "OAuth not configured. Call createMCPServer() in your server initialization file first." }, { status: 500 });
2335
+ }
2336
+ const handler = createNextOAuthHandler(globalServerConfig);
2337
+ const routes = handler.createRoutes();
2338
+ return routes.GET(req, context);
2339
+ };
2340
+ function toNextJsHandler(options) {
2341
+ const POST2 = async (req, context) => {
2342
+ const config = options.config || options.client?.__oauthConfig;
2343
+ if (!config) {
2344
+ return Response.json({ error: 'OAuth not configured. You must pass either "client" (from createMCPServer) or "config" to toNextJsHandler().' }, { status: 500 });
2345
+ }
2346
+ const handler = createNextOAuthHandler(config);
2347
+ const routes = handler.toNextJsHandler({
2348
+ redirectUrl: options.redirectUrl,
2349
+ errorRedirectUrl: options.errorRedirectUrl
2350
+ });
2351
+ return routes.POST(req, context);
2352
+ };
2353
+ const GET2 = async (req, context) => {
2354
+ const config = options.config || options.client?.__oauthConfig;
2355
+ if (!config) {
2356
+ return Response.json({ error: 'OAuth not configured. You must pass either "client" (from createMCPServer) or "config" to toNextJsHandler().' }, { status: 500 });
2357
+ }
2358
+ const handler = createNextOAuthHandler(config);
2359
+ const routes = handler.toNextJsHandler({
2360
+ redirectUrl: options.redirectUrl,
2361
+ errorRedirectUrl: options.errorRedirectUrl
2362
+ });
2363
+ return routes.GET(req, context);
2364
+ };
2365
+ return { POST: POST2, GET: GET2 };
2366
+ }
2367
+ function toAstroHandler(baseHandler, options) {
2368
+ const defaultRedirectUrl = options?.redirectUrl || "/";
2369
+ const errorRedirectUrl = options?.errorRedirectUrl || "/auth-error";
2370
+ return async (ctx) => {
2371
+ const wrappedHandler = async (request, context) => {
2372
+ const url = new URL(request.url);
2373
+ const method = request.method.toUpperCase();
2374
+ const pathParts = url.pathname.split("/").filter(Boolean);
2375
+ const oauthIndex = pathParts.indexOf("oauth");
2376
+ if (method === "GET" && oauthIndex >= 0 && pathParts[oauthIndex + 1] === "callback") {
2377
+ const searchParams = url.searchParams;
2378
+ const code = searchParams.get("code");
2379
+ const state = searchParams.get("state");
2380
+ const error = searchParams.get("error");
2381
+ const errorDescription = searchParams.get("error_description");
2382
+ if (error) {
2383
+ const errorMsg = errorDescription || error;
2384
+ console.error("[OAuth Redirect] Error:", errorMsg);
2385
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent(errorMsg)}`, request.url));
2386
+ }
2387
+ if (!code || !state) {
2388
+ console.error("[OAuth Redirect] Missing code or state parameter");
2389
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent("Invalid OAuth callback")}`, request.url));
2390
+ }
2391
+ let returnUrl = defaultRedirectUrl;
2392
+ try {
2393
+ const { parseState: parseState2 } = await Promise.resolve().then(() => exports_pkce);
2394
+ const stateData = parseState2(state);
2395
+ if (stateData.returnUrl) {
2396
+ returnUrl = stateData.returnUrl;
2397
+ }
2398
+ } catch (e) {
2399
+ try {
2400
+ const referrer = request.headers.get("referer") || request.headers.get("referrer");
2401
+ if (referrer) {
2402
+ const referrerUrl = new URL(referrer);
2403
+ const currentUrl = new URL(request.url);
2404
+ if (referrerUrl.origin === currentUrl.origin) {
2405
+ returnUrl = referrerUrl.pathname + referrerUrl.search;
2406
+ }
2407
+ }
2408
+ } catch {}
2409
+ }
2410
+ const targetUrl = new URL(returnUrl, request.url);
2411
+ targetUrl.hash = `oauth_callback=${encodeURIComponent(JSON.stringify({ code, state }))}`;
2412
+ return Response.redirect(targetUrl);
2413
+ }
2414
+ return baseHandler(request, context);
2415
+ };
2416
+ return wrappedHandler(ctx.request, { params: { all: ctx.params.all } });
2417
+ };
2418
+ }
2419
+ function toSolidStartHandler(baseHandler, options) {
2420
+ const wrappedHandler = toAstroHandler(baseHandler, options);
2421
+ const handler = async (event) => {
2422
+ return wrappedHandler({ request: event.request, params: {} });
2423
+ };
2424
+ return {
2425
+ GET: handler,
2426
+ POST: handler,
2427
+ PATCH: handler,
2428
+ PUT: handler,
2429
+ DELETE: handler
2430
+ };
2431
+ }
2432
+ function toSvelteKitHandler(baseHandler, options) {
2433
+ const wrappedHandler = toAstroHandler(baseHandler, options);
2434
+ return async (event) => {
2435
+ const all = event.params?.all;
2436
+ return wrappedHandler({ request: event.request, params: { all } });
2437
+ };
2438
+ }
2439
+ // src/adapters/svelte-kit.ts
2440
+ async function svelteKitHandler({
2441
+ authConfig,
2442
+ event,
2443
+ resolve,
2444
+ basePath = "/api/integrate"
2445
+ }) {
2446
+ const { url } = event;
2447
+ const baseUrl = new URL(basePath, url.origin);
2448
+ if (!url.pathname.startsWith(baseUrl.pathname)) {
2449
+ return resolve(event);
2450
+ }
2451
+ return authConfig(event.request);
2452
+ }
2453
+ // src/adapters/tanstack-start.ts
2454
+ function toTanStackStartHandler(handler) {
2455
+ const baseHandler = async ({ request }) => {
2456
+ return handler(request);
2457
+ };
2458
+ return {
2459
+ GET: baseHandler,
2460
+ POST: baseHandler
2461
+ };
2462
+ }
2463
+ var createTanStackOAuthHandler = toTanStackStartHandler;
2464
+
2465
+ // src/index.ts
2466
+ init_errors();
2467
+
2182
2468
  // node_modules/zod/v3/external.js
2183
2469
  var exports_external = {};
2184
2470
  __export(exports_external, {
package/dist/server.js CHANGED
@@ -1852,6 +1852,26 @@ function createNextOAuthHandler(config) {
1852
1852
  return handlers;
1853
1853
  }
1854
1854
 
1855
+ // src/utils/env.ts
1856
+ function getEnv(key) {
1857
+ try {
1858
+ const metaEnv = import.meta.env;
1859
+ if (metaEnv && typeof metaEnv === "object" && metaEnv !== null) {
1860
+ const value = metaEnv[key];
1861
+ if (value !== undefined && value !== null && value !== "") {
1862
+ return String(value);
1863
+ }
1864
+ }
1865
+ } catch {}
1866
+ if (typeof process !== "undefined" && process.env) {
1867
+ const value = process.env[key];
1868
+ if (value !== undefined && value !== null && value !== "") {
1869
+ return value;
1870
+ }
1871
+ }
1872
+ return;
1873
+ }
1874
+
1855
1875
  // src/plugins/github.ts
1856
1876
  var GITHUB_TOOLS = [
1857
1877
  "github_create_issue",
@@ -1876,8 +1896,8 @@ var GITHUB_TOOLS = [
1876
1896
  function githubPlugin(config = {}) {
1877
1897
  const oauth = {
1878
1898
  provider: "github",
1879
- clientId: config.clientId ?? process.env.GITHUB_CLIENT_ID,
1880
- clientSecret: config.clientSecret ?? process.env.GITHUB_CLIENT_SECRET,
1899
+ clientId: config.clientId ?? getEnv("GITHUB_CLIENT_ID"),
1900
+ clientSecret: config.clientSecret ?? getEnv("GITHUB_CLIENT_SECRET"),
1881
1901
  scopes: config.scopes || ["repo", "user"],
1882
1902
  redirectUri: config.redirectUri,
1883
1903
  config: {
@@ -1907,8 +1927,8 @@ var GMAIL_TOOLS = [
1907
1927
  function gmailPlugin(config = {}) {
1908
1928
  const oauth = {
1909
1929
  provider: "gmail",
1910
- clientId: config.clientId ?? process.env.GMAIL_CLIENT_ID,
1911
- clientSecret: config.clientSecret ?? process.env.GMAIL_CLIENT_SECRET,
1930
+ clientId: config.clientId ?? getEnv("GMAIL_CLIENT_ID"),
1931
+ clientSecret: config.clientSecret ?? getEnv("GMAIL_CLIENT_SECRET"),
1912
1932
  scopes: config.scopes || [
1913
1933
  "https://www.googleapis.com/auth/gmail.send",
1914
1934
  "https://www.googleapis.com/auth/gmail.readonly",
@@ -1935,8 +1955,8 @@ function genericOAuthPlugin(config) {
1935
1955
  const providerUpper = config.provider.toUpperCase().replace(/[^A-Z0-9]/g, "_");
1936
1956
  const oauth = {
1937
1957
  provider: config.provider,
1938
- clientId: config.clientId ?? process.env[`${providerUpper}_CLIENT_ID`],
1939
- clientSecret: config.clientSecret ?? process.env[`${providerUpper}_CLIENT_SECRET`],
1958
+ clientId: config.clientId ?? getEnv(`${providerUpper}_CLIENT_ID`),
1959
+ clientSecret: config.clientSecret ?? getEnv(`${providerUpper}_CLIENT_SECRET`),
1940
1960
  scopes: config.scopes,
1941
1961
  redirectUri: config.redirectUri,
1942
1962
  config
@@ -1966,11 +1986,13 @@ function getDefaultRedirectUri() {
1966
1986
  if (typeof window !== "undefined") {
1967
1987
  return `${window.location.origin}/api/integrate/oauth/callback`;
1968
1988
  }
1969
- if (process.env.INTEGRATE_URL) {
1970
- return `${process.env.INTEGRATE_URL}/api/integrate/oauth/callback`;
1989
+ const integrateUrl = getEnv("INTEGRATE_URL");
1990
+ if (integrateUrl) {
1991
+ return `${integrateUrl}/api/integrate/oauth/callback`;
1971
1992
  }
1972
- if (process.env.VERCEL_URL) {
1973
- return `https://${process.env.VERCEL_URL}/api/integrate/oauth/callback`;
1993
+ const vercelUrl = getEnv("VERCEL_URL");
1994
+ if (vercelUrl) {
1995
+ return `https://${vercelUrl}/api/integrate/oauth/callback`;
1974
1996
  }
1975
1997
  return "http://localhost:3000/api/integrate/oauth/callback";
1976
1998
  }
@@ -2027,10 +2049,99 @@ function createMCPServer(config) {
2027
2049
  serverUrl: config.serverUrl,
2028
2050
  apiKey: config.apiKey
2029
2051
  });
2052
+ const handler = async (request, context) => {
2053
+ const method = request.method.toUpperCase();
2054
+ let action;
2055
+ let segments = [];
2056
+ if (context?.params?.action) {
2057
+ action = context.params.action;
2058
+ } else if (context?.params?.all) {
2059
+ const all = context.params.all;
2060
+ if (Array.isArray(all)) {
2061
+ segments = all;
2062
+ } else if (typeof all === "string") {
2063
+ segments = all.split("/").filter(Boolean);
2064
+ }
2065
+ if (segments.length === 2 && segments[0] === "oauth") {
2066
+ action = segments[1];
2067
+ } else if (segments.length === 1) {
2068
+ action = segments[0];
2069
+ } else if (segments.length > 0) {
2070
+ action = segments[segments.length - 1];
2071
+ }
2072
+ } else {
2073
+ const url = new URL(request.url);
2074
+ const pathParts = url.pathname.split("/").filter(Boolean);
2075
+ segments = pathParts;
2076
+ const oauthIndex = pathParts.indexOf("oauth");
2077
+ if (oauthIndex >= 0 && oauthIndex < pathParts.length - 1) {
2078
+ action = pathParts[oauthIndex + 1];
2079
+ } else if (pathParts.length > 0) {
2080
+ action = pathParts[pathParts.length - 1];
2081
+ } else {
2082
+ action = "callback";
2083
+ }
2084
+ }
2085
+ if (segments.length > 0) {
2086
+ if (segments.length === 2 && segments[0] !== "oauth") {
2087
+ return Response.json({ error: `Invalid route: /${segments.join("/")}` }, { status: 404 });
2088
+ }
2089
+ }
2090
+ if (method === "GET" && action === "callback") {
2091
+ const url = new URL(request.url);
2092
+ const searchParams = url.searchParams;
2093
+ const code = searchParams.get("code");
2094
+ const state = searchParams.get("state");
2095
+ const error = searchParams.get("error");
2096
+ const errorDescription = searchParams.get("error_description");
2097
+ const defaultRedirectUrl = "/";
2098
+ const errorRedirectUrl = "/auth-error";
2099
+ if (error) {
2100
+ const errorMsg = errorDescription || error;
2101
+ console.error("[OAuth Redirect] Error:", errorMsg);
2102
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent(errorMsg)}`, request.url));
2103
+ }
2104
+ if (!code || !state) {
2105
+ console.error("[OAuth Redirect] Missing code or state parameter");
2106
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent("Invalid OAuth callback")}`, request.url));
2107
+ }
2108
+ let returnUrl = defaultRedirectUrl;
2109
+ try {
2110
+ const { parseState: parseState2 } = await Promise.resolve().then(() => exports_pkce);
2111
+ const stateData = parseState2(state);
2112
+ if (stateData.returnUrl) {
2113
+ returnUrl = stateData.returnUrl;
2114
+ }
2115
+ } catch (e) {
2116
+ try {
2117
+ const referrer = request.headers.get("referer") || request.headers.get("referrer");
2118
+ if (referrer) {
2119
+ const referrerUrl = new URL(referrer);
2120
+ const currentUrl = new URL(request.url);
2121
+ if (referrerUrl.origin === currentUrl.origin) {
2122
+ returnUrl = referrerUrl.pathname + referrerUrl.search;
2123
+ }
2124
+ }
2125
+ } catch {}
2126
+ }
2127
+ const targetUrl = new URL(returnUrl, request.url);
2128
+ targetUrl.hash = `oauth_callback=${encodeURIComponent(JSON.stringify({ code, state }))}`;
2129
+ return Response.redirect(targetUrl);
2130
+ }
2131
+ const handlerContext = { params: { action: action || "callback" } };
2132
+ if (method === "POST") {
2133
+ return POST(request, handlerContext);
2134
+ } else if (method === "GET") {
2135
+ return GET(request, handlerContext);
2136
+ } else {
2137
+ return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
2138
+ }
2139
+ };
2030
2140
  return {
2031
2141
  client,
2032
2142
  POST,
2033
- GET
2143
+ GET,
2144
+ handler
2034
2145
  };
2035
2146
  }
2036
2147
  function createOAuthRouteHandlers(config) {
@@ -2080,8 +2191,83 @@ function toNextJsHandler(options) {
2080
2191
  };
2081
2192
  return { POST: POST2, GET: GET2 };
2082
2193
  }
2194
+ function toAstroHandler(baseHandler, options) {
2195
+ const defaultRedirectUrl = options?.redirectUrl || "/";
2196
+ const errorRedirectUrl = options?.errorRedirectUrl || "/auth-error";
2197
+ return async (ctx) => {
2198
+ const wrappedHandler = async (request, context) => {
2199
+ const url = new URL(request.url);
2200
+ const method = request.method.toUpperCase();
2201
+ const pathParts = url.pathname.split("/").filter(Boolean);
2202
+ const oauthIndex = pathParts.indexOf("oauth");
2203
+ if (method === "GET" && oauthIndex >= 0 && pathParts[oauthIndex + 1] === "callback") {
2204
+ const searchParams = url.searchParams;
2205
+ const code = searchParams.get("code");
2206
+ const state = searchParams.get("state");
2207
+ const error = searchParams.get("error");
2208
+ const errorDescription = searchParams.get("error_description");
2209
+ if (error) {
2210
+ const errorMsg = errorDescription || error;
2211
+ console.error("[OAuth Redirect] Error:", errorMsg);
2212
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent(errorMsg)}`, request.url));
2213
+ }
2214
+ if (!code || !state) {
2215
+ console.error("[OAuth Redirect] Missing code or state parameter");
2216
+ return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent("Invalid OAuth callback")}`, request.url));
2217
+ }
2218
+ let returnUrl = defaultRedirectUrl;
2219
+ try {
2220
+ const { parseState: parseState2 } = await Promise.resolve().then(() => exports_pkce);
2221
+ const stateData = parseState2(state);
2222
+ if (stateData.returnUrl) {
2223
+ returnUrl = stateData.returnUrl;
2224
+ }
2225
+ } catch (e) {
2226
+ try {
2227
+ const referrer = request.headers.get("referer") || request.headers.get("referrer");
2228
+ if (referrer) {
2229
+ const referrerUrl = new URL(referrer);
2230
+ const currentUrl = new URL(request.url);
2231
+ if (referrerUrl.origin === currentUrl.origin) {
2232
+ returnUrl = referrerUrl.pathname + referrerUrl.search;
2233
+ }
2234
+ }
2235
+ } catch {}
2236
+ }
2237
+ const targetUrl = new URL(returnUrl, request.url);
2238
+ targetUrl.hash = `oauth_callback=${encodeURIComponent(JSON.stringify({ code, state }))}`;
2239
+ return Response.redirect(targetUrl);
2240
+ }
2241
+ return baseHandler(request, context);
2242
+ };
2243
+ return wrappedHandler(ctx.request, { params: { all: ctx.params.all } });
2244
+ };
2245
+ }
2246
+ function toSolidStartHandler(baseHandler, options) {
2247
+ const wrappedHandler = toAstroHandler(baseHandler, options);
2248
+ const handler = async (event) => {
2249
+ return wrappedHandler({ request: event.request, params: {} });
2250
+ };
2251
+ return {
2252
+ GET: handler,
2253
+ POST: handler,
2254
+ PATCH: handler,
2255
+ PUT: handler,
2256
+ DELETE: handler
2257
+ };
2258
+ }
2259
+ function toSvelteKitHandler(baseHandler, options) {
2260
+ const wrappedHandler = toAstroHandler(baseHandler, options);
2261
+ return async (event) => {
2262
+ const all = event.params?.all;
2263
+ return wrappedHandler({ request: event.request, params: { all } });
2264
+ };
2265
+ }
2083
2266
  export {
2267
+ toSvelteKitHandler,
2268
+ toSolidStartHandler,
2084
2269
  toNextJsHandler,
2270
+ toAstroHandler,
2085
2271
  gmailPlugin,
2086
2272
  githubPlugin,
2087
2273
  genericOAuthPlugin,