mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	Add function to extract API URL for a model instance
This commit is contained in:
		@@ -1,13 +1,15 @@
 | 
			
		||||
import type { NavigateFunction } from 'react-router-dom';
 | 
			
		||||
import { ModelInformationDict } from '../enums/ModelInformation';
 | 
			
		||||
import type { ModelType } from '../enums/ModelType';
 | 
			
		||||
import { apiUrl } from './Api';
 | 
			
		||||
import { cancelEvent } from './Events';
 | 
			
		||||
 | 
			
		||||
export const getBaseUrl = (): string =>
 | 
			
		||||
  (window as any).INVENTREE_SETTINGS?.base_url || 'web';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Returns the detail view URL for a given model type
 | 
			
		||||
 * Returns the detail view URL for a given model type.
 | 
			
		||||
 * This is the UI URL, not the API URL.
 | 
			
		||||
 */
 | 
			
		||||
export function getDetailUrl(
 | 
			
		||||
  model: ModelType,
 | 
			
		||||
@@ -35,6 +37,27 @@ export function getDetailUrl(
 | 
			
		||||
  return '';
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Returns the API detail URL for a given model type.
 | 
			
		||||
 */
 | 
			
		||||
export function getApiUrl(
 | 
			
		||||
  model: ModelType,
 | 
			
		||||
  pk: number | string
 | 
			
		||||
): string | undefined {
 | 
			
		||||
  const modelInfo = ModelInformationDict[model];
 | 
			
		||||
 | 
			
		||||
  if (pk === undefined || pk === null) {
 | 
			
		||||
    return '';
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!!pk && modelInfo && modelInfo.api_endpoint) {
 | 
			
		||||
    return apiUrl(modelInfo.api_endpoint, pk);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  console.error(`No API detail URL found for model ${model} <${pk}>`);
 | 
			
		||||
  return undefined;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Navigate to a provided link.
 | 
			
		||||
 * - If the link is to be opened externally, open it in a new tab.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user