@nymphjs/client 1.0.0-beta.4 → 1.0.0-beta.40

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
@@ -1,15 +1,15 @@
1
1
  import Entity from './Entity';
2
- import {
2
+ import type {
3
3
  EntityConstructor,
4
- EntityData,
5
4
  EntityInterface,
6
5
  EntityJson,
7
6
  ServerCallResponse,
8
7
  ServerCallStaticResponse,
9
8
  } from './Entity.types';
10
9
  import EntityWeakCache from './EntityWeakCache';
10
+ import type { AbortableAsyncIterator } from './HttpRequester';
11
11
  import HttpRequester from './HttpRequester';
12
- import {
12
+ import type {
13
13
  EventType,
14
14
  NymphOptions,
15
15
  Options,
@@ -17,7 +17,7 @@ import {
17
17
  ResponseCallback,
18
18
  Selector,
19
19
  } from './Nymph.types';
20
- import PubSub from './PubSub';
20
+ import type PubSub from './PubSub';
21
21
  import { entitiesToReferences, entityConstructorsToClassNames } from './utils';
22
22
 
23
23
  let requester: HttpRequester;
@@ -36,7 +36,7 @@ export default class Nymph {
36
36
  /**
37
37
  * The entity class for this instance of Nymph.
38
38
  */
39
- public Entity: typeof Entity = Entity;
39
+ public Entity: typeof Entity;
40
40
 
41
41
  private requestCallbacks: RequestCallback[] = [];
42
42
  private responseCallbacks: ResponseCallback[] = [];
@@ -49,10 +49,7 @@ export default class Nymph {
49
49
  // @ts-ignore TS doesn't know about WeakRef.
50
50
  this.weakCache = !!NymphOptions.weakCache && typeof WeakRef !== 'undefined';
51
51
 
52
- class NymphEntity<T extends EntityData = EntityData> extends Entity<T> {}
53
- NymphEntity.nymph = this;
54
- this.Entity = NymphEntity;
55
- this.addEntityClass(NymphEntity);
52
+ this.Entity = this.addEntityClass(Entity);
56
53
 
57
54
  requester = new HttpRequester(
58
55
  'fetch' in NymphOptions ? NymphOptions.fetch : undefined
@@ -71,20 +68,44 @@ export default class Nymph {
71
68
  });
72
69
  }
73
70
 
74
- public addEntityClass(entityClass: EntityConstructor) {
75
- this.entityClasses[entityClass.class] = entityClass;
76
- entityClass.nymph = this;
71
+ /**
72
+ * Add your class to this instance.
73
+ *
74
+ * This will create a class that extends your class within this instance of
75
+ * Nymph and return it. You can then use this class's constructor and methods,
76
+ * which will use this instance of Nymph.
77
+ *
78
+ * Because this creates a subclass, don't use the class
79
+ * returned from `getEntityClass` to check with `instanceof`.
80
+ */
81
+ public addEntityClass<T extends EntityConstructor>(entityClass: T): T {
82
+ const nymph = this;
83
+ class NymphEntity extends entityClass {
84
+ static nymph: Nymph = nymph;
85
+
86
+ constructor(...args: any[]) {
87
+ super(...args);
88
+ }
89
+ }
90
+ this.entityClasses[entityClass.class] = NymphEntity;
91
+ return NymphEntity;
77
92
  }
78
93
 
79
- public getEntityClass(className: string) {
80
- if (this.entityClasses.hasOwnProperty(className)) {
81
- const EntityClass = this.entityClasses[className];
82
- EntityClass.nymph = this;
83
- return EntityClass;
94
+ public getEntityClass<T extends EntityConstructor>(className: T): T;
95
+ public getEntityClass(className: string): EntityConstructor;
96
+ public getEntityClass<T extends EntityConstructor = EntityConstructor>(
97
+ className: T | string
98
+ ): T | EntityConstructor {
99
+ let key: string | null = null;
100
+ if (typeof className === 'string') {
101
+ key = className;
102
+ } else {
103
+ key = className.class;
84
104
  }
85
- throw new ClassNotAvailableError(
86
- "Tried to get class that's not available: " + className
87
- );
105
+ if (key in this.entityClasses) {
106
+ return this.entityClasses[key];
107
+ }
108
+ throw new ClassNotAvailableError('Tried to use class: ' + key);
88
109
  }
89
110
 
90
111
  public async newUID(name: string) {
@@ -291,7 +312,7 @@ export default class Nymph {
291
312
  options: Options<T>,
292
313
  ...selectors: Selector[] | string[]
293
314
  ): Promise<EntityJson<T> | string | number | null> {
294
- if (options.class === this.getEntityClass('Entity')) {
315
+ if (options.class instanceof Entity) {
295
316
  throw new InvalidRequestError(
296
317
  "You can't make REST requests with the base Entity class."
297
318
  );
@@ -404,10 +425,7 @@ export default class Nymph {
404
425
  if (Array.isArray(item)) {
405
426
  // Recurse into lower arrays.
406
427
  return item.map((entry) => this.initEntitiesFromData(entry)) as T;
407
- } else if (
408
- item instanceof Object &&
409
- !(item instanceof this.getEntityClass('Entity'))
410
- ) {
428
+ } else if (item instanceof Object && !(item instanceof Entity)) {
411
429
  if (
412
430
  item.hasOwnProperty('class') &&
413
431
  item.hasOwnProperty('guid') &&
@@ -504,6 +522,43 @@ export default class Nymph {
504
522
  return this.initEntitiesFromData(data);
505
523
  }
506
524
 
525
+ public async serverCallStaticIterator(
526
+ className: string,
527
+ method: string,
528
+ params: any[]
529
+ ): Promise<AbortableAsyncIterator<ServerCallStaticResponse>> {
530
+ const iterable = await requester.POST_ITERATOR({
531
+ url: this.restUrl,
532
+ dataType: 'json',
533
+ data: {
534
+ action: 'method',
535
+ data: {
536
+ class: className,
537
+ static: true,
538
+ method: method,
539
+ iterator: true,
540
+ params: entitiesToReferences(entityConstructorsToClassNames(params)),
541
+ },
542
+ },
543
+ });
544
+
545
+ const that = this;
546
+ const iterator: AbortableAsyncIterator = {
547
+ abortController: iterable.abortController,
548
+ async *[Symbol.asyncIterator]() {
549
+ for await (let response of iterable) {
550
+ if (response instanceof Error) {
551
+ yield response;
552
+ } else {
553
+ yield that.initEntitiesFromData(response);
554
+ }
555
+ }
556
+ },
557
+ };
558
+
559
+ return iterator;
560
+ }
561
+
507
562
  public on<T extends EventType>(
508
563
  event: T,
509
564
  callback: T extends 'request'
@@ -21,6 +21,10 @@ export type NymphOptions = {
21
21
  * Whether to not output status messages to the console.
22
22
  */
23
23
  noConsole?: boolean;
24
+ /**
25
+ * Don't automatically try to connect to PubSub server.
26
+ */
27
+ noAutoconnect?: boolean;
24
28
  /**
25
29
  * Use a WeakRef based cache of entities.
26
30
  *
@@ -53,7 +57,7 @@ export type Options<T extends EntityConstructor = EntityConstructor> = {
53
57
  limit?: number;
54
58
  offset?: number;
55
59
  reverse?: boolean;
56
- sort?: 'cdate' | 'mdate';
60
+ sort?: 'cdate' | 'mdate' | string;
57
61
  return?: 'entity' | 'guid' | 'count';
58
62
  skipCache?: boolean;
59
63
  };