denotify-client 1.1.4 → 1.1.6

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.
package/dist/index.js CHANGED
@@ -11,10 +11,29 @@ var followRedirects = require('follow-redirects');
11
11
  var zlib = require('zlib');
12
12
  var stream = require('stream');
13
13
  var EventEmitter = require('events');
14
+ var yup = require('yup');
14
15
  var ethers = require('ethers');
15
16
 
16
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
18
 
19
+ function _interopNamespace(e) {
20
+ if (e && e.__esModule) return e;
21
+ var n = Object.create(null);
22
+ if (e) {
23
+ Object.keys(e).forEach(function (k) {
24
+ if (k !== 'default') {
25
+ var d = Object.getOwnPropertyDescriptor(e, k);
26
+ Object.defineProperty(n, k, d.get ? d : {
27
+ enumerable: true,
28
+ get: function () { return e[k]; }
29
+ });
30
+ }
31
+ });
32
+ }
33
+ n["default"] = e;
34
+ return Object.freeze(n);
35
+ }
36
+
18
37
  var FormData__default = /*#__PURE__*/_interopDefaultLegacy(FormData$1);
19
38
  var url__default = /*#__PURE__*/_interopDefaultLegacy(url);
20
39
  var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
@@ -24,6 +43,7 @@ var followRedirects__default = /*#__PURE__*/_interopDefaultLegacy(followRedirect
24
43
  var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib);
25
44
  var stream__default = /*#__PURE__*/_interopDefaultLegacy(stream);
26
45
  var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
46
+ var yup__namespace = /*#__PURE__*/_interopNamespace(yup);
27
47
 
28
48
  function bind(fn, thisArg) {
29
49
  return function wrap() {
@@ -4075,6 +4095,16 @@ class NotifyDiscordWebhook {
4075
4095
  notify: config
4076
4096
  };
4077
4097
  }
4098
+ static validateCreate(options) {
4099
+ const urlRegex = /^(https?|ftp):\/\/(-\.)?([^\s/?\.#]+\.?)+([^\s\.?#]+)?(\?\S*)?$/;
4100
+ const schema = yup__namespace.object({
4101
+ url: yup__namespace.string().matches(urlRegex, 'url is not a valid url').required(),
4102
+ username: yup__namespace.string(),
4103
+ avatar_url: yup__namespace.string().matches(urlRegex, 'url is not a valid url'),
4104
+ message: yup__namespace.string().required()
4105
+ });
4106
+ return schema.validate(options);
4107
+ }
4078
4108
  }
4079
4109
 
4080
4110
  class Notification {
@@ -4096,6 +4126,19 @@ class HandlerFunctionCall {
4096
4126
  handler: config
4097
4127
  };
4098
4128
  }
4129
+ static validateCreate(options) {
4130
+ const requiredWhenConditional = ([condition], schema) => condition === 'true' ? schema.notRequired() : schema.required();
4131
+ const schema = yup__namespace.object({
4132
+ address: yup__namespace.string().required(),
4133
+ abi: yup__namespace.array().required(),
4134
+ nBlocks: yup__namespace.number().min(10),
4135
+ condition: yup__namespace.string().oneOf(['>', '>=', '<', '<=', '=', 'true']),
4136
+ constant: yup__namespace.number().min(0).when('condition', requiredWhenConditional),
4137
+ responseArgIndex: yup__namespace.number().min(0).when('condition', requiredWhenConditional),
4138
+ responseArgDecimals: yup__namespace.number().min(0).when('condition', requiredWhenConditional),
4139
+ });
4140
+ return schema.validate(options);
4141
+ }
4099
4142
  }
4100
4143
 
4101
4144
  const HANDLER_ONCHAIN_EVENT_V1_RAW_ID = 'handler_onchain_event';
@@ -4109,6 +4152,219 @@ class HandlerOnchainEvent {
4109
4152
  handler: config
4110
4153
  };
4111
4154
  }
