mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
fix: improve inngest support
This commit is contained in:
1
package-lock.json
generated
1
package-lock.json
generated
@ -31833,6 +31833,7 @@
|
|||||||
"inngest": "^3.19.13",
|
"inngest": "^3.19.13",
|
||||||
"kysely": "^0.26.3",
|
"kysely": "^0.26.3",
|
||||||
"luxon": "^3.4.0",
|
"luxon": "^3.4.0",
|
||||||
|
"micro": "^10.0.1",
|
||||||
"nanoid": "^4.0.2",
|
"nanoid": "^4.0.2",
|
||||||
"next": "14.0.3",
|
"next": "14.0.3",
|
||||||
"next-auth": "4.24.5",
|
"next-auth": "4.24.5",
|
||||||
|
|||||||
@ -13,7 +13,7 @@ export abstract class BaseJobProvider {
|
|||||||
throw new Error('Not implemented');
|
throw new Error('Not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
public getApiHandler(): (req: NextApiRequest, res: NextApiResponse) => Promise<void> {
|
public getApiHandler(): (req: NextApiRequest, res: NextApiResponse) => Promise<Response | void> {
|
||||||
throw new Error('Not implemented');
|
throw new Error('Not implemented');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
import type { JobDefinition, TriggerJobOptions } from './_internal/job';
|
import type { JobDefinition, TriggerJobOptions } from './_internal/job';
|
||||||
import type { BaseJobProvider as JobClientProvider } from './base';
|
import type { BaseJobProvider as JobClientProvider } from './base';
|
||||||
|
import { InngestJobProvider } from './inngest';
|
||||||
import { LocalJobProvider } from './local';
|
import { LocalJobProvider } from './local';
|
||||||
import { TriggerJobProvider } from './trigger';
|
import { TriggerJobProvider } from './trigger';
|
||||||
|
|
||||||
@ -9,35 +12,20 @@ export class JobClient<T extends Array<JobDefinition> = []> {
|
|||||||
private _provider: JobClientProvider;
|
private _provider: JobClientProvider;
|
||||||
|
|
||||||
public constructor(definitions: T) {
|
public constructor(definitions: T) {
|
||||||
if (process.env.NEXT_PRIVATE_JOBS_PROVIDER === 'trigger') {
|
this._provider = match(process.env.NEXT_PRIVATE_JOBS_PROVIDER)
|
||||||
this._provider = TriggerJobProvider.getInstance();
|
.with('inngest', () => InngestJobProvider.getInstance())
|
||||||
|
.with('trigger', () => TriggerJobProvider.getInstance())
|
||||||
return;
|
.otherwise(() => LocalJobProvider.getInstance());
|
||||||
}
|
|
||||||
|
|
||||||
this._provider = LocalJobProvider.getInstance();
|
|
||||||
|
|
||||||
definitions.forEach((definition) => {
|
definitions.forEach((definition) => {
|
||||||
this._provider.defineJob(definition);
|
this._provider.defineJob(definition);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static getInstance() {
|
|
||||||
// if (!this._instance) {
|
|
||||||
// this._instance = new JobClient();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return this._instance;
|
|
||||||
// }
|
|
||||||
|
|
||||||
public async triggerJob(options: TriggerJobOptions<T>) {
|
public async triggerJob(options: TriggerJobOptions<T>) {
|
||||||
return this._provider.triggerJob(options);
|
return this._provider.triggerJob(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public defineJob<N extends string, T>(job: JobDefinition<N, T>) {
|
|
||||||
// return this._provider.defineJob(job);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public getApiHandler() {
|
public getApiHandler() {
|
||||||
return this._provider.getApiHandler();
|
return this._provider.getApiHandler();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
import type { Context, Handler, InngestFunction } from 'inngest';
|
import type { Context, Handler, InngestFunction } from 'inngest';
|
||||||
import { Inngest as InngestClient } from 'inngest';
|
import { Inngest as InngestClient } from 'inngest';
|
||||||
import type { Logger } from 'inngest/middleware/logger';
|
import type { Logger } from 'inngest/middleware/logger';
|
||||||
import { serve as createPagesRoute } from 'inngest/next';
|
import { serve as createPagesRoute } from 'inngest/next';
|
||||||
|
import { json } from 'micro';
|
||||||
|
|
||||||
import type { JobDefinition, JobRunIO, SimpleTriggerJobOptions } from './_internal/job';
|
import type { JobDefinition, JobRunIO, SimpleTriggerJobOptions } from './_internal/job';
|
||||||
import { BaseJobProvider } from './base';
|
import { BaseJobProvider } from './base';
|
||||||
@ -35,6 +37,7 @@ export class InngestJobProvider extends BaseJobProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public defineJob<N extends string, T>(job: JobDefinition<N, T>): void {
|
public defineJob<N extends string, T>(job: JobDefinition<N, T>): void {
|
||||||
|
console.log('defining job', job.id);
|
||||||
const fn = this._client.createFunction(
|
const fn = this._client.createFunction(
|
||||||
{
|
{
|
||||||
id: job.id,
|
id: job.id,
|
||||||
@ -70,15 +73,25 @@ export class InngestJobProvider extends BaseJobProvider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public getApiHandler(): (req: NextApiRequest, res: NextApiResponse) => Promise<void> {
|
public getApiHandler() {
|
||||||
// !: Ignoring the error here since this is designed to work with the Next.js pages router
|
const handler = createPagesRoute({
|
||||||
// !: but wants a more strict type.
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
||||||
// @ts-expect-error
|
|
||||||
return createPagesRoute({
|
|
||||||
client: this._client,
|
client: this._client,
|
||||||
functions: this._functions,
|
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 }) {
|
private convertInngestIoToJobRunIo(ctx: Context.Any & { logger: Logger }) {
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
|
|
||||||
import { createPagesRoute } from '@trigger.dev/nextjs';
|
import { createPagesRoute } from '@trigger.dev/nextjs';
|
||||||
import type { IO } from '@trigger.dev/sdk';
|
import type { IO } from '@trigger.dev/sdk';
|
||||||
import { TriggerClient, eventTrigger } from '@trigger.dev/sdk';
|
import { TriggerClient, eventTrigger } from '@trigger.dev/sdk';
|
||||||
@ -54,7 +52,7 @@ export class TriggerJobProvider extends BaseJobProvider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public getApiHandler(): (req: NextApiRequest, res: NextApiResponse) => Promise<void> {
|
public getApiHandler() {
|
||||||
const { handler } = createPagesRoute(this._client);
|
const { handler } = createPagesRoute(this._client);
|
||||||
|
|
||||||
return handler;
|
return handler;
|
||||||
|
|||||||
@ -39,6 +39,7 @@
|
|||||||
"inngest": "^3.19.13",
|
"inngest": "^3.19.13",
|
||||||
"kysely": "^0.26.3",
|
"kysely": "^0.26.3",
|
||||||
"luxon": "^3.4.0",
|
"luxon": "^3.4.0",
|
||||||
|
"micro": "^10.0.1",
|
||||||
"nanoid": "^4.0.2",
|
"nanoid": "^4.0.2",
|
||||||
"next": "14.0.3",
|
"next": "14.0.3",
|
||||||
"next-auth": "4.24.5",
|
"next-auth": "4.24.5",
|
||||||
|
|||||||
2
packages/tsconfig/process-env.d.ts
vendored
2
packages/tsconfig/process-env.d.ts
vendored
@ -68,7 +68,7 @@ declare namespace NodeJS {
|
|||||||
//
|
//
|
||||||
NEXT_PRIVATE_BROWSERLESS_URL?: string;
|
NEXT_PRIVATE_BROWSERLESS_URL?: string;
|
||||||
|
|
||||||
NEXT_PRIVATE_JOBS_PROVIDER?: 'trigger' | 'local';
|
NEXT_PRIVATE_JOBS_PROVIDER?: 'trigger' | 'inngest' | 'local';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trigger.dev environment variables
|
* Trigger.dev environment variables
|
||||||
|
|||||||
Reference in New Issue
Block a user