Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit d264629

Browse files
committed
added function examples
1 parent eba5313 commit d264629

25 files changed

+12487
-1
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# code-examples
2-
Code snippets for customers
2+
3+
Code snippets for Netlify users
4+
5+
# Netlify AWS Lambda Function Examples
6+
7+
These next functions are in subfolders, but are meant to be deployed from their own repo. They each include their own netlify.toml files.
8+
9+
## rest
10+
11+
This is a basic example of a function that when called, makes a request to a rest API using node-fetch, and returns a portion of the response back to the client. It includes an index.html that makes the call and displays the response
12+
13+
## random-word
14+
15+
This is a basic example of a function that when called, returns a random word from an array of words back to the user. This function has no build process and does not use any external packages
16+
17+
## use-env
18+
19+
This is a basic example of a function that uses a build environment variable that's specified in the users netlify site's deploy settings.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Netlify Function Examples
2+
Basic Examples of using netlify functions
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Netlify Intercom Tools</title>
9+
</head>
10+
11+
<body>
12+
<div>
13+
<h2>Intercom Tools</h2>
14+
<button type="submit" onclick="getWord()">Click me to get a random word</button>
15+
<p id="random-word-p">Your random word is...</p>
16+
</div>
17+
<script>
18+
const getWord = async () => {
19+
const textBlock = document.getElementById("random-word-p")
20+
try {
21+
const response = await fetch('/.netlify/functions/random')
22+
const resObj = await response.json()
23+
console.log(resObj)
24+
textBlock.innerHTML = `Your random word is <strong>${resObj.word}</strong>`
25+
} catch (err) {
26+
textBlock.innerHTML = "Sorry the request failed"
27+
}
28+
}
29+
</script>
30+
</body>
31+
32+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"extends": [
3+
"airbnb",
4+
"prettier",
5+
"prettier/react"
6+
],
7+
"parser": "babel-eslint",
8+
"parserOptions": {
9+
"ecmaVersion": 8,
10+
"ecmaFeatures": {
11+
"experimentalObjectRestSpread": true,
12+
"impliedStrict": true,
13+
"classes": true
14+
}
15+
},
16+
"env": {
17+
"browser": true,
18+
"node": true,
19+
"jquery": true
20+
},
21+
"rules": {
22+
"no-unused-vars": [
23+
1,
24+
{
25+
"argsIgnorePattern": "res|next|^err"
26+
}
27+
],
28+
"arrow-body-style": [
29+
2,
30+
"as-needed"
31+
],
32+
"no-param-reassign": [
33+
2,
34+
{
35+
"props": false
36+
}
37+
],
38+
"no-console": 0,
39+
"import/prefer-default-export": 0,
40+
"import": 0,
41+
"func-names": 0,
42+
"space-before-function-paren": 0,
43+
"comma-dangle": 0,
44+
"max-len": 0,
45+
"import/extensions": 0,
46+
"no-underscore-dangle": 0,
47+
"consistent-return": 0,
48+
"react/display-name": 1,
49+
"react/react-in-jsx-scope": 0,
50+
"react/forbid-prop-types": 0,
51+
"react/no-unescaped-entities": 0,
52+
"jsx-a11y/accessible-emoji": 0,
53+
"react/jsx-filename-extension": [
54+
1,
55+
{
56+
"extensions": [
57+
".js",
58+
".jsx"
59+
]
60+
}
61+
],
62+
"radix": 0,
63+
"no-shadow": [
64+
2,
65+
{
66+
"hoist": "all",
67+
"allow": [
68+
"resolve",
69+
"reject",
70+
"done",
71+
"next",
72+
"err",
73+
"error"
74+
]
75+
}
76+
],
77+
"quotes": [
78+
2,
79+
"single",
80+
{
81+
"avoidEscape": true,
82+
"allowTemplateLiterals": true
83+
}
84+
],
85+
"prettier/prettier": [
86+
"error",
87+
{
88+
"trailingComma": "es5",
89+
"singleQuote": true,
90+
"printWidth": 120
91+
}
92+
],
93+
"jsx-a11y/href-no-hash": "off",
94+
"jsx-a11y/anchor-is-valid": [
95+
"warn",
96+
{
97+
"aspects": [
98+
"invalidHref"
99+
]
100+
}
101+
]
102+
},
103+
"plugins": [
104+
"html",
105+
"prettier"
106+
]
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"dependencies": {
5+
"node-fetch": "^1.7.3"
6+
},
7+
"private": true,
8+
"devDependencies": {
9+
"lodash": "^4.17.5"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
exports.handler = function(event, context, callback) {
2+
const randomWords = [
3+
'copper',
4+
'explain',
5+
'tenuous',
6+
'neat',
7+
'discovery',
8+
'sweltering',
9+
'dusty',
10+
'unruly',
11+
'week',
12+
'rejoice',
13+
];
14+
const randomNumber = Math.floor(Math.random() * 10);
15+
callback(null, {
16+
statusCode: 200,
17+
headers: {
18+
'Access-Control-Allow-Origin': '*',
19+
},
20+
body: JSON.stringify({ word: randomWords[randomNumber] }),
21+
});
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build]
2+
publish = "_site/"
3+
functions = "functions/"

0 commit comments

Comments
 (0)