Skip to content

Commit

Permalink
feat(openapi-v2): add sugar function for head op
Browse files Browse the repository at this point in the history
  • Loading branch information
biniam committed Jan 15, 2018
1 parent ca81eb2 commit eabbeb2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
12 changes: 12 additions & 0 deletions packages/openapi-v2/src/controller-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ export function del(path: string, spec?: OperationObject) {
return operation('delete', path, spec);
}

/**
* Expose a Controller method as a REST API operation
* mapped to `HEAD` request method.
*
* @param path The URL path of this operation, e.g. `/product/{id}`
* @param spec The OpenAPI specification describing parameters and responses
* of this operation.
*/
export function head(path: string, spec?: OperationObject) {
return operation('head', path, spec);
}

/**
* Expose a Controller method as a REST API operation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
put,
patch,
del,
head,
param,
} from '../../..';
import {expect} from '@loopback/testlab';
Expand Down Expand Up @@ -52,9 +53,7 @@ describe('Routing metadata', () => {
});

it('returns spec defined via @get decorator', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
const operationSpec = givenAnOperationSpec();

class MyController {
@get('/greet', operationSpec)
Expand All @@ -78,9 +77,7 @@ describe('Routing metadata', () => {
});

it('returns spec defined via @post decorator', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
const operationSpec = givenAnOperationSpec();

class MyController {
@post('/greeting', operationSpec)
Expand All @@ -102,9 +99,7 @@ describe('Routing metadata', () => {
});

it('returns spec defined via @put decorator', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
const operationSpec = givenAnOperationSpec();

class MyController {
@put('/greeting', operationSpec)
Expand All @@ -126,9 +121,7 @@ describe('Routing metadata', () => {
});

it('returns spec defined via @patch decorator', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
const operationSpec = givenAnOperationSpec();

class MyController {
@patch('/greeting', operationSpec)
Expand All @@ -150,9 +143,7 @@ describe('Routing metadata', () => {
});

it('returns spec defined via @del decorator', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
const operationSpec = givenAnOperationSpec();

class MyController {
@del('/greeting', operationSpec)
Expand All @@ -173,10 +164,32 @@ describe('Routing metadata', () => {
});
});

it('returns spec defined via @head decorator', () => {
const operationSpec = givenAnOperationSpec();

class MyController {
@head('/greeting', operationSpec)
greet() {
return 'Hello world!';
}
}

const actualSpec = getControllerSpec(MyController);

expect(actualSpec).to.eql({
paths: {
'/greeting': {
head: {
'x-operation-name': 'greet',
...operationSpec,
},
},
},
});
});

it('returns spec defined via @operation decorator', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
const operationSpec = givenAnOperationSpec();

class MyController {
@operation('post', '/greeting', operationSpec)
Expand Down Expand Up @@ -265,9 +278,7 @@ describe('Routing metadata', () => {
});

it('allows children to override parent REST endpoints', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
const operationSpec = givenAnOperationSpec();

class Parent {
@get('/name', operationSpec)
Expand Down Expand Up @@ -371,4 +382,10 @@ describe('Routing metadata', () => {
in: 'query',
});
});

function givenAnOperationSpec() {
return anOperationSpec()
.withStringResponse()
.build();
}
});

0 comments on commit eabbeb2

Please sign in to comment.