|
1 | | -import {Server, Request, Response} from '@blinkk/root'; |
2 | | -import {multipartMiddleware} from '@blinkk/root/middleware'; |
3 | 1 | import {promises as fs} from 'node:fs'; |
4 | 2 | import path from 'node:path'; |
| 3 | +import {Server, Request, Response} from '@blinkk/root'; |
| 4 | +import {multipartMiddleware} from '@blinkk/root/middleware'; |
5 | 5 | import { |
6 | 6 | ChatPrompt, |
7 | 7 | AiResponse, |
@@ -40,6 +40,35 @@ export interface ApiOptions { |
40 | 40 | * Registers API middleware handlers. |
41 | 41 | */ |
42 | 42 | export function api(server: Server, options: ApiOptions) { |
| 43 | + /** |
| 44 | + * Reads the collection's schema defined in `/collections/<id>.schema.ts`. |
| 45 | + */ |
| 46 | + async function getCollectionSchema(req: Request, collectionId: string) { |
| 47 | + // On dev, read the collection's `schema.ts` file directly. |
| 48 | + if (req.viteServer) { |
| 49 | + const app = await options.getRenderer(req); |
| 50 | + return await app.getCollection(collectionId); |
| 51 | + } |
| 52 | + // On prod, read the collection's schema from |
| 53 | + // `dist/collections/<id>.schema.json`. This file is built in the |
| 54 | + // `preBuild()` hook within `plugin.ts`. |
| 55 | + try { |
| 56 | + const schemaPath = path.join( |
| 57 | + req.rootConfig!.rootDir, |
| 58 | + 'dist', |
| 59 | + 'collections', |
| 60 | + `${collectionId}.schema.json` |
| 61 | + ); |
| 62 | + const contents = await fs.readFile(schemaPath, 'utf8'); |
| 63 | + return JSON.parse(contents); |
| 64 | + } catch (err) { |
| 65 | + if (err && err.code === 'ENOENT') { |
| 66 | + return null; |
| 67 | + } |
| 68 | + throw err; |
| 69 | + } |
| 70 | + } |
| 71 | + |
43 | 72 | /** |
44 | 73 | * Returns the schema for a collection. |
45 | 74 | * |
@@ -74,41 +103,21 @@ export function api(server: Server, options: ApiOptions) { |
74 | 103 | res.status(400).json({success: false, error: 'MISSING_COLLECTION_ID'}); |
75 | 104 | return; |
76 | 105 | } |
77 | | - |
78 | 106 | if (!testValidCollectionId(collectionId)) { |
79 | 107 | res.status(400).json({success: false, error: 'INVALID_COLLECTION_ID'}); |
80 | 108 | return; |
81 | 109 | } |
82 | 110 |
|
83 | 111 | try { |
84 | | - if (req.viteServer) { |
85 | | - const app = await options.getRenderer(req); |
86 | | - const collections = app.getCollections(); |
87 | | - const collection = collections[collectionId]; |
88 | | - if (!collection) { |
89 | | - res.status(404).json({success: false, error: 'NOT_FOUND'}); |
90 | | - return; |
91 | | - } |
92 | | - res.status(200).json({success: true, data: collection}); |
| 112 | + const collection = await getCollectionSchema(req, collectionId); |
| 113 | + if (!collection) { |
| 114 | + res.status(404).json({success: false, error: 'NOT_FOUND'}); |
93 | 115 | return; |
94 | 116 | } |
95 | | - |
96 | | - const schemaPath = path.join( |
97 | | - req.rootConfig!.rootDir, |
98 | | - 'dist', |
99 | | - 'collections', |
100 | | - `${collectionId}.json` |
101 | | - ); |
102 | | - const contents = await fs.readFile(schemaPath, 'utf8'); |
103 | | - const collection = JSON.parse(contents); |
104 | 117 | res.status(200).json({success: true, data: collection}); |
105 | 118 | } catch (err: any) { |
106 | | - if (err && err.code === 'ENOENT') { |
107 | | - res.status(404).json({success: false, error: 'NOT_FOUND'}); |
108 | | - } else { |
109 | | - console.error(err.stack || err); |
110 | | - res.status(500).json({success: false, error: 'UNKNOWN'}); |
111 | | - } |
| 119 | + console.error(err.stack || err); |
| 120 | + res.status(500).json({success: false, error: 'UNKNOWN'}); |
112 | 121 | } |
113 | 122 | }); |
114 | 123 |
|
|
0 commit comments