-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add redirect util function for project custom actions (#1865)
- Loading branch information
Showing
4 changed files
with
43 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { logger } from '../../utils/logger'; | ||
|
||
/** | ||
* Extracts the redirect URL from request headers for AdminJS actions | ||
* @param request - The AdminJS action request object | ||
* @param resourceName - The name of the resource (e.g., 'Project', 'User') | ||
* @returns The URL to redirect to after action completion | ||
*/ | ||
export const getRedirectUrl = (request: any, resourceName: string): string => { | ||
const refererIndex = | ||
request?.rawHeaders?.findIndex(h => h.toLowerCase() === 'referer') || -1; | ||
const referrerUrl = | ||
refererIndex !== -1 ? request.rawHeaders[refererIndex + 1] : false; | ||
|
||
// Default fallback URL if no referer is found | ||
const defaultUrl = `/admin/resources/${resourceName}`; | ||
|
||
try { | ||
if (referrerUrl) { | ||
const url = new URL(referrerUrl); | ||
// If it's the main list view (no search params), add a timestamp to force refresh | ||
if (url.pathname === `/admin/resources/${resourceName}` && !url.search) { | ||
return `${url.pathname}?timestamp=${Date.now()}`; | ||
} | ||
return url.pathname + url.search; | ||
} | ||
} catch (error) { | ||
logger.error('Error parsing referrer URL:', error); | ||
} | ||
|
||
// Add timestamp to default URL as well | ||
return `${defaultUrl}?timestamp=${Date.now()}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters