@lagent_titi/kick.js-ts 1.0.1 → 1.0.2

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,17 +1,68 @@
1
- import { UnauthorizedError } from '../errors';
2
- export class PublicKeyService {
3
- constructor(apiClient) {
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.PublicKeyService = void 0;
40
+ var errors_1 = require("../errors");
41
+ var PublicKeyService = /** @class */ (function () {
42
+ function PublicKeyService(apiClient) {
4
43
  this.apiClient = apiClient;
5
44
  this.baseUrl = 'https://api.kick.com/public/v1/public-key';
6
45
  }
7
- async getPublicKey() {
8
- const response = await fetch(this.baseUrl, {
9
- method: 'GET'
46
+ PublicKeyService.prototype.getPublicKey = function () {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var response;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0: return [4 /*yield*/, fetch(this.baseUrl, {
52
+ method: 'GET'
53
+ })];
54
+ case 1:
55
+ response = _a.sent();
56
+ if (response.status === 401)
57
+ throw new errors_1.UnauthorizedError();
58
+ if (!response.ok)
59
+ throw new Error("HTTP error! status: ".concat(response.status));
60
+ return [4 /*yield*/, response.json()];
61
+ case 2: return [2 /*return*/, _a.sent()];
62
+ }
63
+ });
10
64
  });
11
- if (response.status === 401)
12
- throw new UnauthorizedError();
13
- if (!response.ok)
14
- throw new Error(`HTTP error! status: ${response.status}`);
15
- return await response.json();
16
- }
17
- }
65
+ };
66
+ return PublicKeyService;
67
+ }());
68
+ exports.PublicKeyService = PublicKeyService;
@@ -1,39 +1,102 @@
1
- import { UnauthorizedError, ForbiddenError } from '../errors';
2
- export class UsersService {
3
- constructor(apiClient) {
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.UsersService = void 0;
40
+ var errors_1 = require("../errors");
41
+ var UsersService = /** @class */ (function () {
42
+ function UsersService(apiClient) {
4
43
  this.apiClient = apiClient;
5
44
  this.baseUrl = 'https://api.kick.com/public/v1';
6
45
  }
7
- async introspectToken(token) {
8
- const response = await fetch(`${this.baseUrl}/token/introspect`, {
9
- method: 'POST',
10
- headers: {
11
- Authorization: `Bearer ${token}`
12
- }
46
+ UsersService.prototype.introspectToken = function (token) {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var response;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0: return [4 /*yield*/, fetch("".concat(this.baseUrl, "/token/introspect"), {
52
+ method: 'POST',
53
+ headers: {
54
+ Authorization: "Bearer ".concat(token)
55
+ }
56
+ })];
57
+ case 1:
58
+ response = _a.sent();
59
+ if (response.status === 401)
60
+ throw new errors_1.UnauthorizedError();
61
+ if (!response.ok)
62
+ throw new Error("HTTP error! status: ".concat(response.status));
63
+ return [4 /*yield*/, response.json()];
64
+ case 2: return [2 /*return*/, _a.sent()];
65
+ }
66
+ });
13
67
  });
14
- if (response.status === 401)
15
- throw new UnauthorizedError();
16
- if (!response.ok)
17
- throw new Error(`HTTP error! status: ${response.status}`);
18
- return await response.json();
19
- }
20
- async getUsers(userIds = [], token) {
21
- const url = new URL(`${this.baseUrl}/users`);
22
- if (userIds.length > 0) {
23
- userIds.forEach(id => url.searchParams.append('id[]', id));
24
- }
25
- const response = await fetch(url, {
26
- method: 'GET',
27
- headers: {
28
- Authorization: `Bearer ${token}`
29
- }
68
+ };
69
+ UsersService.prototype.getUsers = function () {
70
+ return __awaiter(this, arguments, void 0, function (userIds, token) {
71
+ var url, response;
72
+ if (userIds === void 0) { userIds = []; }
73
+ return __generator(this, function (_a) {
74
+ switch (_a.label) {
75
+ case 0:
76
+ url = new URL("".concat(this.baseUrl, "/users"));
77
+ if (userIds.length > 0) {
78
+ userIds.forEach(function (id) { return url.searchParams.append('id[]', id); });
79
+ }
80
+ return [4 /*yield*/, fetch(url, {
81
+ method: 'GET',
82
+ headers: {
83
+ Authorization: "Bearer ".concat(token)
84
+ }
85
+ })];
86
+ case 1:
87
+ response = _a.sent();
88
+ if (response.status === 401)
89
+ throw new errors_1.UnauthorizedError();
90
+ if (response.status === 403)
91
+ throw new errors_1.ForbiddenError();
92
+ if (!response.ok)
93
+ throw new Error("HTTP error! status: ".concat(response.status));
94
+ return [4 /*yield*/, response.json()];
95
+ case 2: return [2 /*return*/, _a.sent()];
96
+ }
97
+ });
30
98
  });
31
- if (response.status === 401)
32
- throw new UnauthorizedError();
33
- if (response.status === 403)
34
- throw new ForbiddenError();
35
- if (!response.ok)
36
- throw new Error(`HTTP error! status: ${response.status}`);
37
- return await response.json();
38
- }
39
- }
99
+ };
100
+ return UsersService;
101
+ }());
102
+ exports.UsersService = UsersService;
@@ -1,13 +1,17 @@
1
- export const WEBHOOK_TYPES = {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WEBHOOK_TYPES = void 0;
4
+ exports.parseWebhookPayload = parseWebhookPayload;
5
+ exports.WEBHOOK_TYPES = {
2
6
  CHAT_MESSAGE: 'chat.message.sent',
3
7
  CHANNEL_FOLLOWED: 'channel.followed',
4
8
  SUBSCRIPTION_RENEWAL: 'channel.subscription.renewal',
5
9
  SUBSCRIPTION_GIFTS: 'channel.subscription.gifts',
6
10
  SUBSCRIPTION_NEW: 'channel.subscription.new'
7
11
  };
8
- export function parseWebhookPayload(type, payload) {
12
+ function parseWebhookPayload(type, payload) {
9
13
  switch (type) {
10
- case WEBHOOK_TYPES.CHAT_MESSAGE:
14
+ case exports.WEBHOOK_TYPES.CHAT_MESSAGE:
11
15
  return {
12
16
  messageId: payload.message_id,
13
17
  broadcaster: {
@@ -28,13 +32,13 @@ export function parseWebhookPayload(type, payload) {
28
32
  },
29
33
  content: payload.content,
30
34
  emotes: Array.isArray(payload.emotes)
31
- ? payload.emotes.map((emote) => ({
35
+ ? payload.emotes.map(function (emote) { return ({
32
36
  emoteId: emote.emote_id,
33
37
  positions: emote.positions
34
- }))
38
+ }); })
35
39
  : null
36
40
  };
37
- case WEBHOOK_TYPES.CHANNEL_FOLLOWED:
41
+ case exports.WEBHOOK_TYPES.CHANNEL_FOLLOWED:
38
42
  return {
39
43
  broadcaster: {
40
44
  isAnonymous: payload.broadcaster.is_anonymous,
@@ -53,8 +57,8 @@ export function parseWebhookPayload(type, payload) {
53
57
  channelSlug: payload.follower.channel_slug
54
58
  }
55
59
  };
56
- case WEBHOOK_TYPES.SUBSCRIPTION_RENEWAL:
57
- case WEBHOOK_TYPES.SUBSCRIPTION_NEW:
60
+ case exports.WEBHOOK_TYPES.SUBSCRIPTION_RENEWAL:
61
+ case exports.WEBHOOK_TYPES.SUBSCRIPTION_NEW:
58
62
  return {
59
63
  broadcaster: {
60
64
  isAnonymous: payload.broadcaster.is_anonymous,
@@ -75,7 +79,7 @@ export function parseWebhookPayload(type, payload) {
75
79
  duration: payload.duration,
76
80
  createdAt: new Date(payload.created_at)
77
81
  };
78
- case WEBHOOK_TYPES.SUBSCRIPTION_GIFTS:
82
+ case exports.WEBHOOK_TYPES.SUBSCRIPTION_GIFTS:
79
83
  return {
80
84
  broadcaster: {
81
85
  isAnonymous: payload.broadcaster.is_anonymous,
@@ -95,14 +99,14 @@ export function parseWebhookPayload(type, payload) {
95
99
  profilePicture: payload.gifter.profile_picture,
96
100
  channelSlug: payload.gifter.channel_slug
97
101
  },
98
- giftees: payload.giftees.map((giftee) => ({
102
+ giftees: payload.giftees.map(function (giftee) { return ({
99
103
  isAnonymous: giftee.is_anonymous,
100
104
  userId: giftee.user_id,
101
105
  username: giftee.username,
102
106
  isVerified: giftee.is_verified,
103
107
  profilePicture: giftee.profile_picture,
104
108
  channelSlug: giftee.channel_slug
105
- })),
109
+ }); }),
106
110
  createdAt: new Date(payload.created_at)
107
111
  };
108
112
  default:
@@ -1,21 +1,29 @@
1
- import crypto from 'crypto';
2
- export class SignatureVerifier {
3
- constructor(publicKey) {
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SignatureVerifier = void 0;
7
+ var crypto_1 = __importDefault(require("crypto"));
8
+ var SignatureVerifier = /** @class */ (function () {
9
+ function SignatureVerifier(publicKey) {
4
10
  this.publicKey = publicKey;
5
11
  }
6
- createSignaturePayload(messageId, timestamp, body) {
7
- return `${messageId}.${timestamp}.${body}`;
8
- }
9
- verify(messageId, timestamp, body, signature) {
10
- const payload = this.createSignaturePayload(messageId, timestamp, body);
11
- const verifier = crypto.createVerify('RSA-SHA256');
12
+ SignatureVerifier.prototype.createSignaturePayload = function (messageId, timestamp, body) {
13
+ return "".concat(messageId, ".").concat(timestamp, ".").concat(body);
14
+ };
15
+ SignatureVerifier.prototype.verify = function (messageId, timestamp, body, signature) {
16
+ var payload = this.createSignaturePayload(messageId, timestamp, body);
17
+ var verifier = crypto_1.default.createVerify('RSA-SHA256');
12
18
  verifier.update(payload);
13
19
  try {
14
- const signatureBuffer = Buffer.from(signature, 'base64');
20
+ var signatureBuffer = Buffer.from(signature, 'base64');
15
21
  return verifier.verify(this.publicKey, signatureBuffer);
16
22
  }
17
23
  catch (error) {
18
24
  return false;
19
25
  }
20
- }
21
- }
26
+ };
27
+ return SignatureVerifier;
28
+ }());
29
+ exports.SignatureVerifier = SignatureVerifier;
@@ -1,59 +1,105 @@
1
- export class WebhookHandler {
2
- constructor(client) {
3
- this.client = client;
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
4
36
  }
5
- async handleWebhook(headers, rawBody) {
6
- const body = rawBody instanceof Buffer ? rawBody.toString('utf8') : rawBody;
7
- const payload = typeof body === 'string' ? JSON.parse(body) : body;
8
- const eventType = headers['kick-event-type'];
9
- const eventVersion = headers['kick-event-version'];
10
- switch (eventType) {
11
- case 'chat.message.sent':
12
- this.client.emit('chatMessage', {
13
- messageId: payload.message_id,
14
- broadcaster: payload.broadcaster,
15
- sender: payload.sender,
16
- content: payload.content,
17
- emotes: payload.emotes || []
18
- });
19
- break;
20
- case 'channel.followed':
21
- this.client.emit('channelFollowed', {
22
- broadcaster: payload.broadcaster,
23
- follower: payload.follower
24
- });
25
- break;
26
- case 'channel.subscription.renewal':
27
- this.client.emit('subscriptionRenewal', {
28
- broadcaster: payload.broadcaster,
29
- subscriber: payload.subscriber,
30
- duration: payload.duration,
31
- createdAt: payload.created_at
32
- });
33
- break;
34
- case 'channel.subscription.gifts':
35
- this.client.emit('subscriptionGifts', {
36
- broadcaster: payload.broadcaster,
37
- gifter: payload.gifter,
38
- giftees: payload.giftees,
39
- createdAt: payload.created_at
40
- });
41
- break;
42
- case 'channel.subscription.new':
43
- this.client.emit('subscriptionNew', {
44
- broadcaster: payload.broadcaster,
45
- subscriber: payload.subscriber,
46
- duration: payload.duration,
47
- createdAt: payload.created_at
48
- });
49
- break;
50
- default:
51
- this.client.emit('unknownEvent', { eventType, payload });
52
- }
53
- return {
54
- eventType,
55
- eventVersion,
56
- payload
57
- };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.WebhookHandler = void 0;
40
+ var WebhookHandler = /** @class */ (function () {
41
+ function WebhookHandler(client) {
42
+ this.client = client;
58
43
  }
59
- }
44
+ WebhookHandler.prototype.handleWebhook = function (headers, rawBody) {
45
+ return __awaiter(this, void 0, void 0, function () {
46
+ var body, payload, eventType, eventVersion;
47
+ return __generator(this, function (_a) {
48
+ body = rawBody instanceof Buffer ? rawBody.toString('utf8') : rawBody;
49
+ payload = typeof body === 'string' ? JSON.parse(body) : body;
50
+ eventType = headers['kick-event-type'];
51
+ eventVersion = headers['kick-event-version'];
52
+ switch (eventType) {
53
+ case 'chat.message.sent':
54
+ this.client.emit('chatMessage', {
55
+ messageId: payload.message_id,
56
+ broadcaster: payload.broadcaster,
57
+ sender: payload.sender,
58
+ content: payload.content,
59
+ emotes: payload.emotes || []
60
+ });
61
+ break;
62
+ case 'channel.followed':
63
+ this.client.emit('channelFollowed', {
64
+ broadcaster: payload.broadcaster,
65
+ follower: payload.follower
66
+ });
67
+ break;
68
+ case 'channel.subscription.renewal':
69
+ this.client.emit('subscriptionRenewal', {
70
+ broadcaster: payload.broadcaster,
71
+ subscriber: payload.subscriber,
72
+ duration: payload.duration,
73
+ createdAt: payload.created_at
74
+ });
75
+ break;
76
+ case 'channel.subscription.gifts':
77
+ this.client.emit('subscriptionGifts', {
78
+ broadcaster: payload.broadcaster,
79
+ gifter: payload.gifter,
80
+ giftees: payload.giftees,
81
+ createdAt: payload.created_at
82
+ });
83
+ break;
84
+ case 'channel.subscription.new':
85
+ this.client.emit('subscriptionNew', {
86
+ broadcaster: payload.broadcaster,
87
+ subscriber: payload.subscriber,
88
+ duration: payload.duration,
89
+ createdAt: payload.created_at
90
+ });
91
+ break;
92
+ default:
93
+ this.client.emit('unknownEvent', { eventType: eventType, payload: payload });
94
+ }
95
+ return [2 /*return*/, {
96
+ eventType: eventType,
97
+ eventVersion: eventVersion,
98
+ payload: payload
99
+ }];
100
+ });
101
+ });
102
+ };
103
+ return WebhookHandler;
104
+ }());
105
+ exports.WebhookHandler = WebhookHandler;
@@ -1,32 +1,87 @@
1
- import express from 'express';
2
- export class WebhookServer {
3
- constructor(client, options = {}) {
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.WebhookServer = void 0;
43
+ var express_1 = __importDefault(require("express"));
44
+ var WebhookServer = /** @class */ (function () {
45
+ function WebhookServer(client, options) {
46
+ if (options === void 0) { options = {}; }
4
47
  this.client = client;
5
48
  this.port = options.port || 3000;
6
49
  this.path = options.path || '/webhook';
7
- this.app = express();
50
+ this.app = (0, express_1.default)();
8
51
  this.server = null;
9
52
  }
10
- setup() {
11
- this.app.use(express.raw({ type: 'application/json' }));
12
- this.app.post(this.path, async (req, res) => {
13
- try {
14
- const result = await this.client.handleWebhookRequest(req.headers, req.body);
15
- res.json({ success: true, event: result.eventType });
16
- }
17
- catch (error) {
18
- res.status(400).json({ error: error.message });
19
- }
20
- });
21
- }
22
- start() {
23
- return new Promise((resolve, reject) => {
53
+ WebhookServer.prototype.setup = function () {
54
+ var _this = this;
55
+ this.app.use(express_1.default.raw({ type: 'application/json' }));
56
+ this.app.post(this.path, function (req, res) { return __awaiter(_this, void 0, void 0, function () {
57
+ var result, error_1;
58
+ return __generator(this, function (_a) {
59
+ switch (_a.label) {
60
+ case 0:
61
+ _a.trys.push([0, 2, , 3]);
62
+ return [4 /*yield*/, this.client.handleWebhookRequest(req.headers, req.body)];
63
+ case 1:
64
+ result = _a.sent();
65
+ res.json({ success: true, event: result.eventType });
66
+ return [3 /*break*/, 3];
67
+ case 2:
68
+ error_1 = _a.sent();
69
+ res.status(400).json({ error: error_1.message });
70
+ return [3 /*break*/, 3];
71
+ case 3: return [2 /*return*/];
72
+ }
73
+ });
74
+ }); });
75
+ };
76
+ WebhookServer.prototype.start = function () {
77
+ var _this = this;
78
+ return new Promise(function (resolve, reject) {
24
79
  try {
25
- this.setup();
26
- this.server = this.app.listen(this.port, () => {
27
- this.client.emit('webhookServerStarted', {
28
- port: this.port,
29
- path: this.path
80
+ _this.setup();
81
+ _this.server = _this.app.listen(_this.port, function () {
82
+ _this.client.emit('webhookServerStarted', {
83
+ port: _this.port,
84
+ path: _this.path
30
85
  });
31
86
  resolve();
32
87
  });
@@ -35,15 +90,16 @@ export class WebhookServer {
35
90
  reject(error);
36
91
  }
37
92
  });
38
- }
39
- stop() {
40
- return new Promise((resolve, reject) => {
41
- if (this.server) {
42
- this.server.close((err) => {
93
+ };
94
+ WebhookServer.prototype.stop = function () {
95
+ var _this = this;
96
+ return new Promise(function (resolve, reject) {
97
+ if (_this.server) {
98
+ _this.server.close(function (err) {
43
99
  if (err)
44
100
  reject(err);
45
101
  else {
46
- this.client.emit('webhookServerStopped');
102
+ _this.client.emit('webhookServerStopped');
47
103
  resolve();
48
104
  }
49
105
  });
@@ -52,5 +108,7 @@ export class WebhookServer {
52
108
  resolve();
53
109
  }
54
110
  });
55
- }
56
- }
111
+ };
112
+ return WebhookServer;
113
+ }());
114
+ exports.WebhookServer = WebhookServer;