-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo-get-dynamodb-without-aws-sdk.js
71 lines (66 loc) · 1.44 KB
/
demo-get-dynamodb-without-aws-sdk.js
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
import { createAwsSigner } from './src/index-node.js'
import got from 'got'
import secrets from './secrets.js'
const sign = createAwsSigner({
config: {
service: 'dynamodb',
region: secrets.region,
secretAccessKey: secrets.secretAccessKey,
accessKeyId: secrets.accessKeyId,
},
})
const request = {
url: 'https://dynamodb.us-east-1.amazonaws.com',
method: 'POST',
headers: {
'content-type': 'application/x-amz-json-1.0',
'X-Amz-Target': 'DynamoDB_20120810.GetItem',
Host: 'dynamodb.us-east-1.amazonaws.com',
},
body: JSON.stringify({
TableName: 'demo-sign-aws-requests',
Key: {
DemoPrimaryKey: {
S: 'DEMO',
},
DemoSortKey: {
S: '123',
},
},
}),
}
// The 'got' module lets us inject the request signing
// functionality inside a hook:
const awsClient = got.extend({
hooks: {
beforeRequest: [
async options => {
const { authorization } = await sign(options)
options.headers.Authorization = authorization
},
],
},
})
const expected = JSON.stringify({
Item: {
Foo: {
S: 'bar',
},
DemoPrimaryKey: {
S: 'DEMO',
},
DemoSortKey: {
S: '123',
},
},
})
awsClient(request)
.then(response => {
console.log(JSON.stringify(JSON.parse(response.body), undefined, 2))
const equals = expected === response.body
console.log(equals ? 'the results are correct' : 'the results do not match expectations')
})
.catch(response => {
console.error('------------- oh no!')
console.error(response)
})