@olenbetong/appframe-vite 1.0.3 → 1.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.
@@ -1,6 +1,6 @@
1
1
  import { Application, RequestHandler, Router } from "express";
2
2
  import { ViteDevServer } from "vite";
3
- export declare function createProxyMiddleware(): Promise<Router>;
3
+ export declare function createProxyMiddleware(): Router;
4
4
  export declare function createHtmlTransformer(): (html: string) => Promise<string>;
5
- export declare function createDevMiddleware(vite: ViteDevServer): Promise<RequestHandler>;
5
+ export declare function createDevMiddleware(vite: ViteDevServer): RequestHandler;
6
6
  export declare function startDevServer(app: Application): Promise<void>;
package/lib/devServer.js CHANGED
@@ -11,10 +11,11 @@ dotenv.config();
11
11
  let appPkg = await importJson("./package.json", true);
12
12
  let { appframe } = appPkg;
13
13
  let userSession = null;
14
- async function getUserSession(hostname, username, password) {
14
+ async function getUserSession() {
15
15
  if (userSession) {
16
16
  return userSession;
17
17
  }
18
+ let { hostname, username, password } = getLoginInfo();
18
19
  let client = new Client(hostname);
19
20
  await client.login(username, password);
20
21
  let userResult = await client.afFetch("/api/system/usersession", {
@@ -23,15 +24,19 @@ async function getUserSession(hostname, username, password) {
23
24
  userSession = await userResult.json();
24
25
  return userSession;
25
26
  }
26
- export async function createProxyMiddleware() {
27
- var _a, _b, _c, _d, _e;
27
+ function getLoginInfo() {
28
+ var _a, _b, _c, _d;
28
29
  const hostname = (_d = (_b = (_a = appframe.proxy) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : (_c = appframe.deploy) === null || _c === void 0 ? void 0 : _c.hostname) !== null && _d !== void 0 ? _d : "dev.obet.no";
29
30
  const { APPFRAME_LOGIN: username, APPFRAME_PWD: password } = process.env;
30
- const router = express.Router();
31
31
  if (!username || !password) {
32
32
  throw new Error("Your Appframe credentials are not set in the environment variables. Username should be set in APPFRAME_LOGIN and password in APPFRAME_PWD.");
33
33
  }
34
- const proxy = await createAppframeProxy({ hostname, username, password });
34
+ return { hostname, username, password };
35
+ }
36
+ export function createProxyMiddleware() {
37
+ var _a;
38
+ let { hostname, username, password } = getLoginInfo();
39
+ const proxy = createAppframeProxy({ hostname, username, password });
35
40
  const proxyRoutes = [
36
41
  "/api/*",
37
42
  "/file/*",
@@ -47,24 +52,20 @@ export async function createProxyMiddleware() {
47
52
  "/rowcount/*",
48
53
  "/lib/*"
49
54
  ];
50
- if ((_e = appframe.proxy) === null || _e === void 0 ? void 0 : _e.routes) {
55
+ if ((_a = appframe.proxy) === null || _a === void 0 ? void 0 : _a.routes) {
51
56
  proxyRoutes.push(...appframe.proxy.routes);
52
57
  }
58
+ const router = express.Router();
53
59
  proxyRoutes.forEach(route => {
54
60
  router.use(route, proxy);
55
61
  });
56
62
  return router;
57
63
  }
58
64
  export function createHtmlTransformer() {
59
- var _a, _b, _c, _d, _e, _f;
60
- const hostname = (_d = (_b = (_a = appframe.proxy) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : (_c = appframe.deploy) === null || _c === void 0 ? void 0 : _c.hostname) !== null && _d !== void 0 ? _d : "dev.obet.no";
61
- const { APPFRAME_LOGIN: username, APPFRAME_PWD: password } = process.env;
62
- let isPartner = ((_f = (_e = appPkg.appframe) === null || _e === void 0 ? void 0 : _e.article) === null || _f === void 0 ? void 0 : _f.hostname) === "partner.olenbetong.no";
63
- if (!username || !password) {
64
- throw new Error("Your Appframe credentials are not set in the environment variables. Username should be set in APPFRAME_LOGIN and password in APPFRAME_PWD.");
65
- }
66
65
  return async (html) => {
67
- let userSession = await getUserSession(hostname, username, password);
66
+ var _a, _b;
67
+ let isPartner = ((_b = (_a = appPkg.appframe) === null || _a === void 0 ? void 0 : _a.article) === null || _b === void 0 ? void 0 : _b.hostname) === "partner.olenbetong.no";
68
+ let userSession = await getUserSession();
68
69
  return html
69
70
  .replace("<!-- {Appframe} -->", `<script>
70
71
  af = globalThis.af ?? {};
@@ -88,14 +89,14 @@ af.userSession = ${JSON.stringify(userSession)};
88
89
  <link rel="stylesheet" href="/file/site/style/ob.es.application.min.css" />`);
89
90
  };
90
91
  }
91
- export async function createDevMiddleware(vite) {
92
- let transformer = await createHtmlTransformer();
92
+ export function createDevMiddleware(vite) {
93
93
  return async (req, res, next) => {
94
+ let transformer = createHtmlTransformer();
94
95
  const url = req.originalUrl;
95
96
  try {
96
97
  let template = await fs.readFile(path.resolve(process.cwd(), "index.html"), "utf-8");
97
98
  template = await vite.transformIndexHtml(url, template);
98
- let html = transformer(template);
99
+ let html = await transformer(template);
99
100
  res.status(200).set({ "Content-Type": "text/html" }).end(html);
100
101
  }
101
102
  catch (error) {
package/lib/index.js CHANGED
@@ -17,9 +17,9 @@ export default function appframe() {
17
17
  return config;
18
18
  },
19
19
  async configureServer(_server) {
20
- _server.middlewares.use(await createProxyMiddleware());
21
- return async () => {
22
- _server.middlewares.use(await createDevMiddleware(_server));
20
+ _server.middlewares.use(createProxyMiddleware());
21
+ return () => {
22
+ _server.middlewares.use(createDevMiddleware(_server));
23
23
  };
24
24
  },
25
25
  transformIndexHtml
package/lib/proxy.d.ts CHANGED
@@ -11,4 +11,4 @@ export declare type createProxyOptions = {
11
11
  password: string;
12
12
  protocol?: "http" | "https";
13
13
  };
14
- export declare function createAppframeProxy(options: createProxyOptions): Promise<import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>>;
14
+ export declare function createAppframeProxy(options: createProxyOptions): import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
package/lib/proxy.js CHANGED
@@ -63,8 +63,8 @@ export async function login(hostname, username, password) {
63
63
  currentLogin = null;
64
64
  return result;
65
65
  }
66
- export async function createAppframeProxy(options) {
67
- const { autoLogin = true, hostname, password, protocol = "https", username } = options;
66
+ export function createAppframeProxy(options) {
67
+ const { hostname, password, protocol = "https", username } = options;
68
68
  const proxy = expressProxy(`${protocol}://${hostname}`, {
69
69
  proxyReqOptDecorator: async function (proxyReqOpts) {
70
70
  var _a, _b;
@@ -82,8 +82,5 @@ export async function createAppframeProxy(options) {
82
82
  return req.originalUrl;
83
83
  }
84
84
  });
85
- if (autoLogin) {
86
- await login(hostname, username, password);
87
- }
88
85
  return proxy;
89
86
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-vite",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "Tools to use and deploy Vite applications to Appframe",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",