-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
135 lines (116 loc) · 4.17 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import express from 'express';
// import { DynamoDBClient, QueryCommand, ScanCommand } from "@aws-sdk/client-dynamodb";
// import { unmarshall } from '@aws-sdk/util-dynamodb';
// import { String } from 'aws-sdk/clients/appstream';
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand, ScanCommand } from "@aws-sdk/lib-dynamodb";
/*
http://localhost:4000/items
http://localhost:4000/items/did:pkh:eip155:11155111:0xcc7c019e8ab0d72b5676811c8c1ec6fae16228c0
http://localhost:4000/item/did:pkh:eip155:11155111:0xcc7c019e8ab0d72b5676811c8c1ec6fae16228c0/name/Claus
http://localhost:4000/item/did:pkh:eip155:11155111:0xcc7c019e8ab0d72b5676811c8c1ec6fae16228c0/title/task
*/
const PORT = 4000;
const HOSTNAME = 'http://localhost';
async function main() {
const app = express();
const table = 'task';
// List all items
app.get('/items', async (req, res) => {
console.log('Get all items from');
const command = new ScanCommand({
TableName: table,
ProjectionExpression: 'did, uri'
});
const items = await get_items(table, command);
console.log(items);
res.send(items);
})
// List all items with owner DID
app.get('/items/:did', async (req, res) => {
const did: string = req.params.did;
console.log('Get items from: ', did);
const command = new QueryCommand({
TableName: table,
KeyConditionExpression: 'did = :did',
ExpressionAttributeValues: {
':did': did
},
});
const items = await get_items(table, command);
console.log(items);
res.send(items);
})
// List all items from a DID and filter by title
app.get('/item/:did/title/:title', async (req, res) => {
const did: string = req.params.did;
const title: string = req.params.title;
console.log('Get items from: ', did);
console.log('Title: ', title);
const command = new QueryCommand({
TableName: table,
KeyConditionExpression: 'did = :did AND begins_with (title, :title)',
ExpressionAttributeValues: {
':did': did,
':title': title,
},
});
const items = await get_items(table, command);
console.log(items);
res.send(items);
})
// List all items from a DID and filter by name
app.get('/item/:did/name/:name', async (req, res) => {
const did: string = req.params.did;
const name: string = req.params.name;
console.log('Get items from: ', did);
console.log('Name: ', name);
const command = new QueryCommand({
TableName: table,
KeyConditionExpression: 'did = :did',
ExpressionAttributeValues: {
':did': did,
':name': name,
},
ExpressionAttributeNames: {
'#name': 'name'
},
FilterExpression: 'contains (#name, :name)',
});
const items = await get_items(table, command);
console.log(items);
res.send(items);
})
// Run the Express server, listening on port 4000
app.listen(PORT, () => {
console.log(`HTTP API running at ${HOSTNAME}:${PORT}`)
})
}
// Make some queries on DynamoDB
async function get_items(table: string, command: any): Promise<any> {
const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);
try {
const response = await docClient.send(command);
const json = JSON.parse(JSON.stringify(response, null, 4));
return {
'status': 'success',
'count': json?.Count,
'items': json?.Items
}
}
catch (err: any) {
console.log("Error", err);
}
}
main();
// async function test(table: string, key:any) {
// const items = await get_item(table, key);
// console.log(items);
// }
// const table = 'task';
// const did = 'did:pkh:eip155:11155111:0xcc7c019e8ab0d72b5676811c8c1ec6fae16228c0';
// const key = {
// ':did': {S: did}
// };
// test(table, key);