universal-app-opener 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -6
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +47 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +47 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -6
- package/LICENSE +0 -22
package/README.md
CHANGED
|
@@ -34,9 +34,9 @@ openLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
|
34
34
|
import { openLink } from 'universal-app-opener';
|
|
35
35
|
|
|
36
36
|
openLink('https://www.linkedin.com/in/iamsaban/', {
|
|
37
|
-
fallbackToWeb: true,
|
|
38
|
-
fallbackDelay: 2500,
|
|
39
|
-
openInNewTab: false
|
|
37
|
+
fallbackToWeb: true, // Fallback to web if app not installed (default: true)
|
|
38
|
+
fallbackDelay: 2500, // Delay before fallback in ms (default: 2500)
|
|
39
|
+
openInNewTab: false, // Open web URL in new tab (default: false)
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
|
@@ -74,6 +74,7 @@ openLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
|
74
74
|
Opens a URL in the appropriate app or browser. Automatically detects platform and handles deep linking.
|
|
75
75
|
|
|
76
76
|
**Parameters:**
|
|
77
|
+
|
|
77
78
|
- `url` (string): The web URL to open (YouTube or LinkedIn)
|
|
78
79
|
- `options` (optional): Configuration object
|
|
79
80
|
- `fallbackToWeb` (boolean): Fallback to web URL if app not installed (default: `true`)
|
|
@@ -81,6 +82,7 @@ Opens a URL in the appropriate app or browser. Automatically detects platform an
|
|
|
81
82
|
- `openInNewTab` (boolean): Open web URL in new tab (default: `false`)
|
|
82
83
|
|
|
83
84
|
**Example:**
|
|
85
|
+
|
|
84
86
|
```typescript
|
|
85
87
|
openLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
86
88
|
```
|
|
@@ -90,19 +92,22 @@ openLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
|
90
92
|
Converts a web URL into platform-specific deep links. Returns the deep link data without opening it.
|
|
91
93
|
|
|
92
94
|
**Parameters:**
|
|
95
|
+
|
|
93
96
|
- `url` (string): The web URL to convert (YouTube or LinkedIn)
|
|
94
97
|
|
|
95
98
|
**Returns:**
|
|
99
|
+
|
|
96
100
|
```typescript
|
|
97
101
|
interface DeepLinkResult {
|
|
98
|
-
webUrl: string;
|
|
99
|
-
ios: string | null;
|
|
102
|
+
webUrl: string; // Original web URL
|
|
103
|
+
ios: string | null; // iOS deep link (custom scheme)
|
|
100
104
|
android: string | null; // Android deep link (intent URL)
|
|
101
105
|
platform: 'youtube' | 'linkedin' | 'unknown';
|
|
102
106
|
}
|
|
103
107
|
```
|
|
104
108
|
|
|
105
109
|
**Supported Platforms:**
|
|
110
|
+
|
|
106
111
|
- YouTube: `youtube.com/watch?v=*` and `youtu.be/*`
|
|
107
112
|
- LinkedIn: `linkedin.com/in/*`
|
|
108
113
|
|
|
@@ -111,6 +116,7 @@ interface DeepLinkResult {
|
|
|
111
116
|
Detects the current operating system based on user agent.
|
|
112
117
|
|
|
113
118
|
**Returns:**
|
|
119
|
+
|
|
114
120
|
- `'ios'` - iPhone, iPad, or iPod
|
|
115
121
|
- `'android'` - Android devices
|
|
116
122
|
- `'desktop'` - Desktop browsers or unknown
|
|
@@ -149,4 +155,3 @@ Try it out: [Live Demo](https://mdsaban.github.io/universal-app-opener/)
|
|
|
149
155
|
## License
|
|
150
156
|
|
|
151
157
|
MIT
|
|
152
|
-
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -31,10 +31,11 @@ var linkedinHandler = {
|
|
|
31
31
|
match: (url) => url.match(/linkedin\.com\/in\/([^/?]+)/),
|
|
32
32
|
build: (webUrl, match) => {
|
|
33
33
|
const profileId = match[1];
|
|
34
|
+
const urlWithoutProtocol = webUrl.replace(/^https?:\/\//, "");
|
|
34
35
|
return {
|
|
35
36
|
webUrl,
|
|
36
|
-
ios: `linkedin://
|
|
37
|
-
android: `intent
|
|
37
|
+
ios: `linkedin://profile/${profileId}`,
|
|
38
|
+
android: `intent://${urlWithoutProtocol}#Intent;scheme=https;package=com.linkedin.android;S.browser_fallback_url=${webUrl};end`,
|
|
38
39
|
platform: "linkedin"
|
|
39
40
|
};
|
|
40
41
|
}
|
|
@@ -66,24 +67,58 @@ var youtubeHandler = {
|
|
|
66
67
|
|
|
67
68
|
// src/platforms/instagram.ts
|
|
68
69
|
var instagramHandler = {
|
|
69
|
-
match: (url) => url.match(/instagram\.com\/([^/?]+)/),
|
|
70
|
+
match: (url) => url.match(/instagram\.com\/(?:(p|reel|tv)\/)?([^/?]+)/),
|
|
70
71
|
build: (webUrl, match) => {
|
|
71
|
-
const
|
|
72
|
+
const typeOfUrl = match[1];
|
|
73
|
+
const id = match[2];
|
|
74
|
+
const deepLinks = {
|
|
75
|
+
// ios to null? : to trigger Universal Link fallback, as custom scheme requires a numeric ID.
|
|
76
|
+
p: {
|
|
77
|
+
ios: null,
|
|
78
|
+
android: `intent://instagram.com/p/${id}#Intent;package=com.instagram.android;scheme=https;end`
|
|
79
|
+
},
|
|
80
|
+
reel: {
|
|
81
|
+
ios: null,
|
|
82
|
+
android: `intent://instagram.com/reel/${id}#Intent;package=com.instagram.android;scheme=https;end`
|
|
83
|
+
},
|
|
84
|
+
tv: {
|
|
85
|
+
ios: null,
|
|
86
|
+
android: `intent://instagram.com/tv/${id}#Intent;package=com.instagram.android;scheme=https;end`
|
|
87
|
+
},
|
|
88
|
+
// default represents the user profile
|
|
89
|
+
default: {
|
|
90
|
+
ios: `instagram://user?username=${id}`,
|
|
91
|
+
android: `intent://user?username=${id}#Intent;scheme=instagram;package=com.instagram.android;end`
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const config = deepLinks[typeOfUrl] || deepLinks.default;
|
|
72
95
|
return {
|
|
73
96
|
webUrl,
|
|
74
|
-
|
|
75
|
-
android: `intent://user?username=${username}#Intent;scheme=instagram;package=com.instagram.android;end`,
|
|
97
|
+
...config,
|
|
76
98
|
platform: "instagram"
|
|
77
99
|
};
|
|
78
100
|
}
|
|
79
101
|
};
|
|
80
102
|
|
|
103
|
+
// src/platforms/spotify.ts
|
|
104
|
+
var spotifyHandler = {
|
|
105
|
+
match: (url) => url.match(
|
|
106
|
+
/^https?:\/\/open\.spotify\.com\/(track|artist|album|playlist|show|episode|audiobook)\/([^/?#]+)/
|
|
107
|
+
),
|
|
108
|
+
build: (webUrl, match) => {
|
|
109
|
+
const type = match[1];
|
|
110
|
+
const id = match[2];
|
|
111
|
+
return {
|
|
112
|
+
webUrl,
|
|
113
|
+
ios: `spotify:${type}:${id}`,
|
|
114
|
+
android: `intent://${type}/${id}#Intent;scheme=spotify;package=com.spotify.music;end`,
|
|
115
|
+
platform: "spotify"
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
81
120
|
// src/index.ts
|
|
82
|
-
var handlers = [
|
|
83
|
-
youtubeHandler,
|
|
84
|
-
linkedinHandler,
|
|
85
|
-
instagramHandler
|
|
86
|
-
];
|
|
121
|
+
var handlers = [youtubeHandler, linkedinHandler, instagramHandler, spotifyHandler];
|
|
87
122
|
function generateDeepLink(url) {
|
|
88
123
|
const webUrl = url.trim();
|
|
89
124
|
for (const handler of handlers) {
|
|
@@ -108,11 +143,7 @@ function detectOS() {
|
|
|
108
143
|
return "desktop";
|
|
109
144
|
}
|
|
110
145
|
function openLink(url, options = {}) {
|
|
111
|
-
const {
|
|
112
|
-
fallbackToWeb = true,
|
|
113
|
-
fallbackDelay = 2500,
|
|
114
|
-
openInNewTab = false
|
|
115
|
-
} = options;
|
|
146
|
+
const { fallbackToWeb = true, fallbackDelay = 2500, openInNewTab = false } = options;
|
|
116
147
|
const os = detectOS();
|
|
117
148
|
const result = generateDeepLink(url);
|
|
118
149
|
let deepLink = null;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/platforms/linkedin.ts","../src/platforms/unknown.ts","../src/platforms/youtube.ts","../src/platforms/instagram.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/platforms/linkedin.ts","../src/platforms/unknown.ts","../src/platforms/youtube.ts","../src/platforms/instagram.ts","../src/platforms/spotify.ts"],"sourcesContent":["import {\n instagramHandler,\n linkedinHandler,\n unknownHandler,\n youtubeHandler,\n spotifyHandler,\n} from './platforms';\nimport { DeepLinkResult } from './types';\n\nexport * from './types';\n\nconst handlers = [youtubeHandler, linkedinHandler, instagramHandler, spotifyHandler];\nexport function generateDeepLink(url: string): DeepLinkResult {\n const webUrl = url.trim();\n\n for (const handler of handlers) {\n const match = handler.match(webUrl);\n if (match) {\n return handler.build(webUrl, match);\n }\n }\n\n return unknownHandler(webUrl);\n}\n\nexport function detectOS(): 'ios' | 'android' | 'desktop' {\n if (typeof window === 'undefined') {\n return 'desktop';\n }\n\n const userAgent = window.navigator.userAgent.toLowerCase();\n\n if (/iphone|ipad|ipod/.test(userAgent)) {\n return 'ios';\n }\n\n if (/android/.test(userAgent)) {\n return 'android';\n }\n\n return 'desktop';\n}\n\nexport interface OpenLinkOptions {\n fallbackToWeb?: boolean;\n fallbackDelay?: number;\n openInNewTab?: boolean;\n}\n\nexport function openLink(url: string, options: OpenLinkOptions = {}): void {\n const { fallbackToWeb = true, fallbackDelay = 2500, openInNewTab = false } = options;\n\n const os = detectOS();\n const result = generateDeepLink(url);\n\n let deepLink: string | null = null;\n\n if (os === 'ios' && result.ios) {\n deepLink = result.ios;\n } else if (os === 'android' && result.android) {\n deepLink = result.android;\n }\n\n if (deepLink && (os === 'ios' || os === 'android')) {\n window.location.href = deepLink;\n\n if (fallbackToWeb) {\n setTimeout(() => {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }, fallbackDelay);\n }\n } else {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }\n}\n","import { DeepLinkHandler } from '../types';\n\nexport const linkedinHandler: DeepLinkHandler = {\n match: (url) => url.match(/linkedin\\.com\\/in\\/([^/?]+)/),\n\n build: (webUrl, match) => {\n const profileId = match[1];\n const urlWithoutProtocol = webUrl.replace(/^https?:\\/\\//, '');\n\n return {\n webUrl,\n ios: `linkedin://profile/${profileId}`,\n android: `intent://${urlWithoutProtocol}#Intent;scheme=https;package=com.linkedin.android;S.browser_fallback_url=${webUrl};end`,\n platform: 'linkedin',\n };\n },\n};\n","import { DeepLinkResult } from '../types';\n\nexport function unknownHandler(webUrl: string): DeepLinkResult {\n return {\n webUrl,\n ios: null,\n android: null,\n platform: 'unknown',\n };\n}\n","import { DeepLinkHandler } from '../types';\n\nexport const youtubeHandler: DeepLinkHandler = {\n match: (url) => url.match(/youtube\\.com\\/watch\\?v=([^&]+)/) ?? url.match(/youtu\\.be\\/([^?]+)/),\n\n build: (webUrl, match) => {\n const videoId = match[1];\n\n return {\n webUrl,\n ios: `vnd.youtube://watch?v=${videoId}`,\n android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,\n platform: 'youtube',\n };\n },\n};\n","import { DeepLinkHandler } from '../types';\n\nexport const instagramHandler: DeepLinkHandler = {\n match: (url) => url.match(/instagram\\.com\\/(?:(p|reel|tv)\\/)?([^/?]+)/),\n\n build: (webUrl, match) => {\n const typeOfUrl = match[1];\n const id = match[2];\n\n const deepLinks: Record<string, { ios: string | null; android: string }> = {\n // ios to null? : to trigger Universal Link fallback, as custom scheme requires a numeric ID.\n p: {\n ios: null,\n android: `intent://instagram.com/p/${id}#Intent;package=com.instagram.android;scheme=https;end`,\n },\n reel: {\n ios: null,\n android: `intent://instagram.com/reel/${id}#Intent;package=com.instagram.android;scheme=https;end`,\n },\n tv: {\n ios: null,\n android: `intent://instagram.com/tv/${id}#Intent;package=com.instagram.android;scheme=https;end`,\n },\n // default represents the user profile\n default: {\n ios: `instagram://user?username=${id}`,\n android: `intent://user?username=${id}#Intent;scheme=instagram;package=com.instagram.android;end`,\n },\n };\n\n const config = deepLinks[typeOfUrl] || deepLinks.default;\n\n return {\n webUrl,\n ...config,\n platform: 'instagram',\n };\n },\n};","import { DeepLinkHandler } from '../types';\n\nexport const spotifyHandler: DeepLinkHandler = {\n match: (url) =>\n url.match(\n /^https?:\\/\\/open\\.spotify\\.com\\/(track|artist|album|playlist|show|episode|audiobook)\\/([^/?#]+)/,\n ),\n\n build: (webUrl, match) => {\n const type = match[1];\n const id = match[2];\n\n return {\n webUrl,\n ios: `spotify:${type}:${id}`,\n android: `intent://${type}/${id}#Intent;scheme=spotify;package=com.spotify.music;end`,\n platform: 'spotify',\n };\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,kBAAmC;AAAA,EAC9C,OAAO,CAAC,QAAQ,IAAI,MAAM,6BAA6B;AAAA,EAEvD,OAAO,CAAC,QAAQ,UAAU;AACxB,UAAM,YAAY,MAAM,CAAC;AACzB,UAAM,qBAAqB,OAAO,QAAQ,gBAAgB,EAAE;AAE5D,WAAO;AAAA,MACL;AAAA,MACA,KAAK,sBAAsB,SAAS;AAAA,MACpC,SAAS,YAAY,kBAAkB,4EAA4E,MAAM;AAAA,MACzH,UAAU;AAAA,IACZ;AAAA,EACF;AACF;;;ACdO,SAAS,eAAe,QAAgC;AAC7D,SAAO;AAAA,IACL;AAAA,IACA,KAAK;AAAA,IACL,SAAS;AAAA,IACT,UAAU;AAAA,EACZ;AACF;;;ACPO,IAAM,iBAAkC;AAAA,EAC7C,OAAO,CAAC,QAAQ,IAAI,MAAM,gCAAgC,KAAK,IAAI,MAAM,oBAAoB;AAAA,EAE7F,OAAO,CAAC,QAAQ,UAAU;AACxB,UAAM,UAAU,MAAM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@ var linkedinHandler = {
|
|
|
3
3
|
match: (url) => url.match(/linkedin\.com\/in\/([^/?]+)/),
|
|
4
4
|
build: (webUrl, match) => {
|
|
5
5
|
const profileId = match[1];
|
|
6
|
+
const urlWithoutProtocol = webUrl.replace(/^https?:\/\//, "");
|
|
6
7
|
return {
|
|
7
8
|
webUrl,
|
|
8
|
-
ios: `linkedin://
|
|
9
|
-
android: `intent
|
|
9
|
+
ios: `linkedin://profile/${profileId}`,
|
|
10
|
+
android: `intent://${urlWithoutProtocol}#Intent;scheme=https;package=com.linkedin.android;S.browser_fallback_url=${webUrl};end`,
|
|
10
11
|
platform: "linkedin"
|
|
11
12
|
};
|
|
12
13
|
}
|
|
@@ -38,24 +39,58 @@ var youtubeHandler = {
|
|
|
38
39
|
|
|
39
40
|
// src/platforms/instagram.ts
|
|
40
41
|
var instagramHandler = {
|
|
41
|
-
match: (url) => url.match(/instagram\.com\/([^/?]+)/),
|
|
42
|
+
match: (url) => url.match(/instagram\.com\/(?:(p|reel|tv)\/)?([^/?]+)/),
|
|
42
43
|
build: (webUrl, match) => {
|
|
43
|
-
const
|
|
44
|
+
const typeOfUrl = match[1];
|
|
45
|
+
const id = match[2];
|
|
46
|
+
const deepLinks = {
|
|
47
|
+
// ios to null? : to trigger Universal Link fallback, as custom scheme requires a numeric ID.
|
|
48
|
+
p: {
|
|
49
|
+
ios: null,
|
|
50
|
+
android: `intent://instagram.com/p/${id}#Intent;package=com.instagram.android;scheme=https;end`
|
|
51
|
+
},
|
|
52
|
+
reel: {
|
|
53
|
+
ios: null,
|
|
54
|
+
android: `intent://instagram.com/reel/${id}#Intent;package=com.instagram.android;scheme=https;end`
|
|
55
|
+
},
|
|
56
|
+
tv: {
|
|
57
|
+
ios: null,
|
|
58
|
+
android: `intent://instagram.com/tv/${id}#Intent;package=com.instagram.android;scheme=https;end`
|
|
59
|
+
},
|
|
60
|
+
// default represents the user profile
|
|
61
|
+
default: {
|
|
62
|
+
ios: `instagram://user?username=${id}`,
|
|
63
|
+
android: `intent://user?username=${id}#Intent;scheme=instagram;package=com.instagram.android;end`
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const config = deepLinks[typeOfUrl] || deepLinks.default;
|
|
44
67
|
return {
|
|
45
68
|
webUrl,
|
|
46
|
-
|
|
47
|
-
android: `intent://user?username=${username}#Intent;scheme=instagram;package=com.instagram.android;end`,
|
|
69
|
+
...config,
|
|
48
70
|
platform: "instagram"
|
|
49
71
|
};
|
|
50
72
|
}
|
|
51
73
|
};
|
|
52
74
|
|
|
75
|
+
// src/platforms/spotify.ts
|
|
76
|
+
var spotifyHandler = {
|
|
77
|
+
match: (url) => url.match(
|
|
78
|
+
/^https?:\/\/open\.spotify\.com\/(track|artist|album|playlist|show|episode|audiobook)\/([^/?#]+)/
|
|
79
|
+
),
|
|
80
|
+
build: (webUrl, match) => {
|
|
81
|
+
const type = match[1];
|
|
82
|
+
const id = match[2];
|
|
83
|
+
return {
|
|
84
|
+
webUrl,
|
|
85
|
+
ios: `spotify:${type}:${id}`,
|
|
86
|
+
android: `intent://${type}/${id}#Intent;scheme=spotify;package=com.spotify.music;end`,
|
|
87
|
+
platform: "spotify"
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
53
92
|
// src/index.ts
|
|
54
|
-
var handlers = [
|
|
55
|
-
youtubeHandler,
|
|
56
|
-
linkedinHandler,
|
|
57
|
-
instagramHandler
|
|
58
|
-
];
|
|
93
|
+
var handlers = [youtubeHandler, linkedinHandler, instagramHandler, spotifyHandler];
|
|
59
94
|
function generateDeepLink(url) {
|
|
60
95
|
const webUrl = url.trim();
|
|
61
96
|
for (const handler of handlers) {
|
|
@@ -80,11 +115,7 @@ function detectOS() {
|
|
|
80
115
|
return "desktop";
|
|
81
116
|
}
|
|
82
117
|
function openLink(url, options = {}) {
|
|
83
|
-
const {
|
|
84
|
-
fallbackToWeb = true,
|
|
85
|
-
fallbackDelay = 2500,
|
|
86
|
-
openInNewTab = false
|
|
87
|
-
} = options;
|
|
118
|
+
const { fallbackToWeb = true, fallbackDelay = 2500, openInNewTab = false } = options;
|
|
88
119
|
const os = detectOS();
|
|
89
120
|
const result = generateDeepLink(url);
|
|
90
121
|
let deepLink = null;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/platforms/linkedin.ts","../src/platforms/unknown.ts","../src/platforms/youtube.ts","../src/platforms/instagram.ts","../src/index.ts"],"sourcesContent":["import { DeepLinkHandler } from '../types';\n\nexport const linkedinHandler: DeepLinkHandler = {\n match: (url) => url.match(/linkedin\\.com\\/in\\/([^/?]+)/),\n\n build: (webUrl, match) => {\n const profileId = match[1];\n\n return {\n webUrl,\n ios: `linkedin://
|
|
1
|
+
{"version":3,"sources":["../src/platforms/linkedin.ts","../src/platforms/unknown.ts","../src/platforms/youtube.ts","../src/platforms/instagram.ts","../src/platforms/spotify.ts","../src/index.ts"],"sourcesContent":["import { DeepLinkHandler } from '../types';\n\nexport const linkedinHandler: DeepLinkHandler = {\n match: (url) => url.match(/linkedin\\.com\\/in\\/([^/?]+)/),\n\n build: (webUrl, match) => {\n const profileId = match[1];\n const urlWithoutProtocol = webUrl.replace(/^https?:\\/\\//, '');\n\n return {\n webUrl,\n ios: `linkedin://profile/${profileId}`,\n android: `intent://${urlWithoutProtocol}#Intent;scheme=https;package=com.linkedin.android;S.browser_fallback_url=${webUrl};end`,\n platform: 'linkedin',\n };\n },\n};\n","import { DeepLinkResult } from '../types';\n\nexport function unknownHandler(webUrl: string): DeepLinkResult {\n return {\n webUrl,\n ios: null,\n android: null,\n platform: 'unknown',\n };\n}\n","import { DeepLinkHandler } from '../types';\n\nexport const youtubeHandler: DeepLinkHandler = {\n match: (url) => url.match(/youtube\\.com\\/watch\\?v=([^&]+)/) ?? url.match(/youtu\\.be\\/([^?]+)/),\n\n build: (webUrl, match) => {\n const videoId = match[1];\n\n return {\n webUrl,\n ios: `vnd.youtube://watch?v=${videoId}`,\n android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,\n platform: 'youtube',\n };\n },\n};\n","import { DeepLinkHandler } from '../types';\n\nexport const instagramHandler: DeepLinkHandler = {\n match: (url) => url.match(/instagram\\.com\\/(?:(p|reel|tv)\\/)?([^/?]+)/),\n\n build: (webUrl, match) => {\n const typeOfUrl = match[1];\n const id = match[2];\n\n const deepLinks: Record<string, { ios: string | null; android: string }> = {\n // ios to null? : to trigger Universal Link fallback, as custom scheme requires a numeric ID.\n p: {\n ios: null,\n android: `intent://instagram.com/p/${id}#Intent;package=com.instagram.android;scheme=https;end`,\n },\n reel: {\n ios: null,\n android: `intent://instagram.com/reel/${id}#Intent;package=com.instagram.android;scheme=https;end`,\n },\n tv: {\n ios: null,\n android: `intent://instagram.com/tv/${id}#Intent;package=com.instagram.android;scheme=https;end`,\n },\n // default represents the user profile\n default: {\n ios: `instagram://user?username=${id}`,\n android: `intent://user?username=${id}#Intent;scheme=instagram;package=com.instagram.android;end`,\n },\n };\n\n const config = deepLinks[typeOfUrl] || deepLinks.default;\n\n return {\n webUrl,\n ...config,\n platform: 'instagram',\n };\n },\n};","import { DeepLinkHandler } from '../types';\n\nexport const spotifyHandler: DeepLinkHandler = {\n match: (url) =>\n url.match(\n /^https?:\\/\\/open\\.spotify\\.com\\/(track|artist|album|playlist|show|episode|audiobook)\\/([^/?#]+)/,\n ),\n\n build: (webUrl, match) => {\n const type = match[1];\n const id = match[2];\n\n return {\n webUrl,\n ios: `spotify:${type}:${id}`,\n android: `intent://${type}/${id}#Intent;scheme=spotify;package=com.spotify.music;end`,\n platform: 'spotify',\n };\n },\n};\n","import {\n instagramHandler,\n linkedinHandler,\n unknownHandler,\n youtubeHandler,\n spotifyHandler,\n} from './platforms';\nimport { DeepLinkResult } from './types';\n\nexport * from './types';\n\nconst handlers = [youtubeHandler, linkedinHandler, instagramHandler, spotifyHandler];\nexport function generateDeepLink(url: string): DeepLinkResult {\n const webUrl = url.trim();\n\n for (const handler of handlers) {\n const match = handler.match(webUrl);\n if (match) {\n return handler.build(webUrl, match);\n }\n }\n\n return unknownHandler(webUrl);\n}\n\nexport function detectOS(): 'ios' | 'android' | 'desktop' {\n if (typeof window === 'undefined') {\n return 'desktop';\n }\n\n const userAgent = window.navigator.userAgent.toLowerCase();\n\n if (/iphone|ipad|ipod/.test(userAgent)) {\n return 'ios';\n }\n\n if (/android/.test(userAgent)) {\n return 'android';\n }\n\n return 'desktop';\n}\n\nexport interface OpenLinkOptions {\n fallbackToWeb?: boolean;\n fallbackDelay?: number;\n openInNewTab?: boolean;\n}\n\nexport function openLink(url: string, options: OpenLinkOptions = {}): void {\n const { fallbackToWeb = true, fallbackDelay = 2500, openInNewTab = false } = options;\n\n const os = detectOS();\n const result = generateDeepLink(url);\n\n let deepLink: string | null = null;\n\n if (os === 'ios' && result.ios) {\n deepLink = result.ios;\n } else if (os === 'android' && result.android) {\n deepLink = result.android;\n }\n\n if (deepLink && (os === 'ios' || os === 'android')) {\n window.location.href = deepLink;\n\n if (fallbackToWeb) {\n setTimeout(() => {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }, fallbackDelay);\n }\n } else {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }\n}\n"],"mappings":";AAEO,IAAM,kBAAmC;AAAA,EAC9C,OAAO,CAAC,QAAQ,IAAI,MAAM,6BAA6B;AAAA,EAEvD,OAAO,CAAC,QAAQ,UAAU;AACxB,UAAM,YAAY,MAAM,CAAC;AACzB,UAAM,qBAAqB,OAAO,QAAQ,gBAAgB,EAAE;AAE5D,WAAO;AAAA,MACL;AAAA,MACA,KAAK,sBAAsB,SAAS;AAAA,MACpC,SAAS,YAAY,kBAAkB,4EAA4E,MAAM;AAAA,MACzH,UAAU;AAAA,IACZ;AAAA,EACF;AACF;;;ACdO,SAAS,eAAe,QAAgC;AAC7D,SAAO;AAAA,I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C;AAAA,MACF,GAAG,aAAa;AAAA,IAClB;AAAA,EACF,OAAO;AACL,QAAI,cAAc;AAChB,aAAO,KAAK,OAAO,QAAQ,QAAQ;AAAA,IACrC,OAAO;AACL,aAAO,SAAS,OAAO,OAAO;AAAA,IAChC;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "universal-app-opener",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A zero-dependency library to generate deep links from HTTP URLs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,6 +17,12 @@
|
|
|
17
17
|
"dist",
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"dev": "tsup --watch",
|
|
23
|
+
"prepublishOnly": "pnpm build",
|
|
24
|
+
"type-check": "tsc --noEmit"
|
|
25
|
+
},
|
|
20
26
|
"keywords": [
|
|
21
27
|
"deep-link",
|
|
22
28
|
"mobile",
|
|
@@ -38,9 +44,5 @@
|
|
|
38
44
|
"devDependencies": {
|
|
39
45
|
"tsup": "^8.0.0",
|
|
40
46
|
"typescript": "^5.3.0"
|
|
41
|
-
},
|
|
42
|
-
"scripts": {
|
|
43
|
-
"build": "tsup",
|
|
44
|
-
"dev": "tsup --watch"
|
|
45
47
|
}
|
|
46
|
-
}
|
|
48
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 universal-app-opener
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|