-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress-proxy.ts
More file actions
98 lines (86 loc) · 3.17 KB
/
Copy pathexpress-proxy.ts
File metadata and controls
98 lines (86 loc) · 3.17 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
/**
* The same proxy for Express.
*
* npm install @devrobotlab/visionapi express multer
*
* Mount it at /api/vision and the hooks work with
* `<VisionProvider baseUrl="/api/vision">`.
*/
import { VisionAPI, VisionAPIError } from '@devrobotlab/visionapi';
import express from 'express';
import multer from 'multer';
const vision = new VisionAPI(); // reads VISION_API_KEY, server-side only
// 20 MB is the API's own limit; rejecting here saves the upload rather than the round trip.
const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 20 * 1024 * 1024 } });
export const visionRouter = express.Router();
/** Forwarding the envelope is what lets the hooks branch on `error.code`. */
function send(res: express.Response, error: unknown) {
if (error instanceof VisionAPIError) {
res.status(error.status).json({ error: { code: error.code, message: error.message, details: error.details } });
return;
}
console.error('vision proxy', error);
res.status(500).json({ error: { code: 'internal_error', message: 'Something went wrong.' } });
}
visionRouter.post('/analyze', requireUser, upload.single('file'), async (req, res) => {
try {
const result = await vision.analyze({
file: { data: req.file!.buffer, filename: req.file!.originalname },
preset: req.body.preset || 'auto',
pages: req.body.pages || undefined,
minConfidence: req.body.min_confidence || undefined,
});
await recordUsage(req.user!, result.credits_used);
res.json(result);
} catch (error) {
send(res, error);
}
});
visionRouter.post('/detect', requireUser, upload.single('file'), async (req, res) => {
try {
res.json(await vision.detect({ file: { data: req.file!.buffer, filename: req.file!.originalname } }));
} catch (error) {
send(res, error);
}
});
visionRouter.post('/ask', requireUser, upload.single('file'), async (req, res) => {
try {
const questions = Array.isArray(req.body.questions) ? req.body.questions : [req.body.questions];
const result = await vision.ask({
file: { data: req.file!.buffer, filename: req.file!.originalname },
questions,
});
await recordUsage(req.user!, result.credits_used);
res.json(result);
} catch (error) {
send(res, error);
}
});
visionRouter.get('/tasks/:id', requireUser, async (req, res) => {
try {
// Check the task belongs to this user before handing back customer content.
await assertOwnsTask(req.user!, req.params.id);
res.json(await vision.getTask(req.params.id));
} catch (error) {
send(res, error);
}
});
visionRouter.get('/presets', async (_req, res) => {
try {
res.set('cache-control', 'public, max-age=3600');
res.json({ data: await vision.presets() });
} catch (error) {
send(res, error);
}
});
/* Your own middleware — stubs so this file reads as a whole. */
declare function requireUser(req: express.Request, res: express.Response, next: express.NextFunction): void;
declare function recordUsage(user: unknown, credits: number): Promise<void>;
declare function assertOwnsTask(user: unknown, taskId: string): Promise<void>;
declare global {
namespace Express {
interface Request {
user?: { id: string };
}
}
}