@jay-framework/jay-fetch-handler 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +7 -0
- package/dist/index.js +39 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export interface JayFetchHandlerOptions {
|
|
3
|
+
backendDir: string;
|
|
4
|
+
staticBaseUrl?: string;
|
|
5
|
+
frontendDir?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function createJayFetchHandler(options: JayFetchHandlerOptions): (request: Request) => Promise<Response>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { FilesystemArtifactStore, matchRequest, fetchPageRequest, fetchActionRequest, isActionRequest, fetchStaticFile, registerActionsFromManifest, initializeServices, } from '@jay-framework/production-server';
|
|
2
|
+
import { parseCookies } from '@jay-framework/stack-server-runtime';
|
|
3
|
+
import { getLogger } from '@jay-framework/logger';
|
|
4
|
+
export function createJayFetchHandler(options) {
|
|
5
|
+
const { backendDir, staticBaseUrl = '/', frontendDir } = options;
|
|
6
|
+
const artifacts = new FilesystemArtifactStore(backendDir);
|
|
7
|
+
const logger = getLogger();
|
|
8
|
+
let initialized = false;
|
|
9
|
+
async function initialize() {
|
|
10
|
+
const manifest = await artifacts.readManifest();
|
|
11
|
+
logger.info(`[FetchHandler] Loaded manifest: ${manifest.routes.length} routes, v${manifest.version}`);
|
|
12
|
+
await initializeServices(backendDir, process.cwd(), 'FetchHandler');
|
|
13
|
+
if (manifest.actions.length > 0) {
|
|
14
|
+
await registerActionsFromManifest(manifest.actions, backendDir);
|
|
15
|
+
}
|
|
16
|
+
initialized = true;
|
|
17
|
+
}
|
|
18
|
+
return async (request) => {
|
|
19
|
+
if (!initialized) {
|
|
20
|
+
await initialize();
|
|
21
|
+
}
|
|
22
|
+
const url = new URL(request.url);
|
|
23
|
+
if (isActionRequest(url.pathname)) {
|
|
24
|
+
return fetchActionRequest(request);
|
|
25
|
+
}
|
|
26
|
+
if (frontendDir) {
|
|
27
|
+
const staticResponse = await fetchStaticFile(url.pathname, frontendDir);
|
|
28
|
+
if (staticResponse)
|
|
29
|
+
return staticResponse;
|
|
30
|
+
}
|
|
31
|
+
const manifest = await artifacts.readManifest();
|
|
32
|
+
const match = matchRequest(manifest, url.pathname);
|
|
33
|
+
if (!match) {
|
|
34
|
+
return new Response('Not Found', { status: 404 });
|
|
35
|
+
}
|
|
36
|
+
const cookies = parseCookies(request.headers.get('cookie'));
|
|
37
|
+
return fetchPageRequest(match, manifest, url, artifacts, staticBaseUrl, cookies);
|
|
38
|
+
};
|
|
39
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jay-framework/jay-fetch-handler",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"build:watch": "tsc --watch",
|
|
14
|
+
"build:check-types": "tsc --noEmit",
|
|
15
|
+
"clean": "rimraf dist",
|
|
16
|
+
"test": ":"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@jay-framework/logger": "^0.17.4",
|
|
20
|
+
"@jay-framework/production-server": "^0.17.4"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@jay-framework/dev-environment": "^0.17.4",
|
|
24
|
+
"@types/node": "^22.15.21",
|
|
25
|
+
"rimraf": "^5.0.5",
|
|
26
|
+
"typescript": "^5.3.3"
|
|
27
|
+
}
|
|
28
|
+
}
|