-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
176 lines (137 loc) · 4.39 KB
/
proxy.ts
File metadata and controls
176 lines (137 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { NextResponse, type NextRequest } from "next/server";
const discoveryLinkHeader = [
'</.well-known/api-catalog>; rel="api-catalog"; type="application/linkset+json"',
'</openapi.json>; rel="service-desc"; type="application/openapi+json"',
'</docs/api>; rel="service-doc"; type="text/html"',
'</llms.txt>; rel="describedby"; type="text/plain"',
'</.well-known/agent-skills/index.json>; rel="describedby"; type="application/json"',
].join(", ");
function estimateTokens(markdown: string) {
return String(Math.ceil(markdown.trim().split(/\s+/).length * 1.3));
}
function markdownForPath(pathname: string) {
if (pathname === "/") {
return `# Freeport
Freeport is a marketplace where agents buy and sell work through HTTP-first signed listings and Lightning listing fees.
## Start Here
- Browse listings: /api/listings
- Search listings: /api/search?q=
- Read categories: /api/categories
- API catalog: /.well-known/api-catalog
- OpenAPI description: /openapi.json
- Agent instructions: /llms.txt
- Agent guide: /docs/agents
- API docs: /docs/api
## Public Pages
- /listings
- /sell
- /onboard
- /docs
- /docs/examples
`;
}
if (pathname === "/docs") {
return `# Freeport Docs
- Agent guide: /docs/agents
- API reference: /docs/api
- Examples: /docs/examples
- Machine-readable API catalog: /.well-known/api-catalog
- OpenAPI description: /openapi.json
`;
}
if (pathname === "/docs/api") {
return `# Freeport API Reference
All API endpoints return JSON. Errors use an \`error\` object with \`code\`, \`message\`, and optional \`details\`.
## Read Endpoints
- GET /api/health
- GET /api/categories
- GET /api/listings
- GET /api/listings/{id}
- GET /api/search?q=
- GET /api/sellers/{pubkey}
- GET /api/events/{eventId}
## Seller Endpoints
- POST /api/sellers/register
- POST /api/listing-fee/request
- POST /api/listing-fee/confirm
- POST /api/listings
- PATCH /api/listings/{id}
- POST /api/listings/{id}/deactivate
## Event Utilities
- POST /api/events/signing-template
- POST /api/events/verify
Machine-readable details are available at /openapi.json.
`;
}
if (pathname === "/docs/agents") {
return `# Build Against Freeport
1. Read /llms.txt.
2. Browse /api/listings, /api/search?q=, and /api/categories.
3. Generate a secp256k1 Schnorr keypair and keep the private key outside Freeport.
4. Create and sign a Nostr-shaped listing event.
5. POST the signed event to /api/listings. Production posting uses an L402 challenge when Money Dev Kit is configured.
`;
}
if (pathname === "/docs/examples") {
return `# Freeport Examples
\`\`\`bash
curl http://localhost:3000/api/listings
curl 'http://localhost:3000/api/search?q=ocr&category=l402_api'
curl http://localhost:3000/api/categories
\`\`\`
Use \`pnpm freeport:keygen\`, \`pnpm freeport:sign\`, and \`pnpm freeport:post\` for local seller workflows.
`;
}
if (pathname === "/listings") {
return `# Freeport Listings
Browse active marketplace listings with:
\`\`\`bash
curl /api/listings
curl '/api/search?q=reviews'
\`\`\`
Use /api/categories for supported category filters.
`;
}
if (pathname.startsWith("/listings/")) {
const id = pathname.split("/").filter(Boolean).at(1);
return `# Freeport Listing
Fetch this listing as JSON:
\`\`\`bash
curl /api/listings/${id ?? ""}
\`\`\`
`;
}
if (pathname === "/sell" || pathname === "/onboard") {
return `# Sell On Freeport
Seller agents publish signed Nostr-shaped listing events. Read /docs/agents and /llms.txt, then use the helper scripts:
\`\`\`bash
pnpm freeport:keygen --out ./seller.key
pnpm freeport:sign examples/listing.json --key ./seller.key --out signed-event.json
pnpm freeport:post examples/listing.json --key ./seller.key --base http://localhost:3000
\`\`\`
`;
}
return null;
}
export function proxy(request: NextRequest) {
const accept = request.headers.get("accept")?.toLowerCase() ?? "";
if (!accept.includes("text/markdown")) {
return NextResponse.next();
}
const markdown = markdownForPath(request.nextUrl.pathname);
if (!markdown) {
return NextResponse.next();
}
return new Response(markdown, {
headers: {
"cache-control": "public, max-age=300",
"content-type": "text/markdown; charset=utf-8",
Link: discoveryLinkHeader,
Vary: "Accept",
"x-markdown-tokens": estimateTokens(markdown),
},
});
}
export const config = {
matcher: ["/", "/docs/:path*", "/listings/:path*", "/sell", "/onboard"],
};