@nymphjs/client 1.0.0-beta.9 → 1.0.0-beta.91
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/CHANGELOG.md +378 -0
- package/README.md +6 -6
- package/asyncitertest.js +53 -0
- package/dist/Entity.d.ts +156 -0
- package/{lib → dist}/Entity.js +202 -104
- package/dist/Entity.js.map +1 -0
- package/dist/Entity.types.d.ts +218 -0
- package/dist/Entity.types.js +2 -0
- package/{lib → dist}/EntityWeakCache.d.ts +1 -1
- package/{lib → dist}/EntityWeakCache.js +5 -9
- package/dist/EntityWeakCache.js.map +1 -0
- package/dist/HttpRequester.d.ts +78 -0
- package/dist/HttpRequester.js +365 -0
- package/dist/HttpRequester.js.map +1 -0
- package/{lib → dist}/Nymph.d.ts +44 -11
- package/{lib → dist}/Nymph.js +118 -39
- package/dist/Nymph.js.map +1 -0
- package/{lib → dist}/Nymph.types.d.ts +74 -2
- package/dist/Nymph.types.js +2 -0
- package/{lib → dist}/PubSub.d.ts +16 -10
- package/{lib → dist}/PubSub.js +172 -108
- package/dist/PubSub.js.map +1 -0
- package/{lib → dist}/PubSub.types.d.ts +8 -3
- package/dist/PubSub.types.js +2 -0
- package/{lib → dist}/entityRefresh.d.ts +1 -1
- package/{lib → dist}/entityRefresh.js +21 -13
- package/dist/entityRefresh.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -2
- package/dist/index.js.map +1 -1
- package/{lib → dist}/utils.d.ts +1 -1
- package/{lib → dist}/utils.js +28 -21
- package/dist/utils.js.map +1 -0
- package/jest.config.js +11 -2
- package/package.json +23 -27
- package/src/Entity.ts +170 -104
- package/src/Entity.types.ts +29 -47
- package/src/EntityWeakCache.ts +8 -6
- package/src/HttpRequester.ts +308 -35
- package/src/Nymph.ts +134 -70
- package/src/Nymph.types.ts +41 -2
- package/src/PubSub.ts +214 -141
- package/src/PubSub.types.ts +10 -5
- package/src/entityRefresh.ts +6 -6
- package/src/index.ts +10 -10
- package/src/utils.ts +12 -5
- package/tsconfig.json +6 -4
- package/typedoc.json +4 -0
- package/dist/index.js.LICENSE.txt +0 -8
- package/lib/Entity.d.ts +0 -51
- package/lib/Entity.js.map +0 -1
- package/lib/Entity.types.d.ts +0 -65
- package/lib/Entity.types.js +0 -3
- package/lib/EntityWeakCache.js.map +0 -1
- package/lib/HttpRequester.d.ts +0 -42
- package/lib/HttpRequester.js +0 -160
- package/lib/HttpRequester.js.map +0 -1
- package/lib/Nymph.js.map +0 -1
- package/lib/Nymph.types.js +0 -3
- package/lib/PubSub.js.map +0 -1
- package/lib/PubSub.types.js +0 -3
- package/lib/entityRefresh.js.map +0 -1
- package/lib/index.d.ts +0 -13
- package/lib/index.js +0 -34
- package/lib/index.js.map +0 -1
- package/lib/utils.js.map +0 -1
- package/webpack.config.js +0 -28
- /package/{lib → dist}/Entity.types.js.map +0 -0
- /package/{lib → dist}/Nymph.types.js.map +0 -0
- /package/{lib → dist}/PubSub.types.js.map +0 -0
package/src/Nymph.ts
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
|
-
import Entity from './Entity';
|
|
2
|
-
import {
|
|
1
|
+
import Entity, { type EntityInstanceType } from './Entity.js';
|
|
2
|
+
import type {
|
|
3
3
|
EntityConstructor,
|
|
4
|
-
EntityData,
|
|
5
4
|
EntityInterface,
|
|
6
5
|
EntityJson,
|
|
7
6
|
ServerCallResponse,
|
|
8
7
|
ServerCallStaticResponse,
|
|
9
|
-
} from './Entity.types';
|
|
10
|
-
import EntityWeakCache from './EntityWeakCache';
|
|
11
|
-
import
|
|
12
|
-
import
|
|
8
|
+
} from './Entity.types.js';
|
|
9
|
+
import EntityWeakCache from './EntityWeakCache.js';
|
|
10
|
+
import type { AbortableAsyncIterator } from './HttpRequester.js';
|
|
11
|
+
import HttpRequester from './HttpRequester.js';
|
|
12
|
+
import type {
|
|
13
13
|
EventType,
|
|
14
14
|
NymphOptions,
|
|
15
15
|
Options,
|
|
16
16
|
RequestCallback,
|
|
17
17
|
ResponseCallback,
|
|
18
18
|
Selector,
|
|
19
|
-
} from './Nymph.types';
|
|
20
|
-
import PubSub from './PubSub';
|
|
21
|
-
import {
|
|
19
|
+
} from './Nymph.types.js';
|
|
20
|
+
import type PubSub from './PubSub.js';
|
|
21
|
+
import {
|
|
22
|
+
entitiesToReferences,
|
|
23
|
+
entityConstructorsToClassNames,
|
|
24
|
+
} from './utils.js';
|
|
22
25
|
|
|
23
26
|
let requester: HttpRequester;
|
|
24
27
|
|
|
@@ -42,6 +45,15 @@ export default class Nymph {
|
|
|
42
45
|
private responseCallbacks: ResponseCallback[] = [];
|
|
43
46
|
private restUrl: string = '';
|
|
44
47
|
private weakCache = false;
|
|
48
|
+
/**
|
|
49
|
+
* Headers that will be sent with every request.
|
|
50
|
+
*
|
|
51
|
+
* These are used by Tilmeld for authentication.
|
|
52
|
+
*/
|
|
53
|
+
public headers: { [k: string]: string } = {};
|
|
54
|
+
/**
|
|
55
|
+
* The entity cache.
|
|
56
|
+
*/
|
|
45
57
|
public cache = new EntityWeakCache();
|
|
46
58
|
|
|
47
59
|
public constructor(NymphOptions: NymphOptions) {
|
|
@@ -52,9 +64,13 @@ export default class Nymph {
|
|
|
52
64
|
this.Entity = this.addEntityClass(Entity);
|
|
53
65
|
|
|
54
66
|
requester = new HttpRequester(
|
|
55
|
-
'fetch' in NymphOptions ? NymphOptions.fetch : undefined
|
|
67
|
+
'fetch' in NymphOptions ? NymphOptions.fetch : undefined,
|
|
56
68
|
);
|
|
57
69
|
|
|
70
|
+
if ('renewTokens' in NymphOptions && !NymphOptions.renewTokens) {
|
|
71
|
+
this.headers['X-Tilmeld-Token-Renewal'] = 'off';
|
|
72
|
+
}
|
|
73
|
+
|
|
58
74
|
requester.on('request', (_requester, url, options) => {
|
|
59
75
|
for (let i = 0; i < this.requestCallbacks.length; i++) {
|
|
60
76
|
this.requestCallbacks[i] && this.requestCallbacks[i](url, options);
|
|
@@ -75,8 +91,9 @@ export default class Nymph {
|
|
|
75
91
|
* Nymph and return it. You can then use this class's constructor and methods,
|
|
76
92
|
* which will use this instance of Nymph.
|
|
77
93
|
*
|
|
78
|
-
* Because this creates a subclass, don't use the class
|
|
79
|
-
*
|
|
94
|
+
* Because this creates a subclass, don't use the class returned from
|
|
95
|
+
* `getEntityClass` to check with `instanceof`. Instead, use the base class
|
|
96
|
+
* that you passed into this method.
|
|
80
97
|
*/
|
|
81
98
|
public addEntityClass<T extends EntityConstructor>(entityClass: T): T {
|
|
82
99
|
const nymph = this;
|
|
@@ -91,18 +108,27 @@ export default class Nymph {
|
|
|
91
108
|
return NymphEntity;
|
|
92
109
|
}
|
|
93
110
|
|
|
94
|
-
public getEntityClass(className:
|
|
95
|
-
|
|
96
|
-
|
|
111
|
+
public getEntityClass<T extends EntityConstructor>(className: T): T;
|
|
112
|
+
public getEntityClass(className: string): EntityConstructor;
|
|
113
|
+
public getEntityClass<T extends EntityConstructor = EntityConstructor>(
|
|
114
|
+
className: T | string,
|
|
115
|
+
): T | EntityConstructor {
|
|
116
|
+
let key: string | null = null;
|
|
117
|
+
if (typeof className === 'string') {
|
|
118
|
+
key = className;
|
|
119
|
+
} else {
|
|
120
|
+
key = className.class;
|
|
97
121
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
122
|
+
if (key in this.entityClasses) {
|
|
123
|
+
return this.entityClasses[key];
|
|
124
|
+
}
|
|
125
|
+
throw new ClassNotAvailableError('Tried to use class: ' + key);
|
|
101
126
|
}
|
|
102
127
|
|
|
103
128
|
public async newUID(name: string) {
|
|
104
129
|
const data = await requester.POST({
|
|
105
130
|
url: this.restUrl,
|
|
131
|
+
headers: { ...this.headers },
|
|
106
132
|
dataType: 'text',
|
|
107
133
|
data: { action: 'uid', data: name },
|
|
108
134
|
});
|
|
@@ -112,6 +138,7 @@ export default class Nymph {
|
|
|
112
138
|
public async setUID(name: string, value: number) {
|
|
113
139
|
return await requester.PUT({
|
|
114
140
|
url: this.restUrl,
|
|
141
|
+
headers: { ...this.headers },
|
|
115
142
|
dataType: 'json',
|
|
116
143
|
data: { action: 'uid', data: { name, value } },
|
|
117
144
|
});
|
|
@@ -120,6 +147,7 @@ export default class Nymph {
|
|
|
120
147
|
public async getUID(name: string) {
|
|
121
148
|
const data = await requester.GET({
|
|
122
149
|
url: this.restUrl,
|
|
150
|
+
headers: { ...this.headers },
|
|
123
151
|
dataType: 'text',
|
|
124
152
|
data: { action: 'uid', data: name },
|
|
125
153
|
});
|
|
@@ -129,6 +157,7 @@ export default class Nymph {
|
|
|
129
157
|
public async deleteUID(name: string) {
|
|
130
158
|
return await requester.DELETE({
|
|
131
159
|
url: this.restUrl,
|
|
160
|
+
headers: { ...this.headers },
|
|
132
161
|
dataType: 'text',
|
|
133
162
|
data: { action: 'uid', data: name },
|
|
134
163
|
});
|
|
@@ -152,7 +181,7 @@ export default class Nymph {
|
|
|
152
181
|
) {
|
|
153
182
|
throw new InvalidRequestError(
|
|
154
183
|
'Due to REST restriction, you can only create new entities or ' +
|
|
155
|
-
'update existing entities, not both at the same time.'
|
|
184
|
+
'update existing entities, not both at the same time.',
|
|
156
185
|
);
|
|
157
186
|
}
|
|
158
187
|
});
|
|
@@ -162,7 +191,7 @@ export default class Nymph {
|
|
|
162
191
|
public async patchEntity(entity: EntityInterface) {
|
|
163
192
|
if (entity.guid == null) {
|
|
164
193
|
throw new InvalidRequestError(
|
|
165
|
-
"You can't patch an entity that hasn't yet been saved."
|
|
194
|
+
"You can't patch an entity that hasn't yet been saved.",
|
|
166
195
|
);
|
|
167
196
|
}
|
|
168
197
|
|
|
@@ -179,7 +208,7 @@ export default class Nymph {
|
|
|
179
208
|
if (cur.guid == null) {
|
|
180
209
|
throw new InvalidRequestError(
|
|
181
210
|
'Due to REST restriction, you can only create new entities or ' +
|
|
182
|
-
'update existing entities, not both at the same time.'
|
|
211
|
+
'update existing entities, not both at the same time.',
|
|
183
212
|
);
|
|
184
213
|
}
|
|
185
214
|
});
|
|
@@ -191,22 +220,23 @@ export default class Nymph {
|
|
|
191
220
|
entity: T,
|
|
192
221
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
|
|
193
222
|
data: { [k: string]: any },
|
|
194
|
-
plural: false
|
|
223
|
+
plural: false,
|
|
195
224
|
): Promise<T>;
|
|
196
225
|
private async requestWithMethod<T extends EntityInterface>(
|
|
197
226
|
entity: T[],
|
|
198
227
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
|
|
199
228
|
data: { [k: string]: any },
|
|
200
|
-
plural: true
|
|
229
|
+
plural: true,
|
|
201
230
|
): Promise<T[]>;
|
|
202
231
|
private async requestWithMethod<T extends EntityInterface>(
|
|
203
232
|
entity: T | T[],
|
|
204
233
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
|
|
205
234
|
data: { [k: string]: any },
|
|
206
|
-
plural: boolean
|
|
235
|
+
plural: boolean,
|
|
207
236
|
): Promise<T | T[]> {
|
|
208
237
|
const response = await requester[method]({
|
|
209
238
|
url: this.restUrl,
|
|
239
|
+
headers: { ...this.headers },
|
|
210
240
|
dataType: 'json',
|
|
211
241
|
data: {
|
|
212
242
|
action: plural ? 'entities' : 'entity',
|
|
@@ -219,7 +249,7 @@ export default class Nymph {
|
|
|
219
249
|
typeof response[i].guid !== 'undefined' &&
|
|
220
250
|
(e.guid == null || e.guid === response[i].guid)
|
|
221
251
|
? e.$init(response[i])
|
|
222
|
-
: e
|
|
252
|
+
: e,
|
|
223
253
|
) as T[];
|
|
224
254
|
} else if (!Array.isArray(entity) && typeof response.guid !== 'undefined') {
|
|
225
255
|
return entity.$init(response) as T;
|
|
@@ -238,23 +268,23 @@ export default class Nymph {
|
|
|
238
268
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
239
269
|
options: Options<T>,
|
|
240
270
|
...selectors: Selector[]
|
|
241
|
-
): Promise<
|
|
271
|
+
): Promise<EntityInstanceType<T> | null>;
|
|
242
272
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
243
273
|
options: Options<T> & { return: 'count' },
|
|
244
|
-
guid: string
|
|
274
|
+
guid: string,
|
|
245
275
|
): Promise<number>;
|
|
246
276
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
247
277
|
options: Options<T> & { return: 'guid' },
|
|
248
|
-
guid: string
|
|
278
|
+
guid: string,
|
|
249
279
|
): Promise<string | null>;
|
|
250
280
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
251
281
|
options: Options<T>,
|
|
252
|
-
guid: string
|
|
253
|
-
): Promise<
|
|
282
|
+
guid: string,
|
|
283
|
+
): Promise<EntityInstanceType<T> | null>;
|
|
254
284
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
255
285
|
options: Options<T>,
|
|
256
286
|
...selectors: Selector[] | string[]
|
|
257
|
-
): Promise<
|
|
287
|
+
): Promise<EntityInstanceType<T> | string | number | null> {
|
|
258
288
|
// @ts-ignore: Implementation signatures of overloads are not externally visible.
|
|
259
289
|
const data = (await this.getEntityData(options, ...selectors)) as
|
|
260
290
|
| EntityJson<T>
|
|
@@ -290,23 +320,23 @@ export default class Nymph {
|
|
|
290
320
|
): Promise<EntityJson<T> | null>;
|
|
291
321
|
public async getEntityData<T extends EntityConstructor = EntityConstructor>(
|
|
292
322
|
options: Options<T> & { return: 'count' },
|
|
293
|
-
guid: string
|
|
323
|
+
guid: string,
|
|
294
324
|
): Promise<number>;
|
|
295
325
|
public async getEntityData<T extends EntityConstructor = EntityConstructor>(
|
|
296
326
|
options: Options<T> & { return: 'guid' },
|
|
297
|
-
guid: string
|
|
327
|
+
guid: string,
|
|
298
328
|
): Promise<string | null>;
|
|
299
329
|
public async getEntityData<T extends EntityConstructor = EntityConstructor>(
|
|
300
330
|
options: Options<T>,
|
|
301
|
-
guid: string
|
|
331
|
+
guid: string,
|
|
302
332
|
): Promise<EntityJson<T> | null>;
|
|
303
333
|
public async getEntityData<T extends EntityConstructor = EntityConstructor>(
|
|
304
334
|
options: Options<T>,
|
|
305
335
|
...selectors: Selector[] | string[]
|
|
306
336
|
): Promise<EntityJson<T> | string | number | null> {
|
|
307
|
-
if (options.class
|
|
337
|
+
if (options.class instanceof Entity) {
|
|
308
338
|
throw new InvalidRequestError(
|
|
309
|
-
"You can't make REST requests with the base Entity class."
|
|
339
|
+
"You can't make REST requests with the base Entity class.",
|
|
310
340
|
);
|
|
311
341
|
}
|
|
312
342
|
// Set up options and selectors.
|
|
@@ -315,6 +345,7 @@ export default class Nymph {
|
|
|
315
345
|
}
|
|
316
346
|
const data = await requester.GET({
|
|
317
347
|
url: this.restUrl,
|
|
348
|
+
headers: { ...this.headers },
|
|
318
349
|
dataType: 'json',
|
|
319
350
|
data: {
|
|
320
351
|
action: 'entity',
|
|
@@ -342,13 +373,14 @@ export default class Nymph {
|
|
|
342
373
|
public async getEntities<T extends EntityConstructor = EntityConstructor>(
|
|
343
374
|
options: Options<T>,
|
|
344
375
|
...selectors: Selector[]
|
|
345
|
-
): Promise<
|
|
376
|
+
): Promise<EntityInstanceType<T>[]>;
|
|
346
377
|
public async getEntities<T extends EntityConstructor = EntityConstructor>(
|
|
347
378
|
options: Options<T>,
|
|
348
379
|
...selectors: Selector[]
|
|
349
|
-
): Promise<
|
|
380
|
+
): Promise<EntityInstanceType<T>[] | string[] | number> {
|
|
350
381
|
const data = await requester.GET({
|
|
351
382
|
url: this.restUrl,
|
|
383
|
+
headers: { ...this.headers },
|
|
352
384
|
dataType: 'json',
|
|
353
385
|
data: {
|
|
354
386
|
action: 'entities',
|
|
@@ -369,12 +401,12 @@ export default class Nymph {
|
|
|
369
401
|
}
|
|
370
402
|
|
|
371
403
|
public initEntity<T extends EntityConstructor = EntityConstructor>(
|
|
372
|
-
entityJSON: EntityJson<T
|
|
373
|
-
):
|
|
404
|
+
entityJSON: EntityJson<T>,
|
|
405
|
+
): EntityInstanceType<T> {
|
|
374
406
|
const EntityClass = this.getEntityClass(entityJSON.class);
|
|
375
407
|
if (!EntityClass) {
|
|
376
408
|
throw new ClassNotAvailableError(
|
|
377
|
-
entityJSON.class + ' class cannot be found.'
|
|
409
|
+
entityJSON.class + ' class cannot be found.',
|
|
378
410
|
);
|
|
379
411
|
}
|
|
380
412
|
let entity = EntityClass.factorySync();
|
|
@@ -382,30 +414,28 @@ export default class Nymph {
|
|
|
382
414
|
// Try to get it from cache.
|
|
383
415
|
const entityFromCache = this.cache.get(
|
|
384
416
|
EntityClass,
|
|
385
|
-
entityJSON.guid || ''
|
|
417
|
+
entityJSON.guid || '',
|
|
386
418
|
);
|
|
387
419
|
if (entityFromCache != null) {
|
|
388
|
-
entity = entityFromCache
|
|
420
|
+
entity = entityFromCache as EntityInstanceType<T>;
|
|
389
421
|
}
|
|
390
422
|
}
|
|
391
|
-
return entity.$init(entityJSON) as
|
|
423
|
+
return entity.$init(entityJSON) as EntityInstanceType<T>;
|
|
392
424
|
}
|
|
393
425
|
|
|
394
426
|
public getEntityFromCache<T extends EntityConstructor = EntityConstructor>(
|
|
395
427
|
EntityClass: EntityConstructor,
|
|
396
|
-
guid: string
|
|
397
|
-
):
|
|
428
|
+
guid: string,
|
|
429
|
+
): EntityInstanceType<T> | null {
|
|
398
430
|
if (!this.weakCache) {
|
|
399
431
|
return null;
|
|
400
432
|
}
|
|
401
|
-
return this.cache.get(EntityClass, guid) as
|
|
402
|
-
T['factorySync']
|
|
403
|
-
> | null;
|
|
433
|
+
return this.cache.get(EntityClass, guid) as EntityInstanceType<T> | null;
|
|
404
434
|
}
|
|
405
435
|
|
|
406
436
|
public setEntityToCache(
|
|
407
437
|
EntityClass: EntityConstructor,
|
|
408
|
-
entity: EntityInterface
|
|
438
|
+
entity: EntityInterface,
|
|
409
439
|
) {
|
|
410
440
|
if (!this.weakCache) {
|
|
411
441
|
return;
|
|
@@ -417,10 +447,7 @@ export default class Nymph {
|
|
|
417
447
|
if (Array.isArray(item)) {
|
|
418
448
|
// Recurse into lower arrays.
|
|
419
449
|
return item.map((entry) => this.initEntitiesFromData(entry)) as T;
|
|
420
|
-
} else if (
|
|
421
|
-
item instanceof Object &&
|
|
422
|
-
!(item instanceof this.getEntityClass('Entity'))
|
|
423
|
-
) {
|
|
450
|
+
} else if (item instanceof Object && !(item instanceof Entity)) {
|
|
424
451
|
if (
|
|
425
452
|
item.hasOwnProperty('class') &&
|
|
426
453
|
item.hasOwnProperty('guid') &&
|
|
@@ -444,10 +471,11 @@ export default class Nymph {
|
|
|
444
471
|
|
|
445
472
|
public async deleteEntity(
|
|
446
473
|
entity: EntityInterface | EntityInterface[],
|
|
447
|
-
_plural = false
|
|
474
|
+
_plural = false,
|
|
448
475
|
) {
|
|
449
476
|
return await requester.DELETE({
|
|
450
477
|
url: this.restUrl,
|
|
478
|
+
headers: { ...this.headers },
|
|
451
479
|
dataType: 'json',
|
|
452
480
|
data: {
|
|
453
481
|
action: _plural ? 'entities' : 'entity',
|
|
@@ -473,10 +501,11 @@ export default class Nymph {
|
|
|
473
501
|
entity: EntityInterface,
|
|
474
502
|
method: string,
|
|
475
503
|
params: any[],
|
|
476
|
-
stateless = false
|
|
504
|
+
stateless = false,
|
|
477
505
|
): Promise<ServerCallResponse> {
|
|
478
506
|
const data = await requester.POST({
|
|
479
507
|
url: this.restUrl,
|
|
508
|
+
headers: { ...this.headers },
|
|
480
509
|
dataType: 'json',
|
|
481
510
|
data: {
|
|
482
511
|
action: 'method',
|
|
@@ -498,10 +527,11 @@ export default class Nymph {
|
|
|
498
527
|
public async serverCallStatic(
|
|
499
528
|
className: string,
|
|
500
529
|
method: string,
|
|
501
|
-
params: any[]
|
|
530
|
+
params: any[],
|
|
502
531
|
): Promise<ServerCallStaticResponse> {
|
|
503
532
|
const data = await requester.POST({
|
|
504
533
|
url: this.restUrl,
|
|
534
|
+
headers: { ...this.headers },
|
|
505
535
|
dataType: 'json',
|
|
506
536
|
data: {
|
|
507
537
|
action: 'method',
|
|
@@ -517,19 +547,57 @@ export default class Nymph {
|
|
|
517
547
|
return this.initEntitiesFromData(data);
|
|
518
548
|
}
|
|
519
549
|
|
|
550
|
+
public async serverCallStaticIterator(
|
|
551
|
+
className: string,
|
|
552
|
+
method: string,
|
|
553
|
+
params: any[],
|
|
554
|
+
): Promise<AbortableAsyncIterator<ServerCallStaticResponse>> {
|
|
555
|
+
const iterable = await requester.POST_ITERATOR({
|
|
556
|
+
url: this.restUrl,
|
|
557
|
+
headers: { ...this.headers },
|
|
558
|
+
dataType: 'json',
|
|
559
|
+
data: {
|
|
560
|
+
action: 'method',
|
|
561
|
+
data: {
|
|
562
|
+
class: className,
|
|
563
|
+
static: true,
|
|
564
|
+
method: method,
|
|
565
|
+
iterator: true,
|
|
566
|
+
params: entitiesToReferences(entityConstructorsToClassNames(params)),
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
const that = this;
|
|
572
|
+
const iterator: AbortableAsyncIterator = {
|
|
573
|
+
abortController: iterable.abortController,
|
|
574
|
+
async *[Symbol.asyncIterator]() {
|
|
575
|
+
for await (let response of iterable) {
|
|
576
|
+
if (response instanceof Error) {
|
|
577
|
+
yield response;
|
|
578
|
+
} else {
|
|
579
|
+
yield that.initEntitiesFromData(response);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
},
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
return iterator;
|
|
586
|
+
}
|
|
587
|
+
|
|
520
588
|
public on<T extends EventType>(
|
|
521
589
|
event: T,
|
|
522
590
|
callback: T extends 'request'
|
|
523
591
|
? RequestCallback
|
|
524
592
|
: T extends 'response'
|
|
525
|
-
|
|
526
|
-
|
|
593
|
+
? ResponseCallback
|
|
594
|
+
: never,
|
|
527
595
|
) {
|
|
528
596
|
const prop = (event + 'Callbacks') as T extends 'request'
|
|
529
597
|
? 'requestCallbacks'
|
|
530
598
|
: T extends 'request'
|
|
531
|
-
|
|
532
|
-
|
|
599
|
+
? 'responseCallbacks'
|
|
600
|
+
: never;
|
|
533
601
|
if (!(prop in this)) {
|
|
534
602
|
throw new Error('Invalid event type.');
|
|
535
603
|
}
|
|
@@ -543,14 +611,14 @@ export default class Nymph {
|
|
|
543
611
|
callback: T extends 'request'
|
|
544
612
|
? RequestCallback
|
|
545
613
|
: T extends 'response'
|
|
546
|
-
|
|
547
|
-
|
|
614
|
+
? ResponseCallback
|
|
615
|
+
: never,
|
|
548
616
|
) {
|
|
549
617
|
const prop = (event + 'Callbacks') as T extends 'request'
|
|
550
618
|
? 'requestCallbacks'
|
|
551
619
|
: T extends 'request'
|
|
552
|
-
|
|
553
|
-
|
|
620
|
+
? 'responseCallbacks'
|
|
621
|
+
: never;
|
|
554
622
|
if (!(prop in this)) {
|
|
555
623
|
return false;
|
|
556
624
|
}
|
|
@@ -562,10 +630,6 @@ export default class Nymph {
|
|
|
562
630
|
}
|
|
563
631
|
return true;
|
|
564
632
|
}
|
|
565
|
-
|
|
566
|
-
public setXsrfToken(token: string | null) {
|
|
567
|
-
requester.setXsrfToken(token);
|
|
568
|
-
}
|
|
569
633
|
}
|
|
570
634
|
|
|
571
635
|
export class ClassNotAvailableError extends Error {
|
package/src/Nymph.types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EntityConstructor, EntityInterface } from './Entity.types';
|
|
1
|
+
import { EntityConstructor, EntityInterface } from './Entity.types.js';
|
|
2
2
|
|
|
3
3
|
export type NymphOptions = {
|
|
4
4
|
/**
|
|
@@ -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
|
*
|
|
@@ -40,6 +44,16 @@ export type NymphOptions = {
|
|
|
40
44
|
* can help to synchronize them correctly and avoid data conflicts.
|
|
41
45
|
*/
|
|
42
46
|
weakCache?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Whether to renew tokens when a request is made.
|
|
49
|
+
*
|
|
50
|
+
* If you turn this off, the client will request that the server not renew an
|
|
51
|
+
* authentication token, even if it is within the renewal time of the
|
|
52
|
+
* expiration date.
|
|
53
|
+
*
|
|
54
|
+
* This defaults to true.
|
|
55
|
+
*/
|
|
56
|
+
renewTokens?: boolean;
|
|
43
57
|
};
|
|
44
58
|
|
|
45
59
|
export type EventType = 'request' | 'response';
|
|
@@ -49,12 +63,37 @@ export type RequestCallback = (url: string, options: RequestInit) => void;
|
|
|
49
63
|
export type ResponseCallback = (response: Response, text: string) => void;
|
|
50
64
|
|
|
51
65
|
export type Options<T extends EntityConstructor = EntityConstructor> = {
|
|
66
|
+
/**
|
|
67
|
+
* The Entity class to query.
|
|
68
|
+
*/
|
|
52
69
|
class: T;
|
|
70
|
+
/**
|
|
71
|
+
* The limit of entities to be returned. Not needed when using `getEntity`, as
|
|
72
|
+
* it always returns only one.
|
|
73
|
+
*/
|
|
53
74
|
limit?: number;
|
|
75
|
+
/**
|
|
76
|
+
* The offset from the first matching entity, in order, to start retrieving.
|
|
77
|
+
*/
|
|
54
78
|
offset?: number;
|
|
79
|
+
/**
|
|
80
|
+
* If true, entities will be retrieved from newest to oldest/largest to
|
|
81
|
+
* smallest (with regard to `sort`).
|
|
82
|
+
*/
|
|
55
83
|
reverse?: boolean;
|
|
56
|
-
|
|
84
|
+
/**
|
|
85
|
+
* How to sort the entities. Should be "cdate", "mdate", or the name of a
|
|
86
|
+
* property.
|
|
87
|
+
*/
|
|
88
|
+
sort?: 'cdate' | 'mdate' | string;
|
|
89
|
+
/**
|
|
90
|
+
* What to return, the entities with their data, just the GUIDs, or just a
|
|
91
|
+
* count.
|
|
92
|
+
*/
|
|
57
93
|
return?: 'entity' | 'guid' | 'count';
|
|
94
|
+
/**
|
|
95
|
+
* If true, Nymph will skip the cache and retrieve the entity from the DB.
|
|
96
|
+
*/
|
|
58
97
|
skipCache?: boolean;
|
|
59
98
|
};
|
|
60
99
|
|