@nymphjs/nymph 1.0.0-beta.54 → 1.0.0-beta.56

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/src/Nymph.ts CHANGED
@@ -205,13 +205,17 @@ export default class Nymph {
205
205
  }
206
206
  }
207
207
 
208
+ this.config.debugLog('nymph', 'Nymph loaded.');
209
+
208
210
  if (tilmeld != null) {
209
211
  this.tilmeld = tilmeld;
210
212
  }
211
213
 
212
214
  this.driver.init(this);
213
215
  if (this.tilmeld) {
216
+ this.config.debugLog('nymph', 'Tilmeld initializing.');
214
217
  this.tilmeld.init(this);
218
+ this.config.debugLog('nymph', 'Tilmeld initialized.');
215
219
  }
216
220
  }
217
221
 
@@ -235,6 +239,7 @@ export default class Nymph {
235
239
  }
236
240
  }
237
241
  this.entityClasses[entityClass.class] = NymphEntity;
242
+ this.config.debugLog('nymph', `Added entity class "${entityClass.class}".`);
238
243
  return NymphEntity;
239
244
  }
240
245
 
@@ -255,7 +260,8 @@ export default class Nymph {
255
260
  if (key in this.entityClasses) {
256
261
  return this.entityClasses[key];
257
262
  }
258
- throw new ClassNotAvailableError('Tried to use class: ' + key);
263
+ this.config.debugError('nymph', `Tried to use missing class "${key}".`);
264
+ throw new ClassNotAvailableError(`Tried to use missing class "${key}".`);
259
265
  }
260
266
 
261
267
  /**
@@ -278,13 +284,19 @@ export default class Nymph {
278
284
  * @returns Whether the instance is connected to the database.
279
285
  */
