Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/ui-extension-action-requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@godaddy/react': minor
---

A `dom-bundle` UI extension can ask its host to run an action. The mount input now carries an optional `requestAction({ action, input })`, which resolves with the host's result. Hosts handle requests with a new optional `onActionRequest` prop on `Target` and `UiExtensionRuntimeHost`, or through the runtime's existing `onActionRequest` mount option, which now also receives an optional second argument. The handler gets a structured clone of the request and the metadata of the extension that sent it, built from the host's own record, and the extension gets a structured clone of the result. Without a handler, when the host rejects, when a value can't be cloned, without an action name (or with one that is blank or has spaces around it), or when the extension unmounts (including while a request is still waiting on the host), the request rejects with an `action_rejected` runtime error; the host's own reason isn't passed to the extension. The function stays the same for the life of the mount, and under `Target` and `UiExtensionRuntimeHost` it always reaches the host's current committed handler. A request rejected at unmount may still have run on the host. A runtime mounted again before its earlier mount finishes now unmounts the replaced bundle, and that bundle's requests are refused. Existing bundles and hosts work unchanged.

Hosts that pick a target's extensions before rendering can call the new `getEnabledStoreUiExtensions({ storeId, target, apiHost })`, the same discovery request `useEnabledStoreUiExtensions` sends, outside React.
37 changes: 37 additions & 0 deletions packages/react/src/ui-extensions/discovery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it, vi } from 'vitest';
import { getEnabledStoreUiExtensions as requestEnabledStoreUiExtensions } from '@/lib/godaddy/godaddy';
import { getEnabledStoreUiExtensions } from './discovery';

vi.mock('@/lib/godaddy/godaddy', () => ({
getEnabledStoreUiExtensions: vi.fn().mockResolvedValue([{ id: 'app-1' }]),
}));

describe('getEnabledStoreUiExtensions', () => {
it('sends the same discovery request as the hook, outside React', async () => {
await expect(
getEnabledStoreUiExtensions({
storeId: 'store-1',
target: 'some.target',
apiHost: 'api.ote-godaddy.com',
})
).resolves.toEqual([{ id: 'app-1' }]);

expect(requestEnabledStoreUiExtensions).toHaveBeenCalledWith(
{ storeId: 'store-1', target: 'some.target' },
'api.ote-godaddy.com'
);
});

it('sends nothing without a store id or a target, like the hook', async () => {
vi.mocked(requestEnabledStoreUiExtensions).mockClear();

await expect(
getEnabledStoreUiExtensions({ storeId: '', target: 'some.target' })
).resolves.toEqual([]);
await expect(
getEnabledStoreUiExtensions({ storeId: 'store-1', target: '' })
).resolves.toEqual([]);

expect(requestEnabledStoreUiExtensions).not.toHaveBeenCalled();
});
});
29 changes: 29 additions & 0 deletions packages/react/src/ui-extensions/discovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getEnabledStoreUiExtensions as requestEnabledStoreUiExtensions } from '@/lib/godaddy/godaddy';
import type { UiExtensionTargetId } from './types';

export interface GetEnabledStoreUiExtensionsInput {
storeId: string;
target: UiExtensionTargetId;
/** The environment's API host, as `GoDaddyProvider` takes it. */
apiHost?: string;
}

/**
* Reads the UI extensions enabled for a target on a store, outside React. It
* sends the same Checkout API query as `useEnabledStoreUiExtensions`, for a
* host that has to know a target's extensions before it renders. Like the hook,
* it sends nothing without a store id and a target; it then answers an empty
* list.
*/
export function getEnabledStoreUiExtensions({
storeId,
target,
apiHost,
}: GetEnabledStoreUiExtensionsInput): ReturnType<
typeof requestEnabledStoreUiExtensions
> {
if (!storeId || !target) {
return Promise.resolve([]);
}
return requestEnabledStoreUiExtensions({ storeId, target }, apiHost);
}
2 changes: 2 additions & 0 deletions packages/react/src/ui-extensions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type { GetEnabledStoreUiExtensionsInput } from './discovery';
export { getEnabledStoreUiExtensions } from './discovery';
export * from './hooks';
export * from './runtime';
export { Target } from './target';
Expand Down
Loading