@devries-cameron20 / composio-1 / commits / 3165eb0bc
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(docs): move API reference redirect to proxy.ts

Next.js doesn't allow both middleware.ts and proxy.ts. Added kebab-case → camelCase redirect logic to existing proxy.ts. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Sushmithamallesh committed Jan 28, 2026 at 01:37 UTC 3165eb0bcd9f3770ad797cc2146057b64faba698
2 files changed +51 -66
docs/middleware.ts deleted
-63
@@ -1,63 +0,0 @@
1 -import { NextResponse } from 'next/server';
2 -import type { NextRequest } from 'next/server';
3 -
4 -/**
5 - * Convert kebab-case to camelCase
6 - */
7 -function kebabToCamel(str: string): string {
8 - return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
9 -}
10 -
11 -/**
12 - * Check if a string contains kebab-case (has hyphens between lowercase letters)
13 - */
14 -function hasKebabCase(str: string): boolean {
15 - return /[a-z]-[a-z]/.test(str);
16 -}
17 -
18 -export function middleware(request: NextRequest) {
19 - const { pathname } = request.nextUrl;
20 -
21 - // Only handle API reference paths
22 - if (!pathname.startsWith('/reference/api-reference/')) {
23 - return NextResponse.next();
24 - }
25 -
26 - // Check if any path segment has kebab-case
27 - const segments = pathname.split('/');
28 - let hasKebab = false;
29 -
30 - for (const segment of segments) {
31 - if (hasKebabCase(segment)) {
32 - hasKebab = true;
33 - break;
34 - }
35 - }
36 -
37 - if (!hasKebab) {
38 - return NextResponse.next();
39 - }
40 -
41 - // Convert kebab-case segments to camelCase
42 - const newSegments = segments.map((segment, index) => {
43 - // Only convert segments after 'api-reference' (skip the tag/category names)
44 - // api-reference structure: /reference/api-reference/{tag}/{operationId}
45 - if (index <= 3) return segment; // Keep /reference/api-reference/{tag} as-is
46 - return kebabToCamel(segment);
47 - });
48 -
49 - const newPathname = newSegments.join('/');
50 -
51 - // Only redirect if the path actually changed
52 - if (newPathname !== pathname) {
53 - const url = request.nextUrl.clone();
54 - url.pathname = newPathname;
55 - return NextResponse.redirect(url, 301);
56 - }
57 -
58 - return NextResponse.next();
59 -}
60 -
61 -export const config = {
62 - matcher: '/reference/api-reference/:path*',
63 -};
docs/proxy.ts
+51 -3
@@ -1,17 +1,65 @@
1 import { NextRequest, NextResponse } from 'next/server';
2
3 /**
4 - * Serves markdown when AI agents request it via Accept: text/markdown header.
5 - * Test: curl -H "Accept: text/markdown" http://localhost:3000/docs/quickstart
4 + * Convert kebab-case to camelCase
5 + */
6 +function kebabToCamel(str: string): string {
7 + return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
8 +}
9 +
10 +/**
11 + * Check if a string contains kebab-case (has hyphens between lowercase letters)
12 + */
13 +function hasKebabCase(str: string): boolean {
14 + return /[a-z]-[a-z]/.test(str);
15 +}
16 +
17 +/**
18 + * Proxy handles:
19 + * 1. Markdown content negotiation for AI agents (Accept: text/markdown)
20 + * 2. Redirects for old Fern API reference URLs (kebab-case → camelCase)
21 */
22 export function proxy(request: NextRequest) {
23 + const { pathname } = request.nextUrl;
24 const accept = request.headers.get('accept') || '';
25
26 + // Handle markdown content negotiation for AI agents
27 if (accept.includes('text/markdown') || accept.includes('text/plain')) {
11 - const mdUrl = new URL(`/llms.mdx${request.nextUrl.pathname}`, request.nextUrl.origin);
28 + const mdUrl = new URL(`/llms.mdx${pathname}`, request.nextUrl.origin);
29 return NextResponse.rewrite(mdUrl);
30 }
31
32 + // Handle old Fern API reference URLs (kebab-case → camelCase)
33 + // Example: /reference/api-reference/tools/get-tools → /reference/api-reference/tools/getTools
34 + if (pathname.startsWith('/reference/api-reference/')) {
35 + const segments = pathname.split('/');
36 + let hasKebab = false;
37 +
38 + // Check if any segment after the tag has kebab-case
39 + // Structure: /reference/api-reference/{tag}/{operationId}
40 + for (let i = 4; i < segments.length; i++) {
41 + if (hasKebabCase(segments[i])) {
42 + hasKebab = true;
43 + break;
44 + }
45 + }
46 +
47 + if (hasKebab) {
48 + // Convert kebab-case segments to camelCase (only operation IDs, not tags)
49 + const newSegments = segments.map((segment, index) => {
50 + if (index <= 3) return segment; // Keep /reference/api-reference/{tag} as-is
51 + return kebabToCamel(segment);
52 + });
53 +
54 + const newPathname = newSegments.join('/');
55 + if (newPathname !== pathname) {
56 + const url = request.nextUrl.clone();
57 + url.pathname = newPathname;
58 + return NextResponse.redirect(url, 301);
59 + }
60 + }
61 + }
62 +
63 return NextResponse.next();
64 }
65