@hatchet-dev/typescript-sdk 0.8.0-alpha.3 → 0.9.0

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,191 +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
21
- constructor(worker, workflows
22
- // eslint-disable-next-line no-empty-function
23
- ) {
24
- this.worker = worker;
25
- this.workflows = workflows;
26
- }
27
- /**
28
- * Handles a request with a provided body, secret, and signature.
29
- *
30
- * @param {string | undefined} body - The body of the request.
31
- * @param {string | undefined} secret - The secret used for signature verification.
32
- * @param {string | string[] | undefined | null} signature - The signature of the request.
33
- *
34
- * @throws {HatchetError} - If no signature is provided or the signature is not a string.
35
- * @throws {HatchetError} - If no secret is provided.
36
- * @throws {HatchetError} - If no body is provided.
37
- */
38
- handle(body, secret, signature) {
39
- return __awaiter(this, void 0, void 0, function* () {
40
- if (!signature || typeof signature !== 'string') {
41
- throw new hatchet_error_1.default('No signature provided');
42
- }
43
- if (!secret) {
44
- throw new hatchet_error_1.default('No secret provided');
45
- }
46
- if (!body) {
47
- throw new hatchet_error_1.default('No body provided');
48
- }
49
- // verify hmac signature
50
- const actualSignature = (0, crypto_1.createHmac)('sha256', secret).update(body).digest('hex');
51
- if (actualSignature !== signature) {
52
- throw new hatchet_error_1.default(`Invalid signature, expected ${actualSignature}, got ${signature}`);
53
- }
54
- const action = action_listener_1.ActionObject.parse(JSON.parse(body));
55
- yield this.worker.handleAction(action);
56
- });
57
- }
58
- getHealthcheckResponse() {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- for (const workflow of this.workflows) {
61
- yield this.worker.registerWorkflow(workflow);
62
- }
63
- return {
64
- actions: Object.keys(this.worker.action_registry),
65
- };
66
- });
67
- }
68
- /**
69
- * Express Handler
70
- *
71
- * This method is an asynchronous function that returns an Express middleware handler.
72
- * The handler function is responsible for handling incoming requests and invoking the
73
- * corresponding logic based on the provided secret.
74
- *
75
- * @param {string} secret - The secret key used to authenticate and authorize the incoming requests.
76
- *
77
- * @return {Function} - An Express middleware handler function that receives the request and response objects.
78
- */
79
- expressHandler({ secret }) {
80
- return (req, res) => {
81
- if (req.method === 'GET') {
82
- res.sendStatus(200);
83
- res.send('OK!');
84
- return;
85
- }
86
- if (req.method !== 'POST') {
87
- res.sendStatus(405);
88
- res.json({ error: 'Method not allowed' });
89
- return;
90
- }
91
- if (req.headers['x-healthcheck']) {
92
- this.getHealthcheckResponse()
93
- .then((resp) => {
94
- res.sendStatus(200);
95
- res.json(resp);
96
- })
97
- .catch((err) => {
98
- res.sendStatus(500);
99
- this.worker.logger.error(`Error handling request: ${err.message}`);
100
- });
101
- return;
102
- }
103
- this.handle(req.body, req.headers['x-hatchet-signature'], secret)
104
- .then(() => {
105
- res.sendStatus(200);
106
- })
107
- .catch((err) => {
108
- res.sendStatus(500);
109
- this.worker.logger.error(`Error handling request: ${err.message}`);
110
- });
111
- };
112
- }
113
- /**
114
- * A method that returns an HTTP request handler.
115
- *
116
- * @param {string} secret - The secret key used for verification.
117
- *
118
- * @returns {function} - An HTTP request handler function.
119
- */
120
- httpHandler({ secret }) {
121
- return (req, res) => {
122
- const handle = () => __awaiter(this, void 0, void 0, function* () {
123
- if (req.method === 'GET') {
124
- res.writeHead(200, { 'Content-Type': 'application/json' });
125
- res.write('OK!');
126
- res.end();
127
- return;
128
- }
129
- if (req.method !== 'POST') {
130
- res.writeHead(405, { 'Content-Type': 'application/json' });
131
- res.write(JSON.stringify({ error: 'Method not allowed' }));
132
- res.end();
133
- return;
134
- }
135
- if (req.headers['x-healthcheck']) {
136
- const resp = yield this.getHealthcheckResponse();
137
- res.writeHead(200, { 'Content-Type': 'application/json' });
138
- res.write(JSON.stringify(resp));
139
- res.end();
140
- return;
141
- }
142
- const body = yield this.getBody(req);
143
- yield this.handle(body, secret, req.headers['x-hatchet-signature']);
144
- res.writeHead(200, 'OK');
145
- res.end();
146
- });
147
- handle().catch((e) => {
148
- this.worker.logger.error(`Error handling request: ${e.message}`);
149
- res.writeHead(500, 'Internal server error');
150
- res.end();
151
- });
152
- };
153
- }
154
- /**
155
- * A method that returns a Next.js request handler.
156
- *
157
- * @param {any} req - The request object received from Next.js.
158
- * @param {string} secret - The secret key used to verify the request.
159
- * @return {Promise<Response>} - A Promise that resolves with a Response object.
160
- */
161
- nextJSHandler({ secret }) {
162
- const ok = () => __awaiter(this, void 0, void 0, function* () {
163
- return new Response('OK!', { status: 200 });
164
- });
165
- const f = (req) => __awaiter(this, void 0, void 0, function* () {
166
- if (req.headers.get('x-healthcheck')) {
167
- const resp = yield this.getHealthcheckResponse();
168
- return new Response(JSON.stringify(resp), { status: 200 });
169
- }
170
- yield this.handle(yield req.text(), secret, req.headers.get('x-hatchet-signature'));
171
- return new Response('ok', { status: 200 });
172
- });
173
- return {
174
- GET: ok,
175
- POST: f,
176
- PUT: f,
177
- };
178
- }
179
- getBody(req) {
180
- return new Promise((resolve) => {
181
- let body = '';
182
- req.on('data', (chunk) => {
183
- body += chunk;
184
- });
185
- req.on('end', () => {
186
- resolve(body);
187
- });
188
- });
189
- }
190
- }
191
- exports.WebhookHandler = WebhookHandler;