Skip to content

Commit

Permalink
Merge pull request #236 from skedify/develop
Browse files Browse the repository at this point in the history
release/next
  • Loading branch information
BenSwennen authored Apr 12, 2021
2 parents c888866 + e7f8143 commit 5e6cc99
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node CI
name: CI

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node CI
name: Release

on:
push:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
**Release:**
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/skedify/sdk-js/develop/LICENSE)
[![npm](https://img.shields.io/npm/v/skedify-sdk.svg)](https://www.npmjs.com/package/skedify-sdk)
[![Build Status](https://travis-ci.org/skedify/sdk-js.svg?branch=master)](https://travis-ci.org/skedify/sdk-js)
[![Build Status](https://github.com/skedify/sdk-js/actions/workflows/release.yml/badge.svg?branch=master)](https://github.com/skedify/sdk-js/actions/workflows/release.yml)
[![Code Coverage](https://codecov.io/gh/skedify/sdk-js/branch/master/graph/badge.svg)](https://codecov.io/gh/skedify/sdk-js)

**Development:**
[![Build Status](https://travis-ci.org/skedify/sdk-js.svg?branch=develop)](https://travis-ci.org/skedify/sdk-js)
[![Build Status](https://github.com/skedify/sdk-js/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/skedify/sdk-js/actions/workflows/ci.yml)
[![Code Coverage](https://codecov.io/gh/skedify/sdk-js/branch/develop/graph/badge.svg)](https://codecov.io/gh/skedify/sdk-js)
[![GitHub issues](https://img.shields.io/github/issues/skedify/sdk-js.svg)](https://github.com/skedify/sdk-js/issues)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

## Goal
Expand Down Expand Up @@ -97,6 +96,8 @@ The options are:
- `nl-be`: The second part should be uppercase.
- `nl-BE-VWVX`: The last part can only contain 2 or 3 characters.

- **onError**: A function called when there is an API error that can't be recovered automatically by the SDK

## Basic Usage

Let's start with a simple use case: getting a list of subjects
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,9 @@
},
"dependencies": {
"axios": "^0.21.1"
},
"prettier": {
"singleQuote": true,
"semi": false
}
}
41 changes: 41 additions & 0 deletions src/Skedify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ describe('API/Config', () => {
).toThrow()
})

it('should throw when an invalid onError is passed', () => {
expect(
() =>
new API({
auth_provider,
locale: 'nl-BE',
onError: true,
})
).toThrowError('[CONFIG]: onError must be a function.')
})

it('should allow correct forms of locale values', () => {
const valids = [
'nl',
Expand Down Expand Up @@ -637,6 +648,36 @@ describe('API', () => {
).toThrowErrorMatchingSnapshot()
})

it('should call the onError callback of the configuration when there is an API error', async () => {
const mock = jest.fn()

const auth_provider_token = API.createAuthProviderString('token', {
token_type: 'Bearer',
access_token: 'some-access-token-goes-here',
realm: 'https://api.example.com',
})

const SDK2 = new API({
auth_provider: auth_provider_token,
locale: 'nl-BE',
onError: mock,
})

installSkedifySDKMock(SDK2, {
mockAccessTokensCall: false,
})

mockMatchingURLResponse(/identity/, [], [], [], 401)

await SDK2.subjects().catch((err) => {
expect(err).toMatchSnapshot()
})

expect(mock).toHaveBeenCalled()

uninstallSkedifySDKMock(SDK2)
})

it('should convert all the `id` and `XXX_id` keys to strings', async () => {
mockResponse([
{ id: 1, foo: 'foo' },
Expand Down
14 changes: 14 additions & 0 deletions src/__snapshots__/Skedify.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,20 @@ Object {
}
`;

exports[`API should call the onError callback of the configuration when there is an API error 1`] = `
Object {
"data": Array [],
"errors": undefined,
"headers": undefined,
"meta": Array [],
"method": "get",
"query": undefined,
"status": 401,
"url": "https://api.example.com/identity",
"warnings": Array [],
}
`;

exports[`API should expose all available includes on the API instance 1`] = `
Object {
"accepted_possibility": Object {
Expand Down
1 change: 1 addition & 0 deletions src/constants/exported.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const MISCONFIGURED = createToken()
export const MISCONFIGURED_AUTH_PROVIDER = createToken()
export const MISCONFIGURED_LOCALE = createToken()
export const MISCONFIGURED_AUTH_PROVIDER_OPTIONS = createToken()
export const MISCONFIGURED_ON_ERROR_CALLBACK = createToken()

export const MISCONFIGURED_LOGGER = createToken()

Expand Down
10 changes: 10 additions & 0 deletions src/decorators/withConfig/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
MISCONFIGURED,
MISCONFIGURED_AUTH_PROVIDER,
MISCONFIGURED_LOCALE,
MISCONFIGURED_ON_ERROR_CALLBACK,
} from '../../constants'
import isFunction from '../../util/isFunction'

export function validateLocale(input) {
if (!input) {
Expand Down Expand Up @@ -51,6 +53,14 @@ export default function validate(config = {}) {

validateLocale(config.locale)

if (config.onError && !isFunction(config.onError)) {
throw createConfigError(
'onError must be a function.',
MISCONFIGURED,
MISCONFIGURED_ON_ERROR_CALLBACK
)
}

if (
!config.hasOwnProperty('auth_provider') ||
config.auth_provider === undefined
Expand Down
10 changes: 9 additions & 1 deletion src/decorators/withResources/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,15 @@ export default class Resource {
.then(resolve, reject)
})

return get(this).existing_call.then(onFulfilled, onRejected)
return get(this).existing_call.then(onFulfilled, (...args) => {
if (instance.configuration.onError) {
instance.configuration.onError(...args)
}

if (onRejected) {
onRejected(...args)
}
})
}

catch(onRejected) {
Expand Down

0 comments on commit 5e6cc99

Please sign in to comment.