Skip to content

Commit

Permalink
Added a getStatusCode method. (#28)
Browse files Browse the repository at this point in the history
* Added a getStatusCode method.

* Updated documentation and definition for getStatusCode
  • Loading branch information
o0Ignition0o authored and prettymuchbryce committed Oct 27, 2019
1 parent edd12d3 commit 6677afd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ response
.send({
error: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR)
});

response
.status(HttpStatus.getStatusCode('Server Error'))
.send({
error: 'Server Error'
});
```

## Codes
Expand Down Expand Up @@ -107,14 +113,24 @@ response
.send({
error: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR)
})

response
.status(HttpStatus.getStatusCode('Server Error'))
.send({
error: 'Server Error'
})
```

Option 2: Selective import

```typescript
import { OK, getStatusText } from 'http-status-codes'
import { OK, getStatusText, getStatusCode } from 'http-status-codes'

response
.status(OK)
.send(getStatusText(OK))

response
.status(getStatusCode('Server Error')
.send('Server Error')
```
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,14 @@ export declare const USE_PROXY = 305;
* Convert the numeric status code to its appropriate title.
* @param statusCode One of the available status codes in this package
* @returns {String} The associated title of the passed status code
* @throws {Error} The status code does not exist
*/
export declare function getStatusText(statusCode: number): string;

/**
* Convert the status reason phrase to its appropriate numeric value
* @param reasonPhrase One of the available reason phrases in this package
* @returns {Number} The associated status code of the passed reason phrase
* @throws {Error} The reason phrase does not exist
*/
export declare function getStatusCode(reasonPhrase: string): number;
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ exports.getStatusText = function(statusCode) {
throw new Error("Status code does not exist: " + statusCode);
}
};

exports.getStatusCode = function(reasonPhrase) {
for (key in statusCodes) {
if (statusCodes[key] === reasonPhrase) {
return parseInt(key, 10);
}
}
throw new Error("Reason phrase does not exist: " + reasonPhrase);
};

0 comments on commit 6677afd

Please sign in to comment.