Skip to content

Commit

Permalink
Merge pull request #32 from icapps/feature/parse-json-response
Browse files Browse the repository at this point in the history
Feature/parse json response
  • Loading branch information
knor-el-snor committed Jan 14, 2020
2 parents f2903e5 + f452a95 commit b621256
Show file tree
Hide file tree
Showing 6 changed files with 729 additions and 443 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
language: node_js

node_js:
- "6"
- "8"
- "10"
- "lts/*"
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ throw new ApiError(400, errors.BAD_REQUEST);

## Error parsing

### isApiError(object)

Will return boolean indicating whether object has all required properties to be an `ApiError`.

```javascript
// Will return true
isApiError({ status: 200, code: 'MY_CODE', title: 'MY_ERROR', detail: {} })

// Will return false
isApiError({ status: 200, code: 'MY_CODE' })
```

> Will automatically cast to ApiError if succeeds and using Typescript
### parseErrors(error, i18nOptions (optional))

Parse any data into an error object with all properties needed for jsonade parser. Also parses [`express-validation`](https://github.com/andrewkeig/express-validation) and [`celebrate`](https://github.com/arb/celebrate) errors.
Expand Down Expand Up @@ -159,6 +173,22 @@ serializer.serialize([parsedError]);

> The `parseErrors` function will load the i18n configuration once, and reuse the same instance afterwards. It is not possible to overwrite the configuration after the first call. This has to do with performance and caching of translations.
### parseJsonResponse(object)

Parse json object containing errors into javascript `ApiError` instances.

```javascript
try {
await doApiCall(...);
// Returns { errors: [{ status: 400, code: 'BAD_REQUEST', ... }] }
} catch(errorResponse) {
const errors = parseJsonResponse(errorResponse);
// Will return array containing `ApiError` objects
}
```

> Make sure the object contains an `errors` root key: `{ errors: [ ... ] }`
## Tests

- You can run `yarn test` to run all tests
Expand Down
Loading

0 comments on commit b621256

Please sign in to comment.