diff --git a/NavigationServiceExample/package-lock.json b/NavigationServiceExample/package-lock.json index a42affc..b85b37b 100644 --- a/NavigationServiceExample/package-lock.json +++ b/NavigationServiceExample/package-lock.json @@ -7376,9 +7376,9 @@ } }, "react-native-deep-link": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/react-native-deep-link/-/react-native-deep-link-0.2.0.tgz", - "integrity": "sha512-fKi6vPiEqopnVLZPPDv29Ath7pOvsDoESxDcANgd4EpoyRuW1/0072ayVIyVI1kNVKH4YHktDGEmSb9aUWse+g==", + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/react-native-deep-link/-/react-native-deep-link-0.2.1.tgz", + "integrity": "sha512-iqlVlhASJtBXDZPdmLILcRbvXyucPKiV7pylldJXYO0vUHzuJjVH4DP3lDsU/SA5PfnKzV2ijbf3PH17Q5eBpw==", "requires": { "prop-types": "15.6.2", "route-parser": "0.0.5", diff --git a/README.md b/README.md index c15b406..0b994c2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/DeepLinkingHandler.js b/lib/DeepLinkingHandler.js index 0dcc7f1..aa08253 100644 --- a/lib/DeepLinkingHandler.js +++ b/lib/DeepLinkingHandler.js @@ -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; @@ -37,7 +33,16 @@ 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 @@ -45,6 +50,14 @@ export default class DeepLinkingHandler { : 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.'); diff --git a/lib/DeepLinkingHandler.spec.js b/lib/DeepLinkingHandler.spec.js index 2138bde..a30774a 100644 --- a/lib/DeepLinkingHandler.spec.js +++ b/lib/DeepLinkingHandler.spec.js @@ -23,8 +23,7 @@ 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) @@ -32,16 +31,18 @@ describe('DeepLinkingHandler', () => { }); 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(); }) }); diff --git a/lib/Scheme.js b/lib/Scheme.js index a872981..901f28d 100644 --- a/lib/Scheme.js +++ b/lib/Scheme.js @@ -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.'); } diff --git a/lib/createDeepLinkingHandler.js b/lib/createDeepLinkingHandler.js index 0369a4f..2548871 100644 --- a/lib/createDeepLinkingHandler.js +++ b/lib/createDeepLinkingHandler.js @@ -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); }