@nymphjs/client 1.0.0-beta.98 → 1.0.0-beta.99
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 +6 -0
- package/dist/Nymph.types.d.ts +4 -0
- package/lib/Entity.d.ts +155 -0
- package/lib/Entity.js +585 -0
- package/lib/Entity.js.map +1 -0
- package/lib/Entity.types.d.ts +200 -0
- package/lib/Entity.types.js +3 -0
- package/lib/Entity.types.js.map +1 -0
- package/lib/EntityWeakCache.d.ts +6 -0
- package/lib/EntityWeakCache.js +32 -0
- package/lib/EntityWeakCache.js.map +1 -0
- package/lib/HttpRequester.d.ts +78 -0
- package/lib/HttpRequester.js +375 -0
- package/lib/HttpRequester.js.map +1 -0
- package/lib/Nymph.d.ts +111 -0
- package/lib/Nymph.js +426 -0
- package/lib/Nymph.js.map +1 -0
- package/lib/Nymph.types.d.ts +169 -0
- package/lib/Nymph.types.js +3 -0
- package/lib/Nymph.types.js.map +1 -0
- package/lib/PubSub.d.ts +63 -0
- package/lib/PubSub.js +596 -0
- package/lib/PubSub.js.map +1 -0
- package/lib/PubSub.types.d.ts +31 -0
- package/lib/PubSub.types.js +3 -0
- package/lib/PubSub.types.js.map +1 -0
- package/lib/entityRefresh.d.ts +5 -0
- package/lib/entityRefresh.js +83 -0
- package/lib/entityRefresh.js.map +1 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +34 -0
- package/lib/index.js.map +1 -0
- package/lib/utils.d.ts +8 -0
- package/lib/utils.js +97 -0
- package/lib/utils.js.map +1 -0
- package/package.json +2 -2
- package/src/Nymph.types.ts +6 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type Nymph from './Nymph';
|
|
2
|
+
import type Entity from './Entity';
|
|
3
|
+
export type ServerCallResponse = {
|
|
4
|
+
return: any;
|
|
5
|
+
entity?: EntityJson;
|
|
6
|
+
};
|
|
7
|
+
export type ServerCallStaticResponse = any;
|
|
8
|
+
export type EntityReference = ['nymph_entity_reference', string, string];
|
|
9
|
+
export type EntityData = {
|
|
10
|
+
[k: string]: any;
|
|
11
|
+
};
|
|
12
|
+
export type SerializedEntityData = {
|
|
13
|
+
[k: string]: string;
|
|
14
|
+
};
|
|
15
|
+
export type EntityJson<T extends EntityConstructor = EntityConstructor> = {
|
|
16
|
+
class: T['class'];
|
|
17
|
+
guid: string | null;
|
|
18
|
+
cdate: number | null;
|
|
19
|
+
mdate: number | null;
|
|
20
|
+
tags: string[];
|
|
21
|
+
data: EntityData;
|
|
22
|
+
};
|
|
23
|
+
export type EntityPatch = {
|
|
24
|
+
class: string;
|
|
25
|
+
guid: string;
|
|
26
|
+
mdate: number | null;
|
|
27
|
+
set: EntityData;
|
|
28
|
+
unset: string[];
|
|
29
|
+
addTags: string[];
|
|
30
|
+
removeTags: string[];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Data Object interface.
|
|
34
|
+
*
|
|
35
|
+
* Objects which hold data from some type of storage.
|
|
36
|
+
*/
|
|
37
|
+
export interface DataObjectInterface {
|
|
38
|
+
/**
|
|
39
|
+
* Search the array for this object and return the corresponding index.
|
|
40
|
+
*
|
|
41
|
+
* If `strict` is false, `is()` is used to compare. If `strict` is true,
|
|
42
|
+
* `equals()` is used.
|
|
43
|
+
*
|
|
44
|
+
* @param array The array to search.
|
|
45
|
+
* @param strict Whether to use stronger comparison.
|
|
46
|
+
* @returns The index if the object is in the array, -1 if it isn't.
|
|
47
|
+
*/
|
|
48
|
+
$arraySearch(array: any[], strict?: boolean): number;
|
|
49
|
+
/**
|
|
50
|
+
* Delete the object from storage.
|
|
51
|
+
*
|
|
52
|
+
* @returns True on success, false on failure.
|
|
53
|
+
*/
|
|
54
|
+
$delete(): Promise<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* Perform a more strict comparison of this object to another.
|
|
57
|
+
*
|
|
58
|
+
* @param object The object to compare.
|
|
59
|
+
* @returns True or false.
|
|
60
|
+
*/
|
|
61
|
+
$equals(object: any): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Check whether this object is in an array.
|
|
64
|
+
*
|
|
65
|
+
* If `strict` is false, `is()` is used to compare. If `strict` is true,
|
|
66
|
+
* `equals()` is used.
|
|
67
|
+
*
|
|
68
|
+
* @param array The array to search.
|
|
69
|
+
* @param strict Whether to use stronger comparison.
|
|
70
|
+
* @returns True if the object is in the array, false if it isn't.
|
|
71
|
+
*/
|
|
72
|
+
$inArray(array: any[], strict?: boolean): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Perform a less strict comparison of this object to another.
|
|
75
|
+
*
|
|
76
|
+
* @param object The object to compare.
|
|
77
|
+
* @returns True or false.
|
|
78
|
+
*/
|
|
79
|
+
$is(object: any): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Save the object's dirty data to storage.
|
|
82
|
+
*
|
|
83
|
+
* @returns True on success, false on failure.
|
|
84
|
+
*/
|
|
85
|
+
$patch(): Promise<boolean>;
|
|
86
|
+
/**
|
|
87
|
+
* Refresh the object from storage. (Bypasses Nymph's cache.)
|
|
88
|
+
*
|
|
89
|
+
* If the object has been deleted from storage, the database cannot be
|
|
90
|
+
* reached, or a database error occurs, `refresh()` will return 0.
|
|
91
|
+
*
|
|
92
|
+
* @returns False if the data has not been saved, 0 if it can't be refreshed, true on success.
|
|
93
|
+
*/
|
|
94
|
+
$refresh(): Promise<boolean | 0>;
|
|
95
|
+
/**
|
|
96
|
+
* Save the object to storage.
|
|
97
|
+
*
|
|
98
|
+
* @returns True on success, false on failure.
|
|
99
|
+
*/
|
|
100
|
+
$save(): Promise<boolean>;
|
|
101
|
+
/**
|
|
102
|
+
* The object's data.
|
|
103
|
+
*/
|
|
104
|
+
[k: string]: any;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Entity interface.
|
|
108
|
+
*/
|
|
109
|
+
export interface EntityInterface extends DataObjectInterface {
|
|
110
|
+
/**
|
|
111
|
+
* The instance of Nymph to use for queries.
|
|
112
|
+
*/
|
|
113
|
+
$nymph: Nymph;
|
|
114
|
+
/**
|
|
115
|
+
* The entity's Globally Unique ID.
|
|
116
|
+
*
|
|
117
|
+
* This is a 12 byte number represented as a lower case HEX string (24
|
|
118
|
+
* characters).
|
|
119
|
+
*/
|
|
120
|
+
guid: string | null;
|
|
121
|
+
/**
|
|
122
|
+
* The creation date of the entity as a Unix timestamp in milliseconds.
|
|
123
|
+
*/
|
|
124
|
+
cdate: number | null;
|
|
125
|
+
/**
|
|
126
|
+
* The modified date of the entity as a Unix timestamp in milliseconds.
|
|
127
|
+
*/
|
|
128
|
+
mdate: number | null;
|
|
129
|
+
/**
|
|
130
|
+
* Array of the entity's tags.
|
|
131
|
+
*/
|
|
132
|
+
tags: string[];
|
|
133
|
+
/**
|
|
134
|
+
* Add one or more tags.
|
|
135
|
+
*
|
|
136
|
+
* @param tags List of tags.
|
|
137
|
+
*/
|
|
138
|
+
$addTag(...tags: string[]): void;
|
|
139
|
+
/**
|
|
140
|
+
* Get a patch of this entity's dirty data to be applied on the server.
|
|
141
|
+
*/
|
|
142
|
+
$getPatch(): EntityPatch;
|
|
143
|
+
/**
|
|
144
|
+
* Check that the entity has all of the given tags.
|
|
145
|
+
*
|
|
146
|
+
* @param tags List of tags.
|
|
147
|
+
* @returns True or false.
|
|
148
|
+
*/
|
|
149
|
+
$hasTag(...tags: string[]): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Initialize this entity from a JSON representation.
|
|
152
|
+
*
|
|
153
|
+
* @param entityJson The entity JSON.
|
|
154
|
+
*/
|
|
155
|
+
$init(entityJson: EntityJson | null): EntityInterface;
|
|
156
|
+
/**
|
|
157
|
+
* Retrieve this entity's data from the server.
|
|
158
|
+
*
|
|
159
|
+
* @returns The entity.
|
|
160
|
+
*/
|
|
161
|
+
$wake(): Promise<EntityInterface>;
|
|
162
|
+
/**
|
|
163
|
+
* Ready this entity's data, and the data of entity's within this one's.
|
|
164
|
+
*
|
|
165
|
+
* @param level The number of levels deep to wake. If undefined, it will keep going until there are no more entities. (Careful of infinite loops.)
|
|
166
|
+
* @returns The entity.
|
|
167
|
+
*/
|
|
168
|
+
$wakeAll(level?: number): Promise<EntityInterface>;
|
|
169
|
+
/**
|
|
170
|
+
* Remove one or more tags.
|
|
171
|
+
*
|
|
172
|
+
* @param tags List of tags.
|
|
173
|
+
*/
|
|
174
|
+
$removeTag(...tags: string[]): void;
|
|
175
|
+
/**
|
|
176
|
+
* Call an instance method on the server version of this entity.
|
|
177
|
+
*
|
|
178
|
+
* The entity's data will be sent up to the server as well, so the server's
|
|
179
|
+
* state can match the client's state. It won't be propagated into the DB,
|
|
180
|
+
* though.
|
|
181
|
+
*
|
|
182
|
+
* @param method The name of the method.
|
|
183
|
+
* @param params The parameters to call the method with.
|
|
184
|
+
* @param stateless Whether the server should return, and the client update, the data in the entity after the method has run.
|
|
185
|
+
* @returns The value that the method on the server returned.
|
|
186
|
+
*/
|
|
187
|
+
$serverCall(method: string, params: Iterable<any>, stateless: boolean): Promise<any>;
|
|
188
|
+
/**
|
|
189
|
+
* Return a Nymph Entity Reference for this entity.
|
|
190
|
+
*
|
|
191
|
+
* If the entity hasn't been saved yet (and has no GUID), it will be
|
|
192
|
+
* returned instead.
|
|
193
|
+
*
|
|
194
|
+
* @returns A Nymph Entity Reference array as an unsaved entity.
|
|
195
|
+
*/
|
|
196
|
+
$toReference(): EntityReference | EntityInterface;
|
|
197
|
+
}
|
|
198
|
+
export type EntityConstructor<D extends EntityData = EntityData, E extends Entity<D> = Entity<D>> = (new (...args: any[]) => E) & {
|
|
199
|
+
[k in keyof typeof Entity]: (typeof Entity)[k];
|
|
200
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Entity.types.js","sourceRoot":"","sources":["../src/Entity.types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { EntityConstructor, EntityInterface } from './Entity.types';
|
|
2
|
+
export default class EntityWeakCache {
|
|
3
|
+
private references;
|
|
4
|
+
get(EntityClass: EntityConstructor, guid: string): EntityInterface | null;
|
|
5
|
+
set(EntityClass: EntityConstructor, entity: EntityInterface): void;
|
|
6
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class EntityWeakCache {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.references = new WeakMap();
|
|
6
|
+
}
|
|
7
|
+
get(EntityClass, guid) {
|
|
8
|
+
const classMap = this.references.get(EntityClass);
|
|
9
|
+
if (classMap && guid in classMap) {
|
|
10
|
+
const weakRef = classMap[guid];
|
|
11
|
+
const deref = weakRef && weakRef.deref();
|
|
12
|
+
if (deref != null) {
|
|
13
|
+
return deref;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
delete classMap[guid];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
set(EntityClass, entity) {
|
|
22
|
+
if (!entity.guid) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const weakRef = new WeakRef(entity);
|
|
26
|
+
const classMap = this.references.get(EntityClass) || {};
|
|
27
|
+
this.references.set(EntityClass, classMap);
|
|
28
|
+
classMap[entity.guid] = weakRef;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.default = EntityWeakCache;
|
|
32
|
+
//# sourceMappingURL=EntityWeakCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EntityWeakCache.js","sourceRoot":"","sources":["../src/EntityWeakCache.ts"],"names":[],"mappings":";;AAEA,MAAqB,eAAe;IAApC;QACU,eAAU,GAGd,IAAI,OAAO,EAAE,CAAC;IA2BpB,CAAC;IAzBC,GAAG,CAAC,WAA8B,EAAE,IAAY;QAC9C,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAClD,IAAI,QAAQ,IAAI,IAAI,IAAI,QAAQ,EAAE,CAAC;YACjC,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;YAC/B,MAAM,KAAK,GAAG,OAAO,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;YACzC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;gBAClB,OAAO,KAAK,CAAC;YACf,CAAC;iBAAM,CAAC;gBACN,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;YACxB,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,GAAG,CAAC,WAA8B,EAAE,MAAuB;QACzD,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACjB,OAAO;QACT,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;QAEpC,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;QACxD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;QAC3C,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;IAClC,CAAC;CACF;AA/BD,kCA+BC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export type HttpRequesterEventType = 'request' | 'response';
|
|
2
|
+
export type HttpRequesterRequestCallback = (requester: HttpRequester, url: string, options: RequestInit) => void;
|
|
3
|
+
export type HttpRequesterResponseCallback = (requester: HttpRequester, response: Response, text: string) => void;
|
|
4
|
+
export type HttpRequesterIteratorCallback = (requester: HttpRequester, url: string, headers: Record<string, string>) => void;
|
|
5
|
+
export type HttpRequesterRequestOptions = {
|
|
6
|
+
url: string;
|
|
7
|
+
headers?: {
|
|
8
|
+
[k: string]: any;
|
|
9
|
+
};
|
|
10
|
+
data: {
|
|
11
|
+
[k: string]: any;
|
|
12
|
+
};
|
|
13
|
+
dataType: string;
|
|
14
|
+
};
|
|
15
|
+
export interface AbortableAsyncIterator<T extends any = any> extends AsyncIterable<T> {
|
|
16
|
+
abortController: AbortController;
|
|
17
|
+
}
|
|
18
|
+
export default class HttpRequester {
|
|
19
|
+
private fetch;
|
|
20
|
+
private requestCallbacks;
|
|
21
|
+
private responseCallbacks;
|
|
22
|
+
private iteratorCallbacks;
|
|
23
|
+
static makeUrl(url: string, data: {
|
|
24
|
+
[k: string]: any;
|
|
25
|
+
}): string;
|
|
26
|
+
constructor(ponyFetch?: WindowOrWorkerGlobalScope['fetch']);
|
|
27
|
+
on<T extends HttpRequesterEventType>(event: T, callback: T extends 'request' ? HttpRequesterRequestCallback : T extends 'response' ? HttpRequesterResponseCallback : T extends 'iterator' ? HttpRequesterIteratorCallback : never): () => boolean;
|
|
28
|
+
off<T extends HttpRequesterEventType>(event: T, callback: T extends 'request' ? HttpRequesterRequestCallback : T extends 'response' ? HttpRequesterResponseCallback : T extends 'iterator' ? HttpRequesterIteratorCallback : never): boolean;
|
|
29
|
+
GET(opt: HttpRequesterRequestOptions): Promise<any>;
|
|
30
|
+
POST(opt: HttpRequesterRequestOptions): Promise<any>;
|
|
31
|
+
POST_ITERATOR(opt: HttpRequesterRequestOptions): Promise<AbortableAsyncIterator<any>>;
|
|
32
|
+
PUT(opt: HttpRequesterRequestOptions): Promise<any>;
|
|
33
|
+
PATCH(opt: HttpRequesterRequestOptions): Promise<any>;
|
|
34
|
+
DELETE(opt: HttpRequesterRequestOptions): Promise<any>;
|
|
35
|
+
_httpRequest(method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', opt: HttpRequesterRequestOptions): Promise<any>;
|
|
36
|
+
_iteratorRequest(method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', opt: HttpRequesterRequestOptions): Promise<AbortableAsyncIterator>;
|
|
37
|
+
}
|
|
38
|
+
export declare class InvalidResponseError extends Error {
|
|
39
|
+
constructor(message: string);
|
|
40
|
+
}
|
|
41
|
+
export declare class ConnectionClosedUnexpectedlyError extends Error {
|
|
42
|
+
constructor(message: string);
|
|
43
|
+
}
|
|
44
|
+
export declare class ConnectionError extends Error {
|
|
45
|
+
constructor(message: string);
|
|
46
|
+
}
|
|
47
|
+
export declare class HttpError extends Error {
|
|
48
|
+
status: number;
|
|
49
|
+
statusText: string;
|
|
50
|
+
constructor(name: string, response: Response, errObj: {
|
|
51
|
+
textStatus: string;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
export declare class InformationalError extends HttpError {
|
|
55
|
+
constructor(response: Response, errObj: {
|
|
56
|
+
textStatus: string;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export declare class SuccessError extends HttpError {
|
|
60
|
+
constructor(response: Response, errObj: {
|
|
61
|
+
textStatus: string;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
export declare class RedirectError extends HttpError {
|
|
65
|
+
constructor(response: Response, errObj: {
|
|
66
|
+
textStatus: string;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
export declare class ClientError extends HttpError {
|
|
70
|
+
constructor(response: Response, errObj: {
|
|
71
|
+
textStatus: string;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
export declare class ServerError extends HttpError {
|
|
75
|
+
constructor(response: Response, errObj: {
|
|
76
|
+
textStatus: string;
|
|
77
|
+
});
|
|
78
|
+
}
|