const apiPath = 'de.pinuts.cmsbs.restapi.Forms/index'; const getRestProxyUrl = (umopen: string, restProxyUrl: string = '/p') => { return `${restProxyUrl}/${apiPath}?umopen=${encodeURI(umopen)}`; }; /** * This method is used to load contact instances from UM. * It delivers only enabled forms. * * @async * @param {string} umopen * @param {string} restProxyUrl - The REST-Proxy URL. Default is '/p'. * @param {string} locale - 2-letter language/locale key. This is a required parameter. * @param {string} [name] - The name of the form instances to be loaded. This is an optional parameter. * @param {string} [searchTerm] - Search term, case-insensitive substring of * * name, title or description * @returns {Promise} A promise that resolves with the JSON response from the server. * @throws {Error} Throws an error if the locale is not provided or if the HTTP request fails. */ export const loadContactFormInstances = async ( umopen: string, locale: string, restProxyUrl: string, name?: string, searchTerm?: string, ): Promise => { const pluginTypeName = 'de.pinuts.cmsbs.contactform2'; return loadFormInstances(umopen, restProxyUrl, locale, pluginTypeName, name, searchTerm); }; /** * This method is used to load newsletter instances from UM. * It delivers only enabled forms. * * @async * @param {string} umopen * @param {string} restProxyUrl - The REST-Proxy URL. Default is '/p'. * @param {string} locale - 2-letter language/locale key. This is a required parameter. * @param {string} [name] - The name of the form instances to be loaded. This is an optional parameter. * @param {string} [searchTerm] - Search term, case-insensitive substring of * * name, title or description * @returns {Promise} A promise that resolves with the JSON response from the server. * @throws {Error} Throws an error if the locale is not provided or if the HTTP request fails. */ export const loadNewsletterInstances = async ( umopen: string, locale: string, restProxyUrl: string, name?: string, searchTerm?: string, ): Promise => { const pluginTypeName = 'de.pinuts.cmsbs.newsletter'; return loadFormInstances(umopen, restProxyUrl, locale, pluginTypeName, name, searchTerm); }; /** * This method is used to load form instances based on the provided parameters. * * @async * @param {string} umopen * @param {string} restProxyUrl - The REST-Proxy URL. Default is '/p'. * @param {string} locale - 2-letter language/locale key. This is a required parameter. * @param {string} [pluginTypeName] - Plugin's package name * @param {string} [name] - The name of the form instances to be loaded. This is an optional parameter. * @param {string} [searchTerm] - Search term, case-insensitive substring of * * name, title or description * @returns {Promise} A promise that resolves with the JSON response from the server. * @throws {Error} Throws an error if the locale is not provided or if the HTTP request fails. */ export const loadFormInstances = async ( umopen: string, restProxyUrl: string, locale: string, pluginTypeName?: string, name?: string, searchTerm?: string): Promise => { if (!locale) { throw new Error('locale is required'); } const paramString = [ `locale=${encodeURI(locale)}`, pluginTypeName ? `type=${encodeURI(pluginTypeName)}` : '', name ? `name=${encodeURI(name)}` : '', searchTerm ? `searchTerm=${encodeURI(searchTerm)}` : '', ].filter(Boolean).join('&'); const urlWithParams = `${getRestProxyUrl(umopen, restProxyUrl)}&${paramString}`; const response = await fetch(urlWithParams); if (!response.ok) { console.error('UM loadFormInstances failed:', response.status); throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); };