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 4286c0d commit 353e448
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 54 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 @@ -248,6 +248,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 All @@ -25,9 +26,7 @@ describe('Routing metadata', () => {

@api(expectedSpec)
class MyController {
greet() {
return 'Hello world!';
}
greet() {}
}

const actualSpec = getControllerSpec(MyController);
Expand All @@ -41,9 +40,7 @@ describe('Routing metadata', () => {

@api(expectedSpec)
class MyController {
greet() {
return 'Hello world!';
}
greet() {}
}

const spec1 = getControllerSpec(MyController);
Expand All @@ -52,15 +49,11 @@ describe('Routing metadata', () => {
});

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

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

const actualSpec = getControllerSpec(MyController);
Expand All @@ -78,9 +71,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 +93,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 +115,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 +137,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 +158,30 @@ describe('Routing metadata', () => {
});
});

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

class MyController {
@head('/greeting', operationSpec)
greet() {}
}

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 @@ -232,16 +237,12 @@ describe('Routing metadata', () => {

class Parent {
@get('/parent', operationSpec)
getParentName() {
return 'The Parent';
}
getParentName() {}
}

class Child extends Parent {
@get('/child', operationSpec)
getChildName() {
return 'The Child';
}
getChildName() {}
}

const actualSpec = getControllerSpec(Child);
Expand All @@ -265,22 +266,16 @@ describe('Routing metadata', () => {
});

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

class Parent {
@get('/name', operationSpec)
getParentName() {
return 'The Parent';
}
getParentName() {}
}

class Child extends Parent {
@get('/name', operationSpec)
getChildName() {
return 'The Child';
}
getChildName() {}
}

const actualSpec = getControllerSpec(Child);
Expand All @@ -298,16 +293,12 @@ describe('Routing metadata', () => {

class Parent {
@get('/parent-name', operationSpec)
getName() {
return 'The Parent';
}
getName() {}
}

class Child extends Parent {
@get('/child-name', operationSpec)
getName() {
return 'The Child';
}
getName() {}
}

const childSpec = getControllerSpec(Child);
Expand Down Expand Up @@ -337,15 +328,11 @@ describe('Routing metadata', () => {

class Parent {
@get('/greet', operationSpec)
greet(@param.query.string('msg') msg: string) {
return `Parent: ${msg}`;
}
greet(@param.query.string('msg') msg: string) {}
}

class Child extends Parent {
greet(@param.query.string('message') msg: string) {
return `Child: ${msg}`;
}
greet(@param.query.string('message') msg: string) {}
}

const childSpec = getControllerSpec(Child);
Expand All @@ -371,4 +358,10 @@ describe('Routing metadata', () => {
in: 'query',
});
});

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

0 comments on commit 353e448

Please sign in to comment.