Skip to content

Commit a5440ca

Browse files
committed
netlify spike
1 parent 881cd6d commit a5440ca

File tree

7 files changed

+102
-9
lines changed

7 files changed

+102
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
npm-debug.log*
2323
yarn-debug.log*
2424
yarn-error.log*
25+

.netlify/functions/getData.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { config } from "dotenv";
2+
config();
3+
const key = process.env.AIRTABLE_KEY;
4+
5+
const https = require("https");
6+
7+
const request = url => {
8+
return new Promise((resolve, reject) => {
9+
https
10+
.get(url, response => {
11+
let allTheData = "";
12+
response.on("data", chunk => {
13+
allTheData += chunk;
14+
});
15+
response.on("end", () => {
16+
try {
17+
resolve(JSON.parse(allTheData));
18+
} catch (e) {
19+
reject(`There was an error with the JSON`);
20+
}
21+
});
22+
})
23+
.on("error", err => reject(`There was an error: ${err}`));
24+
});
25+
};
26+
27+
export async function handler(event, context) {
28+
console.log("test");
29+
try {
30+
const data = await request(
31+
`https://api.airtable.com/v0/appkLPDVgr4TwR3lM/Imported%20table?maxRecords=3&view=Grid%20view&api_key=${key}`
32+
).then(res => {
33+
if (!res.ok) {
34+
const error = new Error("HTTP error");
35+
error.status = res.status;
36+
throw error;
37+
}
38+
return res.json();
39+
});
40+
return {
41+
statusCode: 200,
42+
body: JSON.stringify(data)
43+
};
44+
} catch (error) {
45+
return {
46+
statusCode: error.status || 500,
47+
body: error.message
48+
};
49+
}
50+
}

.netlify/state.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"siteId": "0a4736b3-c0be-4efe-9b3f-4810feb0b204"
3+
}

netlify.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Settings in the [build] context are global and are applied to all contexts
2+
# unless otherwise overridden by more specific contexts.
3+
[build]
4+
# Directory to change to before starting a build.
5+
# This is where we will look for package.json/.nvmrc/etc.
6+
base = "./"
7+
8+
# Directory (relative to root of your repo) that contains the deploy-ready
9+
# HTML files and assets generated by the build. If a base directory has
10+
# been specified, include it in the publish directory path.
11+
publish = "./build"
12+
13+
14+
# Directory with the serverless Lambda functions to deploy to AWS.
15+
functions = ".netlify/functions/"

package-lock.json

+10-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"dotenv": "^8.0.0",
67
"react": "^16.8.6",
78
"react-dom": "^16.8.6",
89
"react-scripts": "3.0.1"

src/App.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React from "react";
2+
import logo from "./logo.svg";
3+
import "./App.css";
44

55
function App() {
6+
const [data, setData] = React.useState(null);
7+
8+
const getData = () => {
9+
return fetch(`/.netlify/functions/getData`)
10+
.then(result => {
11+
console.log(result.json());
12+
result.json();
13+
})
14+
.catch("error");
15+
};
16+
17+
const assignData = () => {
18+
getData().then(result => {
19+
setData(result.records[0].fields.Item);
20+
});
21+
};
22+
// eslint-disable-next-line
23+
React.useEffect(() => assignData(), []);
624
return (
725
<div className="App">
826
<header className="App-header">
927
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
28+
<p>{data}</p>
1329
<a
1430
className="App-link"
1531
href="https://reactjs.org"

0 commit comments

Comments
 (0)