oc-server 2.1.2 → 3.1.0

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/Server.d.ts CHANGED
@@ -43,6 +43,15 @@ interface ServerOptions<S extends boolean> {
43
43
  * @default false
44
44
  */
45
45
  stream?: S;
46
+ development?: {
47
+ /**
48
+ * If true, the server will log the browser console.
49
+ * Useful for AI agents to debug on the terminal.
50
+ *
51
+ * @default false
52
+ */
53
+ console?: boolean;
54
+ };
46
55
  }
47
56
  export declare class Server<E = {
48
57
  name: string;
package/dist/Server.js CHANGED
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ServerError = exports.getSettings = exports.getInitialData = exports.Server = void 0;
4
+ // Simple ANSI color utility function
5
+ const styleText = (color, text) => {
6
+ const colors = {
7
+ gray: '\x1b[90m',
8
+ reset: '\x1b[0m',
9
+ };
10
+ return `${colors[color] || ''}${text}${colors.reset}`;
11
+ };
4
12
  class Server {
5
13
  _options;
6
14
  _parameters = {};
@@ -38,6 +46,11 @@ class HandledServer {
38
46
  this._middleware = _middleware;
39
47
  this._parameters = _parameters;
40
48
  this._options = _options;
49
+ if (this._options.development?.console) {
50
+ this.actions['$$__oc__server___console__$$'] = (params) => {
51
+ console[params.level]?.(`${styleText('gray', '[browser]')} ${params.message}`);
52
+ };
53
+ }
41
54
  }
42
55
  action(name, action) {
43
56
  this.actions[name] = action;
@@ -67,7 +80,7 @@ class HandledServer {
67
80
  if (res && context.streamSymbol) {
68
81
  res[context.streamSymbol] = stream;
69
82
  }
70
- cb(null, res);
83
+ cb(null, res, { console: !!this._options.development?.console });
71
84
  };
72
85
  }
73
86
  }
@@ -45,7 +45,7 @@ function getDataProvider() {
45
45
  parametersSchema: backend.server?._parameters,
46
46
  };
47
47
  }
