bunderstack-start 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kirill Chuprov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # bunderstack-start
2
+
3
+ TanStack Start integration for
4
+ [bunderstack](https://github.com/kirill-dev-pro/bunderstack): isomorphic
5
+ fetch wiring, auth client, and query/sync setup for SSR apps.
6
+
7
+ ```sh
8
+ bun add bunderstack-start
9
+ ```
10
+
11
+ Full documentation and examples:
12
+ [github.com/kirill-dev-pro/bunderstack](https://github.com/kirill-dev-pro/bunderstack)
13
+
14
+ ## Shipping TypeScript source
15
+
16
+ This package publishes raw TypeScript (`exports` point at `.ts` files). Bun
17
+ consumes it natively. If a Node-based bundler or SSR server processes it,
18
+ make sure the package is bundled rather than externalized — e.g. in Vite:
19
+
20
+ ```ts
21
+ ssr: {
22
+ noExternal: [/^bunderstack/]
23
+ }
24
+ ```
25
+
26
+ ## License
27
+
28
+ MIT
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "bunderstack-start",
3
+ "version": "0.1.0",
4
+ "description": "TanStack Start integration for bunderstack: isomorphic fetch wiring, auth client, and query/sync setup for SSR apps.",
5
+ "keywords": [
6
+ "auth",
7
+ "bun",
8
+ "bunderstack",
9
+ "react",
10
+ "ssr",
11
+ "tanstack-start"
12
+ ],
13
+ "homepage": "https://github.com/kirill-dev-pro/bunderstack#readme",
14
+ "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/kirill-dev-pro/bunderstack.git",
18
+ "directory": "packages/bunderstack-start"
19
+ },
20
+ "files": [
21
+ "src",
22
+ "!src/**/*.test.ts",
23
+ "README.md",
24
+ "LICENSE"
25
+ ],
26
+ "type": "module",
27
+ "main": "./src/index.ts",
28
+ "module": "./src/index.ts",
29
+ "types": "./src/index.ts",
30
+ "exports": {
31
+ ".": "./src/index.ts"
32
+ },
33
+ "scripts": {
34
+ "test": "bun test"
35
+ },
36
+ "dependencies": {
37
+ "bunderstack-query": "0.1.0",
38
+ "bunderstack-sync": "0.1.0"
39
+ },
40
+ "devDependencies": {
41
+ "@tanstack/react-query": "^5.101.1",
42
+ "@tanstack/react-start": "^1.168.26",
43
+ "@types/bun": "^1.3.14",
44
+ "better-auth": "^1.0.0",
45
+ "typescript": "^5.0.0"
46
+ },
47
+ "peerDependencies": {
48
+ "@tanstack/react-query": "^5.101.0",
49
+ "@tanstack/react-start": ">=1.0.0",
50
+ "better-auth": "^1.0.0",
51
+ "typescript": "^5"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "better-auth": {
55
+ "optional": true
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,16 @@
1
+ import { createAuthClient } from 'better-auth/react'
2
+
3
+ /**
4
+ * BetterAuth browser client pointed at the Bunderstack handler's
5
+ * /api/auth/* routes. Defaults to the current origin in the browser and
6
+ * APP_URL (or localhost:3000) during SSR.
7
+ */
8
+ export function createStartAuthClient(options: { baseURL?: string } = {}) {
9
+ return createAuthClient({
10
+ baseURL:
11
+ options.baseURL ??
12
+ (typeof window !== 'undefined'
13
+ ? window.location.origin
14
+ : (process.env.APP_URL ?? 'http://localhost:3000')),
15
+ })
16
+ }
package/src/index.ts ADDED
@@ -0,0 +1,99 @@
1
+ import { QueryClient } from '@tanstack/react-query'
2
+ import {
3
+ createSyncClient,
4
+ type AnyBunderstackApp,
5
+ type BunderstackSyncClient,
6
+ } from 'bunderstack-sync'
7
+
8
+ import { createIsomorphicFetch } from './isomorphic-fetch'
9
+
10
+ export { createIsomorphicFetch } from './isomorphic-fetch'
11
+ export { createStartAuthClient } from './auth-client'
12
+
13
+ export type BunderstackStartOptions = {
14
+ /** API mount point. Defaults to '/api'. */
15
+ baseUrl?: string
16
+ /** Default query staleTime (ms). Defaults to 30_000. */
17
+ staleTime?: number
18
+ }
19
+
20
+ /**
21
+ * One-call client setup for TanStack Start apps: SSR-aware fetch, sensible
22
+ * QueryClient defaults, and a sync client whose tables and buckets are
23
+ * inferred from the server app type.
24
+ *
25
+ * @example
26
+ * // src/api.ts — NOT src/client.ts, which is a reserved TanStack Start
27
+ * // entry-point name (it would replace the hydration entry).
28
+ * import type { App } from './bunderstack'
29
+ * export const { createQueryClient, createApi } = bunderstackStart<App>()
30
+ */
31
+ export function bunderstackStart<TApp extends AnyBunderstackApp>(
32
+ options: BunderstackStartOptions = {},
33
+ ) {
34
+ const isoFetch = createIsomorphicFetch()
35
+ return {
36
+ createQueryClient: () =>
37
+ new QueryClient({
38
+ defaultOptions: {
39
+ queries: { staleTime: options.staleTime ?? 30_000 },
40
+ },
41
+ }),
42
+ createApi: (queryClient: QueryClient): BunderstackSyncClient<TApp> =>
43
+ createSyncClient<TApp>({
44
+ queryClient,
45
+ fetch: isoFetch,
46
+ baseUrl: options.baseUrl,
47
+ }),
48
+ }
49
+ }
50
+
51
+ type StartRequestContext = { request: Request }
52
+ type StartHandler = (ctx: StartRequestContext) => Promise<Response>
53
+
54
+ /**
55
+ * Handlers object for the catch-all API file route.
56
+ *
57
+ * @example
58
+ * // src/routes/api/$.tsx
59
+ * export const Route = createFileRoute('/api/$')({
60
+ * server: { handlers: createApiHandlers(app) },
61
+ * })
62
+ */
63
+ export function createApiHandlers(app: {
64
+ handler: (req: Request) => Promise<Response>
65
+ }): {
66
+ GET: StartHandler
67
+ POST: StartHandler
68
+ PATCH: StartHandler
69
+ DELETE: StartHandler
70
+ } {
71
+ const handle: StartHandler = ({ request }) => app.handler(request)
72
+ return { GET: handle, POST: handle, PATCH: handle, DELETE: handle }
73
+ }
74
+
75
+ export type SessionUser = {
76
+ id: string
77
+ email: string
78
+ name: string
79
+ image?: string | null
80
+ }
81
+
82
+ type SessionApp = {
83
+ auth: {
84
+ api: {
85
+ getSession: (opts: {
86
+ headers: Headers
87
+ }) => Promise<{ user: SessionUser | null } | null>
88
+ }
89
+ }
90
+ }
91
+
92
+ /** Resolve the BetterAuth session user for an incoming request (server-side). */
93
+ export async function getSessionUser(
94
+ app: SessionApp,
95
+ request: Request,
96
+ ): Promise<SessionUser | null> {
97
+ const session = await app.auth.api.getSession({ headers: request.headers })
98
+ return session?.user ?? null
99
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * SSR-aware fetch: the browser passes `/api/...` through as-is; on the
3
+ * server, relative URLs are resolved against the incoming request's origin
4
+ * (via @tanstack/react-start/server), falling back to APP_URL /
5
+ * BETTER_AUTH_URL / localhost:3000 outside a request context.
6
+ *
7
+ * The dynamic import is marked vite-ignore so client bundles never try to
8
+ * resolve the server-only module; the `window` guard means it never runs
9
+ * there either.
10
+ */
11
+ export function createIsomorphicFetch(options: { fetch?: typeof fetch } = {}) {
12
+ const inner = options.fetch ?? fetch
13
+ return async function isomorphicFetch(
14
+ input: RequestInfo | URL,
15
+ init?: RequestInit,
16
+ ): Promise<Response> {
17
+ if (typeof window !== 'undefined') return inner(input, init)
18
+ if (typeof input === 'string' && input.startsWith('/')) {
19
+ let origin: string | undefined
20
+ try {
21
+ const mod = await import(
22
+ /* @vite-ignore */ '@tanstack/react-start/server'
23
+ )
24
+ origin = new URL(mod.getRequest().url).origin
25
+ } catch {
26
+ // No request context (background job, test) — fall through to env.
27
+ }
28
+ origin ??=
29
+ process.env.APP_URL ??
30
+ process.env.BETTER_AUTH_URL ??
31
+ 'http://localhost:3000'
32
+ return inner(new URL(input, origin), init)
33
+ }
34
+ return inner(input, init)
35
+ }
36
+ }