@monkeyplus/flow 5.0.0-rc.108 → 5.0.0-rc.11

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.
@@ -17,7 +17,7 @@ export default async (runtimeConfig) => {
17
17
  });
18
18
  };
19
19
  } catch (err) {
20
- console.error(err);
20
+ console.log(err);
21
21
  }
22
22
  return flow;
23
23
  };
@@ -1,6 +1,6 @@
1
1
  import type { RadixRouter } from 'radix3';
2
2
  import type { Hookable } from 'hookable';
3
- import type { FlowAppUtils, FlowPage, RuntimeConfig } from '@monkeyplus/flow-schema';
3
+ import type { FlowPage, RuntimeConfig } from '@monkeyplus/flow-schema';
4
4
  declare type HookResult = Promise<void> | void;
5
5
  export interface FlowAppHooks {
6
6
  'flow:pages': (page: FlowPage[]) => HookResult;
@@ -11,10 +11,6 @@ export interface FlowAppHooks {
11
11
  bodyStart: string[];
12
12
  }) => HookResult;
13
13
  'page:links': (links: string[]) => HookResult;
14
- 'page:context': (ctx: {
15
- page: string;
16
- localeCode: string;
17
- }, utils: any, data: Record<string, any>) => HookResult;
18
14
  'page:chunks': (bundle: string, scripts: {
19
15
  head: string[];
20
16
  body: string[];
@@ -31,20 +27,16 @@ export interface FlowApp {
31
27
  callHook: FlowApp['hooks']['callHook'];
32
28
  eta: any;
33
29
  app: {
34
- utils: FlowAppUtils;
30
+ utils: Record<string, any>;
35
31
  plugins: Record<string, any>;
36
32
  globals: Record<string, any>;
37
33
  };
38
34
  engine: string;
39
- site: {
40
- origin: string;
41
- };
42
35
  engines: Record<string, (template: string) => string>;
43
36
  generate: boolean;
44
37
  render: (view: Record<string, string>, data: any) => Promise<string> | void | string;
45
38
  provide: (name: string, value: any) => void;
46
39
  setUtil: (name: string, value: any) => void;
47
- $config: RuntimeConfig;
48
40
  [key: string]: any;
49
41
  }
50
42
  export declare const FlowPluginIndicator = "__flow_plugin";
package/dist/app/flow.mjs CHANGED
@@ -20,9 +20,6 @@ export function createFlowApp({ runtimeConfig }) {
20
20
  },
21
21
  engine: "eta",
22
22
  engines: {},
23
- site: {
24
- origin: `${process.env.URL || "http://localhost"}`
25
- },
26
23
  generate: runtimeConfig.generate
27
24
  };
28
25
  flowApp.engines.eta = (template) => {
@@ -1,4 +1,3 @@
1
1
  import 'node-fetch-native/polyfill';
2
- export declare const getFlowRenderer: () => Promise<any>;
3
2
  declare const _default: import("h3").EventHandler<void>;
4
3
  export default _default;
@@ -1,18 +1,17 @@
1
+ import { defineEventHandler } from "h3";
1
2
  import "node-fetch-native/polyfill";
2
- import { eventHandler } from "h3";
3
- import { defineCachedFunction, useRuntimeConfig } from "#internal/nitro";
3
+ import { useRuntimeConfig } from "#internal/nitro";
4
4
  const getServerApp = cachedImport(() => import("#server"));
5
- export const getFlowRenderer = cachedResult(async () => {
5
+ const getFlowRenderer = cachedResult(async () => {
6
6
  const createFlowApp = await getServerApp();
7
7
  if (!createFlowApp)
8
8
  throw new Error("Server bundle is not available");
9
9
  return createFlowApp(useRuntimeConfig());
10
10
  });
11
- export default eventHandler(async (event) => {
11
+ export default defineEventHandler(async ({ context }) => {
12
12
  const flow = await getFlowRenderer();
13
- event.context.flow = flow;
14
- event.context.render = flow.render;
15
- event.context.defineCachedFunction = defineCachedFunction;
13
+ context.flow = flow;
14
+ context.render = flow.render;
16
15
  });
17
16
  function _interopDefault(e) {
18
17
  return e && typeof e === "object" && "default" in e ? e.default : e;
@@ -1,22 +1,4 @@
1
- import { eventHandler, getQuery } from "h3";
2
- import { joinURL } from "ufo";
3
- const normalizeCall = async (seo, ctx) => {
4
- if (typeof seo === "function")
5
- return await seo(ctx);
6
- return seo || {};
7
- };
8
- function replacePath(_path, url) {
9
- if (typeof url === "string") {
10
- _path = joinURL(_path.replace("/**", ""), url);
11
- } else {
12
- Object.entries(url).forEach(([key, value]) => {
13
- _path = _path.replace(`:${key}`, value);
14
- });
15
- if (url._ && _path.endsWith("**"))
16
- _path = joinURL(_path.replace("/**", ""), url._);
17
- }
18
- return _path;
19
- }
1
+ import { eventHandler, useQuery } from "h3";
20
2
  export default eventHandler(async (event) => {
21
3
  const url = event.req.url?.split("?")[0];
22
4
  const flow = event.context.flow;
@@ -27,68 +9,38 @@ export default eventHandler(async (event) => {
27
9
  const scripts = { head: [], bodyStart: [], bodyEnd: [] };
28
10
  const chunks = { head: [], body: [] };
29
11
  const generate = {};
30
- const { page, locale, params, view, name: pageName, path: pagePath } = flow.router.byUrl.lookup(url) || {};
12
+ const { page, locale, params, view } = flow.router.byUrl.lookup(url) || {};
31
13
  if (!page)
32
14
  return;
15
+ await flow.callHook("page:scripts", scripts);
33
16
  if (view.bundle)
34
17
  await flow.callHook("page:chunks", view.bundle, chunks);
35
18
  const templateContext = {};
36
- const isDynamic = !!Object.keys(params || {});
37
- const contextPage = {
38
- chunks,
39
- locale
40
- };
19
+ const dynamyc = params?._;
20
+ const contextPage = {};
41
21
  const contextInject = {};
42
- const utils = Object.assign({ getLocale: () => locale, injectContext: (key, value) => contextInject[key] = value, defineCachedFunction: event.context.defineCachedFunction }, flow.app.utils);
43
- const contextHooks = {};
44
- await flow.callHook("page:context", { localeCode: locale.code, page: pageName }, utils, contextHooks);
22
+ const utils = Object.assign({ getLocale: () => locale, injectContext: (key, value) => contextInject[key] = value }, flow.app.utils);
45
23
  templateContext.url = url;
46
- templateContext.baseURL = flow.$config.app.baseURL;
47
- const pageObject = {
48
- name: pageName,
49
- pathname: url,
50
- url: joinURL(flow.site.origin, templateContext.baseURL, `${url}`),
51
- origin: flow.site.origin,
52
- joinOrigin: (_url, includeBase) => joinURL(flow.site.origin, includeBase ? templateContext.baseURL : "/", _url)
53
- };
54
- templateContext.page = pageObject;
55
- contextPage.page = pageObject;
56
24
  templateContext.locale = locale;
57
25
  templateContext.view = view;
58
- if (isDynamic && page.dynamic) {
26
+ if (dynamyc && page.dynamic) {
59
27
  const pages = await page.dynamic.method({ locale, utils });
60
- const dynamicPage = pages.find((_page) => {
61
- const aUrl = replacePath(pagePath, _page.url);
62
- const bUrl = replacePath(pagePath, params);
63
- return aUrl === bUrl;
64
- });
28
+ const dynamicPage = pages.find((_page) => _page.url === dynamyc);
65
29
  if (!dynamicPage)
66
30
  return;
67
31
  if (page.dynamic.assign)
68
32
  templateContext[page.dynamic.assign] = dynamicPage.context;
69
- contextPage.dynamic = dynamicPage;
33
+ contextPage.dynamyc = dynamicPage;
70
34
  }
71
35
  templateContext.seo = {};
72
- const sharedContext = await utils.getSharedContext({
73
- ...contextHooks,
74
- ...contextPage,
75
- utils
76
- });
77
- templateContext.sharedContext = sharedContext || {};
36
+ templateContext.context = {};
37
+ templateContext.sharedContext = {};
78
38
  templateContext.utils = utils;
79
- templateContext.seo = await normalizeCall(page?.seo, { ...contextHooks, ...contextPage, utils, sharedContext });
80
- const contexData = await page.context?.({ ...contextHooks, ...contextPage, utils, sharedContext }) || {};
81
- templateContext.context = {
82
- ...contextHooks,
83
- ...contexData
84
- };
85
- await flow.callHook("page:scripts", scripts, templateContext);
39
+ templateContext.seo = await page.seo?.({ ...contextPage, utils }) || {};
40
+ templateContext.context = await page.context?.({ ...contextPage, utils }) || {};
86
41
  templateContext.getHeadScripts = () => {
87
42
  return scripts.head.join("\n");
88
43
  };
89
- templateContext.getBodyEndScripts = () => {
90
- return scripts.bodyEnd.join("\n");
91
- };
92
44
  templateContext.getHeadChunks = () => {
93
45
  return chunks.head.join("\n");
94
46
  };
@@ -98,17 +50,11 @@ export default eventHandler(async (event) => {
98
50
  templateContext.getInjectContext = () => {
99
51
  return `<script id="__FLOW_DATA__" type="application/json">${JSON.stringify(contextInject)}<\/script>`;
100
52
  };
101
- const query = getQuery(event);
53
+ const query = useQuery(event);
102
54
  if (query?.context)
103
55
  return { ...templateContext, utils: Object.keys(utils) };
104
- let html;
105
- if (view.render)
106
- html = await view.render(templateContext, event);
107
- else
108
- html = await flow.render(view, templateContext);
109
- event.res.setHeader("Content-Type", view.contentType || "text/html;charset=UTF-8");
110
- if (view.postRender)
111
- html = await view.postRender(html, event);
56
+ const html = await flow.render(view, templateContext);
57
+ event.res.setHeader("Content-Type", "text/html;charset=UTF-8");
112
58
  if (flow.generate) {
113
59
  await flow.callHook("page:generate", generate);
114
60
  globalThis.generate = generate;
@@ -1,5 +1,6 @@
1
1
  import { defineFlowPlugin } from "#app";
2
2
  export default defineFlowPlugin((flow) => {
3
3
  flow._useHead = (_meta) => {
4
+ console.log(_meta);
4
5
  };
5
6
  });