Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mapping for http methods #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ The following decorators can be used:
- @OPTIONS
- @HEAD

Also exists mapping options to use @Path and @Method together:

- @GETMapping
- @POSTMapping
- @PUTMapping
- @PATCHMapping
- @DELETEMapping
- @OPTIONSMapping
- @HEADMapping

Some examples:

```typescript
Expand All @@ -367,6 +377,28 @@ class UserService {
}
```

Using mappings:

```typescript
@Path("/users")
class UserService {
@GET
getUsers(): Promise<Array<User>> {
//...
}

@GETMapping(":userId")
getUser(@PathParam("userId")): Promise<User> {
//...
}

@PUTMapping(":userId")
saveUser(@PathParam("userId"), user: User): void {
//...
}
}
```

Only methods decorated with one of this HTTP method decorators are exposed as handlers for
requests on the server.

Expand Down
238 changes: 238 additions & 0 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,40 @@ export function GET(target: any, propertyKey: string,
processHttpVerb(target, propertyKey, HttpMethod.GET);
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP GET requests for specific path.
*
* For example:
*
* ```
* @ Path('people')
* class PeopleService {
* @ GETMapping('/list')
* getPeople() {
* // ...
* }
* }
* ```
*
* Will create a service that listen for requests like:
*
* ```
* GET http://mydomain/people/list
* ```
*/
export function GETMapping(path: string = '/') {
return function (...args: any[]) {
args = _.without(args, undefined);
if (args.length === 3 && typeof args[2] === 'object') {
GET(args[0], args[1], args[2]);
return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
}

throw new Error('Invalid @GETMapping Decorator declaration.');
};
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP POST requests.
Expand Down Expand Up @@ -440,6 +474,40 @@ export function POST(target: any, propertyKey: string,
processHttpVerb(target, propertyKey, HttpMethod.POST);
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP POST requests for specific path.
*
* For example:
*
* ```
* @ Path('people')
* class PeopleService {
* @ POSTMapping('/add')
* addPerson() {
* // ...
* }
* }
* ```
*
* Will create a service that listen for requests like:
*
* ```
* POST http://mydomain/people/add
* ```
*/
export function POSTMapping(path: string = '/') {
return function (...args: any[]) {
args = _.without(args, undefined);
if (args.length === 3 && typeof args[2] === 'object') {
POST(args[0], args[1], args[2]);
return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
}

throw new Error('Invalid @POSTMapping Decorator declaration.');
};
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP PUT requests.
Expand Down Expand Up @@ -468,6 +536,40 @@ export function PUT(target: any, propertyKey: string,
processHttpVerb(target, propertyKey, HttpMethod.PUT);
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP PUT requests for specific path.
*
* For example:
*
* ```
* @ Path('people')
* class PeopleService {
* @ PUTMapping(':id')
* savePerson(person: Person) {
* // ...
* }
* }
* ```
*
* Will create a service that listen for requests like:
*
* ```
* PUT http://mydomain/people/123
* ```
*/
export function PUTMapping(path: string = '/') {
return function (...args: any[]) {
args = _.without(args, undefined);
if (args.length === 3 && typeof args[2] === 'object') {
PUT(args[0], args[1], args[2]);
return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
}

throw new Error('Invalid @PUTMapping Decorator declaration.');
};
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP DELETE requests.
Expand Down Expand Up @@ -496,6 +598,40 @@ export function DELETE(target: any, propertyKey: string,
processHttpVerb(target, propertyKey, HttpMethod.DELETE);
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP DELETE requests for specific path.
*
* For example:
*
* ```
* @ Path('people')
* class PeopleService {
* @ DELETEMapping(':id')
* removePerson(@ PathParam('id')id: string) {
* // ...
* }
* }
* ```
*
* Will create a service that listen for requests like:
*
* ```
* PUT http://mydomain/people/123
* ```
*/
export function DELETEMapping(path: string = '/') {
return function (...args: any[]) {
args = _.without(args, undefined);
if (args.length === 3 && typeof args[2] === 'object') {
DELETE(args[0], args[1], args[2]);
return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
}

throw new Error('Invalid @DELETEMapping Decorator declaration.');
};
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP HEAD requests.
Expand Down Expand Up @@ -523,6 +659,40 @@ export function HEAD(target: any, propertyKey: string,
processHttpVerb(target, propertyKey, HttpMethod.HEAD);
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP HEAD requests for specific path.
*
* For example:
*
* ```
* @ Path('people')
* class PeopleService {
* @ HEADMapping('/list')
* headPerson() {
* // ...
* }
* }
* ```
*
* Will create a service that listen for requests like:
*
* ```
* HEAD http://mydomain/people/list
* ```
*/
export function HEADMapping(path: string = '/') {
return function (...args: any[]) {
args = _.without(args, undefined);
if (args.length === 3 && typeof args[2] === 'object') {
HEAD(args[0], args[1], args[2]);
return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
}

throw new Error('Invalid @HEADMapping Decorator declaration.');
};
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP OPTIONS requests.
Expand Down Expand Up @@ -550,6 +720,40 @@ export function OPTIONS(target: any, propertyKey: string,
processHttpVerb(target, propertyKey, HttpMethod.OPTIONS);
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP OPTIONS requests for specific path.
*
* For example:
*
* ```
* @ Path('people')
* class PeopleService {
* @ OPTIONSMapping('/list')
* optionsPerson() {
* // ...
* }
* }
* ```
*
* Will create a service that listen for requests like:
*
* ```
* OPTIONS http://mydomain/people/list
* ```
*/
export function OPTIONSMapping(path: string = '/') {
return function (...args: any[]) {
args = _.without(args, undefined);
if (args.length === 3 && typeof args[2] === 'object') {
OPTIONS(args[0], args[1], args[2]);
return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
}

throw new Error('Invalid @OPTIONSMapping Decorator declaration.');
};
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP PATCH requests.
Expand Down Expand Up @@ -578,6 +782,40 @@ export function PATCH(target: any, propertyKey: string,
processHttpVerb(target, propertyKey, HttpMethod.PATCH);
}

/**
* A decorator to tell the [[Server]] that a method
* should be called to process HTTP PATCH requests for specific path.
*
* For example:
*
* ```
* @ Path('people')
* class PeopleService {
* @ PATCHMapping(':id')
* savePerson(person: Person) {
* // ...
* }
* }
* ```
*
* Will create a service that listen for requests like:
*
* ```
* PATCH http://mydomain/people/123
* ```
*/
export function PATCHMapping(path: string = '/') {
return function (...args: any[]) {
args = _.without(args, undefined);
if (args.length === 3 && typeof args[2] === 'object') {
PATCH(args[0], args[1], args[2]);
return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
}

throw new Error('Invalid @PATCHMapping Decorator declaration.');
};
}

/**
* A decorator to inform options to pe passed to bodyParser.
* You can inform any property accepted by
Expand Down
Loading