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 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: Request, params: any, action?: any): {
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 | undefined;
44
+ requestIp: string;
45
45
  setEmptyResponse: null;
46
46
  staticPath: string;
47
47
  setHeader: (header: string, value: string) => void;
@@ -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 express_1 = __importDefault(require("express"));
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, express_1.default)();
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', (req, res) => {
108
- res.status(200).set({ 'Content-Type': 'text/javascript' }).send(clientJs);
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 (req, res, next) => {
113
+ app.post('/', async (request, reply) => {
111
114
  try {
112
- const { action, parameters } = req.body.components[0];
113
- const { context, responseHeaders } = (0, oc_1.getContext)(plugins, req, parameters, action);
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
- if (Object.keys(responseHeaders).length) {
128
- res.set(responseHeaders);
130
+ for (const [key, value] of Object.entries(responseHeaders)) {
131
+ reply.header(key, value);
129
132
  }
130
- res
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
- next(e);
137
+ throw e;
138
138
  }
139
139
  });
140
- app.use('*', async (req, res, next) => {
141
- const url = req.originalUrl;
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, req, params);
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
- if (Object.keys(responseHeaders).length) {
150
- res.set(responseHeaders);
149
+ for (const [key, value] of Object.entries(responseHeaders)) {
150
+ reply.header(key, value);
151
151
  }
152
- res.status(200).set({ 'Content-Type': 'text/html' }).end(htmlTemplate);
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
- next(e);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-server",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",