diff --git a/src/main/resources/lib/enonic/react4xp/asset/assetUrl.ts b/src/main/resources/lib/enonic/react4xp/asset/assetUrl.ts deleted file mode 100644 index a5b456b8..00000000 --- a/src/main/resources/lib/enonic/react4xp/asset/assetUrl.ts +++ /dev/null @@ -1,89 +0,0 @@ -//import {toStr} from '@enonic/js-utils/value/toStr'; -import { - //getSite, - //pageUrl, - serviceUrl as getServiceUrl, - type ServiceUrlParams -} from '/lib/xp/portal'; - - -const URL_TYPE_SERVER: ServiceUrlParams['type'] = 'server'; - - -// http://localhost:8080/admin/site/inline/default/draft/react4xp-site/_/service/com.enonic.app.react4xp/react4xp/site/parts/color/color.js -// siteUrl: http://localhost:8080/admin/site/inline/default/draft/react4xp-site -// / -// serviceRoot: _/service -// / -// application: com.enonic.app.react4xp -// / -// assetRoot: react4xp -// / -// assetPath: site/parts/color/color.js -export function assetUrl< - Params extends object = object ->(functionParams: { - assetPath: string - // Optional - application?: string - serviceName?: string - serviceUrl?: string - /*site? :{ - _id: string - } - siteId? :string - siteUrl? :string*/ - type?: ServiceUrlParams['type'] - urlParams?: Params -}) { - const { - assetPath, - // Optional - application = app.name, - serviceName = 'react4xp', - type = URL_TYPE_SERVER, - urlParams = {} - } = functionParams; - if (!assetPath) { - throw new Error(`assetUrl: Missing required parameter assetPath!`); - } - let { - serviceUrl/*, - site, - siteId, - siteUrl*/ - } = functionParams; - /*if (!siteUrl) { - if (!siteId) { - if (!site) { - site = getSite(); - if (!site) { - throw new Error(`assetUrl: getSite() did not return a site!`); - } - } - siteId = site._id; - } - if (!siteId) { - throw new Error(`assetUrl: Unable to determine siteId!`); - } - siteUrl = pageUrl({id: siteId}); - if (!siteUrl) { - throw new Error(`assetUrl: Unable to determine siteUrl!`); - } - }*/ - if (!serviceUrl) { - if (!serviceName) { - throw new Error(`assetUrl: You have to provide serviceUrl or serviceName!`); - } - serviceUrl = getServiceUrl({ - application, - params: urlParams, - service: serviceName, - type - }); - } - if (!serviceUrl) { - throw new Error(`assetUrl: Unable to determine serviceUrl from application:${application} serviceName:${serviceName} type:${type}!`); - } - return `${serviceUrl}/${assetPath}`.replace(/\/{2,}/g, '/'); -} // assetUrl diff --git a/src/main/resources/lib/enonic/react4xp/assetUrl.ts b/src/main/resources/lib/enonic/react4xp/assetUrl.ts new file mode 100644 index 00000000..aa90fe28 --- /dev/null +++ b/src/main/resources/lib/enonic/react4xp/assetUrl.ts @@ -0,0 +1,26 @@ +import type { AssetUrlParams } from '@enonic-types/lib-portal'; // Might be deprecated in XP8. + +import { getUrlType } from '/lib/enonic/react4xp/React4xp/utils/getUrlType'; +import { serviceUrl } from '/lib/enonic/react4xp/serviceUrl'; + + +export interface R4xAssetUrlParams extends AssetUrlParams { + service?: string; +} + + +export function assetUrl({ + application = app.name, + params, + path = '/', + service = 'asset', + type = getUrlType() +}: R4xAssetUrlParams): string { + return serviceUrl({ + application, + params, + path, + service, + type + }); +} diff --git a/src/main/resources/lib/enonic/react4xp/dependencies/initServiceUrlRoot.ts b/src/main/resources/lib/enonic/react4xp/dependencies/initServiceUrlRoot.ts index 2def1404..aa9c706a 100644 --- a/src/main/resources/lib/enonic/react4xp/dependencies/initServiceUrlRoot.ts +++ b/src/main/resources/lib/enonic/react4xp/dependencies/initServiceUrlRoot.ts @@ -2,34 +2,22 @@ import type { UrlType } from '@enonic-types/lib-react4xp'; import { getUrlType } from '/lib/enonic/react4xp/React4xp/utils/getUrlType'; -interface ServiceUrlBuilder { - setApplication(value: string): void; - - setPath(value: string): void; - - setType(value: string): void; - - setServiceName(value: string): void; - - createUrl(): string; -} +import { serviceUrl } from '/lib/enonic/react4xp/serviceUrl'; /* * Initialize the root path of a service URL for a site mount. */ export function initServiceUrlRoot({ serviceName = '', - urlType // default is app.config['react4xp.urlType'] || 'server' + urlType = getUrlType() }: { serviceName?: string, urlType?: UrlType } = {}) { - const bean: ServiceUrlBuilder = __.newBean('com.enonic.lib.react4xp.url.ServiceUrlBuilder'); - - bean.setApplication(app.name); - bean.setPath('/'); - bean.setType(getUrlType(urlType)); - bean.setServiceName(serviceName); - - return bean.createUrl(); + return serviceUrl({ + application: app.name, + path: '/', + service: serviceName, + type: urlType + }); } diff --git a/src/main/resources/lib/enonic/react4xp/index.ts b/src/main/resources/lib/enonic/react4xp/index.ts index 3807b49e..23921216 100644 --- a/src/main/resources/lib/enonic/react4xp/index.ts +++ b/src/main/resources/lib/enonic/react4xp/index.ts @@ -5,4 +5,6 @@ export { getExecutorUrl, render, } from '/lib/enonic/react4xp/React4xp'; +export {assetUrl} from '/lib/enonic/react4xp/assetUrl'; export {DataFetcher} from '/lib/enonic/react4xp/DataFetcher'; +export {serviceUrl} from '/lib/enonic/react4xp/serviceUrl'; diff --git a/src/main/resources/lib/enonic/react4xp/serviceUrl.ts b/src/main/resources/lib/enonic/react4xp/serviceUrl.ts new file mode 100644 index 00000000..3a2b66c2 --- /dev/null +++ b/src/main/resources/lib/enonic/react4xp/serviceUrl.ts @@ -0,0 +1,37 @@ +import type { ServiceUrlParams } from '@enonic-types/lib-portal'; // Might be deprecated in XP8. + +import { getUrlType } from '/lib/enonic/react4xp/React4xp/utils/getUrlType'; + + +interface ServiceUrlBuilder { + setApplication(value: string): void; + setParams(value: object): void; + setPath(value: string): void; + setType(value: string): void; + setServiceName(value: string): void; + createUrl(): string; +} + + +export interface R4xServiceUrlParams extends ServiceUrlParams { + path?: string; +} + + +export function serviceUrl({ + application = app.name, + params, + path = '/', + service, + type = getUrlType() +}: R4xServiceUrlParams): string { + const bean: ServiceUrlBuilder = __.newBean('com.enonic.lib.react4xp.url.ServiceUrlBuilder'); + + bean.setApplication(application); + bean.setParams(params); + bean.setPath(path); + bean.setType(type); + bean.setServiceName(service); + + return bean.createUrl(); +} diff --git a/src/main/resources/types/React4xp.d.ts b/src/main/resources/types/React4xp.d.ts index 0c60d78f..4d8bcd08 100644 --- a/src/main/resources/types/React4xp.d.ts +++ b/src/main/resources/types/React4xp.d.ts @@ -3,7 +3,7 @@ import type { PageContributions, Request } from '@enonic-types/core'; -import type { AssetUrlParams } from '@enonic-types/lib-portal'; +import type { AssetUrlParams } from '@enonic-types/lib-portal'; // Might be deprecated in XP8. import type { Cache } from './Cache'; type OneOrMore = T | T[]