Skip to content

Commit 7d4bc57

Browse files
committed
initial custom headers example, using development versions of api-builder and claudia
1 parent 2b53034 commit 7d4bc57

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

Diff for: web-api-custom-headers/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
A simple example demonstrating how to use custom headers in API responses.
2+
3+
1. run `npm install` to grab the dependencies
4+
2. run `npm start` to set up the lambda project under the default name on AWS
5+
3. Check out the API ID in `claudia.json` (the previous step creates the file)
6+
4. Open https://API_ID.execute-api.us-east-1.amazonaws.com/latest/hard-coded-headers in a browser (replace API_ID with the API ID from `claudia.json`) and inspect the headers. You should see an `X-Version` header with the value `101`, and `Content-Type` of `text/plain`
7+
8+
Check out [web.js](web.js) to see how the paths are set up. For more information, see the [Claudia Api Builder](https://github.com/claudiajs/claudia-api-builder) documentation.
9+

Diff for: web-api-custom-headers/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "web-api-custom-headers",
3+
"description": "An example for returning custom headers from API Gateway responses",
4+
"private": true,
5+
"files": [
6+
"*.js"
7+
],
8+
"scripts": {
9+
"start": "claudia create --name web-api-custom-headers --region us-east-1 --api-module web",
10+
"deploy": "claudia update"
11+
},
12+
"devDependencies": {
13+
"claudia": "github:claudiajs/claudia"
14+
},
15+
"dependencies": {
16+
"bluebird": "^3.3.4",
17+
"claudia-api-builder": "github:claudiajs/claudia-api-builder"
18+
}
19+
}

Diff for: web-api-custom-headers/web.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*global require, module*/
2+
var ApiBuilder = require('claudia-api-builder'),
3+
api = new ApiBuilder(),
4+
Promise = require('bluebird');
5+
module.exports = api;
6+
7+
// set headers as key-value pairs using the success.headers property */
8+
api.get('/hard-coded-headers', function () {
9+
'use strict';
10+
return {a: 'b'};
11+
}, {success: {headers: {'X-Version': '101', 'Content-Type': 'text/plain'}}});
12+
13+
// or use dynamic headers by enumerating them as an array, then returning new api.ApiResponse(content, headers)
14+
api.get('/programmatic-headers', function () {
15+
'use strict';
16+
return new api.ApiResponse('OK', {'X-Version': '202', 'Content-Type': 'text/plain'});
17+
}, {success: {headers: ['X-Version', 'Content-Type']}});
18+
19+
// dynamic headers also work with promises, just resolve with new api.ApiResponse
20+
21+
api.get('/programmatic-headers-promise', function () {
22+
'use strict';
23+
return Promise.delay(100).then(function () {
24+
return new api.ApiResponse('OK', {'X-Version': '303', 'Content-Type': 'text/plain'});
25+
});
26+
}, {success: {headers: ['X-Version', 'Content-Type']}});
27+
28+
// error handlers can only use hard-coded header values, dynamic headers are not supported
29+
30+
api.get('/error-headers', function () {
31+
'use strict';
32+
throw 'Abort';
33+
}, {error: {headers: {'X-Version': '404'}}});
34+

0 commit comments

Comments
 (0)