@offnominal/ndb2-api-types 0.2.4 → 0.4.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.
@@ -2,4 +2,5 @@ import * as Entities from "./entities";
2
2
  import * as Endpoints from "./endpoints";
3
3
  import { Errors, NDB2APIError } from "./errors";
4
4
  import * as Utils from "./utils";
5
- export { Entities, Endpoints, Errors, NDB2APIError, Utils };
5
+ import * as Webhooks from "./webhooks";
6
+ export { Entities, Endpoints, Errors, NDB2APIError, Utils, Webhooks };
package/dist/v2/index.js CHANGED
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.Utils = exports.Errors = exports.Endpoints = exports.Entities = void 0;
36
+ exports.Webhooks = exports.Utils = exports.Errors = exports.Endpoints = exports.Entities = void 0;
37
37
  const Entities = __importStar(require("./entities"));
38
38
  exports.Entities = Entities;
39
39
  const Endpoints = __importStar(require("./endpoints"));
@@ -42,4 +42,6 @@ const errors_1 = require("./errors");
42
42
  Object.defineProperty(exports, "Errors", { enumerable: true, get: function () { return errors_1.Errors; } });
43
43
  const Utils = __importStar(require("./utils"));
44
44
  exports.Utils = Utils;
45
+ const Webhooks = __importStar(require("./webhooks"));
46
+ exports.Webhooks = Webhooks;
45
47
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAuC;AAK9B,4BAAQ;AAJjB,uDAAyC;AAItB,8BAAS;AAH5B,qCAAgD;AAGlB,uFAHrB,eAAM,OAGqB;AAFpC,+CAAiC;AAEmB,sBAAK"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/v2/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAuC;AAM9B,4BAAQ;AALjB,uDAAyC;AAKtB,8BAAS;AAJ5B,qCAAgD;AAIlB,uFAJrB,eAAM,OAIqB;AAHpC,+CAAiC;AAGmB,sBAAK;AAFzD,qDAAuC;AAEoB,4BAAQ"}
@@ -0,0 +1,20 @@
1
+ import { Prediction } from "./entities/predictions";
2
+ declare const WEBHOOK_EVENTS: readonly ["untriggered_prediction", "triggered_prediction"];
3
+ export type WebhookEvent = (typeof WEBHOOK_EVENTS)[number];
4
+ export type BasePayload<E extends WebhookEvent, D> = {
5
+ event_name: E;
6
+ version: 2;
7
+ date: Date;
8
+ data: D;
9
+ };
10
+ export declare namespace Events {
11
+ type UntriggeredPrediction = BasePayload<"untriggered_prediction", {
12
+ prediction: Prediction;
13
+ }>;
14
+ type TriggeredPrediction = BasePayload<"triggered_prediction", {
15
+ prediction: Prediction;
16
+ }>;
17
+ }
18
+ export type Payload = Events.UntriggeredPrediction | Events.TriggeredPrediction;
19
+ export declare const isWebhookPayloadV2: (payload: any) => payload is Payload;
20
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isWebhookPayloadV2 = void 0;
4
+ // Single source of truth for webhook events
5
+ const WEBHOOK_EVENTS = [
6
+ "untriggered_prediction",
7
+ "triggered_prediction",
8
+ ];
9
+ const isWebhookPayloadV2 = (payload) => {
10
+ if (!payload || typeof payload !== "object") {
11
+ return false;
12
+ }
13
+ // Validate event_name against WebhookEvent type
14
+ if (typeof payload.event_name !== "string" ||
15
+ !WEBHOOK_EVENTS.includes(payload.event_name)) {
16
+ return false;
17
+ }
18
+ // Validate version
19
+ if (payload.version !== 2) {
20
+ return false;
21
+ }
22
+ // Validate date (can be Date object or string that parses to valid date)
23
+ if (!payload.date) {
24
+ return false;
25
+ }
26
+ const date = payload.date instanceof Date ? payload.date : new Date(payload.date);
27
+ if (isNaN(date.getTime())) {
28
+ return false;
29
+ }
30
+ if (payload.data === undefined) {
31
+ return false;
32
+ }
33
+ return true;
34
+ };
35
+ exports.isWebhookPayloadV2 = isWebhookPayloadV2;
36
+ //# sourceMappingURL=webhooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../../src/v2/webhooks.ts"],"names":[],"mappings":";;;AAEA,4CAA4C;AAC5C,MAAM,cAAc,GAAG;IACrB,wBAAwB;IACxB,sBAAsB;CACd,CAAC;AA6BJ,MAAM,kBAAkB,GAAG,CAAC,OAAY,EAAsB,EAAE;IACrE,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAC5C,OAAO,KAAK,CAAC;IACf,CAAC;IAED,gDAAgD;IAChD,IACE,OAAO,OAAO,CAAC,UAAU,KAAK,QAAQ;QACtC,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,UAA0B,CAAC,EAC5D,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,mBAAmB;IACnB,IAAI,OAAO,CAAC,OAAO,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,yEAAyE;IACzE,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;QAClB,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,IAAI,GACR,OAAO,CAAC,IAAI,YAAY,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACvE,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAjCW,QAAA,kBAAkB,sBAiC7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@offnominal/ndb2-api-types",
3
- "version": "0.2.4",
3
+ "version": "0.4.0",
4
4
  "author": "Jake Robins",
