Skip to content

Commit

Permalink
improve handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed May 24, 2024
1 parent 3ffdb0c commit f63d478
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/clients/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,21 @@ export class Worker {
}
}

private async handle(
/**
* Handles a request with a provided body, secret, and signature.
*
* @param {string | undefined} body - The body of the request.
* @param {string | undefined} secret - The secret used for signature verification.
* @param {string | string[] | undefined | null} signature - The signature of the request.
*
* @throws {HatchetError} - If no signature is provided or the signature is not a string.
* @throws {HatchetError} - If no secret is provided.
* @throws {HatchetError} - If no body is provided.
*/
async handle(
body: string | undefined,
secret: string | undefined,
signature: string | string[] | undefined
signature: string | string[] | undefined | null
) {
if (!signature || typeof signature !== 'string') {
throw new HatchetError('No signature provided');
Expand All @@ -451,7 +462,17 @@ export class Worker {
this.handleAction(action);
}

// Handler for expressjs
/**
* Express Handler
*
* This method is an asynchronous function that returns an Express middleware handler.
* The handler function is responsible for handling incoming requests and invoking the
* corresponding logic based on the provided secret.
*
* @param {string} secret - The secret key used to authenticate and authorize the incoming requests.
*
* @return {Function} - An Express middleware handler function that receives the request and response objects.
*/
async expressHandler(secret: string) {
await Promise.all(this.registeredWorkflowPromises);

Expand All @@ -467,7 +488,13 @@ export class Worker {
};
}

// Handler for Node.JS HTTP server
/**
* A method that returns an HTTP request handler.
*
* @param {string} secret - The secret key used for verification.
*
* @returns {function} - An HTTP request handler function.
*/
async httpHandler(secret: string) {
await Promise.all(this.registeredWorkflowPromises);

Expand All @@ -489,6 +516,18 @@ export class Worker {
};
}

/**
* Handles a hatchet webhook request from a Vercel API route (including but not limited to Next.js routes).
*
* @param {any} req - The request object received from Vercel.
* @param {string} secret - The secret key used to verify the request.
* @return {Promise<Response>} - A Promise that resolves with a Response object.
*/
async handleVercelRequest(req: Request, secret: string) {
await this.handle(await req.text(), secret, req.headers.get('x-hatchet-signature'));
return new Response('ok', { status: 200 });
}

async start() {
// ensure all workflows are registered
await Promise.all(this.registeredWorkflowPromises);
Expand Down

0 comments on commit f63d478

Please sign in to comment.