Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup] Utility function for better query params handling #32573

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import {IsEqual, ValueOf} from 'type-fest';
import CONST from './CONST';

// This is a file containing constants for all the routes we want to be able to go to
type QueryParams = Record<string, string | number | boolean | undefined>;

/**
* Builds a URL with an encoded URI component for the `backTo` param which can be added to the end of URLs
*/
function getUrlWithBackToParam<TUrl extends string>(url: TUrl, backTo?: string): `${TUrl}` | `${TUrl}?backTo=${string}` | `${TUrl}&backTo=${string}` {
const backToParam = backTo ? (`${url.includes('?') ? '&' : '?'}backTo=${encodeURIComponent(backTo)}` as const) : '';
return `${url}${backToParam}` as const;
function getUrlWithQueryParams<TUrl extends string, TQuery extends QueryParams>(url: TUrl, query: TQuery): TUrl | `${TUrl}?${string}` {
if (url.includes('?')) {
throw new Error('getUrlWithQueryParams should only be used for urls without query params');
}

const parsedQuery = Object.entries(query)
.filter((param): param is [string, string | number | boolean] => param !== undefined)
.map(([param, value]) => `${param}=${encodeURIComponent(value)}`)
.join('&');

return parsedQuery ? (`${url}?${parsedQuery}` as const) : url;
}

const ROUTES = {
Expand All @@ -22,11 +28,11 @@ const ROUTES = {
SEARCH: 'search',
DETAILS: {
route: 'details',
getRoute: (login: string) => `details?login=${encodeURIComponent(login)}` as const,
getRoute: (login: string) => getUrlWithQueryParams('details', {login}),
},
PROFILE: {
route: 'a/:accountID',
getRoute: (accountID: string | number, backTo?: string) => getUrlWithBackToParam(`a/${accountID}`, backTo),
getRoute: (accountID: string | number, backTo?: string) => getUrlWithQueryParams(`a/${accountID}`, {backTo}),
},

TRANSITION_BETWEEN_APPS: 'transition',
Expand All @@ -52,7 +58,7 @@ const ROUTES = {
BANK_ACCOUNT_PERSONAL: 'bank-account/personal',
BANK_ACCOUNT_WITH_STEP_TO_OPEN: {
route: 'bank-account/:stepToOpen?',
getRoute: (stepToOpen = '', policyID = '', backTo?: string) => getUrlWithBackToParam(`bank-account/${stepToOpen}?policyID=${policyID}`, backTo),
getRoute: (stepToOpen = '', policyID = '', backTo?: string) => getUrlWithQueryParams(`bank-account/${stepToOpen}`, {backTo, policyID}),
},

SETTINGS: 'settings',
Expand Down Expand Up @@ -120,23 +126,23 @@ const ROUTES = {
SETTINGS_PERSONAL_DETAILS_ADDRESS: 'settings/profile/personal-details/address',
SETTINGS_PERSONAL_DETAILS_ADDRESS_COUNTRY: {
route: 'settings/profile/personal-details/address/country',
getRoute: (country: string, backTo?: string) => getUrlWithBackToParam(`settings/profile/personal-details/address/country?country=${country}`, backTo),
getRoute: (country: string, backTo?: string) => getUrlWithQueryParams(`settings/profile/personal-details/address/country`, {backTo, country}),
},
SETTINGS_CONTACT_METHODS: {
route: 'settings/profile/contact-methods',
getRoute: (backTo?: string) => getUrlWithBackToParam('settings/profile/contact-methods', backTo),
getRoute: (backTo?: string) => getUrlWithQueryParams('settings/profile/contact-methods', {backTo}),
},
SETTINGS_CONTACT_METHOD_DETAILS: {
route: 'settings/profile/contact-methods/:contactMethod/details',
getRoute: (contactMethod: string) => `settings/profile/contact-methods/${encodeURIComponent(contactMethod)}/details` as const,
},
SETTINGS_NEW_CONTACT_METHOD: {
route: 'settings/profile/contact-methods/new',
getRoute: (backTo?: string) => getUrlWithBackToParam('settings/profile/contact-methods/new', backTo),
getRoute: (backTo?: string) => getUrlWithQueryParams('settings/profile/contact-methods/new', {backTo}),
},
SETTINGS_2FA: {
route: 'settings/security/two-factor-auth',
getRoute: (backTo?: string) => getUrlWithBackToParam('settings/security/two-factor-auth', backTo),
getRoute: (backTo?: string) => getUrlWithQueryParams('settings/security/two-factor-auth', {backTo}),
},
SETTINGS_STATUS: 'settings/profile/status',
SETTINGS_STATUS_SET: 'settings/profile/status/set',
Expand All @@ -158,15 +164,15 @@ const ROUTES = {
},
EDIT_CURRENCY_REQUEST: {
route: 'r/:threadReportID/edit/currency',
getRoute: (threadReportID: string, currency: string, backTo: string) => `r/${threadReportID}/edit/currency?currency=${currency}&backTo=${backTo}` as const,
getRoute: (threadReportID: string, currency: string, backTo: string) => getUrlWithQueryParams(`r/${threadReportID}/edit/currency`, {currency, backTo}),
},
REPORT_WITH_ID_DETAILS_SHARE_CODE: {
route: 'r/:reportID/details/shareCode',
getRoute: (reportID: string) => `r/${reportID}/details/shareCode` as const,
},
REPORT_ATTACHMENTS: {
route: 'r/:reportID/attachment',
getRoute: (reportID: string, source: string) => `r/${reportID}/attachment?source=${encodeURI(source)}` as const,
getRoute: (reportID: string, source: string) => getUrlWithQueryParams(`r/${reportID}/attachment`, {source}),
},
REPORT_PARTICIPANTS: {
route: 'r/:reportID/participants',
Expand Down Expand Up @@ -207,7 +213,7 @@ const ROUTES = {
EDIT_SPLIT_BILL_CURRENCY: {
route: 'r/:reportID/split/:reportActionID/edit/currency',
getRoute: (reportID: string, reportActionID: string, currency: string, backTo: string) =>
`r/${reportID}/split/${reportActionID}/edit/currency?currency=${currency}&backTo=${backTo}` as const,
getUrlWithQueryParams(`r/${reportID}/split/${reportActionID}/edit/currency`, {currency, backTo}),
},
TASK_TITLE: {
route: 'r/:reportID/title',
Expand Down Expand Up @@ -265,7 +271,7 @@ const ROUTES = {
},
MONEY_REQUEST_CURRENCY: {
route: ':iouType/new/currency/:reportID?',
getRoute: (iouType: string, reportID: string, currency: string, backTo: string) => `${iouType}/new/currency/${reportID}?currency=${currency}&backTo=${backTo}` as const,
getRoute: (iouType: string, reportID: string, currency: string, backTo: string) => getUrlWithQueryParams(`${iouType}/new/currency/${reportID}`, {currency, backTo}),
},
MONEY_REQUEST_DESCRIPTION: {
route: ':iouType/new/description/:reportID?',
Expand Down
Loading