5
5
  "types": "./src/v2/index.ts",
6
6
  "main": "./dist/v2/index.js",
package/src/v2/index.ts CHANGED
@@ -2,5 +2,6 @@ import * as Entities from "./entities";
2
2
  import * as Endpoints from "./endpoints";
3
3
  import { Errors, NDB2APIError } from "./errors";
4
4
  import * as Utils from "./utils";
5
+ import * as Webhooks from "./webhooks";
5
6
 
6
- export { Entities, Endpoints, Errors, NDB2APIError, Utils };
7
+ export { Entities, Endpoints, Errors, NDB2APIError, Utils, Webhooks };
@@ -0,0 +1,69 @@
1
+ import { Prediction } from "./entities/predictions";
2
+
3
+ // Single source of truth for webhook events
4
+ const WEBHOOK_EVENTS = [
5
+ "untriggered_prediction",
6
+ "triggered_prediction",
7
+ ] as const;
8
+
9
+ // Derive the type from the array
10
+ export type WebhookEvent = (typeof WEBHOOK_EVENTS)[number];
11
+
12
+ export type BasePayload<E extends WebhookEvent, D> = {
13
+ event_name: E;
14
+ version: 2;
15
+ date: Date;
16
+ data: D;
17
+ };
18
+
19
+ export namespace Events {
20
+ export type UntriggeredPrediction = BasePayload<
21
+ "untriggered_prediction",
22
+ {
23
+ prediction: Prediction;
24
+ }
25
+ >;
26
+ export type TriggeredPrediction = BasePayload<
27
+ "triggered_prediction",
28
+ {
29
+ prediction: Prediction;
30
+ }
31
+ >;
32
+ }
33
+
34
+ export type Payload = Events.UntriggeredPrediction | Events.TriggeredPrediction;
35
+
36
+ export const isWebhookPayloadV2 = (payload: any): payload is Payload => {
37
+ if (!payload || typeof payload !== "object") {
38
+ return false;
39
+ }
40
+
41
+ // Validate event_name against WebhookEvent type
42
+ if (
43
+ typeof payload.event_name !== "string" ||
44
+ !WEBHOOK_EVENTS.includes(payload.event_name as WebhookEvent)
45
+ ) {
46
+ return false;
47
+ }
48
+
49
+ // Validate version
50
+ if (payload.version !== 2) {
51
+ return false;
52
+ }
53
+
54
+ // Validate date (can be Date object or string that parses to valid date)
55
+ if (!payload.date) {
56
+ return false;
57
+ }
58
+ const date =
59
+ payload.date instanceof Date ? payload.date : new Date(payload.date);
60
+ if (isNaN(date.getTime())) {
61
+ return false;
62
+ }
63
+
64
+ if (payload.data === undefined) {
65
+ return false;
66
+ }
67
+
68
+ return true;
69
+ };