Skip to content

Commit 06d1784

Browse files
committed
feat: add draft of first utlity functions
1 parent 6b806c1 commit 06d1784

File tree

6 files changed

+120
-8
lines changed

6 files changed

+120
-8
lines changed

index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export * as ID from './src/id.js';
1414
*
1515
* @since 0.0.6
1616
*/
17-
export { BASE58_ALLOWED_CHARS, decodeBase58ToUUID, encodeBase58 } from './src/core/base58.js';
17+
export {
18+
BASE58_ALLOWED_CHARS,
19+
decodeBase58ToUUID,
20+
encodeBase58,
21+
} from './src/core/base58.js';
1822

1923
export {
2024
getAcceptEditorArguments,
@@ -33,7 +37,7 @@ export {
3337
*/
3438
export { Account } from './src/account.js';
3539

36-
export { TextBlock, DataBlock, ImageBlock } from './src/blocks.js';
40+
export { DataBlock, ImageBlock, TextBlock } from './src/blocks.js';
3741

3842
/**
3943
* This module provides utility functions for working with knowledge graph
@@ -69,7 +73,7 @@ export { GraphUrl } from './src/scheme.js';
6973
/**
7074
* Provides ids for commonly used entities across the Knowledge Graph.
7175
*/
72-
export { SYSTEM_IDS, NETWORK_IDS, CONTENT_IDS } from './src/system-ids.js';
76+
export { CONTENT_IDS, NETWORK_IDS, SYSTEM_IDS } from './src/system-ids.js';
7377

7478
export { getChecksumAddress } from './src/core/get-checksum-address.js';
7579

@@ -80,3 +84,10 @@ export { getChecksumAddress } from './src/core/get-checksum-address.js';
8084
* @since 0.1.1
8185
*/
8286
export * as IPFS from './src/ipfs.js';
87+
88+
/**
89+
* This module provides high-level utility functions for working with the Knowledge Graph API.
90+
*
91+
* @since 0.5.0
92+
*/
93+
export * as Graph from './src/graph/index.js';

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
"main": "./dist/index.js",
88
"ascMain": "./dist/index.js",
99
"type": "module",
10-
"files": [
11-
"dist"
12-
],
10+
"files": ["dist"],
1311
"exports": {
1412
"./package.json": "./package.json",
1513
".": "./dist/index.js",
@@ -47,6 +45,5 @@
4745
"@bufbuild/buf": "^1.31.0",
4846
"@bufbuild/protoc-gen-es": "^1.9.0",
4947
"typescript": "^5.4.5"
50-
},
51-
"packageManager": "[email protected]"
48+
}
5249
}

src/graph/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_API_HOST = 'https://api-testnet.grc-20.thegraph.com';

src/graph/create-entity.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { publishEdit } from '../ipfs.js';
2+
import { Triple } from '../triple.js';
3+
import type { SetTripleOp } from '../types.js';
4+
import { DEFAULT_API_HOST } from './constants.js';
5+
6+
type Params = {
7+
id: string;
8+
spaceId: string;
9+
accountId: string;
10+
data: {
11+
[filedName: string]: string;
12+
};
13+
mapping: {
14+
[filedName: string]: string;
15+
};
16+
options?: {
17+
apiHost?: string;
18+
network?: 'TESTNET' | 'MAINNET';
19+
};
20+
};
21+
22+
export const createEntity = async ({ id, data, spaceId, accountId, mapping, options }: Params) => {
23+
const ops: SetTripleOp[] = [];
24+
const apiHost = options?.apiHost || DEFAULT_API_HOST;
25+
const network = options?.network || 'TESTNET';
26+
for (const [fieldName, value] of Object.entries(data)) {
27+
const attributeId = mapping[fieldName];
28+
if (!attributeId) {
29+
throw new Error(`Attribute ID for field ${fieldName} not found`);
30+
}
31+
let attributeValueType: 'TEXT' | 'CHECKBOX' | 'NUMBER' | 'URL' | 'POINT';
32+
switch (typeof value) {
33+
case 'string':
34+
attributeValueType = 'TEXT';
35+
break;
36+
case 'boolean':
37+
attributeValueType = 'CHECKBOX';
38+
break;
39+
case 'number':
40+
attributeValueType = 'NUMBER';
41+
break;
42+
default:
43+
throw new Error('Not implemented value type');
44+
}
45+
const setTripleOp: SetTripleOp = Triple.make({
46+
entityId: id,
47+
attributeId,
48+
value: {
49+
type: attributeValueType,
50+
value,
51+
},
52+
});
53+
54+
ops.push(setTripleOp);
55+
}
56+
57+
const cid = await publishEdit({
58+
name: 'Edit name',
59+
ops,
60+
author: accountId,
61+
});
62+
63+
const result = await fetch(`${apiHost}/space/${spaceId}/edit/calldata`, {
64+
method: 'POST',
65+
body: JSON.stringify({
66+
cid: cid,
67+
network,
68+
}),
69+
});
70+
71+
const jsonResult = await result.json();
72+
return jsonResult;
73+
74+
// TODO add walletClient.sendTransaction
75+
};

src/graph/create-space.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { DEFAULT_API_HOST } from './constants.js';
2+
3+
type Params = {
4+
editorAddress: string;
5+
name: string;
6+
options?: {
7+
apiHost?: string;
8+
};
9+
};
10+
11+
export const createSpace = async (params: Params) => {
12+
const apiHost = params.options?.apiHost || DEFAULT_API_HOST;
13+
const result = await fetch(`${apiHost}/deploy`, {
14+
method: 'POST',
15+
body: JSON.stringify({
16+
spaceName: params.name,
17+
initialEditorAddress: params.editorAddress,
18+
}),
19+
headers: {
20+
'Content-Type': 'application/json',
21+
},
22+
});
23+
24+
const { spaceId } = await result.json();
25+
return { id: spaceId };
26+
};

src/graph/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './create-entity.js';
2+
export * from './create-space.js';

0 commit comments

Comments
 (0)