Skip to content

Commit

Permalink
refactor(mdw): scope more precisely the URL try/catch to let malforme…
Browse files Browse the repository at this point in the history
…d params throw an error
  • Loading branch information
sneko committed Jun 9, 2022
1 parent 59a14b1 commit 2f8199c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 86 deletions.
130 changes: 65 additions & 65 deletions src/middlewares/format-firebase-dynamic-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,93 +63,93 @@ export function FormatFirebaseDynamicLinksMiddleware(
options: FormatFirebaseDynamicLinksOptions
): Middleware {
return (properties, element, next) => {
let url: URL;
try {
const url = new URL(properties.href);
let androidStoreUrl: URL | undefined;
let iosStoreUrl: URL | undefined;
url = new URL(properties.href);
} catch (err) {
// In case it's not a valid format the middleware is skipped without affecting next ones
return next();
}

const dynamicLink = new URL(options.dynamicLinkBase);
dynamicLink.searchParams.set('link', properties.href);
let androidStoreUrl: URL | undefined;
let iosStoreUrl: URL | undefined;

if (options.android) {
dynamicLink.searchParams.set('apn', options.android.storeId);
const dynamicLink = new URL(options.dynamicLinkBase);
dynamicLink.searchParams.set('link', properties.href);

if (options.android.storeLink) {
androidStoreUrl = new URL(options.android.storeLink);
}
if (options.android) {
dynamicLink.searchParams.set('apn', options.android.storeId);

if (options.android.storeLink) {
androidStoreUrl = new URL(options.android.storeLink);
}
}

if (options.ios) {
dynamicLink.searchParams.set('isi', options.ios.storeId);
dynamicLink.searchParams.set('ibi', options.ios.bundleId);
if (options.ios) {
dynamicLink.searchParams.set('isi', options.ios.storeId);
dynamicLink.searchParams.set('ibi', options.ios.bundleId);

if (options.ios.storeLink) {
iosStoreUrl = new URL(options.ios.storeLink);
}

if (options.ios.nativeScheme) {
dynamicLink.searchParams.set('ius', options.ios.nativeScheme);
}
if (options.ios.storeLink) {
iosStoreUrl = new URL(options.ios.storeLink);
}

dynamicLink.searchParams.set(
'efr',
options.ios.skipAppPreviewPage || '1'
);
if (options.ios.nativeScheme) {
dynamicLink.searchParams.set('ius', options.ios.nativeScheme);
}

if (
options.injectUtmParamsInDynamicLink ||
options.injectUtmParamsInFallback
) {
const urlParams = url.searchParams;
dynamicLink.searchParams.set(
'efr',
options.ios.skipAppPreviewPage || '1'
);
}

// Get UTM parameters from the original link
Object.values(FirebaseUtmParamEnum).forEach(utmKey => {
const utmValue = urlParams.get(utmKey);
if (
options.injectUtmParamsInDynamicLink ||
options.injectUtmParamsInFallback
) {
const urlParams = url.searchParams;

if (utmValue) {
if (options.injectUtmParamsInDynamicLink) {
dynamicLink.searchParams.set(utmKey, utmValue);
}
// Get UTM parameters from the original link
Object.values(FirebaseUtmParamEnum).forEach(utmKey => {
const utmValue = urlParams.get(utmKey);

if (options.injectUtmParamsInFallback) {
if (androidStoreUrl) {
androidStoreUrl.searchParams.set(utmKey, utmValue);
}
if (utmValue) {
if (options.injectUtmParamsInDynamicLink) {
dynamicLink.searchParams.set(utmKey, utmValue);
}

if (iosStoreUrl) {
iosStoreUrl.searchParams.set(utmKey, utmValue);
}
if (options.injectUtmParamsInFallback) {
if (androidStoreUrl) {
androidStoreUrl.searchParams.set(utmKey, utmValue);
}
}
});
}

// Fallback link (after UTM manipulations because they can be affected)
if (options.usePlatformLinkAsFallback !== false) {
if ((platform as any).os.family === 'iOS' && iosStoreUrl) {
dynamicLink.searchParams.set('ofl', iosStoreUrl.toString());
} else if (
(platform as any).os.family === 'Android' &&
androidStoreUrl
) {
dynamicLink.searchParams.set('ofl', androidStoreUrl.toString());
if (iosStoreUrl) {
iosStoreUrl.searchParams.set(utmKey, utmValue);
}
}
}
}
});
}

if (options.overrideParams) {
for (const [paramKey, paramValue] of Object.entries(
options.overrideParams
)) {
dynamicLink.searchParams.set(paramKey, paramValue);
}
// Fallback link (after UTM manipulations because they can be affected)
if (options.usePlatformLinkAsFallback !== false) {
if ((platform as any).os.family === 'iOS' && iosStoreUrl) {
dynamicLink.searchParams.set('ofl', iosStoreUrl.toString());
} else if ((platform as any).os.family === 'Android' && androidStoreUrl) {
dynamicLink.searchParams.set('ofl', androidStoreUrl.toString());
}
}

properties.href = dynamicLink.toString();
} catch (err) {
// In case it's not a valid format the middleware is skipped without affecting next ones
if (options.overrideParams) {
for (const [paramKey, paramValue] of Object.entries(
options.overrideParams
)) {
dynamicLink.searchParams.set(paramKey, paramValue);
}
}

properties.href = dynamicLink.toString();

next();
};
}
45 changes: 24 additions & 21 deletions src/middlewares/set-utm-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,38 @@ export function SetUtmParametersMiddleware(
options: SetUtmParametersOptions
): Middleware {
return (properties, element, next) => {
let url: URL;
try {
const url = new URL(properties.href);
const urlParams = url.searchParams;
let existingUtmParams: boolean = false;
url = new URL(properties.href);
} catch (err) {
// In case it's not a valid format the middleware is skipped without affecting next ones
return next();
}

Object.values(UtmParamEnum).forEach(utmKey => {
if (urlParams.has(utmKey)) {
if (
options.forceIfPresent !== undefined &&
options.forceIfPresent === true
) {
urlParams.delete(utmKey);
} else {
existingUtmParams = true;
}
}
});
const urlParams = url.searchParams;
let existingUtmParams: boolean = false;

if (!existingUtmParams) {
for (const [utmKey, utmValue] of Object.entries(options.params)) {
urlParams.set(utmKey, utmValue);
Object.values(UtmParamEnum).forEach(utmKey => {
if (urlParams.has(utmKey)) {
if (
options.forceIfPresent !== undefined &&
options.forceIfPresent === true
) {
urlParams.delete(utmKey);
} else {
existingUtmParams = true;
}
}
});

properties.href = url.toString();
} catch (err) {
// In case it's not a valid format the middleware is skipped without affecting next ones
if (!existingUtmParams) {
for (const [utmKey, utmValue] of Object.entries(options.params)) {
urlParams.set(utmKey, utmValue);
}
}

properties.href = url.toString();

next();
};
}

0 comments on commit 2f8199c

Please sign in to comment.