-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhono-usage.ts
More file actions
114 lines (102 loc) · 3.56 KB
/
hono-usage.ts
File metadata and controls
114 lines (102 loc) · 3.56 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
// Example usage of the OCP Core Middleware with Hono
import { Hono } from 'hono';
import { createOcpMiddleware } from '../src/index';
// Create main Hono app
const app = new Hono();
// Sample configuration for the OCP middleware
const ocpConfig = {
baseUrl: 'http://localhost:3000',
stores: [
{
id: 'store_123',
name: 'Sample Restaurant',
location: {
address: '123 Main St, Anytown, USA',
latitude: 40.7128,
longitude: -74.0060,
},
catalogIds: ['catalog_456'],
},
],
catalogs: [
{
id: 'catalog_456',
name: 'Main Menu',
version: '1.0',
items: [
{
id: 'item_1',
name: 'Margherita Pizza',
description: 'Classic pizza with tomato sauce, mozzarella, and basil',
price: { amount: '12.99', currency: 'USD' },
available: true,
fulfillmentType: 'pickup',
},
{
id: 'item_2',
name: 'Caesar Salad',
description: 'Crisp romaine lettuce with Caesar dressing',
price: { amount: '8.99', currency: 'USD' },
available: true,
fulfillmentType: 'pickup',
},
],
},
],
capabilities: [
{
id: 'dev.ocp.cart@1.0',
schemaUrl: 'https://schemas.ocp.dev/cart/v1.json',
status: 'stable',
},
],
};
// Create OCP middleware instance
const ocpMiddleware = createOcpMiddleware(ocpConfig);
// Mount OCP routes under /api/v1
app.route('/api/v1', ocpMiddleware);
// Add authentication middleware (example)
app.use('/api/v1/*', async (c, next) => {
// Simple auth check - in production, validate JWT tokens
const authHeader = c.req.header('Authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return c.json({
type: 'https://schemas.ocp.dev/errors/unauthorized',
title: 'Unauthorized',
status: 401,
detail: 'Valid authorization token required',
timestamp: new Date().toISOString(),
}, 401);
}
// Set user ID for OCP context
c.set('ocp', {
...c.var.ocp,
userId: 'user_123', // Extract from token in production
});
await next();
});
// Add any additional routes your app needs
app.get('/', (c) => {
return c.text('OCP Server is running! Visit /api/v1/.well-known/ocp for discovery.');
});
// Start server
export default {
port: 3000,
fetch: app.fetch,
};
// For development
if (import.meta.main) {
console.log(`
╔════════════════════════════════════════════════════════╗
║ 🚀 OCP Hono Server Example ║
╟────────────────────────────────────────────────────────╢
║ Discovery: http://localhost:3000/api/v1/.well-known/ocp ║
║ Capabilities: http://localhost:3000/api/v1/capabilities ║
║ Catalog: http://localhost:3000/api/v1/catalogs/catalog_456 ║
╟────────────────────────────────────────────────────────╢
║ Example requests: ║
║ curl http://localhost:3000/api/v1/.well-known/ocp ║
║ curl http://localhost:3000/api/v1/capabilities ║
╚════════════════════════════════════════════════════════╝
`);
}