@surfaice/next 0.0.1

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 surfaiceai
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,55 @@
1
+ # @surfaice/next
2
+
3
+ Next.js middleware and plugin for Surfaice — serve your UI as markdown to AI agents via `Accept: text/surfaice`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @surfaice/next @surfaice/react @surfaice/format
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### 1. Wrap your Next.js config
14
+
15
+ ```typescript
16
+ // next.config.ts
17
+ import { withSurfaice } from '@surfaice/next'
18
+
19
+ export default withSurfaice({
20
+ enabled: process.env.SURFAICE_ENABLED === 'true',
21
+ exclude: ['/api', '/_next'],
22
+ })(nextConfig)
23
+ ```
24
+
25
+ ### 2. Add middleware
26
+
27
+ ```typescript
28
+ // middleware.ts
29
+ import { shouldHandleSurfaice, isRouteAllowed } from '@surfaice/next'
30
+ import { NextResponse } from 'next/server'
31
+ import type { NextRequest } from 'next/server'
32
+
33
+ export function middleware(req: NextRequest) {
34
+ const accept = req.headers.get('accept') ?? ''
35
+
36
+ if (shouldHandleSurfaice(accept) && isRouteAllowed(req.nextUrl.pathname, config)) {
37
+ // Rewrite to Surfaice render mode
38
+ const url = req.nextUrl.clone()
39
+ url.searchParams.set('__surfaice', '1')
40
+ return NextResponse.rewrite(url)
41
+ }
42
+
43
+ return NextResponse.next()
44
+ }
45
+ ```
46
+
47
+ ### 3. Request in Surfaice format
48
+
49
+ ```bash
50
+ curl -H "Accept: text/surfaice" http://localhost:3000/settings
51
+ ```
52
+
53
+ ## Same URL, Different Content Type
54
+
55
+ The core principle: your `/settings` page serves HTML to browsers and markdown to agents — same URL, same auth, same data.
@@ -0,0 +1,3 @@
1
+ export { shouldHandleSurfaice, isRouteAllowed } from './middleware.js';
2
+ export type { SurfaiceMiddlewareConfig } from './middleware.js';
3
+ export { withSurfaice } from './plugin.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ // @surfaice/next
2
+ export { shouldHandleSurfaice, isRouteAllowed } from './middleware.js';
3
+ export { withSurfaice } from './plugin.js';
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Core middleware logic for Surfaice — framework-agnostic.
3
+ *
4
+ * The actual Next.js middleware wiring is in middleware-next.ts to avoid
5
+ * importing next/server in tests (which have no Next.js runtime).
6
+ */
7
+ export interface SurfaiceMiddlewareConfig {
8
+ /** Master toggle. Default: true */
9
+ enabled?: boolean;
10
+ /** Only serve Surfaice on these route prefixes */
11
+ include?: string[];
12
+ /** Never serve Surfaice on these route prefixes */
13
+ exclude?: string[];
14
+ /** Auth gate — return false to respond 401 */
15
+ auth?: (headers: Record<string, string | undefined>) => boolean | Promise<boolean>;
16
+ }
17
+ /**
18
+ * Check if the Accept header requests text/surfaice.
19
+ */
20
+ export declare function shouldHandleSurfaice(accept: string | undefined): boolean;
21
+ /**
22
+ * Check if a route is allowed to serve Surfaice responses.
23
+ */
24
+ export declare function isRouteAllowed(pathname: string, config: Pick<SurfaiceMiddlewareConfig, 'enabled' | 'include' | 'exclude'>): boolean;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Core middleware logic for Surfaice — framework-agnostic.
3
+ *
4
+ * The actual Next.js middleware wiring is in middleware-next.ts to avoid
5
+ * importing next/server in tests (which have no Next.js runtime).
6
+ */
7
+ /**
8
+ * Check if the Accept header requests text/surfaice.
9
+ */
10
+ export function shouldHandleSurfaice(accept) {
11
+ if (!accept)
12
+ return false;
13
+ return accept.includes('text/surfaice');
14
+ }
15
+ /**
16
+ * Check if a route is allowed to serve Surfaice responses.
17
+ */
18
+ export function isRouteAllowed(pathname, config) {
19
+ // Master toggle
20
+ if (config.enabled === false)
21
+ return false;
22
+ // Exclude takes priority
23
+ if (config.exclude?.some(prefix => pathname.startsWith(prefix)))
24
+ return false;
25
+ // Include filter (prefix match)
26
+ if (config.include) {
27
+ return config.include.some(prefix => pathname.startsWith(prefix));
28
+ }
29
+ return true;
30
+ }
@@ -0,0 +1,16 @@
1
+ import type { SurfaiceMiddlewareConfig } from './middleware.js';
2
+ interface NextConfig {
3
+ env?: Record<string, string>;
4
+ [key: string]: unknown;
5
+ }
6
+ /**
7
+ * withSurfaice — Next.js config plugin.
8
+ * Wrap your next.config.ts with this to inject Surfaice env vars.
9
+ *
10
+ * @example
11
+ * // next.config.ts
12
+ * import { withSurfaice } from '@surfaice/next'
13
+ * export default withSurfaice({ enabled: true })({ reactStrictMode: true })
14
+ */
15
+ export declare function withSurfaice(config?: SurfaiceMiddlewareConfig): (nextConfig?: NextConfig) => NextConfig;
16
+ export {};
package/dist/plugin.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * withSurfaice — Next.js config plugin.
3
+ * Wrap your next.config.ts with this to inject Surfaice env vars.
4
+ *
5
+ * @example
6
+ * // next.config.ts
7
+ * import { withSurfaice } from '@surfaice/next'
8
+ * export default withSurfaice({ enabled: true })({ reactStrictMode: true })
9
+ */
10
+ export function withSurfaice(config = {}) {
11
+ return (nextConfig = {}) => {
12
+ return {
13
+ ...nextConfig,
14
+ env: {
15
+ ...nextConfig.env,
16
+ __SURFAICE_ENABLED: config.enabled !== false ? 'true' : 'false',
17
+ },
18
+ };
19
+ };
20
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@surfaice/next",
3
+ "version": "0.0.1",
4
+ "description": "Next.js middleware for Surfaice — Accept: text/surfaice runtime serving",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/surfaiceai/surfaice",
18
+ "directory": "packages/next"
19
+ },
20
+ "dependencies": {
21
+ "@surfaice/format": "0.0.1",
22
+ "@surfaice/react": "0.0.1"
23
+ },
24
+ "peerDependencies": {
25
+ "next": ">=14.0.0",
26
+ "react": ">=18.0.0"
27
+ },
28
+ "devDependencies": {
29
+ "vitest": "^2.0.0",
30
+ "typescript": "^5.5.0",
31
+ "@types/node": "^22.0.0"
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "README.md"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org"
40
+ },
41
+ "scripts": {
42
+ "build": "tsc",
43
+ "test": "vitest run",
44
+ "dev": "tsc --watch"
45
+ }
46
+ }