oc-server 1.1.5 → 1.1.6
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/cli/index.js +0 -0
- package/dist/cli/oc.d.ts +3 -3
- package/dist/cli/server.js +25 -23
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
File without changes
|
package/dist/cli/oc.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import { FastifyRequest } from 'fastify';
|
|
2
3
|
import { PluginMock } from './get-mocked-plugins';
|
|
3
|
-
import type { Request } from 'express';
|
|
4
4
|
interface Package {
|
|
5
5
|
name: string;
|
|
6
6
|
version: string;
|
|
@@ -26,7 +26,7 @@ export declare function parsePkg(pkg: Package): {
|
|
|
26
26
|
appEntry: string;
|
|
27
27
|
templateType: string;
|
|
28
28
|
};
|
|
29
|
-
export declare function getContext(plugins: PluginMock[], req:
|
|
29
|
+
export declare function getContext(plugins: PluginMock[], req: FastifyRequest, params: any, action?: any): {
|
|
30
30
|
context: {
|
|
31
31
|
action: any;
|
|
32
32
|
acceptLanguage: string | undefined;
|
|
@@ -41,7 +41,7 @@ export declare function getContext(plugins: PluginMock[], req: Request, params:
|
|
|
41
41
|
renderComponent: () => never;
|
|
42
42
|
renderComponents: () => never;
|
|
43
43
|
requestHeaders: import("http").IncomingHttpHeaders;
|
|
44
|
-
requestIp: string
|
|
44
|
+
requestIp: string;
|
|
45
45
|
setEmptyResponse: null;
|
|
46
46
|
staticPath: string;
|
|
47
47
|
setHeader: (header: string, value: string) => void;
|
package/dist/cli/server.js
CHANGED
|
@@ -7,7 +7,8 @@ exports.createServer = void 0;
|
|
|
7
7
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
9
|
const node_vm_1 = __importDefault(require("node:vm"));
|
|
10
|
-
const
|
|
10
|
+
const fastify_1 = __importDefault(require("fastify"));
|
|
11
|
+
const express_1 = __importDefault(require("@fastify/express"));
|
|
11
12
|
const vite_1 = require("vite");
|
|
12
13
|
const esbuild_1 = __importDefault(require("esbuild"));
|
|
13
14
|
const oc_client_browser_1 = __importDefault(require("oc-client-browser"));
|
|
@@ -87,13 +88,13 @@ async function getHtmlTemplate(appBlock, filePath = 'index.html') {
|
|
|
87
88
|
}
|
|
88
89
|
async function createServer({ template, port, }) {
|
|
89
90
|
const { dev: clientJs } = oc_client_browser_1.default.compileSync();
|
|
90
|
-
const app = (0,
|
|
91
|
+
const app = (0, fastify_1.default)({ logger: false });
|
|
92
|
+
await app.register(express_1.default);
|
|
91
93
|
const dataProvider = getDataProvider();
|
|
92
94
|
const plugins = (0, get_mocked_plugins_1.default)(node_path_1.default.join(process.cwd(), '..'));
|
|
93
95
|
for (const plugin of plugins) {
|
|
94
96
|
plugin.register.register(null, null, () => { });
|
|
95
97
|
}
|
|
96
|
-
app.use(express_1.default.json());
|
|
97
98
|
const vite = await (0, vite_1.createServer)({
|
|
98
99
|
server: { middlewareMode: true },
|
|
99
100
|
plugins: [(0, cssModulesPlugin_1.default)(), template.plugin?.()].filter(Boolean),
|
|
@@ -104,13 +105,15 @@ async function createServer({ template, port, }) {
|
|
|
104
105
|
appType: 'custom',
|
|
105
106
|
});
|
|
106
107
|
app.use(vite.middlewares);
|
|
107
|
-
app.get('/oc-client/client.js', (
|
|
108
|
-
|
|
108
|
+
app.get('/oc-client/client.js', (request, reply) => {
|
|
109
|
+
reply.status(200);
|
|
110
|
+
reply.header('Content-Type', 'text/javascript');
|
|
111
|
+
reply.send(clientJs);
|
|
109
112
|
});
|
|
110
|
-
app.post('/', async (
|
|
113
|
+
app.post('/', async (request, reply) => {
|
|
111
114
|
try {
|
|
112
|
-
const { action, parameters } =
|
|
113
|
-
const { context, responseHeaders } = (0, oc_1.getContext)(plugins,
|
|
115
|
+
const { action, parameters } = request.body.components[0];
|
|
116
|
+
const { context, responseHeaders } = (0, oc_1.getContext)(plugins, request, parameters, action);
|
|
114
117
|
const props = await dataProvider(context);
|
|
115
118
|
const response = [
|
|
116
119
|
{
|
|
@@ -124,39 +127,38 @@ async function createServer({ template, port, }) {
|
|
|
124
127
|
},
|
|
125
128
|
},
|
|
126
129
|
];
|
|
127
|
-
|
|
128
|
-
|
|
130
|
+
for (const [key, value] of Object.entries(responseHeaders)) {
|
|
131
|
+
reply.header(key, value);
|
|
129
132
|
}
|
|
130
|
-
|
|
131
|
-
.status(200)
|
|
132
|
-
.set({ 'Content-Type': 'application/json' })
|
|
133
|
-
.send(response);
|
|
133
|
+
return response;
|
|
134
134
|
}
|
|
135
135
|
catch (e) {
|
|
136
136
|
vite.ssrFixStacktrace(e);
|
|
137
|
-
|
|
137
|
+
throw e;
|
|
138
138
|
}
|
|
139
139
|
});
|
|
140
|
-
app.
|
|
141
|
-
const url =
|
|
140
|
+
app.get('*', async (request, reply) => {
|
|
141
|
+
const url = request.originalUrl;
|
|
142
142
|
try {
|
|
143
143
|
const params = defaultParams;
|
|
144
|
-
const { context, responseHeaders } = (0, oc_1.getContext)(plugins,
|
|
144
|
+
const { context, responseHeaders } = (0, oc_1.getContext)(plugins, request, params);
|
|
145
145
|
let htmlTemplate = await getHtmlTemplate(template.appBlock);
|
|
146
146
|
const data = await dataProvider(context);
|
|
147
147
|
htmlTemplate = htmlTemplate.replace('__INITIAL_DATA__', JSON.stringify(data));
|
|
148
148
|
htmlTemplate = await vite.transformIndexHtml(url, htmlTemplate);
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
for (const [key, value] of Object.entries(responseHeaders)) {
|
|
150
|
+
reply.header(key, value);
|
|
151
151
|
}
|
|
152
|
-
|
|
152
|
+
reply.status(200);
|
|
153
|
+
reply.header('Content-Type', 'text/html');
|
|
154
|
+
reply.send(htmlTemplate);
|
|
153
155
|
}
|
|
154
156
|
catch (e) {
|
|
155
157
|
vite.ssrFixStacktrace(e);
|
|
156
|
-
|
|
158
|
+
throw e;
|
|
157
159
|
}
|
|
158
160
|
});
|
|
159
|
-
app.listen(port);
|
|
161
|
+
await app.listen({ port: port });
|
|
160
162
|
console.log(`Listening on: http://localhost:${port}`);
|
|
161
163
|
}
|
|
162
164
|
exports.createServer = createServer;
|