@coasys/hooks-helpers 0.10.0-rc7.local-ai.3 → 0.10.0

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 CHANGED
@@ -1,22 +1,22 @@
1
- {
2
- "name": "@coasys/hooks-helpers",
3
- "version": "0.10.0-rc7.local-ai.3",
4
- "description": "",
5
- "main": "./src/index.ts",
6
- "module": "./src/index.ts",
7
- "private": false,
8
- "type": "module",
9
- "files": [
10
- "dist",
11
- "src"
12
- ],
13
- "scripts": {},
14
- "keywords": [],
15
- "author": "",
16
- "license": "ISC",
17
- "dependencies": {
18
- "@coasys/ad4m": "*",
19
- "@coasys/ad4m-connect": "*",
20
- "uuid": "*"
21
- }
22
- }
1
+ {
2
+ "name": "@coasys/hooks-helpers",
3
+ "version": "0.10.0",
4
+ "description": "",
5
+ "main": "./src/index.ts",
6
+ "module": "./src/index.ts",
7
+ "private": false,
8
+ "type": "module",
9
+ "files": [
10
+ "dist",
11
+ "src"
12
+ ],
13
+ "scripts": {},
14
+ "keywords": [],
15
+ "author": "",
16
+ "license": "ISC",
17
+ "dependencies": {
18
+ "@coasys/ad4m": "*",
19
+ "@coasys/ad4m-connect": "*",
20
+ "uuid": "*"
21
+ }
22
+ }
package/src/cache.ts CHANGED
@@ -1,70 +1,70 @@
1
- import { PerspectiveProxy } from "@coasys/ad4m";
2
-
3
- const cache: Map<string, any> = new Map();
4
- const subscribers: Map<string, Function[]> = new Map();
5
-
6
- export function getCache<T>(key: string) {
7
- const match: T | undefined = cache.get(key);
8
- return match;
9
- }
10
-
11
- export function setCache<T>(key: string, value: T) {
12
- cache.set(key, value);
13
- getSubscribers(key).forEach((cb) => cb());
14
- }
15
-
16
- export function subscribe(key: string, callback: Function) {
17
- getSubscribers(key).push(callback);
18
- }
19
-
20
- export function unsubscribe(key: string, callback: Function) {
21
- const subs = getSubscribers(key);
22
- const index = subs.indexOf(callback);
23
- if (index >= 0) {
24
- subs.splice(index, 1);
25
- }
26
- }
27
-
28
- export function getSubscribers(key: string) {
29
- if (!subscribers.has(key)) subscribers.set(key, []);
30
- return subscribers.get(key)!;
31
- }
32
-
33
- export function subscribeToPerspective(
34
- perspective: PerspectiveProxy,
35
- added: Function,
36
- removed: Function
37
- ) {
38
- const addedKey = `perspective-${perspective.uuid}-added`;
39
- const removedKey = `perspective-${perspective.uuid}-removed`;
40
-
41
- if (!subscribers.has(addedKey)) {
42
- console.log("subscribing!");
43
- perspective.addListener("link-added", (link) => {
44
- subscribers.get(addedKey).forEach((cb) => cb(link));
45
- return null;
46
- });
47
- }
48
-
49
- if (!subscribers.has(removedKey)) {
50
- perspective.addListener("link-removed", (link) => {
51
- subscribers.get(removedKey).forEach((cb) => cb(link));
52
- return null;
53
- });
54
- }
55
-
56
- subscribe(addedKey, added);
57
- subscribe(removedKey, removed);
58
- }
59
-
60
- export function unsubscribeFromPerspective(
61
- perspective: PerspectiveProxy,
62
- added: Function,
63
- removed: Function
64
- ) {
65
- const addedKey = `perspective-${perspective.uuid}-added`;
66
- const removedKey = `perspective-${perspective.uuid}-removed`;
67
-
68
- unsubscribe(addedKey, added);
69
- unsubscribe(removedKey, removed);
70
- }
1
+ import { PerspectiveProxy } from "@coasys/ad4m";
2
+
3
+ const cache: Map<string, any> = new Map();
4
+ const subscribers: Map<string, Function[]> = new Map();
5
+
6
+ export function getCache<T>(key: string) {
7
+ const match: T | undefined = cache.get(key);
8
+ return match;
9
+ }
10
+
11
+ export function setCache<T>(key: string, value: T) {
12
+ cache.set(key, value);
13
+ getSubscribers(key).forEach((cb) => cb());
14
+ }
15
+
16
+ export function subscribe(key: string, callback: Function) {
17
+ getSubscribers(key).push(callback);
18
+ }
19
+
20
+ export function unsubscribe(key: string, callback: Function) {
21
+ const subs = getSubscribers(key);
22
+ const index = subs.indexOf(callback);
23
+ if (index >= 0) {
24
+ subs.splice(index, 1);
25
+ }
26
+ }
27
+
28
+ export function getSubscribers(key: string) {
29
+ if (!subscribers.has(key)) subscribers.set(key, []);
30
+ return subscribers.get(key)!;
31
+ }
32
+
33
+ export function subscribeToPerspective(
34
+ perspective: PerspectiveProxy,
35
+ added: Function,
36
+ removed: Function
37
+ ) {
38
+ const addedKey = `perspective-${perspective.uuid}-added`;
39
+ const removedKey = `perspective-${perspective.uuid}-removed`;
40
+
41
+ if (!subscribers.has(addedKey)) {
42
+ console.log("subscribing!");
43
+ perspective.addListener("link-added", (link) => {
44
+ subscribers.get(addedKey).forEach((cb) => cb(link));
45
+ return null;
46
+ });
47
+ }
48
+
49
+ if (!subscribers.has(removedKey)) {
50
+ perspective.addListener("link-removed", (link) => {
51
+ subscribers.get(removedKey).forEach((cb) => cb(link));
52
+ return null;
53
+ });
54
+ }
55
+
56
+ subscribe(addedKey, added);
57
+ subscribe(removedKey, removed);
58
+ }
59
+
60
+ export function unsubscribeFromPerspective(
61
+ perspective: PerspectiveProxy,
62
+ added: Function,
63
+ removed: Function
64
+ ) {
65
+ const addedKey = `perspective-${perspective.uuid}-added`;
66
+ const removedKey = `perspective-${perspective.uuid}-removed`;
67
+
68
+ unsubscribe(addedKey, added);
69
+ unsubscribe(removedKey, removed);
70
+ }
@@ -1,245 +1,245 @@
1
- import {
2
- PerspectiveProxy,
3
- Link,
4
- Subject,
5
- Literal,
6
- LinkQuery,
7
- } from "@coasys/ad4m";
8
- import { setProperties } from "./model";
9
- import { v4 as uuidv4 } from "uuid";
10
-
11
- export const SELF = "ad4m://self";
12
-
13
- export type ModelProps = {
14
- perspective: PerspectiveProxy;
15
- source?: string;
16
- };
17
-
18
- export class SubjectRepository<SubjectClass extends { [x: string]: any }> {
19
- source = SELF;
20
- subject: SubjectClass | string;
21
- perspective: PerspectiveProxy;
22
- tempSubject: any | string;
23
-
24
- constructor(subject: { new (): SubjectClass } | string, props: ModelProps) {
25
- this.perspective = props.perspective;
26
- this.source = props.source || this.source;
27
- this.subject = typeof subject === "string" ? subject : new subject();
28
- this.tempSubject = subject;
29
- }
30
-
31
- get className(): string {
32
- return typeof this.subject === "string"
33
- ? this.subject
34
- : this.subject.className;
35
- }
36
-
37
- async ensureSubject() {
38
- if (typeof this.tempSubject === "string") return;
39
- await this.perspective.ensureSDNASubjectClass(this.tempSubject);
40
- }
41
-
42
- async create(
43
- data: SubjectClass,
44
- id?: string,
45
- source?: string
46
- ): Promise<SubjectClass> {
47
- await this.ensureSubject();
48
- const base = id || Literal.from(uuidv4()).toUrl();
49
-
50
- let newInstance = await this.perspective.createSubject(this.subject, base);
51
-
52
- if (!newInstance) {
53
- throw "Failed to create new instance of " + this.subject;
54
- }
55
-
56
- // Connect new instance to source
57
- await this.perspective.add(
58
- new Link({
59
- source: source || this.source,
60
- predicate: "ad4m://has_child",
61
- target: base,
62
- })
63
- );
64
-
65
- Object.keys(data).forEach((key) =>
66
- data[key] === undefined || data[key] === null ? delete data[key] : {}
67
- );
68
-
69
- setProperties(newInstance, data);
70
-
71
- // @ts-ignore
72
- return this.getSubjectData(newInstance);
73
- }
74
-
75
- async update(id: string, data: QueryPartialEntity<SubjectClass>) {
76
- const instance = await this.get(id);
77
-
78
- if (!instance) {
79
- throw "Failed to find instance of " + this.subject + " with id " + id;
80
- }
81
-
82
- Object.keys(data).forEach((key) =>
83
- data[key] === undefined ? delete data[key] : {}
84
- );
85
-
86
- // @ts-ignore
87
- setProperties(instance, data);
88
-
89
- return this.getSubjectData(instance);
90
- }
91
-
92
- async remove(id: string) {
93
- if (this.perspective) {
94
- const linksTo = await this.perspective.get(new LinkQuery({ target: id }));
95
- const linksFrom = await this.perspective.get(
96
- new LinkQuery({ source: id })
97
- );
98
- this.perspective.removeLinks([...linksFrom, ...linksTo]);
99
- }
100
- }
101
-
102
- async get(id: string): Promise<SubjectClass | null> {
103
- if (id) {
104
- await this.ensureSubject();
105
- const subjectProxy = await this.perspective.getSubjectProxy(
106
- id,
107
- this.subject
108
- );
109
-
110
- // @ts-ignore
111
- return subjectProxy || null;
112
- } else {
113
- const all = await this.getAll();
114
- return all[0] || null;
115
- }
116
- }
117
-
118
- async getData(id: string): Promise<SubjectClass | string | null> {
119
- const entry = await this.get(id);
120
- if (entry) {
121
- // @ts-ignore
122
- return await this.getSubjectData(entry);
123
- }
124
-
125
- return null;
126
- }
127
-
128
- private async getSubjectData(entry: any) {
129
- let links = await this.perspective.get(
130
- new LinkQuery({ source: entry.baseExpression })
131
- );
132
-
133
- let data: any = await this.perspective.getSubjectData(this.subject, entry.baseExpression)
134
-
135
- for (const key in data) {
136
- if (this.tempSubject.prototype?.__properties[key]?.transform) {
137
- data[key] =
138
- this.tempSubject.prototype.__properties[key].transform(data[key]);
139
- }
140
- }
141
-
142
- return {
143
- id: entry.baseExpression,
144
- timestamp: links[0].timestamp,
145
- author: links[0].author,
146
- ...data,
147
- }
148
- }
149
-
150
- async getAll(source?: string, query?: QueryOptions): Promise<SubjectClass[]> {
151
- await this.ensureSubject();
152
-
153
- const tempSource = source || this.source;
154
-
155
- let res = [];
156
-
157
- if (query) {
158
- try {
159
- const queryResponse = (
160
- await this.perspective.infer(
161
- `findall([Timestamp, Base], (subject_class("${this.className}", C), instance(C, Base), link("${tempSource}", Predicate, Base, Timestamp, Author)), AllData), length(AllData, DataLength), sort(AllData, SortedData).`
162
- )
163
- )[0];
164
-
165
- if (queryResponse.SortedData >= query.size) {
166
- const isOutofBound =
167
- query.size * query.page > queryResponse.DataLength;
168
-
169
- const newPageSize = isOutofBound
170
- ? queryResponse.DataLength - query.size * (query.page - 1)
171
- : query.size;
172
-
173
- const mainQuery = `findall([Timestamp, Base], (subject_class("${this.className}", C), instance(C, Base), link("${tempSource}", Predicate, Base, Timestamp, Author)), AllData), sort(AllData, SortedData), reverse(SortedData, ReverseSortedData), paginate(ReverseSortedData, ${query.page}, ${newPageSize}, PageData).`;
174
- res = await this.perspective.infer(mainQuery);
175
-
176
- res = res[0].PageData.map((r) => ({
177
- Base: r[1],
178
- Timestamp: r[0],
179
- }));
180
- } else {
181
- res = await this.perspective.infer(
182
- `subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
183
- );
184
- }
185
- } catch (e) {
186
- console.log("Query failed", e);
187
- }
188
- } else {
189
- res = await this.perspective.infer(
190
- `subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
191
- );
192
- }
193
-
194
- const results =
195
- res &&
196
- res.filter(
197
- (obj, index, self) =>
198
- index === self.findIndex((t) => t.Base === obj.Base)
199
- );
200
-
201
- if (!res) return [];
202
-
203
- const data = await Promise.all(
204
- results.map(async (result) => {
205
- let subject = new Subject(
206
- this.perspective!,
207
- result.Base,
208
- this.className
209
- );
210
-
211
- await subject.init();
212
-
213
- return subject;
214
- })
215
- );
216
-
217
- // @ts-ignore
218
- return data;
219
- }
220
-
221
- async getAllData(
222
- source?: string,
223
- query?: QueryOptions
224
- ): Promise<SubjectClass[]> {
225
- const subjects = await this.getAll(source, query);
226
-
227
- const entries = await Promise.all(
228
- subjects.map((e) => this.getSubjectData(e))
229
- );
230
-
231
- // @ts-ignore
232
- return entries;
233
- }
234
- }
235
-
236
- export type QueryPartialEntity<T> = {
237
- [P in keyof T]?: T[P] | (() => string);
238
- };
239
-
240
- export type QueryOptions = {
241
- page: number;
242
- size: number;
243
- infinite: boolean;
244
- uniqueKey: string;
245
- };
1
+ import {
2
+ PerspectiveProxy,
3
+ Link,
4
+ Subject,
5
+ Literal,
6
+ LinkQuery,
7
+ } from "@coasys/ad4m";
8
+ import { setProperties } from "./model";
9
+ import { v4 as uuidv4 } from "uuid";
10
+
11
+ export const SELF = "ad4m://self";
12
+
13
+ export type ModelProps = {
14
+ perspective: PerspectiveProxy;
15
+ source?: string;
16
+ };
17
+
18
+ export class SubjectRepository<SubjectClass extends { [x: string]: any }> {
19
+ source = SELF;
20
+ subject: SubjectClass | string;
21
+ perspective: PerspectiveProxy;
22
+ tempSubject: any | string;
23
+
24
+ constructor(subject: { new (): SubjectClass } | string, props: ModelProps) {
25
+ this.perspective = props.perspective;
26
+ this.source = props.source || this.source;
27
+ this.subject = typeof subject === "string" ? subject : new subject();
28
+ this.tempSubject = subject;
29
+ }
30
+
31
+ get className(): string {
32
+ return typeof this.subject === "string"
33
+ ? this.subject
34
+ : this.subject.className;
35
+ }
36
+
37
+ async ensureSubject() {
38
+ if (typeof this.tempSubject === "string") return;
39
+ await this.perspective.ensureSDNASubjectClass(this.tempSubject);
40
+ }
41
+
42
+ async create(
43
+ data: SubjectClass,
44
+ id?: string,
45
+ source?: string
46
+ ): Promise<SubjectClass> {
47
+ await this.ensureSubject();
48
+ const base = id || Literal.from(uuidv4()).toUrl();
49
+
50
+ let newInstance = await this.perspective.createSubject(this.subject, base);
51
+
52
+ if (!newInstance) {
53
+ throw "Failed to create new instance of " + this.subject;
54
+ }
55
+
56
+ // Connect new instance to source
57
+ await this.perspective.add(
58
+ new Link({
59
+ source: source || this.source,
60
+ predicate: "ad4m://has_child",
61
+ target: base,
62
+ })
63
+ );
64
+
65
+ Object.keys(data).forEach((key) =>
66
+ data[key] === undefined || data[key] === null ? delete data[key] : {}
67
+ );
68
+
69
+ setProperties(newInstance, data);
70
+
71
+ // @ts-ignore
72
+ return this.getSubjectData(newInstance);
73
+ }
74
+
75
+ async update(id: string, data: QueryPartialEntity<SubjectClass>) {
76
+ const instance = await this.get(id);
77
+
78
+ if (!instance) {
79
+ throw "Failed to find instance of " + this.subject + " with id " + id;
80
+ }
81
+
82
+ Object.keys(data).forEach((key) =>
83
+ data[key] === undefined ? delete data[key] : {}
84
+ );
85
+
86
+ // @ts-ignore
87
+ setProperties(instance, data);
88
+
89
+ return this.getSubjectData(instance);
90
+ }
91
+
92
+ async remove(id: string) {
93
+ if (this.perspective) {
94
+ const linksTo = await this.perspective.get(new LinkQuery({ target: id }));
95
+ const linksFrom = await this.perspective.get(
96
+ new LinkQuery({ source: id })
97
+ );
98
+ this.perspective.removeLinks([...linksFrom, ...linksTo]);
99
+ }
100
+ }
101
+
102
+ async get(id: string): Promise<SubjectClass | null> {
103
+ if (id) {
104
+ await this.ensureSubject();
105
+ const subjectProxy = await this.perspective.getSubjectProxy(
106
+ id,
107
+ this.subject
108
+ );
109
+
110
+ // @ts-ignore
111
+ return subjectProxy || null;
112
+ } else {
113
+ const all = await this.getAll();
114
+ return all[0] || null;
115
+ }
116
+ }
117
+
118
+ async getData(id: string): Promise<SubjectClass | string | null> {
119
+ const entry = await this.get(id);
120
+ if (entry) {
121
+ // @ts-ignore
122
+ return await this.getSubjectData(entry);
123
+ }
124
+
125
+ return null;
126
+ }
127
+
128
+ private async getSubjectData(entry: any) {
129
+ let links = await this.perspective.get(
130
+ new LinkQuery({ source: entry.baseExpression })
131
+ );
132
+
133
+ let data: any = await this.perspective.getSubjectData(this.subject, entry.baseExpression)
134
+
135
+ for (const key in data) {
136
+ if (this.tempSubject.prototype?.__properties[key]?.transform) {
137
+ data[key] =
138
+ this.tempSubject.prototype.__properties[key].transform(data[key]);
139
+ }
140
+ }
141
+
142
+ return {
143
+ id: entry.baseExpression,
144
+ timestamp: links[0].timestamp,
145
+ author: links[0].author,
146
+ ...data,
147
+ }
148
+ }
149
+
150
+ async getAll(source?: string, query?: QueryOptions): Promise<SubjectClass[]> {
151
+ await this.ensureSubject();
152
+
153
+ const tempSource = source || this.source;
154
+
155
+ let res = [];
156
+
157
+ if (query) {
158
+ try {
159
+ const queryResponse = (
160
+ await this.perspective.infer(
161
+ `findall([Timestamp, Base], (subject_class("${this.className}", C), instance(C, Base), link("${tempSource}", Predicate, Base, Timestamp, Author)), AllData), length(AllData, DataLength), sort(AllData, SortedData).`
162
+ )
163
+ )[0];
164
+
165
+ if (queryResponse.SortedData >= query.size) {
166
+ const isOutofBound =
167
+ query.size * query.page > queryResponse.DataLength;
168
+
169
+ const newPageSize = isOutofBound
170
+ ? queryResponse.DataLength - query.size * (query.page - 1)
171
+ : query.size;
172
+
173
+ const mainQuery = `findall([Timestamp, Base], (subject_class("${this.className}", C), instance(C, Base), link("${tempSource}", Predicate, Base, Timestamp, Author)), AllData), sort(AllData, SortedData), reverse(SortedData, ReverseSortedData), paginate(ReverseSortedData, ${query.page}, ${newPageSize}, PageData).`;
174
+ res = await this.perspective.infer(mainQuery);
175
+
176
+ res = res[0].PageData.map((r) => ({
177
+ Base: r[1],
178
+ Timestamp: r[0],
179
+ }));
180
+ } else {
181
+ res = await this.perspective.infer(
182
+ `subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
183
+ );
184
+ }
185
+ } catch (e) {
186
+ console.log("Query failed", e);
187
+ }
188
+ } else {
189
+ res = await this.perspective.infer(
190
+ `subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
191
+ );
192
+ }
193
+
194
+ const results =
195
+ res &&
196
+ res.filter(
197
+ (obj, index, self) =>
198
+ index === self.findIndex((t) => t.Base === obj.Base)
199
+ );
200
+
201
+ if (!res) return [];
202
+
203
+ const data = await Promise.all(
204
+ results.map(async (result) => {
205
+ let subject = new Subject(
206
+ this.perspective!,
207
+ result.Base,
208
+ this.className
209
+ );
210
+
211
+ await subject.init();
212
+
213
+ return subject;
214
+ })
215
+ );
216
+
217
+ // @ts-ignore
218
+ return data;
219
+ }
220
+
221
+ async getAllData(
222
+ source?: string,
223
+ query?: QueryOptions
224
+ ): Promise<SubjectClass[]> {
225
+ const subjects = await this.getAll(source, query);
226
+
227
+ const entries = await Promise.all(
228
+ subjects.map((e) => this.getSubjectData(e))
229
+ );
230
+
231
+ // @ts-ignore
232
+ return entries;
233
+ }
234
+ }
235
+
236
+ export type QueryPartialEntity<T> = {
237
+ [P in keyof T]?: T[P] | (() => string);
238
+ };
239
+
240
+ export type QueryOptions = {
241
+ page: number;
242
+ size: number;
243
+ infinite: boolean;
244
+ uniqueKey: string;
245
+ };
@@ -1 +1 @@
1
- export * from "./SubjectRepository";
1
+ export * from "./SubjectRepository";
@@ -1,57 +1,57 @@
1
- type Target = String;
2
-
3
- export type PropertyValueMap = {
4
- [property: string]: Target | Target[];
5
- };
6
-
7
- export function capitalize(str: string) {
8
- return str.charAt(0).toUpperCase() + str.slice(1);
9
- }
10
-
11
- // e.g. "name" -> "setName"
12
- export function propertyNameToSetterName(property: string): string {
13
- return `set${capitalize(property)}`;
14
- }
15
-
16
- export function pluralToSingular(plural: string): string {
17
- if (plural.endsWith("ies")) {
18
- return plural.slice(0, -3) + "y";
19
- } else if (plural.endsWith("s")) {
20
- return plural.slice(0, -1);
21
- } else {
22
- return plural;
23
- }
24
- }
25
-
26
- // e.g. "comments" -> "addComment"
27
- export function collectionToAdderName(collection: string): string {
28
- return `add${capitalize(collection)}`;
29
- }
30
-
31
- export function collectionToSetterName(collection: string): string {
32
- return `setCollection${capitalize(collection)}`;
33
- }
34
-
35
- export function setProperties(subject: any, properties: PropertyValueMap) {
36
- Object.keys(properties).forEach((key) => {
37
- if (Array.isArray(properties[key])) {
38
- // it's a collection
39
- const adderName = collectionToAdderName(key);
40
- const adderFunction = subject[adderName];
41
- if (adderFunction) {
42
- adderFunction(properties[key]);
43
- } else {
44
- throw "No adder function found for collection: " + key;
45
- }
46
- } else {
47
- // it's a property
48
- const setterName = propertyNameToSetterName(key);
49
- const setterFunction = subject[setterName];
50
- if (setterFunction) {
51
- setterFunction(properties[key]);
52
- } else {
53
- throw "No setter function found for property: " + key;
54
- }
55
- }
56
- });
57
- }
1
+ type Target = String;
2
+
3
+ export type PropertyValueMap = {
4
+ [property: string]: Target | Target[];
5
+ };
6
+
7
+ export function capitalize(str: string) {
8
+ return str.charAt(0).toUpperCase() + str.slice(1);
9
+ }
10
+
11
+ // e.g. "name" -> "setName"
12
+ export function propertyNameToSetterName(property: string): string {
13
+ return `set${capitalize(property)}`;
14
+ }
15
+
16
+ export function pluralToSingular(plural: string): string {
17
+ if (plural.endsWith("ies")) {
18
+ return plural.slice(0, -3) + "y";
19
+ } else if (plural.endsWith("s")) {
20
+ return plural.slice(0, -1);
21
+ } else {
22
+ return plural;
23
+ }
24
+ }
25
+
26
+ // e.g. "comments" -> "addComment"
27
+ export function collectionToAdderName(collection: string): string {
28
+ return `add${capitalize(collection)}`;
29
+ }
30
+
31
+ export function collectionToSetterName(collection: string): string {
32
+ return `setCollection${capitalize(collection)}`;
33
+ }
34
+
35
+ export function setProperties(subject: any, properties: PropertyValueMap) {
36
+ Object.keys(properties).forEach((key) => {
37
+ if (Array.isArray(properties[key])) {
38
+ // it's a collection
39
+ const adderName = collectionToAdderName(key);
40
+ const adderFunction = subject[adderName];
41
+ if (adderFunction) {
42
+ adderFunction(properties[key]);
43
+ } else {
44
+ throw "No adder function found for collection: " + key;
45
+ }
46
+ } else {
47
+ // it's a property
48
+ const setterName = propertyNameToSetterName(key);
49
+ const setterFunction = subject[setterName];
50
+ if (setterFunction) {
51
+ setterFunction(properties[key]);
52
+ } else {
53
+ throw "No setter function found for property: " + key;
54
+ }
55
+ }
56
+ });
57
+ }
package/src/getProfile.ts CHANGED
@@ -1,26 +1,26 @@
1
- import { Ad4mClient } from "@coasys/ad4m";
2
- // @ts-ignore
3
- import { getAd4mClient } from "@coasys/ad4m-connect/utils";
4
- import { LinkExpression } from "@coasys/ad4m";
5
-
6
- export interface Payload {
7
- url: string;
8
- perspectiveUuid: string;
9
- }
10
-
11
- export async function getProfile<T>(did: string, formatter?: (links: LinkExpression[]) => T): Promise<T | any> {
12
- const cleanedDid = did.replace("did://", "");
13
- const client: Ad4mClient = await getAd4mClient();
14
-
15
- const agentPerspective = await client.agent.byDID(cleanedDid);
16
-
17
- if (agentPerspective) {
18
- const links = agentPerspective!.perspective!.links;
19
-
20
- if (formatter) {
21
- return formatter(links);
22
- }
23
-
24
- return agentPerspective
25
- }
26
- }
1
+ import { Ad4mClient } from "@coasys/ad4m";
2
+ // @ts-ignore
3
+ import { getAd4mClient } from "@coasys/ad4m-connect/utils";
4
+ import { LinkExpression } from "@coasys/ad4m";
5
+
6
+ export interface Payload {
7
+ url: string;
8
+ perspectiveUuid: string;
9
+ }
10
+
11
+ export async function getProfile<T>(did: string, formatter?: (links: LinkExpression[]) => T): Promise<T | any> {
12
+ const cleanedDid = did.replace("did://", "");
13
+ const client: Ad4mClient = await getAd4mClient();
14
+
15
+ const agentPerspective = await client.agent.byDID(cleanedDid);
16
+
17
+ if (agentPerspective) {
18
+ const links = agentPerspective!.perspective!.links;
19
+
20
+ if (formatter) {
21
+ return formatter(links);
22
+ }
23
+
24
+ return agentPerspective
25
+ }
26
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './cache'
2
- export * from './getProfile'
1
+ export * from './cache'
2
+ export * from './getProfile'
3
3
  export * from './factory'