From c1051c3091638d7d20cb12a3e0f3d5ea1002f1ed Mon Sep 17 00:00:00 2001 From: Rodrigo Gomez Palacio Date: Mon, 25 Sep 2023 10:57:41 -0700 Subject: [PATCH 1/4] Check that plugin props are being provided Motivation: the property "mode" is required. Currently, because we don't check for this, nodejs is throwing a runtime error when trying to access smallIcons (optional prop) when the props object is undefined. This is confusing for folks who first set up and try running without setting a props object to the plugin array. Clear error message will be much more helpful. --- onesignal/withOneSignal.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/onesignal/withOneSignal.ts b/onesignal/withOneSignal.ts index 76d4a75..f52217d 100644 --- a/onesignal/withOneSignal.ts +++ b/onesignal/withOneSignal.ts @@ -9,6 +9,13 @@ import { withOneSignalAndroid } from './withOneSignalAndroid'; import { withOneSignalIos } from './withOneSignalIos'; 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.' + ); + } + config = withOneSignalIos(config, props); config = withOneSignalAndroid(config, props); From 1d92b562e69987846ab6a9d5daa3fda70b8e58f9 Mon Sep 17 00:00:00 2001 From: Rodrigo Gomez Palacio Date: Tue, 26 Sep 2023 16:00:18 -0700 Subject: [PATCH 2/4] Plugin props validation Motivation: we can provide better error messages by throwing when the user provides props of the wrong type or not in the model --- onesignal/withOneSignal.ts | 3 +++ support/helpers.ts | 41 ++++++++++++++++++++++++++++++++++++++ types/types.ts | 10 ++++++++++ 3 files changed, 54 insertions(+) create mode 100644 support/helpers.ts diff --git a/onesignal/withOneSignal.ts b/onesignal/withOneSignal.ts index f52217d..c0e1a31 100644 --- a/onesignal/withOneSignal.ts +++ b/onesignal/withOneSignal.ts @@ -7,6 +7,7 @@ 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 @@ -16,6 +17,8 @@ const withOneSignal: ConfigPlugin = (config, props) => { ); } + validatePluginProps(props); + config = withOneSignalIos(config, props); config = withOneSignalAndroid(config, props); 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..b3d5952 100644 --- a/types/types.ts +++ b/types/types.ts @@ -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" From 3e64f7942beef34071b13e49be2695ac0f9a2984 Mon Sep 17 00:00:00 2001 From: Rodrigo Gomez Palacio Date: Tue, 26 Sep 2023 16:00:50 -0700 Subject: [PATCH 3/4] Correct type definition Motivation: devTeam & iPhoneDeploymentTarget are optional. --- types/types.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/types.ts b/types/types.ts index b3d5952..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 From c28407f9311e4b23a1351ce111042c9fa27e080b Mon Sep 17 00:00:00 2001 From: Rodrigo Gomez Palacio Date: Tue, 26 Sep 2023 16:06:27 -0700 Subject: [PATCH 4/4] 2.0.1 Version Release Update version in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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": {