diff --git a/.eslintrc.js b/.eslintrc.js index 91a77a81eb4..f37cb8a6d65 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -26,11 +26,9 @@ module.exports = { 'fp/no-this': 0, 'import/max-dependencies': 0, 'node/no-sync': 0, - 'promise/catch-or-return': 0, '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, diff --git a/src/functions-templates/js/fauna-crud/create-schema.js b/src/functions-templates/js/fauna-crud/create-schema.js index ac71ad8bd75..f967c67b2ee 100755 --- a/src/functions-templates/js/fauna-crud/create-schema.js +++ b/src/functions-templates/js/fauna-crud/create-schema.js @@ -4,7 +4,7 @@ const process = require('process') /* bootstrap database in your FaunaDB account - use with `netlify dev:exec ` */ 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') } @@ -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( + 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() diff --git a/src/functions-templates/js/fauna-crud/create.js b/src/functions-templates/js/fauna-crud/create.js index 530528e1da7..5c429669a09 100644 --- a/src/functions-templates/js/fauna-crud/create.js +++ b/src/functions-templates/js/fauna-crud/create.js @@ -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 } diff --git a/src/functions-templates/js/fauna-crud/delete.js b/src/functions-templates/js/fauna-crud/delete.js index 62e38ac1b92..9c9153e1f5b 100644 --- a/src/functions-templates/js/fauna-crud/delete.js +++ b/src/functions-templates/js/fauna-crud/delete.js @@ -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 } diff --git a/src/functions-templates/js/fauna-crud/read.js b/src/functions-templates/js/fauna-crud/read.js index 8ded583d3eb..ab919aa580b 100644 --- a/src/functions-templates/js/fauna-crud/read.js +++ b/src/functions-templates/js/fauna-crud/read.js @@ -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 } diff --git a/src/functions-templates/js/fauna-crud/update.js b/src/functions-templates/js/fauna-crud/update.js index 2aee42ddda0..b8af69f7b92 100644 --- a/src/functions-templates/js/fauna-crud/update.js +++ b/src/functions-templates/js/fauna-crud/update.js @@ -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 } diff --git a/src/functions-templates/js/sanity-create/sanity-create.js b/src/functions-templates/js/sanity-create/sanity-create.js index 2afdf683ea4..10671d427d6 100644 --- a/src/functions-templates/js/sanity-create/sanity-create.js +++ b/src/functions-templates/js/sanity-create/sanity-create.js @@ -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 } diff --git a/src/functions-templates/js/sanity-groq/sanity-groq.js b/src/functions-templates/js/sanity-groq/sanity-groq.js index 18fb81acb42..221c6212800 100644 --- a/src/functions-templates/js/sanity-groq/sanity-groq.js +++ b/src/functions-templates/js/sanity-groq/sanity-groq.js @@ -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 } diff --git a/src/functions-templates/js/slack-rate-limit/slack-rate-limit.js b/src/functions-templates/js/slack-rate-limit/slack-rate-limit.js index 55cbc657b37..2446231aa0e 100644 --- a/src/functions-templates/js/slack-rate-limit/slack-rate-limit.js +++ b/src/functions-templates/js/slack-rate-limit/slack-rate-limit.js @@ -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 = {}) { @@ -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() } diff --git a/src/utils/read-repo-url.js b/src/utils/read-repo-url.js index 77d492e1a99..8e20e7055dd 100644 --- a/src/utils/read-repo-url.js +++ b/src/utils/read-repo-url.js @@ -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) }