forked from fac18/safe-space
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
un-gitignore the netlify functions folder
- Loading branch information
groupanimal
committed
Feb 12, 2020
1 parent
080e802
commit 5ff0774
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ yarn-error.log* | |
|
||
# Local Netlify folders | ||
.netlify | ||
functions | ||
|
||
# Logs | ||
logs | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// import Airtable.js package | ||
// import Airtable from 'airtable'; // produces a SyntaxError ('cannot use import statement outside a module') | ||
const Airtable = require('airtable'); | ||
|
||
// import local environment variables if in dev environment - is this necessary? | ||
// import dotenv from 'dotenv'; | ||
// if (process.env.NODE_ENV === 'development') { | ||
// dotenv.config() | ||
// { AIRTABLE_API_URL, AIRTABLE_API_KEY, AIRTABLE_BASE_ID } = process.env; | ||
// } | ||
|
||
// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method | ||
// the handler function can take event and context params (not required in this case) | ||
// it can also take a callback as third argument, but we will stick to promises | ||
exports.handler = async () => { | ||
// grab airtable variables from Netlify environment (uploaded via UI) | ||
const { AIRTABLE_API_KEY, AIRTABLE_BASE_ID, AIRTABLE_API_URL } = process.env; | ||
|
||
// configure connection to airtable base | ||
Airtable.configure({ | ||
endpointUrl: AIRTABLE_API_URL, | ||
apiKey: AIRTABLE_API_KEY, | ||
}); | ||
const base = Airtable.base(AIRTABLE_BASE_ID); | ||
|
||
let data = []; | ||
|
||
// select all records - only need to check first page since questions won't exceed 100 entries | ||
// need an await to ensure data is assigned to records before returning response | ||
await base('UserQuestions') | ||
.select({ | ||
maxRecords: 100, | ||
view: 'Grid view', | ||
}) | ||
.firstPage() | ||
.then(records => { | ||
records.forEach(record => { | ||
data.push(record.fields); | ||
}); | ||
}) | ||
.catch(err => { | ||
console.log(err.status); | ||
}); | ||
|
||
// finally return any data collected as body of a response object | ||
try { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(data), | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}; | ||
} catch (err) { | ||
return { statusCode: 500, body: err.toString() }; | ||
} | ||
}; |
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