Skip to content

Commit

Permalink
Check route callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Starotitorov committed Nov 27, 2018
1 parent 38df7a5 commit 0bd9424
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
6 changes: 3 additions & 3 deletions NavigationServiceExample/package-lock.json

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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ the next props can be passed to this component:

Property | Type | Optional | Default | Description
--------------------- | -------- | -------- | ----------- | -----------
onGetInitialUrlError | function | true | () => {} | a callback, which will be called when [Linking.getInitialUrl](https://facebook.github.io/react-native/docs/linking#getinitialurl) throws an error. The function receives the error.
onCanOpenUrlError | function | true | () => {} | a callback, which will be called when [Linking.canOpenUrl](https://facebook.github.io/react-native/docs/linking#canopenurl) throws an error. The function receives the error.
onGetInitialUrlError | function | true | () => {} | a callback, which will be called in case the application throws an error trying to get initial url. The function receives the error.
onCanOpenUrlError | function | true | () => {} | a callback, which will be called in case the application throws an error trying to open url. The function receives the error.
onUrlIsNotSupported | function | true | () => {} | a callback, which will be called in case the app does not support received url. The function receives the url.
onCannotHandleUrl | function | true | () => {} | a callback, which will be called in case a handler for the given url was not specified. The function receives the url.

Expand Down
35 changes: 24 additions & 11 deletions lib/DeepLinkingHandler.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import Url from './Url';
import Scheme from './Scheme';
import { isString, isArray } from './utils';
import { isString, isArray, isFunction } from './utils';

export default class DeepLinkingHandler {
static create(schemes = []) {
return new DeepLinkingHandler(schemes);
}

constructor(schemes = []) {
this.schemes = {};

this.registerSchemes(schemes);
this._registerSchemes(schemes);
}

findScheme(schemeName) {
return this.schemes[schemeName];
}

registerSchemes(schemes) {
if (!isArray(schemes)) {
throw new Error('The method accepts an array of schemes.');
}

schemes.forEach(scheme => this._registerScheme(scheme));
}

getUrlCallback(urlString) {
if (!urlString) {
return null;
Expand All @@ -37,14 +33,31 @@ export default class DeepLinkingHandler {
const urlCallbacks = scheme.routes.reduce((acc, route) => {
const result = urlObj.match(route);

return result ? [...acc, route.callback(result)] : acc;
if (!result) {
return acc;
}

const urlCallback = route.callback(result);
if (!isFunction(urlCallback)) {
throw new Error(`The route ${route.expression} callback is not a higher order function.`);
}

return [...acc, urlCallback];
}, []);

return urlCallbacks.length > 0
? (...args) => urlCallbacks.forEach(callback => callback(...args))
: null;
}

_registerSchemes(schemes) {
if (!isArray(schemes)) {
throw new Error('The method accepts an array of schemes.');
}

schemes.forEach(scheme => this._registerScheme(scheme));
}

_registerScheme({ name: schemeName, routes }) {
if (!isString(schemeName) || !isArray(routes)) {
throw new Error('The argument is not a valid scheme.');
Expand Down
23 changes: 12 additions & 11 deletions lib/DeepLinkingHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ describe('DeepLinkingHandler', () => {
name: schemeName,
routes
}];
const deepLinkingHandler = new DeepLinkingHandler();
deepLinkingHandler.registerSchemes(schemes);
const deepLinkingHandler = new DeepLinkingHandler(schemes);

expect(deepLinkingHandler.schemes).toEqual({
[schemeName]: expect.any(Scheme)
});
});

it('should throw an error when the scheme has already been registered', () => {
const schemes = [{
name: schemeName,
routes
}];
const deepLinkingHandler = new DeepLinkingHandler();
deepLinkingHandler.registerSchemes(schemes);
const schemes = [
{
name: schemeName,
routes
},
{
name: schemeName,
routes: []
}
];

expect(() => {
deepLinkingHandler.registerSchemes([{ name: schemeName, routes: [] }]);
}).toThrow();
expect(() => new DeepLinkingHandler(schemes)).toThrow();
})

});
Expand Down
4 changes: 2 additions & 2 deletions lib/Scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export default class Scheme {
this.name = name;
this.routes = [];

routes.forEach(route => this.registerRoute(route));
routes.forEach(route => this._registerRoute(route));
}

registerRoute({ expression, callback }) {
_registerRoute({ expression, callback }) {
if (!isString(expression) || !isFunction(callback)) {
throw new Error('The argument is not a valid route.');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/createDeepLinkingHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DeepLinkingHandler from './DeepLinkingHandler';
import withDeepLinking from './withDeepLinking';

export default function createDeepLinkingHandler(schemes) {
const deepLinkingHandler = new DeepLinkingHandler(schemes);
const deepLinkingHandler = DeepLinkingHandler.create(schemes);

return withDeepLinking(deepLinkingHandler);
}

0 comments on commit 0bd9424

Please sign in to comment.