Skip to content

Commit 6e47543

Browse files
Implement widgetUrl in admin lib #10697
1 parent c86fe96 commit 6e47543

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

modules/lib/lib-admin/src/main/resources/lib/xp/admin.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ declare global {
1616
const i18n = require('/lib/xp/i18n');
1717
const portal = require('/lib/xp/portal');
1818

19+
function checkRequired<T extends object>(obj: T, name: keyof T): void {
20+
if (obj == null || obj[name] == null) {
21+
throw `Parameter '${String(name)}' is required`;
22+
}
23+
}
24+
1925
const helper = __.newBean<AdminLibHelper>('com.enonic.xp.lib.admin.AdminLibHelper');
2026

2127
interface AdminLibHelper {
@@ -160,3 +166,32 @@ export function getInstallation(): string {
160166
export function getVersion(): string {
161167
return helper.getVersion();
162168
}
169+
170+
export interface WidgetUrlParams {
171+
application: string;
172+
widget: string;
173+
type?: 'server' | 'absolute';
174+
params?: object;
175+
}
176+
177+
/**
178+
* Returns the URL for a widget.
179+
*
180+
* @param {object} [params] Parameter object
181+
* @param {string} params.application Application to reference to a widget.
182+
* @param {string} params.widget Name of the widget.
183+
* @param {string} [params.type=server] URL type. Either `server` (server-relative URL) or `absolute`.
184+
* @param {object} [params.params] Custom parameters to append to the url.
185+
*
186+
* @returns {string} URL.
187+
*/
188+
export function widgetUrl(params: WidgetUrlParams): string {
189+
checkRequired(params, 'application');
190+
checkRequired(params, 'widget');
191+
192+
return portal.apiUrl({
193+
application: 'admin',
194+
api: `widget/${params.application}/${params.widget}`,
195+
params: params.params || {},
196+
});
197+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.enonic.xp.lib.admin;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.enonic.xp.testing.ScriptTestSupport;
6+
7+
public class LibAdminTest
8+
extends ScriptTestSupport
9+
{
10+
@Test
11+
void testWidgetUrl()
12+
{
13+
runFunction( "/test/admin-test.js", "testWidgetUrl" );
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const t = require('/lib/xp/testing');
2+
3+
t.mock('/lib/xp/admin', {
4+
widgetUrl(params) {
5+
return 'generated_url';
6+
}
7+
});
8+
9+
exports.testWidgetUrl = function () {
10+
const adminLib = require('/lib/xp/admin');
11+
12+
const result = adminLib.widgetUrl({
13+
application: 'app',
14+
widget: 'widget',
15+
});
16+
17+
t.assertEquals("generated_url", result);
18+
};

0 commit comments

Comments
 (0)