Skip to content

Commit

Permalink
Merge pull request #201 from OneSignal/check-plugin-props
Browse files Browse the repository at this point in the history
Plugin Prop Validation & Improvements
  • Loading branch information
rgomezp authored Sep 27, 2023
2 parents 87ff596 + c28407f commit cf59ea9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
10 changes: 10 additions & 0 deletions onesignal/withOneSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OneSignalPluginProps> = (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);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
41 changes: 41 additions & 0 deletions support/helpers.ts
Original file line number Diff line number Diff line change
@@ -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.`);
}
}
}
16 changes: 13 additions & 3 deletions types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit cf59ea9

Please sign in to comment.