|
| 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