├── index.js ├── function.json ├── package.json ├── .gitignore ├── index.dev.js └── README.md /index.js: -------------------------------------------------------------------------------- 1 | module.exports.handler = (event, context, callback) => { 2 | const response = { 3 | statusCode: 200, 4 | headers: { 5 | 'Access-Control-Allow-Origin': '*', 6 | 'Content-Type': 'text/plain' 7 | }, 8 | body: 'Hello world! Custom message: ' + process.env.CUSTOM_MESSAGE + ' Event body: ' + (event ? event.body : null) 9 | }; 10 | callback(null, response); 11 | } -------------------------------------------------------------------------------- /function.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "Function Starter", 3 | "description": "Cosmic JS Function starter.", 4 | "image_url": "https://cosmicjs.com/images/marketing/logo-w-brand.jpg", 5 | "repo_url": "https://github.com/cosmicjs/function-starter", 6 | "env_vars": [ 7 | { 8 | "key": "CUSTOM_MESSAGE", 9 | "value": "" 10 | } 11 | ], 12 | "routes": [ 13 | { 14 | "path": "hello-world", 15 | "method": "get", 16 | "cors": true 17 | }, 18 | { 19 | "path": "poster", 20 | "method": "post", 21 | "cors": true 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "function-starter", 3 | "version": "1.0.0", 4 | "description": "A Cosmic JS Function Starter", 5 | "main": "index.js", 6 | "scripts": { 7 | "develop": "node index.dev.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/cosmicjs/function-starter.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/cosmicjs/function-starter/issues" 17 | }, 18 | "homepage": "https://github.com/cosmicjs/function-starter#readme", 19 | "dependencies": { 20 | "body-parser": "^1.18.3", 21 | "express": "^4.16.3" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # parcel-bundler cache (https://parceljs.org/) 61 | .cache 62 | 63 | # next.js build output 64 | .next 65 | 66 | # nuxt.js build output 67 | .nuxt 68 | 69 | # vuepress build output 70 | .vuepress/dist 71 | 72 | # Serverless directories 73 | .serverless 74 | .DS_Store -------------------------------------------------------------------------------- /index.dev.js: -------------------------------------------------------------------------------- 1 | const app = require('express')() 2 | const handler = require('./index').handler 3 | const routes = require('./function').routes 4 | const bodyParser = require('body-parser') 5 | app.use(bodyParser.json()) 6 | const PORT = process.env.PORT || 3000 7 | routes.forEach(route => { 8 | if (route.method === 'get') { 9 | app.get('/' + route.path, (req, res) => { 10 | const event = { 11 | body: JSON.stringify(req.body) 12 | } 13 | handler(event, null, (err, response) => { 14 | res.send(response.body) 15 | }) 16 | }) 17 | } 18 | if (route.method === 'post') { 19 | app.post('/' + route.path, (req, res) => { 20 | const event = { 21 | body: JSON.stringify(req.body) 22 | } 23 | handler(event, null, (err, response) => { 24 | res.send(response.body) 25 | }) 26 | }) 27 | } 28 | if (route.method === 'put') { 29 | app.put('/' + route.path, (req, res) => { 30 | const event = { 31 | body: JSON.stringify(req.body) 32 | } 33 | handler(event, null, (err, response) => { 34 | res.send(response.body) 35 | }) 36 | }) 37 | } 38 | if (route.method === 'delete') { 39 | app.del('/' + route.path, (req, res) => { 40 | const event = { 41 | body: JSON.stringify(req.body) 42 | } 43 | handler(event, null, (err, response) => { 44 | res.send(response.body) 45 | }) 46 | }) 47 | } 48 | }) 49 | app.listen(PORT) 50 | console.log(`Listening on port ${PORT}`) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Function Starter 2 | Cosmic JS Function Starter. 3 | 4 | ### Getting Started 5 | You can run this Function locally for development: 6 | ``` 7 | git clone https://github.com/cosmicjs/function-starter 8 | cd function-starter 9 | npm install 10 | npm run develop 11 | ``` 12 | 13 | ### Installation 14 | 1. [Login to Cosmic JS](https://cosmicjs.com) and go to Your Bucket > Settings > Functions 15 | 2. Add the link to this repo: `https://github.com/cosmicjs/function-starter` and click "Install Function" 16 | 17 | #### function.json file properties 18 | Key | Type | Description 19 | --- | --- | --- 20 | | title | String | Function title 21 | | description | String | Function description (HTML allowed) 22 | | image_url | String | Image thumbnail 23 | | repo_url | String | Function git repo url 24 | | env_vars | Array | key / value for environment variables 25 | | routes | Array | Function routes: properties include path (string), method (string) and cors (bool) 26 | 27 | Example `function.json` file: 28 | ```json 29 | { 30 | "title": "Function Starter", 31 | "description": "Cosmic JS Function starter.", 32 | "image_url": "https://cosmicjs.com/images/marketing/logo-w-brand.jpg", 33 | "repo_url": "https://github.com/cosmicjs/function-starter", 34 | "env_vars": [ 35 | { 36 | "key": "CUSTOM_MESSAGE", 37 | "value": "" 38 | } 39 | ], 40 | "routes": [ 41 | { 42 | "path": "hello-world", 43 | "method": "get", 44 | "cors": true 45 | } 46 | ] 47 | } 48 | ``` 49 | --------------------------------------------------------------------------------