4155
+ static validateCreate(options) {
4156
+ const requiredWhenConditional = ([condition], schema) => condition === 'true' ? schema.notRequired() : schema.required();
4157
+ const onchainEventSchema = yup__namespace.object({
4158
+ address: yup__namespace.string().required(),
4159
+ event: yup__namespace.string().required(),
4160
+ abi: yup__namespace.array().required(),
4161
+ condition: yup__namespace.string().oneOf(['>', '>=', '<', '<=', '=', 'true']),
4162
+ constant: yup__namespace.number().min(0).when('condition', requiredWhenConditional),
4163
+ paramsIndex: yup__namespace.number().min(0).when('condition', requiredWhenConditional),
4164
+ paramsDecimals: yup__namespace.number().min(0).when('condition', requiredWhenConditional),
4165
+ });
4166
+ return onchainEventSchema.validate(options);
4167
+ }
4168
+ static validateUpdate(options) {
4169
+ }
4170
+ }
4171
+
4172
+ class FunctionBuilder {
4173
+ api;
4174
+ data = [];
4175
+ constructor(api) {
4176
+ this.api = api;
4177
+ }
4178
+ static create(api) {
4179
+ return new FunctionBuilder(api);
4180
+ }
4181
+ getAbiHash(abi) {
4182
+ if (this.api === undefined)
4183
+ throw new Error('FunctionBuilder must be initialised with api');
4184
+ return this.api.getAbiHash(abi);
4185
+ }
4186
+ async addFunction(address, func, args, abi) {
4187
+ const contract = new ethers.ethers.Contract(address, abi);
4188
+ contract.interface.encodeFunctionData(func, args);
4189
+ this.data.push({
4190
+ address,
4191
+ bytecode: contract.interface.encodeFunctionData(func, args),
4192
+ abiHash: await this.getAbiHash(abi),
4193
+ function: func,
4194
+ });
4195
+ return this;
4196
+ }
4197
+ get() {
4198
+ return this.data;
4199
+ }
4200
+ static schema() {
4201
+ return yup__namespace.array().of(yup__namespace.object({
4202
+ address: yup__namespace.string().required(),
4203
+ bytecode: yup__namespace.string().matches(/^(0x)?([0-9a-fA-F]{2})*$/).required(),
4204
+ abiHash: yup__namespace.string().matches(/^[A-Fa-f0-9]{64}/).required(),
4205
+ function: yup__namespace.string().required()
4206
+ }));
4207
+ }
4208
+ }
4209
+
4210
+ class Filter {
4211
+ static version() {
4212
+ return 'v1';
4213
+ }
4214
+ static execute(record, groupsIn, version = 'v1') {
4215
+ // Deep copy to so we can edit the object during recursion
4216
+ const groups = JSON.parse(JSON.stringify(groupsIn));
4217
+ let lhs = true;
4218
+ for (const group of groups) {
4219
+ const rhs = Filter.process(record, group.conditions);
4220
+ lhs = Filter.execLogic(lhs, group.logic, rhs);
4221
+ }
4222
+ return lhs;
4223
+ }
4224
+ static process(record, conditions, lhs) {
4225
+ const condition = conditions.shift();
4226
+ if (!condition) {
4227
+ if (lhs === undefined)
4228
+ return true;
4229
+ return lhs;
4230
+ }
4231
+ // lhs <logic> rhs
4232
+ const rhs = Filter.execCondition(record, condition);
4233
+ if (lhs === undefined || condition.logic === 'WHERE')
4234
+ return Filter.process(record, conditions, rhs);
4235
+ if (condition.logic === undefined)
4236
+ throw new Error('Invalid filter. Condition must have logic value');
4237
+ const next = Filter.execLogic(lhs, condition.logic, rhs);
4238
+ return Filter.process(record, conditions, next);
4239
+ }
4240
+ static execLogic(lhs, logic, rhs) {
4241
+ switch (logic) {
4242
+ case 'AND': return lhs && rhs;
4243
+ case 'OR': return lhs || rhs;
4244
+ case 'XOR': return (lhs || rhs) && !(lhs && rhs);
4245
+ case 'NAND': return !(lhs && rhs);
4246
+ case 'NOR': return !(lhs && rhs);
4247
+ case 'WHERE': return rhs;
4248
+ default: throw new Error(`Invalid Filter. Bad logic value: ${logic}`);
4249
+ }
4250
+ }
4251
+ static execCondition(record, condition) {
4252
+ const data = record[condition.key];
4253
+ if (condition.type === 'Number') {
4254
+ if (typeof condition.constant !== 'number' || typeof data !== 'number')
4255
+ throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
4256
+ const n = data;
4257
+ const constant = condition.constant;
4258
+ switch (condition.operation) {
4259
+ case 'eq': return n === constant;
4260
+ case '!eq': return n !== constant;
4261
+ case 'gt': return n > constant;
4262
+ case 'gte': return n >= constant;
4263
+ case 'lt': return n < constant;
4264
+ case 'lte': return n <= constant;
4265
+ default: throw new Error('Invalid Filter. Operation does not match type');
4266
+ }
4267
+ }
4268
+ if (condition.type === 'String') {
4269
+ if (typeof condition.constant !== 'string' || typeof data !== 'string')
4270
+ throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
4271
+ const str = data;
4272
+ const constant = condition.constant;
4273
+ switch (condition.operation) {
4274
+ case 'contains': return str.includes(constant);
4275
+ case '!contains': return !str.includes(constant);
4276
+ case 'is': return str === constant;
4277
+ case '!is': return str !== constant;
4278
+ case 'isEmpty': return str === '';
4279
+ case '!isEmpty': return str !== '';
4280
+ default: throw new Error('Invalid Filter. Operation does not match type');
4281
+ }
4282
+ }
4283
+ if (condition.type === 'Address') {
4284
+ if (typeof condition.constant !== 'string' || typeof data !== 'string')
4285
+ throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
4286
+ const str = data;
4287
+ const constant = condition.constant;
4288
+ switch (condition.operation) {
4289
+ case 'contains': return str.toLowerCase().includes(constant.toLowerCase());
4290
+ case '!contains': return !str.toLowerCase().includes(constant.toLowerCase());
4291
+ case 'is': return str.toLowerCase() === constant.toLowerCase();
4292
+ case '!is': return str.toLowerCase() !== constant.toLowerCase();
4293
+ case 'isEmpty': return str === '';
4294
+ case '!isEmpty': return str !== '';
4295
+ default: throw new Error('Invalid Filter. Operation does not match type');
4296
+ }
4297
+ }
4298
+ throw new Error('Invalid Filter. Unknown Type');
4299
+ }
4300
+ }
4301
+ class FilterBuilder {
4302
+ groups = [];
4303
+ constructor() {
4304
+ this.newGroup('WHERE');
4305
+ }
4306
+ static version() {
4307
+ return Filter.version();
4308
+ }
4309
+ static new() {
4310
+ return new FilterBuilder();
4311
+ }
4312
+ newGroup(logic) {
4313
+ if (this.groups.length !== 0 && logic === 'WHERE')
4314
+ throw new Error('Only the first groups can start with "WHERE"');
4315
+ this.groups.push({ logic: 'WHERE', conditions: [] });
4316
+ return this;
4317
+ }
4318
+ addCondition(logic, key, type, operation, constant) {
4319
+ const group = this.groups[this.groups.length - 1];
4320
+ if (group.conditions.length === 0 && logic !== 'WHERE')
4321
+ throw new Error('Logic for the first condition of a group must be "WHERE"');
4322
+ if (group.conditions.length > 0 && logic === 'WHERE')
4323
+ throw new Error('Only the first condition of a group can be "WHERE"');
4324
+ group.conditions.push({
4325
+ logic,
4326
+ key,
4327
+ type,
4328
+ operation,
4329
+ constant
4330
+ });
4331
+ return this;
4332
+ }
4333
+ finalise() {
4334
+ for (const group of this.groups) {
4335
+ if (group.conditions.length === 0)
4336
+ throw new Error('Bad Filter. All Groups must have atleast one condition');
4337
+ }
4338
+ return this.groups;
4339
+ }
4340
+ static schema() {
4341
+ const logic = yup__namespace.string().oneOf(['AND', 'OR', 'XOR', 'NAND', 'NOR', 'WHERE']).required();
4342
+ const condition = yup__namespace.object({
4343
+ logic,
4344
+ key: yup__namespace.string().required(),
4345
+ type: yup__namespace.string().oneOf(['String', 'Address', 'Number']).required(),
4346
+ operation: yup__namespace.string().when('type', ([type], schema) => {
4347
+ switch (type) {
4348
+ case 'String': return schema.oneOf(['contains', '!contains', 'is', '!is', 'isEmpty', '!isEmpty']);
4349
+ case 'Address': return schema.oneOf(['is', '!is', 'isEmpty', '!isEmpty']);
4350
+ case 'Number': return schema.oneOf(['String', 'Address', 'Number']);
4351
+ default: throw new Error('Invalid Filter Data Type');
4352
+ }
4353
+ }),
4354
+ constant: yup__namespace.mixed().when('type', ([type]) => {
4355
+ switch (type) {
4356
+ case 'String': return yup__namespace.string();
4357
+ case 'Address': return yup__namespace.string();
4358
+ case 'Number': return yup__namespace.number();
4359
+ default: throw new Error('Invalid Filter Data Type');
4360
+ }
4361
+ }),
4362
+ });
4363
+ return yup__namespace.array().of(yup__namespace.object({
4364
+ logic,
4365
+ conditions: yup__namespace.array().of(condition)
4366
+ }));
4367
+ }
4112
4368
  }
