This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Sid": "PublicReadGetObject", | |
"Effect": "Allow", | |
"Principal": "*", | |
"Action": [ | |
"s3:GetObject" | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
await insertNewUser(user); | |
} catch (error) { | |
return { | |
error: { | |
message: error.message, | |
code: 'FAILED_INSERT_USER', | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
await sendMessage('User created', { userId, user }); | |
} catch (error) { | |
console.log('Failed to send message, but it is ok', error); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const user = { | |
username: 'JohnDoe', | |
gender: 'male', | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const user = { | |
username: 'JohnDoe', | |
gender: 'male', | |
} | |
try { | |
validateUsernameFormat(user.username); | |
await checkUsernameExists(user.username); | |
} catch (error) { | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
const user = { | |
username: 'JohnDoe', | |
gender: 'male', | |
} | |
validateUsernameFormat(user.username); | |
await checkUsernameExists(user.username); | |
const userId = await insertNewUser(user); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
NewerOlder