Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ module.exports = {
'fp/no-this': 0,
'import/max-dependencies': 0,
'node/no-sync': 0,
'promise/catch-or-return': 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule didn't require any fixes

'promise/no-callback-in-promise': 0,
'promise/no-return-wrap': 0,
'promise/prefer-await-to-callbacks': 0,
'promise/prefer-await-to-then': 0,
'unicorn/prefer-spread': 0,
'unicorn/consistent-destructuring': 0,

Expand Down
36 changes: 17 additions & 19 deletions src/functions-templates/js/fauna-crud/create-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const process = require('process')
/* bootstrap database in your FaunaDB account - use with `netlify dev:exec <path-to-this-file>` */
const { query, Client } = require('faunadb')

const createFaunaDB = function () {
const createFaunaDB = async function () {
if (!process.env.FAUNADB_SERVER_SECRET) {
console.log('No FAUNADB_SERVER_SECRET in environment, skipping DB setup')
}
Expand All @@ -14,25 +14,23 @@ const createFaunaDB = function () {
})

/* Based on your requirements, change the schema here */
return client
.query(query.CreateCollection({ name: 'items' }))
.then(() => {
console.log('Created items class')
return client.query(
query.CreateIndex({
name: 'all_items',
source: query.Collection('items'),
active: true,
}),
)
})
try {
await client.query(query.CreateCollection({ name: 'items' }))

.catch((error) => {
if (error.requestResult.statusCode === 400 && error.message === 'instance not unique') {
console.log('DB already exists')
}
throw error
})
console.log('Created items class')
return client.query(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return client.query(
return await client.query(

For proper error handling.

query.CreateIndex({
name: 'all_items',
source: query.Collection('items'),
active: true,
}),
)
} catch (error) {
if (error.requestResult.statusCode === 400 && error.message === 'instance not unique') {
console.log('DB already exists')
}
throw error
}
}

createFaunaDB()
34 changes: 16 additions & 18 deletions src/functions-templates/js/fauna-crud/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@ const handler = async (event) => {
data,
}
/* construct the fauna query */
return client
.query(query.Create(query.Collection('items'), item))
.then((response) => {
console.log('success', response)
/* Success! return the response with statusCode 200 */
return {
statusCode: 200,
body: JSON.stringify(response),
}
})
.catch((error) => {
console.log('error', error)
/* Error! return the error with statusCode 400 */
return {
statusCode: 400,
body: JSON.stringify(error),
}
})
try {
const response = await client.query(query.Create(query.Collection('items'), item))
console.log('success', response)
/* Success! return the response with statusCode 200 */
return {
statusCode: 200,
body: JSON.stringify(response),
}
} catch (error) {
console.log('error', error)
/* Error! return the error with statusCode 400 */
return {
statusCode: 400,
body: JSON.stringify(error),
}
}
}

module.exports = { handler }
30 changes: 14 additions & 16 deletions src/functions-templates/js/fauna-crud/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ const client = new Client({
const handler = async (event) => {
const { id } = event
console.log(`Function 'delete' invoked. delete id: ${id}`)
return client
.query(query.Delete(query.Ref(query.Collection('items'), id)))
.then((response) => {
console.log('success', response)
return {
statusCode: 200,
body: JSON.stringify(response),
}
})
.catch((error) => {
console.log('error', error)
return {
statusCode: 400,
body: JSON.stringify(error),
}
})
try {
const response = await client.query(query.Delete(query.Ref(query.Collection('items'), id)))
console.log('success', response)
return {
statusCode: 200,
body: JSON.stringify(response),
}
} catch (error) {
console.log('error', error)
return {
statusCode: 400,
body: JSON.stringify(error),
}
}
}

module.exports = { handler }
31 changes: 15 additions & 16 deletions src/functions-templates/js/fauna-crud/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ const client = new Client({
const handler = async (event) => {
const { id } = event
console.log(`Function 'read' invoked. Read id: ${id}`)
return client
.query(query.Get(query.Ref(query.Collection('items'), id)))
.then((response) => {
console.log('success', response)
return {
statusCode: 200,
body: JSON.stringify(response),
}
})
.catch((error) => {
console.log('error', error)
return {
statusCode: 400,
body: JSON.stringify(error),
}
})

try {
const response = await client.query(query.Get(query.Ref(query.Collection('items'), id)))
console.log('success', response)
return {
statusCode: 200,
body: JSON.stringify(response),
}
} catch (error) {
console.log('error', error)
return {
statusCode: 400,
body: JSON.stringify(error),
}
}
}

module.exports = { handler }
30 changes: 14 additions & 16 deletions src/functions-templates/js/fauna-crud/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ const handler = async (event) => {
const data = JSON.parse(event.body)
const { id } = event
console.log(`Function 'update' invoked. update id: ${id}`)
return client
.query(query.Update(query.Ref(query.Collection('items'), id), { data }))
.then((response) => {
console.log('success', response)
return {
statusCode: 200,
body: JSON.stringify(response),
}
})
.catch((error) => {
console.log('error', error)
return {
statusCode: 400,
body: JSON.stringify(error),
}
})
try {
const response = await client.query(query.Update(query.Ref(query.Collection('items'), id), { data }))
console.log('success', response)
return {
statusCode: 200,
body: JSON.stringify(response),
}
} catch (error) {
console.log('error', error)
return {
statusCode: 400,
body: JSON.stringify(error),
}
}
}

module.exports = { handler }
14 changes: 8 additions & 6 deletions src/functions-templates/js/sanity-create/sanity-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,20 @@ const handler = async (event) => {
message: payload.message,
}

return client
.create(document)
.then((result) => ({
try {
const result = await client.create(document)
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(result),
}))
.catch((error) => ({
}
} catch (error) {
return {
headers: { 'Content-Type': 'application/json' },
statusCode: 500,
body: error.responseBody || JSON.stringify({ error: 'An error occurred' }),
}))
}
}
}

module.exports = { handler }
14 changes: 8 additions & 6 deletions src/functions-templates/js/sanity-groq/sanity-groq.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ const handler = async (event) => {
// The rest of the query params are handled as parameters to the query
const params = { ...event.queryStringParameters, query: null }

return client
.fetch(query, params)
.then((result) => ({
try {
const result = await client.fetch(query, params)
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(result),
}))
.catch((error) => ({
}
} catch (error) {
return {
headers: { 'Content-Type': 'application/json' },
statusCode: error.statusCode || 500,
body: error.responseBody || JSON.stringify({ error: 'Unknown error occurred' }),
}))
}
}
}

module.exports = { handler }
18 changes: 8 additions & 10 deletions src/functions-templates/js/slack-rate-limit/slack-rate-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ class IdentityAPI {
}
}

parseJsonResponse(response) {
return response.json().then((json) => {
if (!response.ok) {
const error = `JSON: ${JSON.stringify(json)}. Status: ${response.status}`
return Promise.reject(new Error(error))
}

return json
})
async parseJsonResponse(response) {
const json = await response.json()
if (!response.ok) {
const error = `JSON: ${JSON.stringify(json)}. Status: ${response.status}`
throw new Error(error)
}
return json
}

async request(path, options = {}) {
Expand All @@ -42,7 +40,7 @@ class IdentityAPI {
if (!response.ok) {
const data = await response.text()
const error = `Data: ${data}. Status: ${response.status}`
return Promise.reject(new Error(error))
throw new Error(error)
}
return await response.text()
}
Expand Down
11 changes: 7 additions & 4 deletions src/utils/read-repo-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ const readRepoURL = async function (_url) {
return folderContents
}

const getRepoURLContents = function (repoHost, ownerAndRepo, contentsPath) {
const getRepoURLContents = async function (repoHost, ownerAndRepo, contentsPath) {
// naive joining strategy for now
if (repoHost === GITHUB) {
// https://developer.github.com/v3/repos/contents/#get-contents
const APIURL = safeJoin('https://api.github.com/repos', ownerAndRepo, 'contents', contentsPath)
return fetch(APIURL)
.then((res) => res.json())
.catch((error) => console.error(`Error occurred while fetching ${APIURL}`, error))
try {
const res = await fetch(APIURL)
return await res.json()
} catch (error) {
console.error(`Error occurred while fetching ${APIURL}`, error)
}
}
throw new Error('unsupported host ', repoHost)
}
Expand Down