@hatchet-dev/typescript-sdk 0.8.0-alpha.2 → 0.8.1

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.
@@ -1,56 +0,0 @@
1
- /// <reference types="node" />
2
- import { IncomingMessage, ServerResponse } from 'http';
3
- import { Worker } from './worker';
4
- export interface HandlerOpts {
5
- secret: string;
6
- }
7
- export declare class WebhookHandler {
8
- private worker;
9
- constructor(worker: Worker);
10
- /**
11
- * Handles a request with a provided body, secret, and signature.
12
- *
13
- * @param {string | undefined} body - The body of the request.
14
- * @param {string | undefined} secret - The secret used for signature verification.
15
- * @param {string | string[] | undefined | null} signature - The signature of the request.
16
- *
17
- * @throws {HatchetError} - If no signature is provided or the signature is not a string.
18
- * @throws {HatchetError} - If no secret is provided.
19
- * @throws {HatchetError} - If no body is provided.
20
- */
21
- handle(body: string | undefined, secret: string | undefined, signature: string | string[] | undefined | null): Promise<void>;
22
- private getHealthcheckResponse;
23
- /**
24
- * Express Handler
25
- *
26
- * This method is an asynchronous function that returns an Express middleware handler.
27
- * The handler function is responsible for handling incoming requests and invoking the
28
- * corresponding logic based on the provided secret.
29
- *
30
- * @param {string} secret - The secret key used to authenticate and authorize the incoming requests.
31
- *
32
- * @return {Function} - An Express middleware handler function that receives the request and response objects.
33
- */
34
- expressHandler({ secret }: HandlerOpts): (req: any, res: any) => void;
35
- /**
36
- * A method that returns an HTTP request handler.
37
- *
38
- * @param {string} secret - The secret key used for verification.
39
- *
40
- * @returns {function} - An HTTP request handler function.
41
- */
42
- httpHandler({ secret }: HandlerOpts): (req: IncomingMessage, res: ServerResponse) => void;
43
- /**
44
- * A method that returns a Next.js request handler.
45
- *
46
- * @param {any} req - The request object received from Next.js.
47
- * @param {string} secret - The secret key used to verify the request.
48
- * @return {Promise<Response>} - A Promise that resolves with a Response object.
49
- */
50
- nextJSHandler({ secret }: HandlerOpts): {
51
- GET: () => Promise<Response>;
52
- POST: (req: Request) => Promise<Response>;
53
- PUT: (req: Request) => Promise<Response>;
54
- };
55
- private getBody;
56
- }
@@ -1,160 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.WebhookHandler = void 0;
16
- const hatchet_error_1 = __importDefault(require("../../util/errors/hatchet-error"));
17
- const crypto_1 = require("crypto");
18
- const action_listener_1 = require("../dispatcher/action-listener");
19
- class WebhookHandler {
20
- // eslint-disable-next-line no-useless-constructor,no-empty-function
21
- constructor(worker) {
22
- this.worker = worker;
23
- }
24
- /**
25
- * Handles a request with a provided body, secret, and signature.
26
- *
27
- * @param {string | undefined} body - The body of the request.
28
- * @param {string | undefined} secret - The secret used for signature verification.
29
- * @param {string | string[] | undefined | null} signature - The signature of the request.
30
- *
31
- * @throws {HatchetError} - If no signature is provided or the signature is not a string.
32
- * @throws {HatchetError} - If no secret is provided.
33
- * @throws {HatchetError} - If no body is provided.
34
- */
35
- handle(body, secret, signature) {
36
- return __awaiter(this, void 0, void 0, function* () {
37
- if (!signature || typeof signature !== 'string') {
38
- throw new hatchet_error_1.default('No signature provided');
39
- }
40
- if (!secret) {
41
- throw new hatchet_error_1.default('No secret provided');
42
- }
43
- if (!body) {
44
- throw new hatchet_error_1.default('No body provided');
45
- }
46
- // verify hmac signature
47
- const actualSignature = (0, crypto_1.createHmac)('sha256', secret).update(body).digest('hex');
48
- if (actualSignature !== signature) {
49
- throw new hatchet_error_1.default(`Invalid signature, expected ${actualSignature}, got ${signature}`);
50
- }
51
- const action = action_listener_1.ActionObject.parse(JSON.parse(body));
52
- yield this.worker.handleAction(action);
53
- });
54
- }
55
- getHealthcheckResponse() {
56
- return {
57
- actions: Object.keys(this.worker.action_registry),
58
- };
59
- }
60
- /**
61
- * Express Handler
62
- *
63
- * This method is an asynchronous function that returns an Express middleware handler.
64
- * The handler function is responsible for handling incoming requests and invoking the
65
- * corresponding logic based on the provided secret.
66
- *
67
- * @param {string} secret - The secret key used to authenticate and authorize the incoming requests.
68
- *
69
- * @return {Function} - An Express middleware handler function that receives the request and response objects.
70
- */
71
- expressHandler({ secret }) {
72
- return (req, res) => {
73
- if (req.method === 'GET') {
74
- res.sendStatus(200);
75
- res.json(this.getHealthcheckResponse());
76
- return;
77
- }
78
- if (req.method !== 'POST') {
79
- res.sendStatus(405);
80
- res.json({ error: 'Method not allowed' });
81
- return;
82
- }
83
- this.handle(req.body, req.headers['x-hatchet-signature'], secret)
84
- .then(() => {
85
- res.sendStatus(200);
86
- })
87
- .catch((e) => {
88
- res.sendStatus(500);
89
- this.worker.logger.error(`Error handling request: ${e.message}`);
90
- });
91
- };
92
- }
93
- /**
94
- * A method that returns an HTTP request handler.
95
- *
96
- * @param {string} secret - The secret key used for verification.
97
- *
98
- * @returns {function} - An HTTP request handler function.
99
- */
100
- httpHandler({ secret }) {
101
- return (req, res) => {
102
- const handle = () => __awaiter(this, void 0, void 0, function* () {
103
- if (req.method === 'GET') {
104
- res.writeHead(200, { 'Content-Type': 'application/json' });
105
- res.write(JSON.stringify(this.getHealthcheckResponse()));
106
- res.end();
107
- return;
108
- }
109
- if (req.method !== 'POST') {
110
- res.writeHead(405, { 'Content-Type': 'application/json' });
111
- res.write(JSON.stringify({ error: 'Method not allowed' }));
112
- res.end();
113
- return;
114
- }
115
- const body = yield this.getBody(req);
116
- yield this.handle(body, secret, req.headers['x-hatchet-signature']);
117
- res.writeHead(200, 'OK');
118
- res.end();
119
- });
120
- handle().catch((e) => {
121
- this.worker.logger.error(`Error handling request: ${e.message}`);
122
- res.writeHead(500, 'Internal server error');
123
- res.end();
124
- });
125
- };
126
- }
127
- /**
128
- * A method that returns a Next.js request handler.
129
- *
130
- * @param {any} req - The request object received from Next.js.
131
- * @param {string} secret - The secret key used to verify the request.
132
- * @return {Promise<Response>} - A Promise that resolves with a Response object.
133
- */
134
- nextJSHandler({ secret }) {
135
- const healthcheck = () => __awaiter(this, void 0, void 0, function* () {
136
- return new Response(JSON.stringify(this.getHealthcheckResponse()), { status: 200 });
137
- });
138
- const f = (req) => __awaiter(this, void 0, void 0, function* () {
139
- yield this.handle(yield req.text(), secret, req.headers.get('x-hatchet-signature'));
140
- return new Response('ok', { status: 200 });
141
- });
142
- return {
143
- GET: healthcheck,
144
- POST: f,
145
- PUT: f,
146
- };
147
- }
148
- getBody(req) {
149
- return new Promise((resolve) => {
150
- let body = '';
151
- req.on('data', (chunk) => {
152
- body += chunk;
153
- });
154
- req.on('end', () => {
155
- resolve(body);
156
- });
157
- });
158
- }
159
- }
160
- exports.WebhookHandler = WebhookHandler;