Skip to content

Commit 2672c18

Browse files
authored
Updated readme to address #47 and also taking a stab at updating #40. (#49)
* Updated readme to address #47 and also taking a stab at updating #40. * Updating testing configs to get Travis build passing * PR feedback - non-breaking changes for GATSBY_AIRTABLE_API_KEY, updated Readme, and reverted test changes * Applying Readme suggestions from code review. Will update examples once more to "right" way in next commit. * Re-updating tests to reflect the 'right' way to reference .env values * Updating .env values! * Updating to reflect best practice with environment variable naming
1 parent 380f718 commit 2672c18

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,55 @@ Within graphql (the language you query information from and that this plugin put
126126

127127
Keys can be found in Airtable by clicking `Help > API Documentation`.
128128

129-
The API key can be specified in `gatsby-config.js` as noted in the previous section-- **this exposes your key to anyone viewing your repository and is not recommended**.
129+
The API key can be hard coded directly in `gatsby-config.js` as noted in the previous section-- **this exposes your key to anyone viewing your repository and is not recommended. You should inject your API key as recommended below to prevent it from being committed to source control**.
130130

131-
Alternatively, you may specify your API key using an [Environment Variable](https://www.gatsbyjs.org/docs/environment-variables/). This plugin looks for an environment variable called `GATSBY_AIRTABLE_API_KEY` and will use it prior to resorting to the `apiKey` defined in `gatsby-config.js`. You may also specify it in your command line such as `GATSBY_AIRTABLE_API_KEY=XXXXXX gatsby develop`.
131+
We recommended specifying your API key using an [Environment Variable](https://www.gatsbyjs.org/docs/environment-variables/). You may also specify it in your command line such as `AIRTABLE_API_KEY=XXXXXX gatsby develop`. Note: If you use an environment variable prepended with `GATSBY_`, it takes advantage of some syntactic sugar courtesy of Gatsby, which automatically makes it available - but any references to environment variables like this that are rendered client-side will **automatically** expose your API key within the browser. To avoid accidentally exposing it, we recommend *not* prepending it with `GATSBY_`.
132+
133+
To be safe, you can also setup your API key via a config variable, `apiKey` defined in `gatsby-config.js`. This is the recommended way to inject your API key.
134+
135+
```javascript
136+
// In gatsby-config.js
137+
plugins: [
138+
{
139+
resolve: `gatsby-source-airtable`,
140+
options: {
141+
//not prefaced with "GATSBY_", will not automatically be included client-side unless you explicitly expose it
142+
apiKey: process.env.AIRTABLE_API_KEY
143+
//...etc
144+
}
145+
}
146+
]
147+
```
148+
149+
You can either use a node tool like "dotenv" to load secrets like your Airtable API key from a .env file, or you can specify it in your command line such as `AIRTABLE_API_KEY=XXXXXX gatsby develop`.
132150

133151
If you add or change your API key in an environment variable at the system level, you may need to reload your code editor / IDE for that variable to reload.
152+
153+
### Columns without any values (yet)
154+
155+
If you want to perform conditional logic based on data that may or may not be present in Airtable, but you do not yet have tabular data for the "may" case, you can update the gatsby-source-airtable section of `gatsby-config.js` to include sensible defaults for those fields
156+
so that they will be returned via your graphql calls:
157+
158+
```javascript
159+
// In gatsby-config.js
160+
plugins: [
161+
{
162+
resolve: `gatsby-source-airtable`,
163+
options: {
164+
apiKey: process.env.AIRTABLE_API_KEY,
165+
tables: [
166+
{
167+
baseId: process.env.AIRTABLE_BASE,
168+
tableName: process.env.AIRTABLE_TABLE_NAME,
169+
defaultValues: {
170+
//currently does not accept null / undefined. use empty string instead
171+
//and perform your conditional logic on name_of_field.length > 0 ? condition_1 : condition_2
172+
NAME_OF_FIELD_THAT_WILL_OTHERWISE_NOT_BE_RETURNED_IF_ALL_VALUES_ARE_BLANK: ""
173+
//... etc
174+
}
175+
}
176+
]
177+
}
178+
}
179+
];
180+
```

examples/markdown-docs/gatsby-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
{
88
resolve: `gatsby-source-airtable`,
99
options: {
10-
// apiKey: `YOUR_API_KEY`, (set via environment variable for this example)
10+
apiKey: process.env.AIRTABLE_API_KEY, //(set via environment variable for this example)
1111
tables: [
1212
{
1313
baseId: `appN0O40W9X0mzbsl`,

examples/recipes-with-photos/gatsby-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
options: {
2323
tables: [
2424
{
25+
apiKey: process.env.AIRTABLE_API_KEY, //(set via environment variable for this example)
2526
baseId: `appM8D8wmSJX9WJDE`,
2627
tableName: `Recipes`,
2728
tableView: `List`,

examples/testing-options/gatsby-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
{
88
resolve: `gatsby-source-airtable`,
99
options: {
10-
// apiKey: `YOUR_API_KEY`, (set via environment variable for this example)
10+
apiKey: process.env.AIRTABLE_API_KEY, // (set via environment variable for this example)
1111
tables: [
1212
{
1313
baseId: `appv8dCu3p2PAaA2F`,

gatsby-node.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ exports.sourceNodes = async (
1111

1212
try {
1313
// hoist api so we can use in scope outside of this block
14-
var api = await new Airtable({
15-
apiKey: process.env.GATSBY_AIRTABLE_API_KEY || apiKey
16-
});
14+
if (!apiKey && process.env.GATSBY_AIRTABLE_API_KEY) {
15+
console.warn("\nImplicit setting of GATSBY_AIRTABLE_API_KEY as apiKey will be deprecated in future release, apiKey should be set in gatsby-config.js, please see Readme!")
16+
}
17+
var api = await new Airtable({ apiKey: process.env.GATSBY_AIRTABLE_API_KEY || apiKey });
1718
} catch (e) {
1819
// airtable uses `assert` which doesn't exit the process,
1920
// but rather just makes gatsby hang. Warn, don't create any

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gatsby-source-airtable",
3-
"version": "2.0.6",
3+
"version": "2.0.7",
44
"description": "Gatsby Airtable Source with Linked Nodes, Multiple Tables and File Nodes.",
55
"main": "index.js",
66
"files": [

0 commit comments

Comments
 (0)