aws-dynamodb-axios
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -24,3 +24,3 @@ const dynamodb = require('../index') | ||
await db.scan({ TableName: testTableName }) | ||
expect(unmarshall(items)).toEqual([{ hash: 'foo', range: 'bar' }]) | ||
expect(items.map(unmarshall)).toEqual([{ hash: 'foo', range: 'bar' }]) | ||
}) | ||
@@ -37,4 +37,4 @@ | ||
}) | ||
expect(unmarshall(fetched.Item.hash)).toBe('foo') | ||
expect(unmarshall(fetched.Item.range)).toBe('bar') | ||
expect(unmarshall(fetched.Item).hash).toBe('foo') | ||
expect(unmarshall(fetched.Item).range).toBe('bar') | ||
}) | ||
@@ -56,3 +56,5 @@ | ||
const { Items: items } = await db.scan({ TableName: testTableName }) | ||
expect(unmarshall(items).map(i => i.foo)).toEqual(['after']) | ||
expect( | ||
items.map(item => unmarshall(item)).map(i => i.foo).sort() | ||
).toEqual(['after']) | ||
}) | ||
@@ -69,5 +71,6 @@ | ||
}) | ||
const { Items: items } = | ||
await db.scan({ TableName: testTableName }) | ||
expect(unmarshall(items)).toEqual([]) | ||
const { Items: items } = await db.scan({ TableName: testTableName }) | ||
expect( | ||
items.map(item => unmarshall(item)).map(i => i.hash).sort() | ||
).toEqual([]) | ||
}) | ||
@@ -100,3 +103,3 @@ | ||
}) | ||
expect(unmarshall(items)).toHaveLength(2) | ||
expect(items.map(unmarshall)).toHaveLength(2) | ||
}) | ||
@@ -119,3 +122,5 @@ | ||
await db.scan({ TableName: testTableName }) | ||
expect(unmarshall(items).map(i => i.hash).sort()).toEqual(['1', '2', '3']) | ||
expect( | ||
items.map(item => unmarshall(item)).map(i => i.hash).sort() | ||
).toEqual(['1', '2', '3']) | ||
}) | ||
@@ -133,5 +138,6 @@ | ||
}) | ||
const { Items: items } = | ||
await db.scan({ TableName: testTableName }) | ||
expect(unmarshall(items).map(i => i.hash).sort()).toEqual(['1', '2', '3']) | ||
const { Items: items } = await db.scan({ TableName: testTableName }) | ||
expect( | ||
items.map(item => unmarshall(item)).map(i => i.hash).sort() | ||
).toEqual(['1', '2', '3']) | ||
}) | ||
@@ -155,7 +161,7 @@ | ||
[testTableName]: { | ||
Keys: marshall([ | ||
Keys: [ | ||
{ hash: '1', range: 'a' }, | ||
{ hash: '2', range: 'b' }, | ||
{ hash: '3', range: 'c' } | ||
]) | ||
].map(marshall) | ||
} | ||
@@ -165,3 +171,3 @@ } | ||
const data = response.Responses[testTableName] | ||
expect(unmarshall(data).map(i => i.hash).sort()) | ||
expect(data.map(unmarshall).map(i => i.hash).sort()) | ||
.toEqual(['1', '2', '3']) | ||
@@ -178,5 +184,6 @@ }) | ||
}) | ||
const { Items: items } = | ||
await db.scan({ TableName: testTableName }) | ||
expect(unmarshall(items).map(i => i.hash).sort()).toEqual(['1', '2', '3']) | ||
const { Items: items } = await db.scan({ TableName: testTableName }) | ||
expect( | ||
items.map(item => unmarshall(item)).map(i => i.hash).sort() | ||
).toEqual(['1', '2', '3']) | ||
}) | ||
@@ -204,4 +211,5 @@ | ||
}) | ||
expect(unmarshall(response.Responses).map(r => r.Item.hash).sort()) | ||
.toEqual(['1', '2', '3']) | ||
expect( | ||
response.Responses.map(r => unmarshall(r.Item).hash) | ||
).toEqual(['1', '2', '3']) | ||
}) | ||
@@ -208,0 +216,0 @@ }) |
30
index.js
@@ -6,2 +6,3 @@ const axios = require('axios') | ||
const { apiVersion, regionToEndpoint } = require('./constants') | ||
const configureDebug = require('debug') | ||
@@ -16,5 +17,12 @@ const credentials = { | ||
const debugRequest = configureDebug('aws-dynamodb-axios:requests') | ||
const debugResponse = configureDebug('aws-dynamodb-axios:responses') | ||
const debugError = configureDebug('aws-dynamodb-axios:errors') | ||
const debugSplits = configureDebug('aws-dynamodb-axios:splits') | ||
const buildHandler = ({ client, host, url, protocol }) => | ||
action => | ||
async data => { | ||
debugRequest('action: %s', action) | ||
const headers = { | ||
@@ -28,2 +36,5 @@ 'X-Amz-Target': `DynamoDB_${apiVersion}.${action}`, | ||
debugRequest('signing request: %O', { url, body, host, headers, method: 'POST' }) | ||
debugSplits('sign request start %s', action) | ||
const signedRequest = aws4.sign( | ||
@@ -33,5 +44,7 @@ { url, body, host, headers, method: 'POST' }, | ||
) | ||
debugSplits('sign request end %s', action) | ||
let response | ||
try { | ||
debugSplits('send request start %s', action) | ||
response = await client({ | ||
@@ -42,4 +55,8 @@ data, | ||
}) | ||
debugSplits('send request end %s', action) | ||
} catch (error) { | ||
debugError('error message: %s', error.message) | ||
if (!error.response) throw error | ||
debugError('error data: %O', error.response.data) | ||
debugError('error status: %s', error.response.status) | ||
const { status, data: errorData } = error.response | ||
@@ -51,15 +68,6 @@ const dynamoError = createError(status, errorData.message, error) | ||
debugResponse('response data: %O', response.data) | ||
return response.data | ||
} | ||
const allApiVerbs = [ | ||
'get', | ||
'put', | ||
'options', | ||
'patch', | ||
'post', | ||
'delete', | ||
'head' | ||
] | ||
const dynamodb = (options = {}) => { | ||
@@ -76,3 +84,3 @@ const host = options.host || regionToEndpoint[options.region] | ||
return { | ||
...omit(client, allApiVerbs), | ||
interceptors: client.interceptors, | ||
put: createHandler('PutItem'), | ||
@@ -79,0 +87,0 @@ get: createHandler('GetItem'), |
@@ -1,35 +0,7 @@ | ||
const mapValues = require('./modules/mapValues') | ||
const omit = require('./modules/omit') | ||
const converters = require('dynamo-converters') | ||
// A minimal replacement for AWS Converter. Mainly this was added | ||
// because the SDK resists bundling with rollup. | ||
// https://github.com/aws/aws-sdk-js/issues/1769 | ||
const handleObject = obj => { | ||
return mapValues(obj, (val, key) => { | ||
if (val !== null && !Array.isArray(val) && typeof val === 'object') { | ||
return { M: marshall(val) } | ||
} | ||
return marshall(val) | ||
}) | ||
} | ||
const handleArray = arr => { | ||
if (typeof arr[0] === 'string') { | ||
return { SS: arr } | ||
} | ||
return arr.map(marshall) | ||
} | ||
const marshall = val => { | ||
if (val === null) return { NULL: true } | ||
if (typeof val === 'undefined') return | ||
if (val === false) return { BOOL: false } | ||
if (val === true) return { BOOL: true } | ||
if (typeof val === 'string') return { S: val } | ||
if (typeof val === 'number') return { N: val.toString() } | ||
if (Array.isArray(val)) return handleArray(val) | ||
if (typeof val === 'object') return handleObject(val) | ||
throw new Error(false, `Marshalling type \`${typeof val}\` is not yet supported`) | ||
} | ||
module.exports = marshall | ||
module.exports = obj => omit( | ||
converters.dataToItem(obj), | ||
['created', 'modified'] | ||
) |
{ | ||
"name": "aws-dynamodb-axios", | ||
"license": "MIT", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"scripts": { | ||
@@ -12,3 +12,5 @@ "test": "./test-local.sh", | ||
"aws4": "^1.8.0", | ||
"axios": "^0.19.0" | ||
"axios": "^0.19.0", | ||
"debug": "^4.1.1", | ||
"dynamo-converters": "^3.1.9" | ||
}, | ||
@@ -15,0 +17,0 @@ "devDependencies": { |
@@ -5,2 +5,6 @@ # AWS DynamoDB with Axios [![CircleCI](https://circleci.com/gh/possibilities/aws-dynamodb-axios.svg?style=svg)](https://circleci.com/gh/possibilities/aws-dynamodb-axios) | ||
## Motivation | ||
The primary motivation for this library is to have a tools that are similar to DynamoDb tools in the AWS SDK that are [rollup friendly](https://rollupjs.org/guide/en/) for deploying as-small-as-possible bundles to AWS Lambda. In the future if the SDK becomes more bundle-friendly we can deprecate this project. TODO provide more context and bundle size info. | ||
## Usage | ||
@@ -7,0 +11,0 @@ |
@@ -1,46 +0,2 @@ | ||
const mapValues = require('./modules/mapValues') | ||
// A minimal replacement for AWS Converter. Mainly this was added | ||
// because the SDK resists bundling with rollup. | ||
// https://github.com/aws/aws-sdk-js/issues/1769 | ||
const types = { | ||
S: true, | ||
N: true, | ||
NULL: true, | ||
BOOL: true, | ||
M: true, | ||
SS: true | ||
} | ||
const unmarshall = attributeValue => { | ||
if (attributeValue === null || attributeValue === undefined) return null | ||
const key = Object.keys(attributeValue).pop() | ||
const val = Object.values(attributeValue).pop() | ||
if (!types[key]) { | ||
if (Array.isArray(attributeValue)) { | ||
return attributeValue.map(unmarshall) | ||
} else { | ||
return mapValues(attributeValue, unmarshall) | ||
} | ||
} | ||
switch (key) { | ||
case 'S': | ||
return val | ||
case 'N': | ||
return parseInt(val, 10) | ||
case 'NULL': | ||
if (val === true) return null | ||
break | ||
case 'BOOL': | ||
return val | ||
case 'M': | ||
return unmarshall(val) | ||
case 'SS': | ||
return val | ||
} | ||
throw new Error(`Unmarshalling type \`${key}\` is not yet supported`) | ||
} | ||
module.exports = unmarshall | ||
const converters = require('dynamo-converters') | ||
module.exports = obj => converters.itemToData(obj) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52
191951
4
17
396
+ Addeddebug@^4.1.1
+ Addeddynamo-converters@^3.1.9
+ Addeddebug@4.3.7(transitive)
+ Addeddynamo-converters@3.1.11(transitive)
+ Addedms@2.1.3(transitive)