@bull-board/h3 5.13.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/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # <img alt="@bull-board" src="https://raw.githubusercontent.com/felixmosh/bull-board/master/packages/ui/src/static/images/logo.svg" width="35px" /> @bull-board/h3
2
+
3
+ [h3](https://github.com/unjs/h3) server adapter for `bull-board`.
4
+
5
+ <p align="center">
6
+ <a href="https://www.npmjs.com/package/@bull-board/h3">
7
+ <img alt="npm version" src="https://img.shields.io/npm/v/@bull-board/h3">
8
+ </a>
9
+ <a href="https://www.npmjs.com/package/bull-board">
10
+ <img alt="npm downloads" src="https://img.shields.io/npm/dw/bull-board">
11
+ </a>
12
+ <a href="https://github.com/vcapretz/bull-board/blob/master/LICENSE">
13
+ <img alt="licence" src="https://img.shields.io/github/license/vcapretz/bull-board">
14
+ </a>
15
+ <p>
16
+
17
+ ![Overview](https://raw.githubusercontent.com/felixmosh/bull-board/master/screenshots/overview.png)
18
+ ![UI](https://raw.githubusercontent.com/felixmosh/bull-board/master/screenshots/dashboard.png)
19
+
20
+ # Usage examples
21
+
22
+ 1. [Simple h3 setup](https://github.com/felixmosh/bull-board/tree/master/examples/with-h3)
23
+
24
+ For more info visit the main [README](https://github.com/felixmosh/bull-board#readme)
@@ -0,0 +1,21 @@
1
+ import { AppControllerRoute, AppViewRoute, BullBoardQueues, ControllerHandlerReturnType, IServerAdapter, UIConfig } from '@bull-board/api/dist/typings/app';
2
+ export declare class H3Adapter implements IServerAdapter {
3
+ private uiHandler;
4
+ private basePath;
5
+ private entryRoute;
6
+ private statics;
7
+ private errorHandler;
8
+ private bullBoardQueues;
9
+ private viewPath;
10
+ private uiConfig;
11
+ setBasePath(path: string): H3Adapter;
12
+ setStaticPath(staticsRoute: string, staticsPath: string): H3Adapter;
13
+ setViewsPath(viewPath: string): H3Adapter;
14
+ setErrorHandler(handler: (error: Error) => ControllerHandlerReturnType): this;
15
+ setApiRoutes(routes: AppControllerRoute[]): H3Adapter;
16
+ setEntryRoute(routeDef: AppViewRoute): H3Adapter;
17
+ setQueues(bullBoardQueues: BullBoardQueues): H3Adapter;
18
+ setUIConfig(config?: UIConfig): H3Adapter;
19
+ registerHandlers(): import("h3").Router;
20
+ private registerRoute;
21
+ }
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.H3Adapter = void 0;
7
+ const fs_1 = require("fs");
8
+ const node_path_1 = require("node:path");
9
+ const h3_1 = require("h3");
10
+ const ejs_1 = __importDefault(require("ejs"));
11
+ const getContentType_1 = require("./utils/getContentType");
12
+ class H3Adapter {
13
+ constructor() {
14
+ this.uiHandler = (0, h3_1.createRouter)();
15
+ this.basePath = '';
16
+ this.uiConfig = {};
17
+ }
18
+ setBasePath(path) {
19
+ this.basePath = path;
20
+ return this;
21
+ }
22
+ setStaticPath(staticsRoute, staticsPath) {
23
+ this.statics = { route: staticsRoute, path: staticsPath };
24
+ return this;
25
+ }
26
+ setViewsPath(viewPath) {
27
+ this.viewPath = viewPath;
28
+ return this;
29
+ }
30
+ setErrorHandler(handler) {
31
+ this.errorHandler = handler;
32
+ return this;
33
+ }
34
+ setApiRoutes(routes) {
35
+ routes.forEach(({ route, handler, method: methodOrMethods }) => {
36
+ const methods = Array.isArray(methodOrMethods) ? methodOrMethods : [methodOrMethods];
37
+ methods.forEach((method) => {
38
+ this.registerRoute(route, method, handler);
39
+ });
40
+ });
41
+ return this;
42
+ }
43
+ setEntryRoute(routeDef) {
44
+ this.entryRoute = routeDef;
45
+ return this;
46
+ }
47
+ setQueues(bullBoardQueues) {
48
+ this.bullBoardQueues = bullBoardQueues;
49
+ return this;
50
+ }
51
+ setUIConfig(config = {}) {
52
+ this.uiConfig = config;
53
+ return this;
54
+ }
55
+ registerHandlers() {
56
+ if (!this.statics) {
57
+ throw new Error(`Please call 'setStaticPath' before using 'registerHandlers'`);
58
+ }
59
+ else if (!this.entryRoute) {
60
+ throw new Error(`Please call 'setEntryRoute' before using 'registerHandlers'`);
61
+ }
62
+ else if (!this.viewPath) {
63
+ throw new Error(`Please call 'setViewsPath' before using 'registerHandlers'`);
64
+ }
65
+ else if (!this.errorHandler) {
66
+ throw new Error(`Please call 'setErrorHandler' before using 'registerHandlers'`);
67
+ }
68
+ else if (!this.uiConfig) {
69
+ throw new Error(`Please call 'setUIConfig' before using 'registerHandlers'`);
70
+ }
71
+ const getStaticPath = (relativePath) => {
72
+ if (!this.statics)
73
+ return '';
74
+ const relativeRoot = `${this.basePath}${this.statics.route}/`;
75
+ // Ensure that the path is relative to the statics route
76
+ if (!relativePath.startsWith(relativeRoot))
77
+ return '';
78
+ // Normalize the path
79
+ const normalizedPath = (0, node_path_1.normalize)(relativePath);
80
+ const staticRelativePath = normalizedPath.replace(relativeRoot, '');
81
+ // Resolve the absolute path
82
+ const absolutePath = (0, node_path_1.resolve)(this.statics.path, staticRelativePath);
83
+ // Check if the absolute path is still within the statics directory
84
+ if (!absolutePath.startsWith((0, node_path_1.resolve)(this.statics.path)))
85
+ return '';
86
+ return absolutePath;
87
+ };
88
+ const { method, route, handler } = this.entryRoute;
89
+ const routes = Array.isArray(route) ? route : [route];
90
+ routes.forEach((route) => {
91
+ this.uiHandler.use(`${this.basePath}${route}`, (0, h3_1.eventHandler)(async () => {
92
+ const { name: filename, params } = handler({
93
+ basePath: this.basePath,
94
+ uiConfig: this.uiConfig,
95
+ });
96
+ return ejs_1.default.renderFile(`${this.viewPath}/${filename}`, params);
97
+ }), method);
98
+ });
99
+ this.uiHandler.get(`${this.basePath}${this.statics.route}/**`, (0, h3_1.eventHandler)(async (event) => {
100
+ return await (0, h3_1.serveStatic)(event, {
101
+ fallthrough: false,
102
+ getContents: (id) => (0, fs_1.readFileSync)(getStaticPath(id)),
103
+ getMeta: (id) => {
104
+ try {
105
+ const fileStat = (0, fs_1.statSync)(getStaticPath(id));
106
+ return {
107
+ size: fileStat.size,
108
+ type: (0, getContentType_1.getContentType)(id),
109
+ };
110
+ }
111
+ catch (e) {
112
+ return undefined;
113
+ }
114
+ },
115
+ });
116
+ }));
117
+ return this.uiHandler;
118
+ }
119
+ registerRoute(routeOrRoutes, method, handler) {
120
+ const { bullBoardQueues } = this;
121
+ if (!bullBoardQueues) {
122
+ throw new Error(`Please call 'setQueues' before using 'registerHandlers'`);
123
+ }
124
+ const routes = Array.isArray(routeOrRoutes) ? routeOrRoutes : [routeOrRoutes];
125
+ routes.forEach((route) => {
126
+ this.uiHandler.use(`${this.basePath}${route}`, (0, h3_1.eventHandler)(async (event) => {
127
+ try {
128
+ const { body } = await handler({
129
+ queues: this.bullBoardQueues,
130
+ params: (0, h3_1.getRouterParams)(event),
131
+ query: (0, h3_1.getQuery)(event),
132
+ });
133
+ return body;
134
+ }
135
+ catch (e) {
136
+ if (this.errorHandler) {
137
+ const { body, status } = this.errorHandler(e);
138
+ return (0, h3_1.createError)({
139
+ statusCode: status,
140
+ data: body,
141
+ });
142
+ }
143
+ }
144
+ }), method);
145
+ });
146
+ }
147
+ }
148
+ exports.H3Adapter = H3Adapter;
149
+ //# sourceMappingURL=H3Adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"H3Adapter.js","sourceRoot":"","sources":["../src/H3Adapter.ts"],"names":[],"mappings":";;;;;;AAQA,2BAA4C;AAC5C,yCAA+C;AAC/C,2BAOY;AACZ,8CAAsB;AACtB,2DAAwD;AAGxD,MAAa,SAAS;IAAtB;QACU,cAAS,GAAG,IAAA,iBAAY,GAAE,CAAC;QAC3B,aAAQ,GAAG,EAAE,CAAC;QAMd,aAAQ,GAAa,EAAE,CAAC;IA6KlC,CAAC;IA3KQ,WAAW,CAAC,IAAY;QAC7B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,aA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B,IAAI,EAAE,IAAI;yBACX,CAAC,CAAC;qBACJ;iBACF;YACH,CAAC,CAAC,EACF,MAAM,CACP,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AArLD,8BAqLC"}
package/dist/index.cjs ADDED
@@ -0,0 +1,170 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const h3 = require('h3');
5
+ const ejs = require('ejs');
6
+
7
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
8
+
9
+ const ejs__default = /*#__PURE__*/_interopDefaultCompat(ejs);
10
+
11
+ const getContentType = (filename) => {
12
+ let contentType = "text/html";
13
+ if (!filename)
14
+ return contentType;
15
+ switch (filename.split(".").pop()) {
16
+ case "js":
17
+ contentType = "text/javascript";
18
+ break;
19
+ case "css":
20
+ contentType = "text/css";
21
+ break;
22
+ case "png":
23
+ contentType = "image/png";
24
+ break;
25
+ case "svg":
26
+ contentType = "image/svg+xml";
27
+ break;
28
+ case "json":
29
+ contentType = "application/json";
30
+ break;
31
+ case "ico":
32
+ contentType = "image/x-icon";
33
+ break;
34
+ }
35
+ return contentType;
36
+ };
37
+
38
+ var __defProp = Object.defineProperty;
39
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
40
+ var __publicField = (obj, key, value) => {
41
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
42
+ return value;
43
+ };
44
+ class H3Adapter {
45
+ constructor() {
46
+ __publicField(this, "uiHandler", h3.createRouter());
47
+ __publicField(this, "basePath", "");
48
+ __publicField(this, "entryRoute");
49
+ __publicField(this, "apiRoutes");
50
+ __publicField(this, "statics");
51
+ __publicField(this, "errorHandler");
52
+ __publicField(this, "bullBoardQueues");
53
+ __publicField(this, "viewPath");
54
+ __publicField(this, "uiConfig", {});
55
+ }
56
+ setBasePath(path) {
57
+ this.basePath = path;
58
+ return this;
59
+ }
60
+ setStaticPath(staticsRoute, staticsPath) {
61
+ this.statics = { route: staticsRoute, path: staticsPath };
62
+ return this;
63
+ }
64
+ setViewsPath(viewPath) {
65
+ this.viewPath = viewPath;
66
+ return this;
67
+ }
68
+ setErrorHandler(handler) {
69
+ this.errorHandler = handler;
70
+ return this;
71
+ }
72
+ setApiRoutes(routes) {
73
+ this.apiRoutes = routes;
74
+ return this;
75
+ }
76
+ setEntryRoute(routeDef) {
77
+ this.entryRoute = routeDef;
78
+ return this;
79
+ }
80
+ setQueues(bullBoardQueues) {
81
+ this.bullBoardQueues = bullBoardQueues;
82
+ return this;
83
+ }
84
+ setUIConfig(config = {}) {
85
+ this.uiConfig = config;
86
+ return this;
87
+ }
88
+ registerHandlers() {
89
+ if (!this.statics) {
90
+ throw new Error(`Please call 'setStaticPath' before using 'registerPlugin'`);
91
+ } else if (!this.entryRoute) {
92
+ throw new Error(`Please call 'setEntryRoute' before using 'registerPlugin'`);
93
+ } else if (!this.viewPath) {
94
+ throw new Error(`Please call 'setViewsPath' before using 'registerPlugin'`);
95
+ } else if (!this.apiRoutes) {
96
+ throw new Error(`Please call 'setApiRoutes' before using 'registerPlugin'`);
97
+ } else if (!this.bullBoardQueues) {
98
+ throw new Error(`Please call 'setQueues' before using 'registerPlugin'`);
99
+ } else if (!this.errorHandler) {
100
+ throw new Error(`Please call 'setErrorHandler' before using 'registerPlugin'`);
101
+ }
102
+ const getStaticPath = (relativePath) => {
103
+ if (!this.statics)
104
+ return "";
105
+ return `${this.statics.path}${relativePath.replace(
106
+ `${this.basePath}${this.statics.route}`,
107
+ ""
108
+ )}`;
109
+ };
110
+ const { method, route, handler } = this.entryRoute;
111
+ const routes = Array.isArray(route) ? route : [route];
112
+ routes.forEach((route2) => {
113
+ this.uiHandler.use(
114
+ `${this.basePath}${route2}`,
115
+ h3.eventHandler(async () => {
116
+ const { name: filename, params } = handler({
117
+ basePath: this.basePath,
118
+ uiConfig: this.uiConfig
119
+ });
120
+ return ejs__default.renderFile(`${this.viewPath}/${filename}`, params);
121
+ }),
122
+ method
123
+ );
124
+ });
125
+ this.uiHandler.get(
126
+ `${this.basePath}${this.statics.route}/**`,
127
+ h3.eventHandler(async (event) => {
128
+ await h3.serveStatic(event, {
129
+ fallthrough: true,
130
+ indexNames: void 0,
131
+ getContents: (id) => fs.readFileSync(getStaticPath(id)),
132
+ getMeta: (id) => {
133
+ const fileStat = fs.statSync(getStaticPath(id));
134
+ return {
135
+ size: fileStat.size,
136
+ type: getContentType(id)
137
+ };
138
+ }
139
+ });
140
+ })
141
+ );
142
+ this.apiRoutes.forEach(({ route: route2, handler: handler2, method: method2 }) => {
143
+ this.uiHandler.use(
144
+ `${this.basePath}${route2}`,
145
+ h3.eventHandler(async (event) => {
146
+ try {
147
+ const { body } = await handler2({
148
+ queues: this.bullBoardQueues,
149
+ params: h3.getRouterParams(event),
150
+ query: h3.getQuery(event)
151
+ });
152
+ return body;
153
+ } catch (e) {
154
+ if (this.errorHandler) {
155
+ const { body, status } = this.errorHandler(e);
156
+ return h3.createError({
157
+ statusCode: status,
158
+ data: body
159
+ });
160
+ }
161
+ }
162
+ }),
163
+ method2
164
+ );
165
+ });
166
+ return this.uiHandler;
167
+ }
168
+ }
169
+
170
+ exports.H3Adapter = H3Adapter;
@@ -0,0 +1,25 @@
1
+ import * as h3 from 'h3';
2
+ import { IServerAdapter, ControllerHandlerReturnType, AppControllerRoute, AppViewRoute, BullBoardQueues, UIConfig } from '@bull-board/api/dist/typings/app';
3
+
4
+ declare class H3Adapter implements IServerAdapter {
5
+ private uiHandler;
6
+ private basePath;
7
+ private entryRoute;
8
+ private apiRoutes;
9
+ private statics;
10
+ private errorHandler;
11
+ private bullBoardQueues;
12
+ private viewPath;
13
+ private uiConfig;
14
+ setBasePath(path: string): H3Adapter;
15
+ setStaticPath(staticsRoute: string, staticsPath: string): H3Adapter;
16
+ setViewsPath(viewPath: string): H3Adapter;
17
+ setErrorHandler(handler: (error: Error) => ControllerHandlerReturnType): this;
18
+ setApiRoutes(routes: AppControllerRoute[]): H3Adapter;
19
+ setEntryRoute(routeDef: AppViewRoute): H3Adapter;
20
+ setQueues(bullBoardQueues: BullBoardQueues): H3Adapter;
21
+ setUIConfig(config?: UIConfig): H3Adapter;
22
+ registerHandlers(): h3.Router;
23
+ }
24
+
25
+ export { H3Adapter };
@@ -0,0 +1,25 @@
1
+ import * as h3 from 'h3';
2
+ import { IServerAdapter, ControllerHandlerReturnType, AppControllerRoute, AppViewRoute, BullBoardQueues, UIConfig } from '@bull-board/api/dist/typings/app';
3
+
4
+ declare class H3Adapter implements IServerAdapter {
5
+ private uiHandler;
6
+ private basePath;
7
+ private entryRoute;
8
+ private apiRoutes;
9
+ private statics;
10
+ private errorHandler;
11
+ private bullBoardQueues;
12
+ private viewPath;
13
+ private uiConfig;
14
+ setBasePath(path: string): H3Adapter;
15
+ setStaticPath(staticsRoute: string, staticsPath: string): H3Adapter;
16
+ setViewsPath(viewPath: string): H3Adapter;
17
+ setErrorHandler(handler: (error: Error) => ControllerHandlerReturnType): this;
18
+ setApiRoutes(routes: AppControllerRoute[]): H3Adapter;
19
+ setEntryRoute(routeDef: AppViewRoute): H3Adapter;
20
+ setQueues(bullBoardQueues: BullBoardQueues): H3Adapter;
21
+ setUIConfig(config?: UIConfig): H3Adapter;
22
+ registerHandlers(): h3.Router;
23
+ }
24
+
25
+ export { H3Adapter };
@@ -0,0 +1 @@
1
+ export { H3Adapter } from './H3Adapter';
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.H3Adapter = void 0;
4
+ var H3Adapter_1 = require("./H3Adapter");
5
+ Object.defineProperty(exports, "H3Adapter", { enumerable: true, get: function () { return H3Adapter_1.H3Adapter; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAAwC;AAA/B,sGAAA,SAAS,OAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,164 @@
1
+ import { readFileSync, statSync } from 'fs';
2
+ import { createRouter, eventHandler, serveStatic, getRouterParams, getQuery, createError } from 'h3';
3
+ import ejs from 'ejs';
4
+
5
+ const getContentType = (filename) => {
6
+ let contentType = "text/html";
7
+ if (!filename)
8
+ return contentType;
9
+ switch (filename.split(".").pop()) {
10
+ case "js":
11
+ contentType = "text/javascript";
12
+ break;
13
+ case "css":
14
+ contentType = "text/css";
15
+ break;
16
+ case "png":
17
+ contentType = "image/png";
18
+ break;
19
+ case "svg":
20
+ contentType = "image/svg+xml";
21
+ break;
22
+ case "json":
23
+ contentType = "application/json";
24
+ break;
25
+ case "ico":
26
+ contentType = "image/x-icon";
27
+ break;
28
+ }
29
+ return contentType;
30
+ };
31
+
32
+ var __defProp = Object.defineProperty;
33
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
34
+ var __publicField = (obj, key, value) => {
35
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
36
+ return value;
37
+ };
38
+ class H3Adapter {
39
+ constructor() {
40
+ __publicField(this, "uiHandler", createRouter());
41
+ __publicField(this, "basePath", "");
42
+ __publicField(this, "entryRoute");
43
+ __publicField(this, "apiRoutes");
44
+ __publicField(this, "statics");
45
+ __publicField(this, "errorHandler");
46
+ __publicField(this, "bullBoardQueues");
47
+ __publicField(this, "viewPath");
48
+ __publicField(this, "uiConfig", {});
49
+ }
50
+ setBasePath(path) {
51
+ this.basePath = path;
52
+ return this;
53
+ }
54
+ setStaticPath(staticsRoute, staticsPath) {
55
+ this.statics = { route: staticsRoute, path: staticsPath };
56
+ return this;
57
+ }
58
+ setViewsPath(viewPath) {
59
+ this.viewPath = viewPath;
60
+ return this;
61
+ }
62
+ setErrorHandler(handler) {
63
+ this.errorHandler = handler;
64
+ return this;
65
+ }
66
+ setApiRoutes(routes) {
67
+ this.apiRoutes = routes;
68
+ return this;
69
+ }
70
+ setEntryRoute(routeDef) {
71
+ this.entryRoute = routeDef;
72
+ return this;
73
+ }
74
+ setQueues(bullBoardQueues) {
75
+ this.bullBoardQueues = bullBoardQueues;
76
+ return this;
77
+ }
78
+ setUIConfig(config = {}) {
79
+ this.uiConfig = config;
80
+ return this;
81
+ }
82
+ registerHandlers() {
83
+ if (!this.statics) {
84
+ throw new Error(`Please call 'setStaticPath' before using 'registerPlugin'`);
85
+ } else if (!this.entryRoute) {
86
+ throw new Error(`Please call 'setEntryRoute' before using 'registerPlugin'`);
87
+ } else if (!this.viewPath) {
88
+ throw new Error(`Please call 'setViewsPath' before using 'registerPlugin'`);
89
+ } else if (!this.apiRoutes) {
90
+ throw new Error(`Please call 'setApiRoutes' before using 'registerPlugin'`);
91
+ } else if (!this.bullBoardQueues) {
92
+ throw new Error(`Please call 'setQueues' before using 'registerPlugin'`);
93
+ } else if (!this.errorHandler) {
94
+ throw new Error(`Please call 'setErrorHandler' before using 'registerPlugin'`);
95
+ }
96
+ const getStaticPath = (relativePath) => {
97
+ if (!this.statics)
98
+ return "";
99
+ return `${this.statics.path}${relativePath.replace(
100
+ `${this.basePath}${this.statics.route}`,
101
+ ""
102
+ )}`;
103
+ };
104
+ const { method, route, handler } = this.entryRoute;
105
+ const routes = Array.isArray(route) ? route : [route];
106
+ routes.forEach((route2) => {
107
+ this.uiHandler.use(
108
+ `${this.basePath}${route2}`,
109
+ eventHandler(async () => {
110
+ const { name: filename, params } = handler({
111
+ basePath: this.basePath,
112
+ uiConfig: this.uiConfig
113
+ });
114
+ return ejs.renderFile(`${this.viewPath}/${filename}`, params);
115
+ }),
116
+ method
117
+ );
118
+ });
119
+ this.uiHandler.get(
120
+ `${this.basePath}${this.statics.route}/**`,
121
+ eventHandler(async (event) => {
122
+ await serveStatic(event, {
123
+ fallthrough: true,
124
+ indexNames: void 0,
125
+ getContents: (id) => readFileSync(getStaticPath(id)),
126
+ getMeta: (id) => {
127
+ const fileStat = statSync(getStaticPath(id));
128
+ return {
129
+ size: fileStat.size,
130
+ type: getContentType(id)
131
+ };
132
+ }
133
+ });
134
+ })
135
+ );
136
+ this.apiRoutes.forEach(({ route: route2, handler: handler2, method: method2 }) => {
137
+ this.uiHandler.use(
138
+ `${this.basePath}${route2}`,
139
+ eventHandler(async (event) => {
140
+ try {
141
+ const { body } = await handler2({
142
+ queues: this.bullBoardQueues,
143
+ params: getRouterParams(event),
144
+ query: getQuery(event)
145
+ });
146
+ return body;
147
+ } catch (e) {
148
+ if (this.errorHandler) {
149
+ const { body, status } = this.errorHandler(e);
150
+ return createError({
151
+ statusCode: status,
152
+ data: body
153
+ });
154
+ }
155
+ }
156
+ }),
157
+ method2
158
+ );
159
+ });
160
+ return this.uiHandler;
161
+ }
162
+ }
163
+
164
+ export { H3Adapter };
@@ -0,0 +1 @@
1
+ export declare const getContentType: (filename?: string) => string;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getContentType = void 0;
4
+ const getContentType = (filename) => {
5
+ let contentType = "text/html";
6
+ if (!filename)
7
+ return contentType;
8
+ switch (filename.split(".").pop()) {
9
+ case "js":
10
+ contentType = "text/javascript";
11
+ break;
12
+ case "css":
13
+ contentType = "text/css";
14
+ break;
15
+ case "png":
16
+ contentType = "image/png";
17
+ break;
18
+ case "svg":
19
+ contentType = "image/svg+xml";
20
+ break;
21
+ case "json":
22
+ contentType = "application/json";
23
+ break;
24
+ case "ico":
25
+ contentType = "image/x-icon";
26
+ break;
27
+ }
28
+ return contentType;
29
+ };
30
+ exports.getContentType = getContentType;
31
+ //# sourceMappingURL=getContentType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getContentType.js","sourceRoot":"","sources":["../../src/utils/getContentType.ts"],"names":[],"mappings":";;;AAAO,MAAM,cAAc,GAAG,CAAC,QAAiB,EAAE,EAAE;IAClD,IAAI,WAAW,GAAG,WAAW,CAAC;IAE9B,IAAI,CAAC,QAAQ;QAAE,OAAO,WAAW,CAAC;IAElC,QAAQ,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACjC,KAAK,IAAI;YACP,WAAW,GAAG,iBAAiB,CAAC;YAChC,MAAM;QACR,KAAK,KAAK;YACR,WAAW,GAAG,UAAU,CAAC;YACzB,MAAM;QACR,KAAK,KAAK;YACR,WAAW,GAAG,WAAW,CAAC;YAC1B,MAAM;QACR,KAAK,KAAK;YACR,WAAW,GAAG,eAAe,CAAC;YAC9B,MAAM;QACR,KAAK,MAAM;YACT,WAAW,GAAG,kBAAkB,CAAC;YACjC,MAAM;QACR,KAAK,KAAK;YACR,WAAW,GAAG,cAAc,CAAC;YAC7B,MAAM;KACT;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AA3BW,QAAA,cAAc,kBA2BzB"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@bull-board/h3",
3
+ "version": "5.13.0",
4
+ "description": "A H3 server adapter for Bull-Board dashboard.",
5
+ "keywords": [
6
+ "bull",
7
+ "bullmq",
8
+ "redis",
9
+ "h3",
10
+ "adapter",
11
+ "queue",
12
+ "monitoring",
13
+ "dashboard"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/felixmosh/bull-board.git",
18
+ "directory": "packages/h3"
19
+ },
20
+ "license": "MIT",
21
+ "author": "genu",
22
+ "main": "./dist/index.js",
23
+ "types": "./dist/index.d.ts",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "scripts": {
28
+ "build": "tsc",
29
+ "clean": "rm -rf dist"
30
+ },
31
+ "dependencies": {
32
+ "@bull-board/api": "5.13.0",
33
+ "@bull-board/ui": "5.13.0",
34
+ "ejs": "^3.1.9",
35
+ "h3": "^1.10.0"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "devDependencies": {
41
+ "@types/ejs": "^3.1.5"
42
+ }
43
+ }