Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update H3Adapter.ts use that for unjs/nitro #866

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Mittcio
Copy link

@Mittcio Mittcio commented Dec 27, 2024

basePath is applied to all routes, app.use() in nuxt/nitro I think not work well

@felixmosh
Copy link
Owner

Hi thanks for the PR.
What issue do you try to solve?

@Mittcio
Copy link
Author

Mittcio commented Dec 28, 2024

I try to use that with @bull-board/nestjs which seems to auto call some of that !this.entryRoute), !this.viewPath), !this.errorHandler) and !this.uiConfig) before setBasePath
``

@Mittcio
Copy link
Author

Mittcio commented Dec 28, 2024

If use in nitrojs plugin i get 404 error

import { H3Adapter } from '@bull-board/h3';
import { eventHandler } from 'h3';
import { createBullBoard } from '@bull-board/api';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
import { Queue as QueueMQ, RedisOptions, Worker } from 'bullmq';

const sleep = (t: number) => new Promise((resolve) => setTimeout(resolve, t * 1000));

export default defineNitroPlugin((nitroApp) => {
    let serverAdapter = new H3Adapter();
    serverAdapter.setBasePath('/b');

    const redisOptions: RedisOptions = {
        port: 6379,
        host: 'localhost',
        password: '',
    };
  
    const createQueueMQ = (name: string) => new QueueMQ(name, { connection: redisOptions });

    async function setupBullMQProcessor(queueName: string) {
        new Worker(
          queueName,
          async (job) => {
            for (let i = 0; i <= 100; i++) {
                await sleep(Math.random());
                await job.updateProgress(i);
                await job.log(`Processing job at interval ${i}`);
      
                if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`);
              }
      
              return { jobId: `This is the return value of job (${job.id})` };
            },
            { connection: redisOptions }
        );
    }
    const exampleBullMq = createQueueMQ('BullMQ');
    setupBullMQProcessor(exampleBullMq.name);
    createBullBoard({
        queues: [new BullMQAdapter(exampleBullMq)],
        serverAdapter,
    });

    nitroApp.h3App.use(serverAdapter.registerHandlers());

    nitroApp.router.use(
        '/add',
        eventHandler(async () => {
            await exampleBullMq.add('myJob', { foo: 'bar' });
            return true;
        })
    );
})
  

@Mittcio
Copy link
Author

Mittcio commented Dec 28, 2024

I try http://localhost:3000/api/api/b, http://localhost:3000/api/b and http://localhost:3000/b

@Mittcio
Copy link
Author

Mittcio commented Dec 28, 2024

In your example is app.use() but work with router.use like nitroApp.router.use('/**', serverAdapter.registerHandlers().handler);. But that path '/**' will cause problems it will match other routes, wil return 204 no content for some routes and other

@Mittcio
Copy link
Author

Mittcio commented Dec 28, 2024

Plus you create a second router in adapter this.uiHandler = (0, h3_1.createRouter)() but not pass existing router (created in h3 example) or not get only routes and attach its to that exisiting router (form h3 expale index.ts).

It can be .forEach({ path, handler, method }) => nitroApp.router.add(path, hadler, method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants