diff --git a/onesignal/withOneSignal.ts b/onesignal/withOneSignal.ts index 76d4a75..c0e1a31 100644 --- a/onesignal/withOneSignal.ts +++ b/onesignal/withOneSignal.ts @@ -7,8 +7,18 @@ import { ConfigPlugin } from '@expo/config-plugins'; import { OneSignalPluginProps } from '../types/types'; import { withOneSignalAndroid } from './withOneSignalAndroid'; import { withOneSignalIos } from './withOneSignalIos'; +import { validatePluginProps } from '../support/helpers'; const withOneSignal: ConfigPlugin = (config, props) => { + // if props are undefined, throw error + if (!props) { + throw new Error( + 'You are trying to use the OneSignal plugin without any props. Property "mode" is required. Please see https://github.com/OneSignal/onesignal-expo-plugin for more info.' + ); + } + + validatePluginProps(props); + config = withOneSignalIos(config, props); config = withOneSignalAndroid(config, props); diff --git a/package.json b/package.json index 1e2999b..b63f098 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "onesignal-expo-plugin", - "version": "2.0.0", + "version": "2.0.1", "description": "The OneSignal Expo plugin allows you to use OneSignal without leaving the managed workflow. Developed in collaboration with SweetGreen.", "main": "./app.plugin.js", "scripts": { diff --git a/support/helpers.ts b/support/helpers.ts new file mode 100644 index 0000000..96285b0 --- /dev/null +++ b/support/helpers.ts @@ -0,0 +1,41 @@ +import { ONESIGNAL_PLUGIN_PROPS } from "../types/types"; + +export function validatePluginProps(props: any): void { + // check the type of each property + if (typeof props.mode !== "string") { + throw new Error("OneSignal Expo Plugin: 'mode' must be a string."); + } + + if (props.devTeam && typeof props.devTeam !== "string") { + throw new Error("OneSignal Expo Plugin: 'devTeam' must be a string."); + } + + if (props.iPhoneDeploymentTarget && typeof props.iPhoneDeploymentTarget !== "string") { + throw new Error("OneSignal Expo Plugin: 'iPhoneDeploymentTarget' must be a string."); + } + + if (props.smallIcons && !Array.isArray(props.smallIcons)) { + throw new Error("OneSignal Expo Plugin: 'smallIcons' must be an array."); + } + + if (props.smallIconAccentColor && typeof props.smallIconAccentColor !== "string") { + throw new Error("OneSignal Expo Plugin: 'smallIconAccentColor' must be a string."); + } + + if (props.largeIcons && !Array.isArray(props.largeIcons)) { + throw new Error("OneSignal Expo Plugin: 'largeIcons' must be an array."); + } + + if (props.iosNSEFilePath && typeof props.iosNSEFilePath !== "string") { + throw new Error("OneSignal Expo Plugin: 'iosNSEFilePath' must be a string."); + } + + // check for extra properties + const inputProps = Object.keys(props); + + for (const prop of inputProps) { + if (!ONESIGNAL_PLUGIN_PROPS.includes(prop)) { + throw new Error(`OneSignal Expo Plugin: You have provided an invalid property "${prop}" to the OneSignal plugin.`); + } + } +} diff --git a/types/types.ts b/types/types.ts index ad48ac8..66e091b 100644 --- a/types/types.ts +++ b/types/types.ts @@ -8,15 +8,15 @@ mode: Mode; /** - * (optional) Used to configure Apple Team ID. You can find your Apple Team ID by running expo credentials:manager e.g: "91SW8A37CR" + * (optional) Used to configure Apple Team ID. You can find your Apple Team ID by running expo credentials:manager e.g: "91SW8A37CR" */ - devTeam: string; + devTeam?: string; /** * (optional) Target IPHONEOS_DEPLOYMENT_TARGET value to be used when adding the iOS NSE. A deployment target is nothing more than * the minimum version of the operating system the application can run on. This value should match the value in your Podfile e.g: "12.0". */ - iPhoneDeploymentTarget: string; + iPhoneDeploymentTarget?: string; /** * (optional) The small notification icons for Android. Images will be automatically scaled up/down, the recommended size @@ -42,6 +42,16 @@ iosNSEFilePath?: string; }; +export const ONESIGNAL_PLUGIN_PROPS: string[] = [ + "mode", + "devTeam", + "iPhoneDeploymentTarget", + "smallIcons", + "largeIcons", + "iosNSEFilePath", + "smallIconAccentColor" +]; + export enum Mode { Dev = "development", Prod = "production"