Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
hiukky committed Mar 8, 2020
2 parents fee6829 + adc5140 commit 9fe53ed
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 72 deletions.
80 changes: 63 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

## Using

The http-handler-response provides two main functions. `makeErr` and `makeRes`.
The http-handler-response provides two main functions. `createError` and `createResponse`.

#### makeErr
### createError

The `makeErr` function is the function responsible for formulating your return messages in unsuccessful requests. It follows the [RFC-7807](https://tools.ietf.org/html/rfc7807) standard.
The `createError` function is the function responsible for formulating your return messages in unsuccessful requests. It follows the [RFC-7807](https://tools.ietf.org/html/rfc7807) standard.

##### Parameters
#### Parameters

```js
code: number
Expand All @@ -35,10 +35,10 @@ The `makeErr` function is the function responsible for formulating your return m
instance: string,
```

##### Example
#### Example

```js
import { makeErr } from 'http-handler-response'
import { createError, handlerErrorAdonis } from 'http-handler-response'
import User from 'models/User'

class UserController {
Expand All @@ -47,7 +47,7 @@ class UserController {
const user = await User.find(1)

if (!user)
makeErr({
createError({
code: 404,
detail: 'The user informed is not registered.',
instance: '/user/1',
Expand All @@ -56,13 +56,13 @@ class UserController {

return user
} catch (error) {
response.status(error.status).send(error)
handlerErrorAdonis({ response, error })
}
}
}
```

##### Response
#### Response

```js
{
Expand All @@ -75,11 +75,11 @@ class UserController {

```

#### makeRes
### createResponse

The `makeRes` function is the function responsible for formulating your return messages in successful requisitions.
The `createResponse` function is the function responsible for formulating your return messages in successful requisitions.

##### Parameters
#### Parameters

```js
code: number,
Expand All @@ -88,10 +88,10 @@ The `makeRes` function is the function responsible for formulating your return m
data: array | object
```

##### Example
#### Example

```js
import { makeRes } from 'http-handler-response'
import { createResponse, handlerErrorAdonis } from 'http-handler-response'
import User from 'models/User'

class UserController {
Expand All @@ -105,20 +105,20 @@ class UserController {
await user.save()

return response.status(201).send(
makeRes({
createResponse({
code: 201,
message: 'Successful registered user.',
data: user,
}),
)
} catch (error) {
response.status(error.status).send(error)
handlerErrorAdonis({ response, error })
}
}
}
```

##### Response
#### Response

```js
{
Expand All @@ -133,3 +133,49 @@ class UserController {
}

```

### handlerError

The http-handler-response has custom handlers for handling errors for various web frameworks such as `AdonisJs`, `Express` and `KoaJs`. The functions have a default prefix `handlerError` followed by the name of the framework . Ex: `handlerErrorExpress`.

#### Parameters

Some parameters vary in nomenclature, depending on the framework. The idea is to avoid desessentiary statements like `{ response: res }` to simply `{ res }`.

##### AdonisJs

```js
response: Response,
error: Error,
```

##### Express

```js
res: Response,
error: Error,
```

##### KoaJs

```js
ctx: Context,
error: Error,
```

#### Example

```js
import { handlerErrorAdonis } from 'http-handler-response'
import User from 'models/User'

class UserController {
async store(request, response) {
try {
// Your code..
} catch (error) {
handlerErrorAdonis({ response, error })
}
}
}
```
8 changes: 4 additions & 4 deletions __tests__/makeErr.spec.ts → __tests__/createError.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import makeErr from '../lib/makeErr'
import createError from '../lib/builders/createError'

describe('Make error', () => {
describe('Create error', () => {
it('Must return a 401 error for a failed login attempt.', () => {
const error = makeErr({
const error = createError({
code: '401',
detail: 'Informed credentials are invalidated.',
instance: '/auth/user',
Expand All @@ -19,7 +19,7 @@ describe('Make error', () => {
})

it('You must mount an error message with a custom title.', () => {
const error = makeErr({
const error = createError({
code: '401',
title: 'Credentials invalid',
detail: 'Informed credentials are invalidated.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import makeModelMessage from '../lib/makeModelMessage'
import createModelMessage from '../lib/builders/createModelMessage'

describe('Make message model', () => {
describe('Create message model', () => {
it('Must mount a base header with status 500 and title Internal Server Error', () => {
const model = makeModelMessage({ code: '500' })
const model = createModelMessage({ code: '500' })

expect(model).toEqual({ status: 500, title: 'Internal Server Error' })
})

it('Must mount a custom headline for a 500 error.', () => {
const model = makeModelMessage({
const model = createModelMessage({
code: '500',
title: 'Server found a problem',
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import makeRes from '../lib/makeRes'
import createResponse from '../lib/builders/createResponse'

describe('Make response', () => {
describe('Create response', () => {
it('Must mount a successful response with status code 201', () => {
const response = makeRes({
const response = createResponse({
code: '201',
message: 'Successfully registered',
data: {
Expand All @@ -23,7 +23,7 @@ describe('Make response', () => {
})

it('You must assemble a response with a personalized title', () => {
const response = makeRes({
const response = createResponse({
code: '201',
title: 'Success',
message: 'Successfully registered',
Expand Down
12 changes: 6 additions & 6 deletions lib/makeErr.ts → lib/builders/createError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IMakeErr, IError } from './types'
import makeModelMessage from './makeModelMessage'
import { ICreateError, IError } from '../types/builders'
import makeModelMessage from './createModelMessage'

/**
* @function makeErr
* @function createError
*
* Creates an error response following the RFC 7807 pattern.
*
Expand All @@ -12,13 +12,13 @@ import makeModelMessage from './makeModelMessage'
* @param instance - URI exclusive for or specific error
* @param title - Short and descriptive information
*/
const makeErr = ({
const createError = ({
code,
type = 'about:blank',
title,
detail,
instance,
}: IMakeErr): IError =>
}: ICreateError): IError =>
Object.assign(makeModelMessage({ code, title }), { type, detail, instance })

export default makeErr
export default createError
24 changes: 12 additions & 12 deletions lib/makeModelMessage.ts → lib/builders/createModelMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@ import {
REDIRECTION,
CLIENT_ERROR,
SERVER_ERROR,
} from './httpCodes'
} from '../utils/httpCodes'
import {
IMakeMessage,
ICreateModelMessage,
TInformational,
TSuccess,
TRedirection,
TClientError,
TServerError,
} from './types'
} from '../types/builders'

/**
* @function makeModelMessage
* @function createModelMessage
*
* Assemble the default message header.
*
* @param code - HTTP status code 1xx to 5xx
* @param title - Short and descriptive information
*/
const makeModelMessage = ({ code, title }: IMakeMessage) => {
const createModelMessage = ({ code, title }: ICreateModelMessage) => {
const _code = Number(code)
var _title

if (_code >= 100 && _code < 200) {
_title = title ?? INFORMATIONAL[code as TInformational]
_title = INFORMATIONAL[code as TInformational]
} else if (_code >= 200 && _code < 300) {
_title = title ?? SUCCESS[code as TSuccess]
_title = SUCCESS[code as TSuccess]
} else if (_code >= 300 && _code < 400) {
_title = title ?? REDIRECTION[code as TRedirection]
_title = REDIRECTION[code as TRedirection]
} else if (_code >= 400 && _code < 500) {
_title = title ?? CLIENT_ERROR[code as TClientError]
_title = CLIENT_ERROR[code as TClientError]
} else {
_title = title ?? SERVER_ERROR[code as TServerError]
_title = SERVER_ERROR[code as TServerError]
}

return { title: _title, status: _code }
return { title: title ?? _title, status: _code }
}

export default makeModelMessage
export default createModelMessage
22 changes: 22 additions & 0 deletions lib/builders/createResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ICreateResponse, IResponse } from '../types/builders'
import createModelMessage from './createModelMessage'

/**
* @function createResponse
*
* Simple model for successful responses.
*
* @param code - HTTP status code 1xx to 3xx
* @param message - Legible action response
* @param data - Back Data
* @param title - Short and descriptive information
*/
const createResponse = ({
code,
data,
message,
title,
}: ICreateResponse): IResponse =>
Object.assign(createModelMessage({ code, title }), { data, message })

export default createResponse
2 changes: 2 additions & 0 deletions lib/builders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as createResponse } from './createResponse'
export { default as createError } from './createError'
13 changes: 13 additions & 0 deletions lib/handlers/defaultError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import createError from '../builders/createError'
import { IError } from '../types/builders'

/**
* Default Server error
*/

const defaultError: IError = createError({
code: '500',
detail: 'There was an internal server error.',
})

export default defaultError
17 changes: 17 additions & 0 deletions lib/handlers/handlerErrorAdonis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IAdonisErrorHandler } from '../types/handlers'
import defaultError from './defaultError'

/**
* @function handlerErrorAdonis
*
* Adonis Error handler
*
* @param response - Adonis response object instance
* @param error - Error object instance
*/
const handlerErrorAdonis = ({ response, error }: IAdonisErrorHandler) => {
error = error?.status ? error : defaultError
return response.status(error.status).json(error)
}

export default handlerErrorAdonis
17 changes: 17 additions & 0 deletions lib/handlers/handlerErrorExpress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IExpressErrorHandler } from '../types/handlers'
import defaultError from './defaultError'

/**
* @function handlerErrorExpress
*
* Express Error handler
*
* @param res - Express response object instance
* @param error - Error object instance
*/
const handlerErrorExpress = ({ res, error }: IExpressErrorHandler) => {
error = error?.status ? error : defaultError
return res.status(error.status).json(error)
}

export default handlerErrorExpress
21 changes: 21 additions & 0 deletions lib/handlers/handlerErrorKoa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IKoaErrorHandler } from '../types/handlers'
import defaultError from './defaultError'

/**
* @function handlerErrorKoa
*
* Koa Error handler
*
* @param ctx - Koa CTX object instance
* @param error - Error object instance
*/
const handlerErrorKoa = ({ ctx, error }: IKoaErrorHandler) => {
error = error?.status ? error : defaultError

ctx.type = 'json'
ctx.status = error.status
ctx.body = error
return ctx
}

export default handlerErrorKoa
Loading

0 comments on commit 9fe53ed

Please sign in to comment.