Skip to content

Commit

Permalink
feat(cors): Enable CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
anishkny committed May 16, 2018
1 parent d0b2c73 commit 94a3c09
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
87 changes: 69 additions & 18 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,95 +34,146 @@ functions:
createUser:
handler: src/User.create
events:
- http: POST /users
- http:
method: POST
path: /users
cors: true

loginUser:
handler: src/User.login
events:
- http: POST /users/login
- http:
method: POST
path: /users/login
cors: true

getUser:
handler: src/User.get
events:
- http: GET /user
- http:
method: GET
path: /user
cors: true

getProfile:
handler: src/User.getProfile
events:
- http: GET /profiles/{username}
- http:
method: GET
path: /profiles/{username}
cors: true

followUser:
handler: src/User.follow
events:
- http: POST /profiles/{username}/follow
- http:
method: POST
path: /profiles/{username}/follow
cors: true

unfollowUser:
handler: src/User.follow
events:
- http: DELETE /profiles/{username}/follow
- http:
method: DELETE
path: /profiles/{username}/follow
cors: true

## Articles API
createArticle:
handler: src/Article.create
events:
- http: POST /articles
- http:
method: POST
path: /articles
cors: true

getArticle:
handler: src/Article.get
events:
- http: GET /articles/{slug}
- http:
method: GET
path: /articles/{slug}
cors: true

deleteArticle:
handler: src/Article.delete
events:
- http: DELETE /articles/{slug}
- http:
method: DELETE
path: /articles/{slug}
cors: true

favoriteArticle:
handler: src/Article.favorite
events:
- http: POST /articles/{slug}/favorite
- http:
method: POST
path: /articles/{slug}/favorite
cors: true

unfavoriteArticle:
handler: src/Article.favorite
events:
- http: DELETE /articles/{slug}/favorite
- http:
method: DELETE
path: /articles/{slug}/favorite
cors: true

listArticles:
handler: src/Article.list
events:
- http: GET /articles
- http:
method: GET
path: /articles
cors: true

getArticlesFeed:
handler: src/Article.getFeed
events:
- http: GET /articles/feed
- http:
method: GET
path: /articles/feed
cors: true

## Comments API
createComment:
handler: src/Comment.create
events:
- http: POST /articles/{slug}/comments
- http:
method: POST
path: /articles/{slug}/comments
cors: true

getComments:
handler: src/Comment.get
events:
- http: GET /articles/{slug}/comments
- http:
method: GET
path: /articles/{slug}/comments
cors: true

deleteComment:
handler: src/Comment.delete
events:
- http: DELETE /articles/{slug}/comments/{id}
- http:
method: DELETE
path: /articles/{slug}/comments/{id}
cors: true

## Utils API
ping:
handler: src/Util.ping
events:
- http: GET ping
- http:
method: GET
path: /ping
cors: true

purgeData:
handler: src/Util.purgeData
events:
- http: DELETE /TESTUTILS/purge
- http: DELETE /__TESTUTILS__/purge

resources:
Resources:
Expand Down
4 changes: 4 additions & 0 deletions src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module.exports = {
function RESPOND(statusCode, bodyObject, callback) {
callback(null, {
statusCode,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true
},
body: JSON.stringify(bodyObject),
});
}
Expand Down
8 changes: 4 additions & 4 deletions test/test.Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ before(async () => {
console.log(`Testing API_URL: [${API_URL}]`);
axios.defaults.baseURL = API_URL;

process.stdout.write('Purging data... ');
await axios.delete(`/__TESTUTILS__/purge`);
console.log('Done!\n');

// Setup request/response interceptors for testing
axios.interceptors.request.use(async (config) => {

Expand Down Expand Up @@ -52,10 +56,6 @@ before(async () => {
});
}


process.stdout.write('Purging data... ');
await axios.delete(`/TESTUTILS/purge`);
console.log('Done!\n');
});

describe('Util', async () => {
Expand Down

0 comments on commit 94a3c09

Please sign in to comment.