4113
4369
 
4114
4370
  const HANDLER_FUNCTION_CALL_V2_RAW_ID = 'handler_function_call_v2';
@@ -4122,6 +4378,32 @@ class HandlerFunctionCallV2 {
4122
4378
  handler: config
4123
4379
  };
4124
4380
  }
4381
+ static validateCreate(options) {
4382
+ const timePeriodRegex = /^(\d+)([SMHD])$/i;
4383
+ const onchainEventSchema = yup__namespace.object({
4384
+ timeBase: yup__namespace.string().oneOf(['blocks', 'time']).required(),
4385
+ // Blocks config
4386
+ nBlocks: yup__namespace.number()
4387
+ .min(10)
4388
+ .when('timeBase', ([timeBase], schema) => timeBase === 'blocks' ? schema.required() : schema),
4389
+ // Or Time config
4390
+ timePeriod: yup__namespace.string()
4391
+ .matches(timePeriodRegex, 'timePeriod must be of the format n[s|m|h|d], eg 10s for 10 seconds')
4392
+ .when('timeBase', ([timeBase], schema) => timeBase === 'time' ? schema.required() : schema),
4393
+ startTime: yup__namespace.number().default(0),
4394
+ // Debouncing. Default is 0
4395
+ debounceCount: yup__namespace.number(),
4396
+ // Functions
4397
+ functions: FunctionBuilder.schema(),
4398
+ // Trigger
4399
+ triggerOn: yup__namespace.string().oneOf(['always', 'filter']).default('always'),
4400
+ latch: yup__namespace.boolean().default(false),
4401
+ // Filter
4402
+ filterVersion: yup__namespace.string().when('triggerOn', ([triggerOn], schema) => triggerOn === 'filter' ? schema.required() : schema),
4403
+ filter: FilterBuilder.schema().when('triggerOn', ([triggerOn], schema) => triggerOn === 'filter' ? schema.required() : schema),
4404
+ });
4405
+ return onchainEventSchema.validate(options);
4406
+ }
4125
4407
  }
