Skip to content

Instantly share code, notes, and snippets.

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
import db from 'db';
const checkUsernameExists = async (username: string):Promise<boolean> => {
const user = await db.select('id')
->from('users')
->where('username', username)
->first();
return Object.keys(user).length > 0;
}
try {
await insertNewUser(user);
} catch (error) {
return {
error: {
message: error.message,
code: 'FAILED_INSERT_USER',
}
}
}
try {
await sendMessage('User created', { userId, user });
} catch (error) {
console.log('Failed to send message, but it is ok', error);
}
try {
validateUsernameFormat(user.username);
await checkUsernameExists(user.username);
} catch (error) {
return {
// a better approach is to have a response success/error function
error: {
message: error.message,
code: 'FAILED_USERNAME_VALIDATION',
}
const user = {
username: 'JohnDoe',
gender: 'male',
}
const user = {
username: 'JohnDoe',
gender: 'male',
}
try {
validateUsernameFormat(user.username);
await checkUsernameExists(user.username);
} catch (error) {
return {
try {
const user = {
username: 'JohnDoe',
gender: 'male',
}
validateUsernameFormat(user.username);
await checkUsernameExists(user.username);
const userId = await insertNewUser(user);
import { SQS } from 'aws-sdk';
export const sendQueue = (payload, options) => {
const sqs = new SQS(options);
sqs.sendMessage(payload, (error, data) => {
return new Promise((resolve, reject) => {
if (error) {
reject(new Error('Failed to send message'));
} else {
import { SQS } from 'aws-sdk';
export const sendQueue = async (payload, options) => {
const sqs = new SQS(options);
try {
await sqs.sendMessage(payload).promise();
return 'messageSent';
} catch (error) {
throw new Error('Failed to send message');