Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async saveAttributes(requestEnvelope : RequestEnvelope, attributes : {[key : string] : string}) : Promise {
const objectId = path.join(this.pathPrefix, this.objectKeyGenerator(requestEnvelope));
const putParams : S3.PutObjectRequest = {
Bucket : this.bucketName,
Key : objectId,
Body : JSON.stringify(attributes),
};
try {
await this.s3Client.putObject(putParams).promise();
} catch (err) {
throw createAskSdkError(
this.constructor.name,
`Could not save item (${objectId}) to bucket (${putParams.Bucket}): ${err.message}`,
);
}
}
deviceId(requestEnvelope : RequestEnvelope) : string {
if (!(requestEnvelope
&& requestEnvelope.context
&& requestEnvelope.context.System
&& requestEnvelope.context.System.device
&& requestEnvelope.context.System.device.deviceId)) {
throw createAskSdkError(
'PartitionKeyGenerators',
'Cannot retrieve device id from request envelope!',
);
}
return requestEnvelope.context.System.device.deviceId;
},
};
userId(requestEnvelope : RequestEnvelope) : string {
if (!(requestEnvelope
&& requestEnvelope.context
&& requestEnvelope.context.System
&& requestEnvelope.context.System.user
&& requestEnvelope.context.System.user.userId)) {
throw createAskSdkError(
'PartitionKeyGenerators',
'Cannot retrieve user id from request envelope!',
);
}
return requestEnvelope.context.System.user.userId;
},
this.dynamoDBClient.createTable(createTableParams, (createTableErr) => {
if (createTableErr && createTableErr.code !== 'ResourceInUseException') {
throw createAskSdkError(
this.constructor.name,
`Could not create table (${this.tableName}): ${createTableErr.message}`,
);
}
});
}
public async deleteAttributes(requestEnvelope : RequestEnvelope) : Promise {
const attributesId = this.partitionKeyGenerator(requestEnvelope);
const deleteParams : DynamoDB.DocumentClient.DeleteItemInput = {
Key : {
[this.partitionKeyName] : attributesId,
},
TableName : this.tableName,
};
try {
await this.dynamoDBDocumentClient.delete(deleteParams).promise();
} catch (err) {
throw createAskSdkError(
this.constructor.name,
`Could not delete item (${attributesId}) from table (${deleteParams.TableName}): ${err.message}`,
);
}
}
}
deviceId(requestEnvelope : RequestEnvelope) : string {
if (!(requestEnvelope
&& requestEnvelope.context
&& requestEnvelope.context.System
&& requestEnvelope.context.System.device
&& requestEnvelope.context.System.device.deviceId)) {
throw createAskSdkError(
'PartitionKeyGenerators',
'Cannot retrieve device id from request envelope!',
);
}
return requestEnvelope.context.System.device.deviceId;
},
};
const getParams : S3.GetObjectRequest = {
Bucket : this.bucketName,
Key : objectId,
};
let data : S3.GetObjectOutput;
try {
data = await this.s3Client.getObject(getParams).promise();
} catch (err) {
if (err.code === 'NoSuchKey') {
return {};
}
throw createAskSdkError(
this.constructor.name,
`Could not read item (${objectId}) from bucket (${getParams.Bucket}): ${err.message}`,
);
}
const bodyString = data.Body ? data.Body.toString() : '';
let bodyObj;
try {
bodyObj = bodyString ? JSON.parse(bodyString) : {};
} catch (err) {
throw new SyntaxError(`Failed trying to parse the data body: ${data.Body.toString()}`);
}
return bodyObj;
public async deleteAttributes(requestEnvelope : RequestEnvelope) : Promise {
const objectId = path.join(this.pathPrefix, this.objectKeyGenerator(requestEnvelope));
const deleteParams : S3.DeleteObjectRequest = {
Bucket : this.bucketName,
Key : objectId,
};
try {
await this.s3Client.deleteObject(deleteParams).promise();
} catch (err) {
throw createAskSdkError(
this.constructor.name,
`Could not delete item (${objectId}) from bucket (${deleteParams.Bucket}): ${err.message}`,
);
}
}
}
public async saveAttributes(requestEnvelope : RequestEnvelope, attributes : {[key : string] : any}) : Promise {
const attributesId = this.partitionKeyGenerator(requestEnvelope);
const putParams : DynamoDB.DocumentClient.PutItemInput = {
Item: {
[this.partitionKeyName] : attributesId,
[this.attributesName] : attributes,
},
TableName : this.tableName,
};
try {
await this.dynamoDBDocumentClient.put(putParams).promise();
} catch (err) {
throw createAskSdkError(
this.constructor.name,
`Could not save item (${attributesId}) to table (${putParams.TableName}): ${err.message}`,
);
}
}
public async getAttributes(requestEnvelope : RequestEnvelope) : Promise<{[key : string] : any}> {
const attributesId = this.partitionKeyGenerator(requestEnvelope);
const getParams : DynamoDB.DocumentClient.GetItemInput = {
Key : {
[this.partitionKeyName] : attributesId,
},
TableName : this.tableName,
ConsistentRead : true,
};
let data : DynamoDB.DocumentClient.GetItemOutput;
try {
data = await this.dynamoDBDocumentClient.get(getParams).promise();
} catch (err) {
throw createAskSdkError(
this.constructor.name,
`Could not read item (${attributesId}) from table (${getParams.TableName}): ${err.message}`,
);
}
if (!Object.keys(data).length) {
return {};
} else {
return data.Item[this.attributesName];
}
}