280
286
  public async connect(): Promise<boolean> {
281
- const result = this.driver.connect();
282
- for (let callback of this.connectCallbacks) {
283
- if (callback) {
284
- await callback(this, result);
287
+ try {
288
+ const result = this.driver.connect();
289
+ for (let callback of this.connectCallbacks) {
290
+ if (callback) {
291
+ await callback(this, result);
292
+ }
285
293
  }
294
+ this.config.debugLog('nymph', 'Driver connected.');
295
+ return await result;
296
+ } catch (e: any) {
297
+ this.config.debugError('nymph', `Failed to connect: ${e}`);
298
+ throw e;
286
299
  }
287
- return await result;
288
300
  }
289
301
 
290
302
  /**
@@ -293,13 +305,19 @@ export default class Nymph {
293
305
  * @returns Whether the instance is connected to the database.
294
306
  */
295
307
  public async disconnect(): Promise<boolean> {
296
- const result = this.driver.disconnect();
297
- for (let callback of this.disconnectCallbacks) {
298
- if (callback) {
299
- await callback(this, result);
308
+ try {
309
+ const result = this.driver.disconnect();
310
+ for (let callback of this.disconnectCallbacks) {
311
+ if (callback) {
312
+ await callback(this, result);
313
+ }
300
314
  }
315
+ this.config.debugLog('nymph', 'Driver disconnected.');
316
+ return await result;
317
+ } catch (e: any) {
318
+ this.config.debugError('nymph', `Failed to disconnect: ${e}`);
319
+ throw e;
301
320
  }
302
- return await result;
303
321
  }
304
322
 
305
323
  /**
@@ -326,18 +344,24 @@ export default class Nymph {
326
344
  * @returns A new instance of Nymph that should be used for the transaction.
327
345
  */
328
346
  public async startTransaction(name: string): Promise<Nymph> {
329
- for (let callback of this.beforeStartTransactionCallbacks) {
330
- if (callback) {
331
- await callback(this, name);
347
+ try {
348
+ for (let callback of this.beforeStartTransactionCallbacks) {
349
+ if (callback) {
350
+ await callback(this, name);
351
+ }
332
352
  }
333
- }
334
- const result = await this.driver.startTransaction(name);
335
- for (let callback of this.afterStartTransactionCallbacks) {
336
- if (callback) {
337
- await callback(this, name, result);
353
+ this.config.debugLog('nymph', `Starting transaction "${name}".`);
354
+ const result = await this.driver.startTransaction(name);
355
+ for (let callback of this.afterStartTransactionCallbacks) {
356
+ if (callback) {
357
+ await callback(this, name, result);
358
+ }
338
359
  }
360
+ return result;
361
+ } catch (e: any) {
362
+ this.config.debugError('nymph', `Failed to start transaction: ${e}`);
363
+ throw e;
339
364
  }
340
- return result;
341
365
  }
342
366
 
343
367
  /**
@@ -348,18 +372,24 @@ export default class Nymph {
348
372
  * @returns True on success, false on failure.
349
373
  */
350
374
  public async commit(name: string): Promise<boolean> {
351
- for (let callback of this.beforeCommitTransactionCallbacks) {
352
- if (callback) {
353
- await callback(this, name);
375
+ try {
376
+ for (let callback of this.beforeCommitTransactionCallbacks) {
377
+ if (callback) {
378
+ await callback(this, name);
379
+ }
354
380
  }
355
- }
356
- const result = await this.driver.commit(name);
357
- for (let callback of this.afterCommitTransactionCallbacks) {
358
- if (callback) {
359
- await callback(this, name, result);
381
+ this.config.debugLog('nymph', `Committing transaction "${name}".`);
382
+ const result = await this.driver.commit(name);
383
+ for (let callback of this.afterCommitTransactionCallbacks) {
384
+ if (callback) {
385
+ await callback(this, name, result);
386
+ }
360
387
  }
388
+ return result;
389
+ } catch (e: any) {
390
+ this.config.debugError('nymph', `Failed to commit transaction: ${e}`);
391
+ throw e;
361
392
  }
362
- return result;
363
393
  }
364
394
 
365
395
  /**
@@ -370,18 +400,24 @@ export default class Nymph {
370
400
  * @returns True on success, false on failure.
371
401
  */
372
402
  public async rollback(name: string): Promise<boolean> {
373
- for (let callback of this.beforeRollbackTransactionCallbacks) {
374
- if (callback) {
375
- await callback(this, name);
403
+ try {
404
+ for (let callback of this.beforeRollbackTransactionCallbacks) {
405
+ if (callback) {
406
+ await callback(this, name);
407
+ }
376
408
  }
377
- }
378
- const result = await this.driver.rollback(name);
379
- for (let callback of this.afterRollbackTransactionCallbacks) {
380
- if (callback) {
381
- await callback(this, name, result);
409
+ this.config.debugLog('nymph', `Rolling back transaction "${name}".`);
410
+ const result = await this.driver.rollback(name);
411
+ for (let callback of this.afterRollbackTransactionCallbacks) {
412
+ if (callback) {
413
+ await callback(this, name, result);
414
+ }
382
415
  }
416
+ return result;
417
+ } catch (e: any) {
418
+ this.config.debugError('nymph', `Failed to roll back transaction: ${e}`);
419
+ throw e;
383
420
  }
384
- return result;
385
421
  }
386
422
 
387
423
  /**
@@ -415,28 +451,33 @@ export default class Nymph {
415
451
  * @returns The UID's new value, or null on failure.
416
452
  */
417
453
  public async newUID(name: string): Promise<number | null> {
418
- for (let callback of this.beforeNewUIDCallbacks) {
419
- if (callback) {
420
- await callback(this, name);
421
- }
422
- }
423
- let result: Promise<number | null>;
424
454
  try {
425
- result = this.driver.newUID(name);
426
- } catch (e: any) {
427
- for (let callback of this.failedNewUIDCallbacks) {
455
+ for (let callback of this.beforeNewUIDCallbacks) {
428
456
  if (callback) {
429
- await callback(this, e);
457
+ await callback(this, name);
430
458
  }
431
459
  }
432
- throw e;
433
- }
434
- for (let callback of this.afterNewUIDCallbacks) {
435
- if (callback) {
436
- await callback(this, result);
460
+ let result: Promise<number | null>;
461
+ try {
462
+ result = this.driver.newUID(name);
463
+ } catch (e: any) {
464
+ for (let callback of this.failedNewUIDCallbacks) {
465
+ if (callback) {
466
+ await callback(this, e);
467
+ }
468
+ }
469
+ throw e;
470
+ }
471
+ for (let callback of this.afterNewUIDCallbacks) {
472
+ if (callback) {
473
+ await callback(this, result);
474
+ }
437
475
  }
476
+ return await result;
477
+ } catch (e: any) {
478
+ this.config.debugError('nymph', `Failed to create UID "${name}": ${e}`);
479
+ throw e;
438
480
  }
439
- return await result;
440
481
  }
441
482
 
442
483
  /**
@@ -445,7 +486,12 @@ export default class Nymph {
445
486
  * @returns The UID's value, or null on failure and if it doesn't exist.
446
487
  */
447
488
  public async getUID(name: string): Promise<number | null> {
448
- return await this.driver.getUID(name);
489
+ try {
490
+ return await this.driver.getUID(name);
491
+ } catch (e: any) {
492
+ this.config.debugError('nymph', `Failed to get UID "${name}": ${e}`);
493
+ throw e;
494
+ }
449
495
  }
450
496
 
451
497
  /**
@@ -456,28 +502,33 @@ export default class Nymph {
456
502
  * @returns True on success, false on failure.
457
503
  */
458
504
  public async setUID(name: string, value: number): Promise<boolean> {
459
- for (let callback of this.beforeSetUIDCallbacks) {
460
- if (callback) {
461
- await callback(this, name, value);
462
- }
463
- }
464
- let result: Promise<boolean>;
465
505
  try {
466
- result = this.driver.setUID(name, value);
467
- } catch (e: any) {
468
- for (let callback of this.failedSetUIDCallbacks) {
506
+ for (let callback of this.beforeSetUIDCallbacks) {
469
507
  if (callback) {
470
- await callback(this, e);
508
+ await callback(this, name, value);
471
509
  }
472
510
  }
473
- throw e;
474
- }
475
- for (let callback of this.afterSetUIDCallbacks) {
476
- if (callback) {
477
- await callback(this, result);
511
+ let result: Promise<boolean>;
512
+ try {
513
+ result = this.driver.setUID(name, value);
514
+ } catch (e: any) {
515
+ for (let callback of this.failedSetUIDCallbacks) {
516
+ if (callback) {
517
+ await callback(this, e);
518
+ }
519
+ }
520
+ throw e;
478
521
  }
522
+ for (let callback of this.afterSetUIDCallbacks) {
523
+ if (callback) {
524
+ await callback(this, result);
525
+ }
526
+ }
527
+ return await result;
528
+ } catch (e: any) {
529
+ this.config.debugError('nymph', `Failed to set UID "${name}": ${e}`);
530
+ throw e;
479
531
  }
480
- return await result;
481
532
  }
482
533
 
483
534
  /**
@@ -487,28 +538,33 @@ export default class Nymph {
487
538
  * @returns True on success, false on failure.
488
539
  */
489
540
  public async deleteUID(name: string): Promise<boolean> {
490
- for (let callback of this.beforeDeleteUIDCallbacks) {
491
- if (callback) {
492
- await callback(this, name);
493
- }
494
- }
495
- let result: Promise<boolean>;
496
541
  try {
497
- result = this.driver.deleteUID(name);
498
- } catch (e: any) {
499
- for (let callback of this.failedDeleteUIDCallbacks) {
542
+ for (let callback of this.beforeDeleteUIDCallbacks) {
500
543
  if (callback) {
501
- await callback(this, e);
544
+ await callback(this, name);
502
545
  }
503
546
  }
504
- throw e;
505
- }
506
- for (let callback of this.afterDeleteUIDCallbacks) {
507
- if (callback) {
508
- await callback(this, result);
547
+ let result: Promise<boolean>;
548
+ try {
549
+ result = this.driver.deleteUID(name);
550
+ } catch (e: any) {
551
+ for (let callback of this.failedDeleteUIDCallbacks) {
552
+ if (callback) {
553
+ await callback(this, e);
554
+ }
555
+ }
556
+ throw e;
509
557
  }
558
+ for (let callback of this.afterDeleteUIDCallbacks) {
559
+ if (callback) {
560
+ await callback(this, result);
561
+ }
562
+ }
563
+ return await result;
564
+ } catch (e: any) {
565
+ this.config.debugError('nymph', `Failed to delete UID "${name}": ${e}`);
566
+ throw e;
510
567
  }
511
- return await result;
512
568
  }
513
569
 
514
570
  /**
@@ -519,28 +575,36 @@ export default class Nymph {
519
575
  * @returns True on success, false on failure.
520
576
  */
521
577
  public async renameUID(oldName: string, newName: string): Promise<boolean> {
522
- for (let callback of this.beforeRenameUIDCallbacks) {
523
- if (callback) {
524
- await callback(this, oldName, newName);
525
- }
526
- }
527
- let result: Promise<boolean>;
528
578
  try {
529
- result = this.driver.renameUID(oldName, newName);
530
- } catch (e: any) {
531
- for (let callback of this.failedRenameUIDCallbacks) {
579
+ for (let callback of this.beforeRenameUIDCallbacks) {
532
580
  if (callback) {
533
- await callback(this, e);
581
+ await callback(this, oldName, newName);
534
582
  }
535
583
  }
536
- throw e;
537
- }
538
- for (let callback of this.afterRenameUIDCallbacks) {
539
- if (callback) {
540
- await callback(this, result);
584
+ let result: Promise<boolean>;
585
+ try {
586
+ result = this.driver.renameUID(oldName, newName);
587
+ } catch (e: any) {
588
+ for (let callback of this.failedRenameUIDCallbacks) {
589
+ if (callback) {
590
+ await callback(this, e);
591
+ }
592
+ }
593
+ throw e;
594
+ }
595
+ for (let callback of this.afterRenameUIDCallbacks) {
596
+ if (callback) {
597
+ await callback(this, result);
598
+ }
541
599
  }
600
+ return await result;
601
+ } catch (e: any) {
602
+ this.config.debugError(
603
+ 'nymph',
604
+ `Failed to rename UID "${oldName}" to "${newName}": ${e}`,
605
+ );
606
+ throw e;
542
607
  }
543
- return await result;
544
608
  }
545
609
 
546
610
  /**
@@ -555,28 +619,33 @@ export default class Nymph {
555
619
  * @returns True on success, false on failure.
556
620
  */
557
621
  public async saveEntity(entity: EntityInterface): Promise<boolean> {
558
- for (let callback of this.beforeSaveEntityCallbacks) {
559
- if (callback) {
560
- await callback(this, entity);
561
- }
562
- }
563
- let result: Promise<boolean>;
564
622
  try {
565
- result = this.driver.saveEntity(entity);
566
- } catch (e: any) {
567
- for (let callback of this.failedSaveEntityCallbacks) {
623
+ for (let callback of this.beforeSaveEntityCallbacks) {
568
624
  if (callback) {
569
- await callback(this, e);
625
+ await callback(this, entity);
570
626
  }
571
627
  }
572
- throw e;
573
- }
574
- for (let callback of this.afterSaveEntityCallbacks) {
575
- if (callback) {
576
- await callback(this, result);
628
+ let result: Promise<boolean>;
629
+ try {
630
+ result = this.driver.saveEntity(entity);
631
+ } catch (e: any) {
632
+ for (let callback of this.failedSaveEntityCallbacks) {
633
+ if (callback) {
634
+ await callback(this, e);
635
+ }
636
+ }
637
+ throw e;
638
+ }
639
+ for (let callback of this.afterSaveEntityCallbacks) {
640
+ if (callback) {
641
+ await callback(this, result);
642
+ }
577
643
  }
644
+ return await result;
645
+ } catch (e: any) {
646
+ this.config.debugError('nymph', `Failed to save entity: ${e}`);
647
+ throw e;
578
648
  }
579
- return await result;
580
649
  }
581
650
 
582
651
  /**
@@ -756,12 +825,17 @@ export default class Nymph {
756
825
  options: Options<T> = {},
757
826
  ...selectors: Selector[]
758
827
  ): Promise<ReturnType<T['factorySync']>[] | string[] | number> {
759
- for (let callback of this.beforeGetEntitiesCallbacks) {
760
- if (callback) {
761
- await callback(this, options, selectors);
828
+ try {
829
+ for (let callback of this.beforeGetEntitiesCallbacks) {
830
+ if (callback) {
831
+ await callback(this, options, selectors);
832
+ }
762
833
  }
834
+ return await this.driver.getEntities(options, ...selectors);
835
+ } catch (e: any) {
836
+ this.config.debugError('nymph', `Failed to get entities: ${e}`);
837
+ throw e;
763
838
  }
764
- return await this.driver.getEntities(options, ...selectors);
765
839
  }
766
840
 
767
841
  /**
@@ -801,27 +875,32 @@ export default class Nymph {
801
875
  options: Options<T> = {},
802
876
  ...selectors: Selector[] | string[]
803
877
  ): Promise<ReturnType<T['factorySync']> | string | number | null> {
804
- // Set up options and selectors.
805
- if (typeof selectors[0] === 'string') {
806
- selectors = [{ type: '&', guid: selectors[0] }];
807
- }
808
- options.limit = 1;
809
- for (let callback of this.beforeGetEntityCallbacks) {
810
- if (callback) {
811
- await callback(this, options, selectors as Selector[]);
878
+ try {
879
+ // Set up options and selectors.
880
+ if (typeof selectors[0] === 'string') {
881
+ selectors = [{ type: '&', guid: selectors[0] }];
812
882
  }
883
+ options.limit = 1;
884
+ for (let callback of this.beforeGetEntityCallbacks) {
885
+ if (callback) {
886
+ await callback(this, options, selectors as Selector[]);
887
+ }
888
+ }
889
+ const entities = await this.driver.getEntities(
890
+ options,
891
+ ...(selectors as Selector[]),
892
+ );
893
+ if (options.return === 'count') {
894
+ return entities as unknown as number;
895
+ }
896
+ if (!entities || !entities.length) {
897
+ return null;
898
+ }
899
+ return entities[0];
900
+ } catch (e: any) {
901
+ this.config.debugError('nymph', `Failed to get entity: ${e}`);
902
+ throw e;
813
903
  }
814
- const entities = await this.driver.getEntities(
815
- options,
816
- ...(selectors as Selector[]),
817
- );
818
- if (options.return === 'count') {
819
- return entities as unknown as number;
820
- }
821
- if (!entities || !entities.length) {
822
- return null;
823
- }
824
- return entities[0];
825
904
  }
826
905
 
827
906
  /**
@@ -831,28 +910,33 @@ export default class Nymph {
831
910
  * @returns True on success, false on failure.
832
911
  */
833
912
  public async deleteEntity(entity: EntityInterface): Promise<boolean> {
834
- for (let callback of this.beforeDeleteEntityCallbacks) {
835
- if (callback) {
836
- await callback(this, entity);
837
- }
838
- }
839
- let result: Promise<boolean>;
840
913
  try {
841
- result = this.driver.deleteEntity(entity);
842
- } catch (e: any) {
843
- for (let callback of this.failedDeleteEntityCallbacks) {
914
+ for (let callback of this.beforeDeleteEntityCallbacks) {
844
915
  if (callback) {
845
- await callback(this, e);
916
+ await callback(this, entity);
846
917
  }
847
918
  }
848
- throw e;
849
- }
850
- for (let callback of this.afterDeleteEntityCallbacks) {
851
- if (callback) {
852
- await callback(this, result);
919
+ let result: Promise<boolean>;
920
+ try {
921
+ result = this.driver.deleteEntity(entity);
922
+ } catch (e: any) {
923
+ for (let callback of this.failedDeleteEntityCallbacks) {
924
+ if (callback) {
925
+ await callback(this, e);
926
+ }
927
+ }
928
+ throw e;
929
+ }
930
+ for (let callback of this.afterDeleteEntityCallbacks) {
931
+ if (callback) {
932
+ await callback(this, result);
933
+ }
853
934
  }
935
+ return await result;
936
+ } catch (e: any) {
937
+ this.config.debugError('nymph', `Failed to delete entity: ${e}`);
938
+ throw e;
854
939
  }
855
- return await result;
856
940
  }
857
941
 
858
942
  /**
@@ -866,28 +950,33 @@ export default class Nymph {
866
950
  guid: string,
867
951
  className?: string,
868
952
  ): Promise<boolean> {
869
- for (let callback of this.beforeDeleteEntityByIDCallbacks) {
870
- if (callback) {
871
- await callback(this, guid, className);
872
- }
873
- }
874
- let result: Promise<boolean>;
875
953
  try {
876
- result = this.driver.deleteEntityByID(guid, className);
877
- } catch (e: any) {
878
- for (let callback of this.failedDeleteEntityByIDCallbacks) {
954
+ for (let callback of this.beforeDeleteEntityByIDCallbacks) {
879
955
  if (callback) {
880
- await callback(this, e);
956
+ await callback(this, guid, className);
881
957
  }
882
958
  }
883
- throw e;
884
- }
885
- for (let callback of this.afterDeleteEntityByIDCallbacks) {
886
- if (callback) {
887
- await callback(this, result);
959
+ let result: Promise<boolean>;
960
+ try {
961
+ result = this.driver.deleteEntityByID(guid, className);
962
+ } catch (e: any) {
963
+ for (let callback of this.failedDeleteEntityByIDCallbacks) {
964
+ if (callback) {
965
+ await callback(this, e);
966
+ }
967
+ }
968
+ throw e;
888
969
  }
970
+ for (let callback of this.afterDeleteEntityByIDCallbacks) {
971
+ if (callback) {
972
+ await callback(this, result);
973
+ }
974
+ }
975
+ return await result;
976
+ } catch (e: any) {
977
+ this.config.debugError('nymph', `Failed to delete entity by ID: ${e}`);
978
+ throw e;
889
979
  }
890
- return await result;
891
980
  }
892
981
 
893
982
  /**
@@ -927,7 +1016,12 @@ export default class Nymph {
927
1016
  * @returns True on success, false on failure.
928
1017
  */
929
1018
  public async export(filename: string): Promise<boolean> {
930
- return await this.driver.export(filename);
1019
+ try {
1020
+ return await this.driver.export(filename);
1021
+ } catch (e: any) {
1022
+ this.config.debugError('nymph', `Failed to export: ${e}`);
1023
+ throw e;
1024
+ }
931
1025
  }
932
1026
 
933
1027
  /**
@@ -936,7 +1030,12 @@ export default class Nymph {
936
1030
  * @returns True on success, false on failure.
937
1031
  */
938
1032
  public async exportPrint(): Promise<boolean> {
939
- return await this.driver.exportPrint();
1033
+ try {
1034
+ return await this.driver.exportPrint();
1035
+ } catch (e: any) {
1036
+ this.config.debugError('nymph', `Failed to export: ${e}`);
1037
+ throw e;
1038
+ }
940
1039
  }
941
1040
 
942
1041
  /**
@@ -946,7 +1045,12 @@ export default class Nymph {
946
1045
  * @returns True on success, false on failure.
947
1046
  */
948
1047
  public async import(filename: string): Promise<boolean> {
949
- return await this.driver.import(filename);
1048
+ try {
1049
+ return await this.driver.import(filename);
1050
+ } catch (e: any) {
1051
+ this.config.debugError('nymph', `Failed to import: ${e}`);
1052
+ throw e;
1053
+ }
950
1054
  }
951
1055
 
952
1056
  public on<T extends NymphEventType>(
@@ -377,7 +377,7 @@ export interface TilmeldInterface {
377
377
  init(nymph: Nymph): void;
378
378
  clone(): TilmeldInterface;
379
379
  gatekeeper(ability?: string): boolean;
380
- authenticate(): Promise<boolean>;
380
+ authenticate(skipXsrfToken?: boolean, skipRenew?: boolean): Promise<boolean>;
381
381
  clearSession(): void;
382
382
  extractToken(token: string): Promise<any>;
383
383
  fillSession(user: any): Promise<void>;
package/src/conf/d.ts CHANGED
@@ -21,4 +21,12 @@ export interface Config {
21
21
  * return a 404 error.
22
22
  */
23
23
  emptyListError: boolean;
24
+ /**
25
+ * A function to log debug messages. By default, uses the `debug` package.
26
+ */
27
+ debugLog: (source: string, message: string) => void;
28
+ /**
29
+ * A function to log error messages. By default, uses the `debug` package.
30
+ */
31
+ debugError: (source: string, message: string) => void;
24
32
  }