48
- function getBaseTemplate(appBlock) {
48
+ function getBaseTemplate(appBlock, imports = {}) {
49
49
  const baseTemplate = `
50
50
  <!DOCTYPE html>
51
51
  <html>
@@ -60,6 +60,9 @@ function getBaseTemplate(appBlock) {
60
60
  margin: 0;
61
61
  }
62
62
  </style>
63
+ ${imports
64
+ ? `<script type="importmap">{"imports": ${JSON.stringify(imports)}}</script>`
65
+ : ''}
63
66
  </head>
64
67
  <body>
65
68
  <script src="/oc-client/client.js"></script>
@@ -69,7 +72,7 @@ function getBaseTemplate(appBlock) {
69
72
  `;
70
73
  return baseTemplate;
71
74
  }
72
- async function getHtmlTemplate(appBlock, filePath = 'index.html') {
75
+ async function getHtmlTemplate(appBlock, imports = {}, filePath = 'index.html') {
73
76
  let template;
74
77
  try {
75
78
  template = node_fs_1.default.readFileSync(node_path_1.default.resolve(process.cwd(), filePath), 'utf-8');
@@ -90,7 +93,7 @@ async function getHtmlTemplate(appBlock, filePath = 'index.html') {
90
93
  })}</body>`);
91
94
  }
92
95
  catch (error) {
93
- template = getBaseTemplate(appBlock);
96
+ template = getBaseTemplate(appBlock, imports);
94
97
  }
95
98
  return template;
96
99
  }
@@ -104,6 +107,11 @@ async function createServer({ template, port, }) {
104
107
  plugin.register.register(null, null, () => { });
105
108
  }
106
109
  const defaultParams = (0, oc_1.getDefaultParams)(pk.oc?.parameters ?? parametersSchema);
110
+ const baseConfig = await (0, vite_1.loadConfigFromFile)({
111
+ command: 'build',
112
+ mode: 'development',
113
+ }, undefined, process.cwd()).catch(() => null);
114
+ let imports = baseConfig?.config?.resolve?.alias;
107
115
  const vite = await (0, vite_1.createServer)({
108
116
  server: { middlewareMode: true },
109
117
  plugins: [(0, cssModulesPlugin_1.default)(), template.plugin?.()].filter(Boolean),
@@ -151,7 +159,9 @@ async function createServer({ template, port, }) {
151
159
  try {
152
160
  const params = defaultParams;
153
161
  const { context, responseHeaders } = (0, oc_1.getContext)(plugins, request, params);
154
- let htmlTemplate = await getHtmlTemplate(template.appBlock);
162
+ let htmlTemplate = await getHtmlTemplate(template.appBlock, typeof imports === 'object' && !!imports
163
+ ? imports
164
+ : {});
155
165
  const data = await dataProvider(context);
156
166
  htmlTemplate = htmlTemplate.replace('__INITIAL_DATA__', JSON.stringify(data));
157
167
  htmlTemplate = await vite.transformIndexHtml(url, htmlTemplate);
package/dist/types.d.ts CHANGED
@@ -2,6 +2,14 @@ export type Prettify<T> = {
2
2
  [K in keyof T]: T[K];
3
3
  } & {};
4
4
  type JsonPrimitive = string | number | boolean | null | undefined;
5
+ type JsonObject = {
6
+ [key: string]: JsonPrimitive | JsonArray | JsonObject;
7
+ };
8
+ interface JsonArray extends Array<JsonPrimitive | JsonArray | JsonObject> {
9
+ }
10
+ interface JsonLike {
11
+ [key: string]: JsonPrimitive | JsonArray | JsonObject;
12
+ }
5
13
  type CleanNeverProperties<T> = {
6
14
  [K in keyof T as T[K] extends never ? never : K]: T[K];
7
15
  };
@@ -28,6 +36,36 @@ export interface Template {
28
36
  version: string;
29
37
  externals: External[];
30
38
  }
39
+ export interface CookieOptions {
40
+ /** Convenient option for setting the expiry time relative to the current time in **milliseconds**. */
41
+ maxAge?: number | undefined;
42
+ /** Indicates if the cookie should be signed. */
43
+ signed?: boolean | undefined;
44
+ /** Expiry date of the cookie in GMT. If not specified (undefined), creates a session cookie. */
45
+ expires?: Date | undefined;
46
+ /** Flags the cookie to be accessible only by the web server. */
47
+ httpOnly?: boolean | undefined;
48
+ /** Path for the cookie. Defaults to “/”. */
49
+ path?: string | undefined;
50
+ /** Domain name for the cookie. Defaults to the domain name of the app. */
51
+ domain?: string | undefined;
52
+ /** Marks the cookie to be used with HTTPS only. */
53
+ secure?: boolean | undefined;
54
+ /** A synchronous function used for cookie value encoding. Defaults to encodeURIComponent. */
55
+ encode?: ((val: string) => string) | undefined;
56
+ /**
57
+ * Value of the “SameSite” Set-Cookie attribute.
58
+ * @link https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1.
59
+ */
60
+ sameSite?: boolean | 'lax' | 'strict' | 'none' | undefined;
61
+ /**
62
+ * Value of the “Priority” Set-Cookie attribute.
63
+ * @link https://datatracker.ietf.org/doc/html/draft-west-cookie-priority-00#section-4.3
64
+ */
65
+ priority?: 'low' | 'medium' | 'high';
66
+ /** Marks the cookie to use partioned storage. */
67
+ partitioned?: boolean | undefined;
68
+ }
31
69
  export type DataContext<T = any, E = Env, P = any, S = unknown> = {
32
70
  action?: string;
33
71
  acceptLanguage: AcceptLanguage[];
@@ -39,6 +77,7 @@ export type DataContext<T = any, E = Env, P = any, S = unknown> = {
39
77
  requestIp: string;
40
78
  setEmptyResponse: () => void;
41
79
  setHeader: (header: string, value: string) => void;
80
+ setCookie: (name: string, value: string | JsonLike, options?: CookieOptions) => void;
42
81
  staticPath: string;
43
82
  templates: Template[];
44
83
  } & (S extends Record<string, unknown> ? {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-server",
3
- "version": "2.1.2",
3
+ "version": "3.1.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",