Skip to content

Commit a3344a5

Browse files
authored
refactor: enable and fix prefer-await-to-then (#2002)
1 parent 7011f86 commit a3344a5

File tree

10 files changed

+107
-113
lines changed

10 files changed

+107
-113
lines changed

.eslintrc.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ module.exports = {
2626
'fp/no-this': 0,
2727
'import/max-dependencies': 0,
2828
'node/no-sync': 0,
29-
'promise/catch-or-return': 0,
3029
'promise/no-callback-in-promise': 0,
3130
'promise/no-return-wrap': 0,
3231
'promise/prefer-await-to-callbacks': 0,
33-
'promise/prefer-await-to-then': 0,
3432
'unicorn/prefer-spread': 0,
3533
'unicorn/consistent-destructuring': 0,
3634

src/functions-templates/js/fauna-crud/create-schema.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const process = require('process')
44
/* bootstrap database in your FaunaDB account - use with `netlify dev:exec <path-to-this-file>` */
55
const { query, Client } = require('faunadb')
66

7-
const createFaunaDB = function () {
7+
const createFaunaDB = async function () {
88
if (!process.env.FAUNADB_SERVER_SECRET) {
99
console.log('No FAUNADB_SERVER_SECRET in environment, skipping DB setup')
1010
}
@@ -14,25 +14,23 @@ const createFaunaDB = function () {
1414
})
1515

1616
/* Based on your requirements, change the schema here */
17-
return client
18-
.query(query.CreateCollection({ name: 'items' }))
19-
.then(() => {
20-
console.log('Created items class')
21-
return client.query(
22-
query.CreateIndex({
23-
name: 'all_items',
24-
source: query.Collection('items'),
25-
active: true,
26-
}),
27-
)
28-
})
17+
try {
18+
await client.query(query.CreateCollection({ name: 'items' }))
2919

30-
.catch((error) => {
31-
if (error.requestResult.statusCode === 400 && error.message === 'instance not unique') {
32-
console.log('DB already exists')
33-
}
34-
throw error
35-
})
20+
console.log('Created items class')
21+
return client.query(
22+
query.CreateIndex({
23+
name: 'all_items',
24+
source: query.Collection('items'),
25+
active: true,
26+
}),
27+
)
28+
} catch (error) {
29+
if (error.requestResult.statusCode === 400 && error.message === 'instance not unique') {
30+
console.log('DB already exists')
31+
}
32+
throw error
33+
}
3634
}
3735

3836
createFaunaDB()

src/functions-templates/js/fauna-crud/create.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,22 @@ const handler = async (event) => {
1616
data,
1717
}
1818
/* construct the fauna query */
19-
return client
20-
.query(query.Create(query.Collection('items'), item))
21-
.then((response) => {
22-
console.log('success', response)
23-
/* Success! return the response with statusCode 200 */
24-
return {
25-
statusCode: 200,
26-
body: JSON.stringify(response),
27-
}
28-
})
29-
.catch((error) => {
30-
console.log('error', error)
31-
/* Error! return the error with statusCode 400 */
32-
return {
33-
statusCode: 400,
34-
body: JSON.stringify(error),
35-
}
36-
})
19+
try {
20+
const response = await client.query(query.Create(query.Collection('items'), item))
21+
console.log('success', response)
22+
/* Success! return the response with statusCode 200 */
23+
return {
24+
statusCode: 200,
25+
body: JSON.stringify(response),
26+
}
27+
} catch (error) {
28+
console.log('error', error)
29+
/* Error! return the error with statusCode 400 */
30+
return {
31+
statusCode: 400,
32+
body: JSON.stringify(error),
33+
}
34+
}
3735
}
3836

3937
module.exports = { handler }

src/functions-templates/js/fauna-crud/delete.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ const client = new Client({
1010
const handler = async (event) => {
1111
const { id } = event
1212
console.log(`Function 'delete' invoked. delete id: ${id}`)
13-
return client
14-
.query(query.Delete(query.Ref(query.Collection('items'), id)))
15-
.then((response) => {
16-
console.log('success', response)
17-
return {
18-
statusCode: 200,
19-
body: JSON.stringify(response),
20-
}
21-
})
22-
.catch((error) => {
23-
console.log('error', error)
24-
return {
25-
statusCode: 400,
26-
body: JSON.stringify(error),
27-
}
28-
})
13+
try {
14+
const response = await client.query(query.Delete(query.Ref(query.Collection('items'), id)))
15+
console.log('success', response)
16+
return {
17+
statusCode: 200,
18+
body: JSON.stringify(response),
19+
}
20+
} catch (error) {
21+
console.log('error', error)
22+
return {
23+
statusCode: 400,
24+
body: JSON.stringify(error),
25+
}
26+
}
2927
}
3028

3129
module.exports = { handler }

src/functions-templates/js/fauna-crud/read.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ const client = new Client({
1010
const handler = async (event) => {
1111
const { id } = event
1212
console.log(`Function 'read' invoked. Read id: ${id}`)
13-
return client
14-
.query(query.Get(query.Ref(query.Collection('items'), id)))
15-
.then((response) => {
16-
console.log('success', response)
17-
return {
18-
statusCode: 200,
19-
body: JSON.stringify(response),
20-
}
21-
})
22-
.catch((error) => {
23-
console.log('error', error)
24-
return {
25-
statusCode: 400,
26-
body: JSON.stringify(error),
27-
}
28-
})
13+
14+
try {
15+
const response = await client.query(query.Get(query.Ref(query.Collection('items'), id)))
16+
console.log('success', response)
17+
return {
18+
statusCode: 200,
19+
body: JSON.stringify(response),
20+
}
21+
} catch (error) {
22+
console.log('error', error)
23+
return {
24+
statusCode: 400,
25+
body: JSON.stringify(error),
26+
}
27+
}
2928
}
3029

3130
module.exports = { handler }

src/functions-templates/js/fauna-crud/update.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ const handler = async (event) => {
1111
const data = JSON.parse(event.body)
1212
const { id } = event
1313
console.log(`Function 'update' invoked. update id: ${id}`)
14-
return client
15-
.query(query.Update(query.Ref(query.Collection('items'), id), { data }))
16-
.then((response) => {
17-
console.log('success', response)
18-
return {
19-
statusCode: 200,
20-
body: JSON.stringify(response),
21-
}
22-
})
23-
.catch((error) => {
24-
console.log('error', error)
25-
return {
26-
statusCode: 400,
27-
body: JSON.stringify(error),
28-
}
29-
})
14+
try {
15+
const response = await client.query(query.Update(query.Ref(query.Collection('items'), id), { data }))
16+
console.log('success', response)
17+
return {
18+
statusCode: 200,
19+
body: JSON.stringify(response),
20+
}
21+
} catch (error) {
22+
console.log('error', error)
23+
return {
24+
statusCode: 400,
25+
body: JSON.stringify(error),
26+
}
27+
}
3028
}
3129

3230
module.exports = { handler }

src/functions-templates/js/sanity-create/sanity-create.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,20 @@ const handler = async (event) => {
5353
message: payload.message,
5454
}
5555

56-
return client
57-
.create(document)
58-
.then((result) => ({
56+
try {
57+
const result = await client.create(document)
58+
return {
5959
statusCode: 200,
6060
headers: { 'Content-Type': 'application/json' },
6161
body: JSON.stringify(result),
62-
}))
63-
.catch((error) => ({
62+
}
63+
} catch (error) {
64+
return {
6465
headers: { 'Content-Type': 'application/json' },
6566
statusCode: 500,
6667
body: error.responseBody || JSON.stringify({ error: 'An error occurred' }),
67-
}))
68+
}
69+
}
6870
}
6971

7072
module.exports = { handler }

src/functions-templates/js/sanity-groq/sanity-groq.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@ const handler = async (event) => {
3737
// The rest of the query params are handled as parameters to the query
3838
const params = { ...event.queryStringParameters, query: null }
3939

40-
return client
41-
.fetch(query, params)
42-
.then((result) => ({
40+
try {
41+
const result = await client.fetch(query, params)
42+
return {
4343
statusCode: 200,
4444
headers: { 'Content-Type': 'application/json' },
4545
body: JSON.stringify(result),
46-
}))
47-
.catch((error) => ({
46+
}
47+
} catch (error) {
48+
return {
4849
headers: { 'Content-Type': 'application/json' },
4950
statusCode: error.statusCode || 500,
5051
body: error.responseBody || JSON.stringify({ error: 'Unknown error occurred' }),
51-
}))
52+
}
53+
}
5254
}
5355

5456
module.exports = { handler }

src/functions-templates/js/slack-rate-limit/slack-rate-limit.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ class IdentityAPI {
2020
}
2121
}
2222

23-
parseJsonResponse(response) {
24-
return response.json().then((json) => {
25-
if (!response.ok) {
26-
const error = `JSON: ${JSON.stringify(json)}. Status: ${response.status}`
27-
return Promise.reject(new Error(error))
28-
}
29-
30-
return json
31-
})
23+
async parseJsonResponse(response) {
24+
const json = await response.json()
25+
if (!response.ok) {
26+
const error = `JSON: ${JSON.stringify(json)}. Status: ${response.status}`
27+
throw new Error(error)
28+
}
29+
return json
3230
}
3331

3432
async request(path, options = {}) {
@@ -42,7 +40,7 @@ class IdentityAPI {
4240
if (!response.ok) {
4341
const data = await response.text()
4442
const error = `Data: ${data}. Status: ${response.status}`
45-
return Promise.reject(new Error(error))
43+
throw new Error(error)
4644
}
4745
return await response.text()
4846
}

src/utils/read-repo-url.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ const readRepoURL = async function (_url) {
2323
return folderContents
2424
}
2525

26-
const getRepoURLContents = function (repoHost, ownerAndRepo, contentsPath) {
26+
const getRepoURLContents = async function (repoHost, ownerAndRepo, contentsPath) {
2727
// naive joining strategy for now
2828
if (repoHost === GITHUB) {
2929
// https://developer.github.com/v3/repos/contents/#get-contents
3030
const APIURL = safeJoin('https://api.github.com/repos', ownerAndRepo, 'contents', contentsPath)
31-
return fetch(APIURL)
32-
.then((res) => res.json())
33-
.catch((error) => console.error(`Error occurred while fetching ${APIURL}`, error))
31+
try {
32+
const res = await fetch(APIURL)
33+
return await res.json()
34+
} catch (error) {
35+
console.error(`Error occurred while fetching ${APIURL}`, error)
36+
}
3437
}
3538
throw new Error('unsupported host ', repoHost)
3639
}

0 commit comments

Comments
 (0)