hono 1.4.6 → 1.4.7

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/README.md CHANGED
@@ -73,6 +73,7 @@ Built-in middleware make _"**Write Less, do more**"_ in reality. You can use a l
73
73
  - [CORS](https://github.com/honojs/hono/tree/master/src/middleware/cors/)
74
74
  - [ETag](https://github.com/honojs/hono/tree/master/src/middleware/etag/)
75
75
  - [GraphQL Server](https://github.com/honojs/hono/tree/master/src/middleware/graphql-server/)
76
+ - [html](https://github.com/honojs/hono/tree/master/src/middleware/html/)
76
77
  - [JSX](https://github.com/honojs/hono/tree/master/src/middleware/jsx/)
77
78
  - [JWT Authentication](https://github.com/honojs/hono/tree/master/src/middleware/jwt/)
78
79
  - [Logger](https://github.com/honojs/hono/tree/master/src/middleware/logger/)
@@ -0,0 +1,5 @@
1
+ export declare type HtmlEscapedString = string & {
2
+ isEscaped: true;
3
+ };
4
+ export declare const raw: (value: any) => HtmlEscapedString;
5
+ export declare const html: (strings: TemplateStringsArray, ...values: any[]) => HtmlEscapedString;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.html = exports.raw = void 0;
4
+ const html_1 = require("../../utils/html");
5
+ const raw = (value) => {
6
+ const escapedString = new String(value);
7
+ escapedString.isEscaped = true;
8
+ return escapedString;
9
+ };
10
+ exports.raw = raw;
11
+ const html = (strings, ...values) => {
12
+ let result = '';
13
+ for (let i = 0, len = strings.length - 1; i < len; i++) {
14
+ result += strings[i];
15
+ const children = values[i] instanceof Array ? values[i].flat(Infinity) : [values[i]];
16
+ for (let i = 0, len = children.length; i < len; i++) {
17
+ const child = children[i];
18
+ if (typeof child === 'boolean' || child === null || child === undefined) {
19
+ continue;
20
+ }
21
+ else if (typeof child === 'object' && child.isEscaped) {
22
+ result += child;
23
+ }
24
+ else {
25
+ result += (0, html_1.escape)(child.toString());
26
+ }
27
+ }
28
+ }
29
+ result += strings[strings.length - 1];
30
+ return (0, exports.raw)(result);
31
+ };
32
+ exports.html = html;
@@ -1,17 +1,16 @@
1
- import type { Context } from '../../context';
2
- import type { Next } from '../../hono';
1
+ import type { HtmlEscapedString } from '../../utils/html';
3
2
  declare global {
4
- namespace h.JSX {
3
+ namespace jsx.JSX {
5
4
  interface IntrinsicElements {
6
5
  [tagName: string]: Record<string, any>;
7
6
  }
8
7
  }
9
8
  }
10
- export declare const jsx: () => (c: Context, next: Next) => Promise<void>;
11
- declare type EscapedString = string & {
12
- isEscaped: true;
13
- };
14
- export declare const h: (tag: string | Function, props: Record<string, any>, ...children: (string | EscapedString)[]) => EscapedString;
15
- declare type FC<T = Record<string, any>> = (props: T) => EscapedString;
9
+ export declare const jsx: (tag: string | Function, props: Record<string, any>, ...children: (string | HtmlEscapedString)[]) => HtmlEscapedString;
10
+ declare type FC<T = Record<string, any>> = (props: T) => HtmlEscapedString;
16
11
  export declare const memo: <T>(component: FC<T>, propsAreEqual?: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean) => FC<T>;
12
+ export declare const Fragment: (props: {
13
+ key?: string;
14
+ children?: any;
15
+ }) => HtmlEscapedString;
17
16
  export {};
@@ -1,22 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.memo = exports.h = exports.jsx = void 0;
3
+ exports.Fragment = exports.memo = exports.jsx = void 0;
4
4
  const html_1 = require("../../utils/html");
5
- const jsx = () => {
6
- return async (c, next) => {
7
- c.render = (content) => {
8
- const output = `<!doctype html>${content.toString()}`;
9
- return c.html(output);
10
- };
11
- await next();
12
- };
13
- };
14
- exports.jsx = jsx;
15
- const h = (tag, props, ...children) => {
5
+ const jsx = (tag, props, ...children) => {
16
6
  if (typeof tag === 'function') {
17
7
  return tag.call(null, { ...props, children: children.length <= 1 ? children[0] : children });
18
8
  }
19
- let result = `<${tag}`;
9
+ let result = tag !== '' ? `<${tag}` : '';
20
10
  const propsKeys = Object.keys(props || {});
21
11
  for (let i = 0, len = propsKeys.length; i < len; i++) {
22
12
  const v = props[propsKeys[i]];
@@ -34,7 +24,9 @@ const h = (tag, props, ...children) => {
34
24
  }
35
25
  result += ` ${propsKeys[i]}="${(0, html_1.escape)(v.toString())}"`;
36
26
  }
37
- result += '>';
27
+ if (tag !== '') {
28
+ result += '>';
29
+ }
38
30
  const flattenChildren = children.flat(Infinity);
39
31
  for (let i = 0, len = flattenChildren.length; i < len; i++) {
40
32
  const child = flattenChildren[i];
@@ -48,12 +40,14 @@ const h = (tag, props, ...children) => {
48
40
  result += (0, html_1.escape)(child.toString());
49
41
  }
50
42
  }
51
- result += `</${tag}>`;
43
+ if (tag !== '') {
44
+ result += `</${tag}>`;
45
+ }
52
46
  const escapedString = new String(result);
53
47
  escapedString.isEscaped = true;
54
48
  return escapedString;
55
49
  };
56
- exports.h = h;
50
+ exports.jsx = jsx;
57
51
  const shallowEqual = (a, b) => {
58
52
  if (a === b) {
59
53
  return true;
@@ -82,3 +76,7 @@ const memo = (component, propsAreEqual = shallowEqual) => {
82
76
  });
83
77
  };
84
78
  exports.memo = memo;
79
+ const Fragment = (props) => {
80
+ return (0, exports.jsx)('', {}, ...(props.children || []));
81
+ };
82
+ exports.Fragment = Fragment;
@@ -1 +1,4 @@
1
+ export declare type HtmlEscapedString = string & {
2
+ isEscaped: true;
3
+ };
1
4
  export declare const escape: (str: string) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "1.4.6",
3
+ "version": "1.4.7",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,6 +24,7 @@
24
24
  "./cors": "./dist/middleware/cors/index.js",
25
25
  "./etag": "./dist/middleware/etag/index.js",
26
26
  "./graphql-server": "./dist/middleware/graphql-server/index.js",
27
+ "./html": "./dist/middleware/html/index.js",
27
28
  "./jsx": "./dist/middleware/jsx/index.js",
28
29
  "./jwt": "./dist/middleware/jwt/index.js",
29
30
  "./logger": "./dist/middleware/logger/index.js",
@@ -61,6 +62,9 @@
61
62
  "graphql-server": [
62
63
  "./dist/middleware/graphql-server"
63
64
  ],
65
+ "html": [
66
+ "./dist/middleware/html"
67
+ ],
64
68
  "jsx": [
65
69
  "./dist/middleware/jsx"
66
70
  ],
@@ -1,3 +0,0 @@
1
- export interface Context {
2
- render(template: string, params?: object, options?: object): Promise<Response>;
3
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });