zavadil-ts-common 1.1.40 → 1.1.41
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/package.json +1 -1
- package/src/cache/index.ts +5 -0
- package/src/client/EntityCachedClient.ts +30 -0
- package/src/client/EntityClient.ts +35 -0
- package/src/client/EntityClientWithStub.ts +26 -0
- package/src/client/LookupClient.ts +45 -0
- package/src/client/index.ts +6 -0
- package/src/component/index.ts +1 -8
- package/src/index.ts +5 -2
- package/src/{component → oauth}/OAuthRestClient.ts +1 -1
- package/src/{component → oauth}/RestClientWithOAuth.ts +1 -1
- package/src/oauth/index.ts +6 -0
- /package/src/{component → cache}/CacheAsync.ts +0 -0
- /package/src/{component → cache}/HashCacheAsync.ts +0 -0
- /package/src/{component → cache}/Lazy.ts +0 -0
- /package/src/{component → cache}/LazyAsync.ts +0 -0
- /package/src/{component → client}/RestClient.ts +0 -0
- /package/src/{component → oauth}/OAuthSubject.ts +0 -0
- /package/src/{component → oauth}/OAuthTokenManager.ts +0 -0
package/package.json
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
import {HashCacheAsync, RestClient} from "zavadil-ts-common";
|
2
|
+
import {EntityClient} from "./EntityClient";
|
3
|
+
import {EntityBase} from "../../../oauth-server/src/ui/src/types/entity/EntityBase";
|
4
|
+
|
5
|
+
export class EntityCachedClient<T extends EntityBase> extends EntityClient<T> {
|
6
|
+
|
7
|
+
private cache: HashCacheAsync<number, T>;
|
8
|
+
|
9
|
+
constructor(client: RestClient, name: string, maxSize?: number) {
|
10
|
+
super(client, name);
|
11
|
+
this.cache = new HashCacheAsync<number, T>((id: number) => super.loadSingle(id), maxSize);
|
12
|
+
}
|
13
|
+
|
14
|
+
loadSingle(id: number): Promise<T> {
|
15
|
+
return this.cache.get(id);
|
16
|
+
}
|
17
|
+
|
18
|
+
save(d: T): Promise<T> {
|
19
|
+
return super.save(d).then(
|
20
|
+
(s: T): T => {
|
21
|
+
if (s.id) this.cache.set(s.id, s);
|
22
|
+
return s;
|
23
|
+
});
|
24
|
+
}
|
25
|
+
|
26
|
+
delete(id: number): Promise<any> {
|
27
|
+
return super.delete(id).then(() => this.cache.reset(id));
|
28
|
+
}
|
29
|
+
|
30
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import {Page, PagingRequest, RestClient} from "zavadil-ts-common";
|
2
|
+
import {EntityBase} from "../../../oauth-server/src/ui/src/types/entity/EntityBase";
|
3
|
+
|
4
|
+
export class EntityClient<T extends EntityBase> {
|
5
|
+
|
6
|
+
client: RestClient;
|
7
|
+
|
8
|
+
name: string;
|
9
|
+
|
10
|
+
constructor(client: RestClient, name: string) {
|
11
|
+
this.client = client;
|
12
|
+
this.name = name;
|
13
|
+
}
|
14
|
+
|
15
|
+
loadSingle(id: number): Promise<T> {
|
16
|
+
return this.client.getJson(`${this.name}/${id}`);
|
17
|
+
}
|
18
|
+
|
19
|
+
loadPage(pr: PagingRequest): Promise<Page<T>> {
|
20
|
+
return this.client.getJson(this.name, RestClient.pagingRequestToQueryParams(pr));
|
21
|
+
}
|
22
|
+
|
23
|
+
save(d: T): Promise<T> {
|
24
|
+
if (d.id) {
|
25
|
+
return this.client.putJson(`${this.name}/${d.id}`, d);
|
26
|
+
} else {
|
27
|
+
return this.client.postJson(this.name, d);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
delete(id: number): Promise<any> {
|
32
|
+
return this.client.del(`${this.name}/${id}`);
|
33
|
+
}
|
34
|
+
|
35
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import {EntityBase} from "../../../oauth-server/src/ui/src/types/entity/EntityBase";
|
2
|
+
import {EntityClient} from "./EntityClient";
|
3
|
+
|
4
|
+
export class EntityClientWithStub<T extends EntityBase, TStub extends EntityBase> extends EntityClient<T> {
|
5
|
+
|
6
|
+
loadSingle(id: number): Promise<T> {
|
7
|
+
throw new Error("Use loadSingleStub() instead!");
|
8
|
+
}
|
9
|
+
|
10
|
+
loadSingleStub(id: number): Promise<TStub> {
|
11
|
+
return this.client.getJson(`${this.name}/${id}`);
|
12
|
+
}
|
13
|
+
|
14
|
+
save(d: T): Promise<T> {
|
15
|
+
throw new Error("Use saveStub() instead!");
|
16
|
+
}
|
17
|
+
|
18
|
+
saveStub(d: TStub): Promise<TStub> {
|
19
|
+
if (d.id) {
|
20
|
+
return this.client.putJson(`${this.name}/${d.id}`, d);
|
21
|
+
} else {
|
22
|
+
return this.client.postJson(this.name, d);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import {LazyAsync, RestClient} from "zavadil-ts-common";
|
2
|
+
import {EntityBase} from "../../../oauth-server/src/ui/src/types/entity/EntityBase";
|
3
|
+
import {EntityClient} from "./EntityClient";
|
4
|
+
|
5
|
+
export class LookupClient<T extends EntityBase> extends EntityClient<T> {
|
6
|
+
|
7
|
+
private cache: LazyAsync<Array<T>>;
|
8
|
+
|
9
|
+
constructor(client: RestClient, name: string) {
|
10
|
+
super(client, name);
|
11
|
+
this.cache = new LazyAsync<Array<T>>(() => this.loadAllInternal());
|
12
|
+
}
|
13
|
+
|
14
|
+
private loadAllInternal(): Promise<Array<T>> {
|
15
|
+
return this.client.getJson(`${this.name}/all`);
|
16
|
+
}
|
17
|
+
|
18
|
+
loadAll(): Promise<Array<T>> {
|
19
|
+
return this.cache.get();
|
20
|
+
}
|
21
|
+
|
22
|
+
loadSingle(id: number): Promise<T> {
|
23
|
+
// @ts-ignore
|
24
|
+
return this.loadAll().then(
|
25
|
+
(all: Array<T>) => {
|
26
|
+
const d = all.find((l) => l.id === id);
|
27
|
+
if (id === undefined) throw new Error(`${this.name} id ${id} not found!`);
|
28
|
+
return d;
|
29
|
+
}
|
30
|
+
);
|
31
|
+
}
|
32
|
+
|
33
|
+
save(d: T): Promise<T> {
|
34
|
+
return super.save(d)
|
35
|
+
.then((s) => {
|
36
|
+
this.cache.reset();
|
37
|
+
return s;
|
38
|
+
});
|
39
|
+
}
|
40
|
+
|
41
|
+
delete(id: number): Promise<any> {
|
42
|
+
return super.delete(id).then(() => this.cache.reset());
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
package/src/component/index.ts
CHANGED
@@ -1,11 +1,4 @@
|
|
1
1
|
export * from './EventManager';
|
2
2
|
export * from './UserAlerts';
|
3
|
-
export * from './RestClient';
|
4
|
-
export * from './OAuthRestClient';
|
5
|
-
export * from './OAuthTokenManager';
|
6
|
-
export * from './OAuthSubject';
|
7
3
|
export * from './CancellablePromise';
|
8
|
-
|
9
|
-
export * from './LazyAsync';
|
10
|
-
export * from './CacheAsync';
|
11
|
-
export * from './HashCacheAsync';
|
4
|
+
|
package/src/index.ts
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|