hanni 1.0.7 → 1.0.8
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/package.json +1 -1
- package/src/context.js +11 -11
- package/src/hanni.js +2 -2
- package/src/middleware.js +11 -38
package/package.json
CHANGED
package/src/context.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export function createContext(req, params = {}) {
|
|
2
|
-
const url = new URL(req.url)
|
|
3
|
-
const headers = Object.fromEntries(req.headers)
|
|
4
|
-
const cookies = {}
|
|
2
|
+
const url = new URL(req.url);
|
|
3
|
+
const headers = Object.fromEntries(req.headers);
|
|
4
|
+
const cookies = {};
|
|
5
5
|
|
|
6
6
|
if (headers.cookie) {
|
|
7
7
|
headers.cookie.split(';').forEach(c => {
|
|
8
|
-
const [k, v] = c.trim().split('=')
|
|
9
|
-
cookies[k] = decodeURIComponent(v)
|
|
10
|
-
})
|
|
8
|
+
const [k, v] = c.trim().split('=');
|
|
9
|
+
cookies[k] = decodeURIComponent(v);
|
|
10
|
+
});
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
return {
|
|
@@ -21,28 +21,28 @@ export function createContext(req, params = {}) {
|
|
|
21
21
|
body: null,
|
|
22
22
|
|
|
23
23
|
text(data, status = 200, extra = {}) {
|
|
24
|
-
return new Response(data, { status, headers: extra })
|
|
24
|
+
return new Response(data, { status, headers: extra });
|
|
25
25
|
},
|
|
26
26
|
|
|
27
27
|
json(data, status = 200, extra = {}) {
|
|
28
28
|
return new Response(JSON.stringify(data), {
|
|
29
29
|
status,
|
|
30
30
|
headers: { 'Content-Type': 'application/json', ...extra }
|
|
31
|
-
})
|
|
31
|
+
});
|
|
32
32
|
},
|
|
33
33
|
|
|
34
34
|
html(data, status = 200, extra = {}) {
|
|
35
35
|
return new Response(data, {
|
|
36
36
|
status,
|
|
37
37
|
headers: { 'Content-Type': 'text/html', ...extra }
|
|
38
|
-
})
|
|
38
|
+
});
|
|
39
39
|
},
|
|
40
40
|
|
|
41
41
|
redirect(loc, status = 302) {
|
|
42
42
|
return new Response(null, {
|
|
43
43
|
status,
|
|
44
44
|
headers: { Location: loc }
|
|
45
|
-
})
|
|
45
|
+
});
|
|
46
46
|
}
|
|
47
|
-
}
|
|
47
|
+
};
|
|
48
48
|
}
|
package/src/hanni.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Router as _Router } from './router.js';
|
|
2
2
|
import { createContext } from './context.js';
|
|
3
3
|
import { buildSpec, swaggerHTML } from './swagger.js';
|
|
4
|
-
import { compose
|
|
4
|
+
import { compose } from './middleware.js';
|
|
5
5
|
import { parseBody, corsHeaders } from './utils.js';
|
|
6
6
|
|
|
7
|
-
export { _Router as Router
|
|
7
|
+
export { _Router as Router };
|
|
8
8
|
|
|
9
9
|
export function Hanni(config = {}) {
|
|
10
10
|
const router = new _Router();
|
package/src/middleware.js
CHANGED
|
@@ -1,44 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export function Static({ path: urlPath, folder }) {
|
|
5
|
-
return async function (ctx, next) {
|
|
6
|
-
const reqPath = ctx.req.url;
|
|
7
|
-
if (!reqPath.startsWith(urlPath)) return next();
|
|
1
|
+
export function compose(middlewares, handler) {
|
|
2
|
+
return async function (c) {
|
|
3
|
+
let idx = -1;
|
|
8
4
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return;
|
|
5
|
+
async function dispatch(i) {
|
|
6
|
+
if (i <= idx) throw new Error("next() called multiple times");
|
|
7
|
+
idx = i;
|
|
8
|
+
const fn = i === middlewares.length ? handler : middlewares[i];
|
|
9
|
+
if (!fn) return;
|
|
10
|
+
await fn(c, () => dispatch(i + 1));
|
|
14
11
|
}
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
ctx.res.body = file;
|
|
18
|
-
ctx.res.headers.set("Content-Type", getContentType(filePath));
|
|
19
|
-
};
|
|
20
|
-
}
|
|
13
|
+
await dispatch(0);
|
|
21
14
|
|
|
22
|
-
|
|
23
|
-
if (filePath.endsWith(".html")) return "text/html";
|
|
24
|
-
if (filePath.endsWith(".css")) return "text/css";
|
|
25
|
-
if (filePath.endsWith(".js")) return "application/javascript";
|
|
26
|
-
if (filePath.endsWith(".json")) return "application/json";
|
|
27
|
-
if (filePath.endsWith(".png")) return "image/png";
|
|
28
|
-
if (filePath.endsWith(".jpg") || filePath.endsWith(".jpeg")) return "image/jpeg";
|
|
29
|
-
return "text/plain";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function compose(middlewares, handler) {
|
|
33
|
-
return function (ctx) {
|
|
34
|
-
let i = -1;
|
|
35
|
-
const dispatch = (idx) => {
|
|
36
|
-
if (idx <= i) return Promise.reject();
|
|
37
|
-
i = idx;
|
|
38
|
-
const fn = idx === middlewares.length ? handler : middlewares[idx];
|
|
39
|
-
if (!fn) return Promise.resolve();
|
|
40
|
-
return Promise.resolve(fn(ctx, () => dispatch(idx + 1)));
|
|
41
|
-
};
|
|
42
|
-
return dispatch(0);
|
|
15
|
+
return c.res || c.text("Not Found", 404);
|
|
43
16
|
};
|
|
44
17
|
}
|