-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
201 lines (184 loc) · 6.25 KB
/
Copy pathindex.ts
File metadata and controls
201 lines (184 loc) · 6.25 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { type Context, Hono } from 'hono';
import './src/banner.js';
import { describeRoute, openAPIRouteHandler } from 'hono-openapi';
import { logServerInit } from './src/banner.js';
import { APP_DESCRIPTION, APP_VERSION, config } from './src/config.js';
import { logger } from './src/logger.js';
import routes from './src/routes/index.js';
import { APIErrorResponse } from './src/utils.js';
const app = new Hono();
// Tracking all incoming requests
app.use(async (c: Context, next) => {
logger.trace(`Incoming request: '${c.req.path}'`);
await next();
});
// -----------
// --- API ---
// -----------
app.route('/', routes);
// ------------
// --- Docs ---
// ------------
app.get('/', () => new Response(Bun.file('./public/index.html')));
app.get('/favicon.svg', () => new Response(Bun.file('./public/favicon.svg')));
app.get('/banner.jpg', () => new Response(Bun.file('./public/banner.jpg')));
const contentTypeMarkdown = 'text/markdown; charset=UTF-8';
const skillsMarkdownOpenapi = describeRoute({
operationId: 'getSkillsMarkdown',
summary: 'Agent Skills Reference',
description: 'Returns the public Markdown reference for AI agents integrating with Token API.',
tags: ['Documentation'],
security: [],
responses: {
200: {
description: 'Successful Response',
content: {
[contentTypeMarkdown]: {
schema: {
type: 'string',
example: `---
name: Token API
description: Real-time token, balance, transfer, holder, DEX, NFT, Polymarket, and Hyperliquid data across EVM, SVM, and TVM networks.
---
# Token API
> Quick reference for AI agents using Token API. The authoritative machine-readable contract is \`GET /openapi\`.
...`,
},
},
},
},
},
});
const llmsTextOpenapi = describeRoute({
operationId: 'getLlmsText',
summary: 'LLM Documentation Index',
description: 'Returns the public llms.txt documentation index for AI tools discovering Token API.',
tags: ['Documentation'],
security: [],
responses: {
200: {
description: 'Successful Response',
content: {
[contentTypeMarkdown]: {
schema: {
type: 'string',
example: `# Token API
> Real-time token, balance, transfer, holder, DEX, NFT, Polymarket, and Hyperliquid data across EVM, SVM, and TVM networks.
...`,
},
},
},
},
},
});
const openapiSpecOpenapi = describeRoute({
operationId: 'getOpenapiSpec',
summary: 'OpenAPI Specification',
description: 'Returns the public OpenAPI specification for Token API.',
tags: ['Documentation'],
security: [],
responses: {
200: {
description: 'Successful Response',
content: {
'application/json': {
schema: {
type: 'object',
example: {
openapi: '3.1.0',
info: {},
servers: [],
paths: {},
components: {},
tags: [],
},
},
},
},
},
},
});
app.get(
'/SKILLS.md',
skillsMarkdownOpenapi,
() => new Response(Bun.file('./public/skills.md'), { headers: { 'Content-Type': contentTypeMarkdown } })
);
app.get(
'/skills.md',
() => new Response(Bun.file('./public/skills.md'), { headers: { 'Content-Type': contentTypeMarkdown } })
);
app.get('/skill.md', (c) => c.redirect('/skills.md', 301));
app.get('/SKILL.md', (c) => c.redirect('/SKILLS.md', 301));
app.get(
'/llms.txt',
llmsTextOpenapi,
() => new Response(Bun.file('./public/llms.txt'), { headers: { 'Content-Type': contentTypeMarkdown } })
);
app.get(
'/openapi',
openapiSpecOpenapi,
openAPIRouteHandler(app, {
documentation: {
info: {
title: 'Token API',
version: APP_VERSION,
description: 'Power your apps & AI agents with real-time token data.',
},
servers: config.disableOpenapiServers
? [{ url: `http://${config.hostname}:${config.port}`, description: `${APP_DESCRIPTION} - Local` }]
: [{ url: config.apiUrl, description: `${APP_DESCRIPTION} - Remote` }],
tags: [
// Documentation
{ name: 'Documentation' },
// SVM
{ name: 'SVM Tokens' },
{ name: 'SVM Tokens (Native)' },
{ name: 'SVM DEXs' },
// EVM
{ name: 'EVM Tokens (ERC-20)' },
{ name: 'EVM Tokens (Native)' },
{ name: 'EVM DEXs' },
{ name: 'EVM NFTs' },
// TVM
{ name: 'TVM Tokens (ERC-20)' },
{ name: 'TVM Tokens (Native)' },
{ name: 'TVM DEXs' },
// Polymarket
{ name: 'Polymarket Markets' },
{ name: 'Polymarket Platform' },
{ name: 'Polymarket Users' },
// Hyperliquid
{ name: 'Hyperliquid Markets' },
{ name: 'Hyperliquid Users' },
{ name: 'Hyperliquid Vaults' },
{ name: 'Hyperliquid Platform' },
// Monitoring
{ name: 'Monitoring' },
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
apiKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'X-Api-Key',
},
},
},
},
excludeStaticFile: false,
})
);
// 404 NOT FOUND
app.notFound((c: Context) =>
APIErrorResponse(c, 404, 'route_not_found', `Path not found: ${c.req.method} ${c.req.path}`)
);
logServerInit();
export default {
...app,
idleTimeout: config.idleTimeout,
};