Skip to content

Commit

Permalink
fix(@whook/cors): Avoid requiring parameters in CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed May 28, 2019
1 parent 3ed96fd commit fc15a89
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
22 changes: 19 additions & 3 deletions packages/whook-cors/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,24 @@ Object {
},
"/users/{userid}": Object {
"head": Object {
"operationId": "ping",
"operationId": "getUser",
"parameters": Array [
Object {
"in": "path",
"name": "userId",
"required": true,
"type": "number",
},
Object {
"in": "query",
"name": "full",
"required": true,
"type": "boolean",
},
Object {
"in": "query",
"name": "retry",
"required": false,
"type": "boolean",
},
],
Expand All @@ -63,19 +71,27 @@ Object {
"description": "The user",
},
},
"summary": "Checks API's availability.",
"summary": "Return a user.",
},
"options": Object {
"operationId": "optionsWithCORS",
"parameters": Array [
Object {
"in": "path",
"name": "userId",
"required": true,
"type": "number",
},
Object {
"in": "query",
"name": "full",
"required": false,
"type": "boolean",
},
Object {
"in": "query",
"name": "retry",
"required": false,
"type": "boolean",
},
],
Expand All @@ -90,7 +106,7 @@ Object {
],
"x-whook": Object {
"private": true,
"sourceOperationId": "ping",
"sourceOperationId": "getUser",
"suffix": "CORS",
"type": "http",
},
Expand Down
26 changes: 17 additions & 9 deletions packages/whook-cors/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ export async function augmentAPIWithCORS(API) {
const operations = await getOpenAPIOperations(await flattenOpenAPI(API));

return operations.reduce((newAPI, operation) => {
if ('options' === operation.method) {
return newAPI;
}

const existingOperation = newAPI.paths[operation.path].options;

if (existingOperation) {
return newAPI;
}

const whookConfig = {
type: 'http',
...(operation['x-whook'] || {}),
Expand All @@ -60,12 +69,6 @@ export async function augmentAPIWithCORS(API) {
if (whookConfig.type !== 'http') {
return newAPI;
}
if ('options' === operation.method) {
return newAPI;
}
if (existingOperation) {
return newAPI;
}

newAPI.paths[operation.path].options = {
operationId: 'optionsWithCORS',
Expand All @@ -74,9 +77,14 @@ export async function augmentAPIWithCORS(API) {
'x-whook': {
...whookConfig,
},
parameters: (operation.parameters || []).filter(
parameter => 'path' === parameter.in || 'query' === parameter.in,
),
parameters: (operation.parameters || [])
.filter(
parameter => 'path' === parameter.in || 'query' === parameter.in,
)
.map(parameter => ({
...parameter,
required: 'path' === parameter.in,
})),
responses: {
200: {
description: 'CORS sent.',
Expand Down
12 changes: 10 additions & 2 deletions packages/whook-cors/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,26 @@ describe('augmentAPIWithCORS()', () => {
},
'/users/{userid}': {
head: {
operationId: 'ping',
summary: "Checks API's availability.",
operationId: 'getUser',
summary: 'Return a user.',
parameters: [
{
in: 'path',
name: 'userId',
type: 'number',
required: true,
},
{
in: 'query',
name: 'full',
type: 'boolean',
required: true,
},
{
in: 'query',
name: 'retry',
type: 'boolean',
required: false,
},
],
responses: {
Expand Down

0 comments on commit fc15a89

Please sign in to comment.