fix: improve inngest support

This commit is contained in:
Mythie
2024-06-17 16:59:14 +10:00
parent 6977381e00
commit b8d6484ff0
7 changed files with 31 additions and 30 deletions

View File

@ -1,9 +1,11 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import type { NextRequest } from 'next/server';
import type { Context, Handler, InngestFunction } from 'inngest';
import { Inngest as InngestClient } from 'inngest';
import type { Logger } from 'inngest/middleware/logger';
import { serve as createPagesRoute } from 'inngest/next';
import { json } from 'micro';
import type { JobDefinition, JobRunIO, SimpleTriggerJobOptions } from './_internal/job';
import { BaseJobProvider } from './base';
@ -35,6 +37,7 @@ export class InngestJobProvider extends BaseJobProvider {
}
public defineJob<N extends string, T>(job: JobDefinition<N, T>): void {
console.log('defining job', job.id);
const fn = this._client.createFunction(
{
id: job.id,
@ -70,15 +73,25 @@ export class InngestJobProvider extends BaseJobProvider {
});
}
public getApiHandler(): (req: NextApiRequest, res: NextApiResponse) => Promise<void> {
// !: Ignoring the error here since this is designed to work with the Next.js pages router
// !: but wants a more strict type.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return createPagesRoute({
public getApiHandler() {
const handler = createPagesRoute({
client: this._client,
functions: this._functions,
});
return async (req: NextApiRequest, res: NextApiResponse) => {
// Since body-parser is disabled for this route we need to patch in the parsed body
if (req.headers['content-type'] === 'application/json') {
Object.assign(req, {
body: await json(req),
});
}
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const nextReq = req as unknown as NextRequest;
return await handler(nextReq, res);
};
}
private convertInngestIoToJobRunIo(ctx: Context.Any & { logger: Logger }) {