4126
4408
 
4127
4409
  class Trigger {
@@ -4129,7 +4411,7 @@ class Trigger {
4129
4411
  switch (id) {
4130
4412
  case 'PollFunctionV2': return HandlerFunctionCallV2.SimpleToRaw(name, network, config);
4131
4413
  case 'PollFunctionV1': return HandlerFunctionCall.SimpleToRaw(name, network, config);
4132
- case 'OnchainEventV2': return HandlerOnchainEvent.SimpleToRaw(name, network, config);
4414
+ case 'OnchainEventV1': return HandlerOnchainEvent.SimpleToRaw(name, network, config);
4133
4415
  default:
4134
4416
  throw new Error('Invalid Trigger ID');
4135
4417
  }
@@ -4181,10 +4463,19 @@ class DeNotifyClient {
4181
4463
  throw new Error('Authentication Required. DeNotify supports either username/password or API key authentication');
4182
4464
  }
4183
4465
  }
4184
- async alertHistory(id, pagination = {}) {
4185
- const alerts = await this.request('get', `alert-history${id ? '/' + id : ''}`, { params: pagination });
4186
- return alerts;
4466
+ async alertHistory(id, pagination) {
4467
+ const url = `alert-history`;
4468
+ const params = pagination ? pagination : {};
4469
+ if (id)
4470
+ params.id = id;
4471
+ if (Object.keys(params).length > 0) {
4472
+ return await this.request('get', url, { params: pagination });
4473
+ }
4474
+ else {
4475
+ return await this.request('get', url);
4476
+ }
4187
4477
  }
4478
+ // TODO - Beutify the reponse
4188
4479
  async getAlert(id) {
4189
4480
  const alerts = await this.request('get', `alerts/${id}`);
4190
4481
  return alerts[0];
@@ -4211,6 +4502,7 @@ class DeNotifyClient {
4211
4502
  url.searchParams.append(param, options.params[param]);
4212
4503
  }
4213
4504
  }
4505
+ console.log(url.toString());
4214
4506
  const payload = {
4215
4507
  method,
4216
4508
  url: url.toString(),
@@ -4265,6 +4557,13 @@ class AlertBuilder {
4265
4557
  this.network = network;
4266
4558
  return this;
4267
4559
  }
4560
+ /**
4561
+ * Call withTrigger with one of the TriggerConfig types:
4562
+ * PollFunctionV2 | PollFunctionV1 | OnchainEventV1
4563
+ * @param id Simple ID
4564
+ * @param options Desired trigger configuration
4565
+ * @returns self for piping
4566
+ */
4268
4567
  withTrigger(id, options) {
4269
4568
  this.triggerId = id;
4270
4569
  this.trigger = options;
@@ -4275,13 +4574,25 @@ class AlertBuilder {
4275
4574
  this.notification = options;
4276
4575
  return this;
4277
4576
  }
4278
- config() {
4577
+ async validate() {
4578
+ // Validate trigger
4579
+ switch (this.triggerId) {
4580
+ case 'OnchainEventV1': return HandlerOnchainEvent.validateCreate(this.trigger);
4581
+ case 'PollFunctionV1': return HandlerFunctionCall.validateCreate(this.trigger);
4582
+ case 'PollFunctionV2': return HandlerFunctionCallV2.validateCreate(this.trigger);
4583
+ }
4584
+ switch (this.notificationId) {
4585
+ case 'Discord': return NotifyDiscordWebhook.validateCreate(this.notification);
4586
+ }
4587
+ }
4588
+ async config() {
4279
4589
  if (this.trigger === undefined || this.triggerId === undefined)
4280
4590
  throw new Error('Trigger not configured');
4281
4591
  if (this.notification === undefined || this.notificationId === undefined)
4282
4592
  throw new Error('Notification not configured');
4283
4593
  if (this.network === undefined)
4284
4594
  throw new Error('Network not configured');
4595
+ await this.validate();
4285
4596
  return {
4286
4597
  name: this.name,
4287
4598
  network: this.network,
@@ -4293,168 +4604,6 @@ class AlertBuilder {
4293
4604
  }
4294
4605
  }
4295
4606
 
4296
- class FunctionBuilder {
4297
- api;
4298
- data = [];
4299
- constructor(api) {
4300
- this.api = api;
4301
- }
4302
- static create(api) {
4303
- return new FunctionBuilder(api);
4304
- }
4305
- getAbiHash(abi) {
4306
- if (this.api === undefined)
4307
- throw new Error('FunctionBuilder must be initialised with api');
4308
- return this.api.getAbiHash(abi);
4309
- }
4310
- async addFunction(address, func, args, abi) {
4311
- const contract = new ethers.ethers.Contract(address, abi);
4312
- contract.interface.encodeFunctionData(func, args);
4313
- this.data.push({
4314
- address,
4315
- bytecode: contract.interface.encodeFunctionData(func, args),
4316
- abiHash: await this.getAbiHash(abi),
4317
- function: func,
4318
- });
4319
- return this;
4320
- }
4321
- get() {
4322
- return this.data;
4323
- }
4324
- }
4325
-
4326
- class Filter {
4327
- static version() {
4328
- return 'v1';
4329
- }
4330
- static execute(record, groupsIn, version = 'v1') {
4331
- // Deep copy to so we can edit the object during recursion
4332
- const groups = JSON.parse(JSON.stringify(groupsIn));
4333
- let lhs = true;
4334
- for (const group of groups) {
4335
- const rhs = Filter.process(record, group.conditions);
4336
- lhs = Filter.execLogic(lhs, group.logic, rhs);
4337
- }
4338
- return lhs;
4339
- }
4340
- static process(record, conditions, lhs) {
4341
- const condition = conditions.shift();
4342
- if (!condition) {
4343
- if (lhs === undefined)
4344
- return true;
4345
- return lhs;
4346
- }
4347
- // lhs <logic> rhs
4348
- const rhs = Filter.execCondition(record, condition);
4349
- if (lhs === undefined || condition.logic === 'WHERE')
4350
- return Filter.process(record, conditions, rhs);
4351
- if (condition.logic === undefined)
4352
- throw new Error('Invalid filter. Condition must have logic value');
4353
- const next = Filter.execLogic(lhs, condition.logic, rhs);
4354
- return Filter.process(record, conditions, next);
4355
- }
4356
- static execLogic(lhs, logic, rhs) {
4357
- switch (logic) {
4358
- case 'AND': return lhs && rhs;
4359
- case 'OR': return lhs || rhs;
4360
- case 'XOR': return (lhs || rhs) && !(lhs && rhs);
4361
- case 'NAND': return !(lhs && rhs);
4362
- case 'NOR': return !(lhs && rhs);
4363
- case 'WHERE': return rhs;
4364
- default: throw new Error(`Invalid Filter. Bad logic value: ${logic}`);
4365
- }
4366
- }
4367
- static execCondition(record, condition) {
4368
- const data = record[condition.key];
4369
- if (condition.type === 'Number') {
4370
- if (typeof condition.constant !== 'number' || typeof data !== 'number')
4371
- throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
4372
- const n = data;
4373
- const constant = condition.constant;
4374
- switch (condition.operation) {
4375
- case 'eq': return n === constant;
4376
- case '!eq': return n !== constant;
4377
- case 'gt': return n > constant;
4378
- case 'gte': return n >= constant;
4379
- case 'lt': return n < constant;
4380
- case 'lte': return n <= constant;
4381
- default: throw new Error('Invalid Filter. Operation does not match type');
4382
- }
4383
- }
4384
- if (condition.type === 'String') {
4385
- if (typeof condition.constant !== 'string' || typeof data !== 'string')
4386
- throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
4387
- const str = data;
4388
- const constant = condition.constant;
4389
- switch (condition.operation) {
4390
- case 'contains': return str.includes(constant);
4391
- case '!contains': return !str.includes(constant);
4392
- case 'is': return str === constant;
4393
- case '!is': return str !== constant;
4394
- case 'isEmpty': return str === '';
4395
- case '!isEmpty': return str !== '';
4396
- default: throw new Error('Invalid Filter. Operation does not match type');
4397
- }
4398
- }
4399
- if (condition.type === 'Address') {
4400
- if (typeof condition.constant !== 'string' || typeof data !== 'string')
4401
- throw new Error(`Invalid filter. Type miss-match. Type: ${condition.type}, Variable: ${condition.constant}, Data: ${data}`);
4402
- const str = data;
4403
- const constant = condition.constant;
4404
- switch (condition.operation) {
4405
- case 'contains': return str.toLowerCase().includes(constant.toLowerCase());
4406
- case '!contains': return !str.toLowerCase().includes(constant.toLowerCase());
4407
- case 'is': return str.toLowerCase() === constant.toLowerCase();
4408
- case '!is': return str.toLowerCase() !== constant.toLowerCase();
4409
- case 'isEmpty': return str === '';
4410
- case '!isEmpty': return str !== '';
4411
- default: throw new Error('Invalid Filter. Operation does not match type');
4412
- }
4413
- }
4414
- throw new Error('Invalid Filter. Unknown Type');
4415
- }
4416
- }
4417
- class FilterBuilder {
4418
- groups = [];
4419
- constructor() {
4420
- this.newGroup('WHERE');
4421
- }
4422
- static version() {
4423
- return Filter.version();
4424
- }
4425
- static new() {
4426
- return new FilterBuilder();
4427
- }
4428
- newGroup(logic) {
4429
- if (this.groups.length !== 0 && logic === 'WHERE')
4430
- throw new Error('Only the first groups can start with "WHERE"');
4431
- this.groups.push({ logic: 'WHERE', conditions: [] });
4432
- return this;
4433
- }
4434
- addCondition(logic, key, type, operation, constant) {
4435
- const group = this.groups[this.groups.length - 1];
4436
- if (group.conditions.length === 0 && logic !== 'WHERE')
4437
- throw new Error('Logic for the first condition of a group must be "WHERE"');
4438
- if (group.conditions.length > 0 && logic === 'WHERE')
4439
- throw new Error('Only the first condition of a group can be "WHERE"');
4440
- group.conditions.push({
4441
- logic,
4442
- key,
4443
- type,
4444
- operation,
4445
- constant
4446
- });
4447
- return this;
4448
- }
4449
- finalise() {
4450
- for (const group of this.groups) {
4451
- if (group.conditions.length === 0)
4452
- throw new Error('Bad Filter. All Groups must have atleast one condition');
4453
- }
4454
- return this.groups;
4455
- }
4456
- }
4457
-
4458
4607
  var index$1 = /*#__PURE__*/Object.freeze({
4459
4608
  __proto__: null,
4460
4609
  Trigger: Trigger,