Skip to content

Commit 033eb8d

Browse files
committed
updates
1 parent eb6d87e commit 033eb8d

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/index.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -324,44 +324,46 @@ export type IntercomType = {
324324

325325
const Intercom: IntercomType = {
326326
initialize: (apiKey, appId) => {
327-
if (!apiKey || typeof apiKey !== 'string') {
327+
if (!apiKey || typeof apiKey !== 'string' || apiKey.trim() === '') {
328328
return Promise.reject(
329329
new Error('Intercom: apiKey is required and must be a string')
330330
);
331331
}
332+
if (!appId || typeof appId !== 'string' || appId.trim() === '') {
333+
return Promise.reject(
334+
new Error('Intercom: appId is required and must be a string')
335+
);
336+
}
332337

333-
const isIOS = Platform.OS === 'ios';
334-
const isAndroid = Platform.OS === 'android';
338+
const platform = Platform.OS as 'ios' | 'android';
339+
const platformRules = {
340+
ios: { prefix: 'ios_sdk-', minLength: 48 },
341+
android: { prefix: 'android_sdk-', minLength: 52 },
342+
};
335343

336-
if (isIOS) {
337-
if (!apiKey.startsWith('ios_sdk-')) {
338-
return Promise.reject(
339-
new Error('Intercom: iOS API key must start with "ios_sdk-"')
340-
);
341-
}
342-
if (apiKey.length < 48) {
343-
return Promise.reject(
344-
new Error('Intercom: iOS API key must be at least 48 characters long')
345-
);
346-
}
347-
} else if (isAndroid) {
348-
if (!apiKey.startsWith('android_sdk-')) {
349-
return Promise.reject(
350-
new Error('Intercom: Android API key must start with "android_sdk-"')
351-
);
352-
}
353-
if (apiKey.length < 52) {
354-
return Promise.reject(
355-
new Error(
356-
'Intercom: Android API key must be at least 52 characters long'
357-
)
358-
);
359-
}
344+
const rules = platformRules[platform];
345+
346+
if (!rules) {
347+
return Promise.reject(
348+
new Error(
349+
`Intercom: Platform "${platform}" is not supported. Only iOS and Android are supported.`
350+
)
351+
);
360352
}
361353

362-
if (!appId || typeof appId !== 'string' || appId.trim() === '') {
354+
if (!apiKey.startsWith(rules.prefix)) {
355+
return Promise.reject(
356+
new Error(
357+
`Intercom: ${platform} API key must start with "${rules.prefix}"`
358+
)
359+
);
360+
}
361+
362+
if (apiKey.length < rules.minLength) {
363363
return Promise.reject(
364-
new Error('Intercom: appId is required and must be a non-empty string')
364+
new Error(
365+
`Intercom: ${platform} API key must be at least ${rules.minLength} characters long`
366+
)
365367
);
366368
}
367369

0 commit comments

Comments
 (0)