diff --git a/.changeset/ui-extensions-server-kit-package-contents.md b/.changeset/ui-extensions-server-kit-package-contents.md new file mode 100644 index 00000000000..b8bfffa6a59 --- /dev/null +++ b/.changeset/ui-extensions-server-kit-package-contents.md @@ -0,0 +1,5 @@ +--- +'@shopify/ui-extensions-server-kit': patch +--- + +Publish only the build output, type the `update` persist payload as partial, and add the `error` build status diff --git a/packages/ui-extensions-server-kit/package.json b/packages/ui-extensions-server-kit/package.json index 466c77c09d3..5fa235a8ee4 100644 --- a/packages/ui-extensions-server-kit/package.json +++ b/packages/ui-extensions-server-kit/package.json @@ -26,7 +26,15 @@ "module": "index.mjs", "types": "index.d.ts", "files": [ - "**" + "/dist", + "!/dist/**/*.test.d.ts", + "!/dist/tsconfig.tsbuildinfo", + "/index.js", + "/index.mjs", + "/index.d.ts", + "/testing.js", + "/testing.mjs", + "/testing.d.ts" ], "scripts": { "build": "nx build", diff --git a/packages/ui-extensions-server-kit/src/ExtensionServerClient/ExtensionServerClient.test.ts b/packages/ui-extensions-server-kit/src/ExtensionServerClient/ExtensionServerClient.test.ts index 8ef4ffa1aea..2453279a888 100644 --- a/packages/ui-extensions-server-kit/src/ExtensionServerClient/ExtensionServerClient.test.ts +++ b/packages/ui-extensions-server-kit/src/ExtensionServerClient/ExtensionServerClient.test.ts @@ -1051,6 +1051,17 @@ describe('ExtensionServerClient', () => { }) }) + test('persists a partial app update, which the server merges into its payload', async () => { + const client = new ExtensionServerClient(defaultOptions) + mockSocketServer.connect(mockSocket) + + client.persist('update', {app: {apiKey: 'api-key'}}) + + expect(mockSocketServer.messages).toStrictEqual([ + JSON.stringify({event: 'update', data: {app: {apiKey: 'api-key'}}}), + ]) + }) + test('warns if trying to "persist" a dispatch event', async () => { // Spy on console.warn const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined) diff --git a/packages/ui-extensions-server-kit/src/ExtensionServerClient/server-types.ts b/packages/ui-extensions-server-kit/src/ExtensionServerClient/server-types.ts index 51375c4a962..3981f92c9da 100644 --- a/packages/ui-extensions-server-kit/src/ExtensionServerClient/server-types.ts +++ b/packages/ui-extensions-server-kit/src/ExtensionServerClient/server-types.ts @@ -1,5 +1,5 @@ import {Surface} from './types.js' -import {ExtensionPayload, ExtensionPoint, App} from '../types' +import {ExtensionPayload, ExtensionPoint, App, UpdatePersistPayload} from '../types' import {FlattenedLocalization, LocalesOptions, Localization} from '../i18n' // Re-export and augment the global ExtensionServer namespace @@ -46,10 +46,7 @@ export namespace ExtensionServer { } export interface OutboundPersistEvents { - update: { - extensions?: ExtensionPayload[] - app?: App - } + update: UpdatePersistPayload } export interface DispatchEvents { diff --git a/packages/ui-extensions-server-kit/src/types.test.ts b/packages/ui-extensions-server-kit/src/types.test.ts new file mode 100644 index 00000000000..71cd243cb20 --- /dev/null +++ b/packages/ui-extensions-server-kit/src/types.test.ts @@ -0,0 +1,41 @@ +import {App, ExtensionPayload, Status, UpdatePersistPayload} from './types' + +describe('Status', () => { + test('lists every build status that the extension server sends', () => { + expect(Object.values(Status)).toStrictEqual(['success', 'error', '']) + }) +}) + +// The build's `tsc` checks these assertions; Vitest does not. +describe('UpdatePersistPayload', () => { + test('accepts a partial app update that names the app', () => { + expectTypeOf<{app: {apiKey: string; title: string}}>().toExtend() + }) + + test('rejects an app update without the API key, which the server ignores', () => { + expectTypeOf<{app: {title: string}}>().not.toExtend() + }) + + test('accepts a partial extension update that names the extension', () => { + expectTypeOf<{extensions: [{uuid: string; version: string}]}>().toExtend() + expectTypeOf<{extensions: [{version: string}]}>().not.toExtend() + }) + + test('accepts a partial extension point update that names the target', () => { + expectTypeOf<{ + extensions: [{uuid: string; extensionPoints: [{target: string; resource: {url: string}}]}] + }>().toExtend() + expectTypeOf<{ + extensions: [{uuid: string; extensionPoints: [{resource: {url: string}}]}] + }>().not.toExtend() + }) + + test('accepts whole extension payloads and a whole app', () => { + expectTypeOf<{app: App; extensions: ExtensionPayload[]}>().toExtend() + }) + + test('accepts the string and null forms of extension points', () => { + expectTypeOf<{extensions: [{uuid: string; extensionPoints: string[]}]}>().toExtend() + expectTypeOf<{extensions: [{uuid: string; extensionPoints: null}]}>().toExtend() + }) +}) diff --git a/packages/ui-extensions-server-kit/src/types.ts b/packages/ui-extensions-server-kit/src/types.ts index 2f6421a5fba..a34906cd455 100644 --- a/packages/ui-extensions-server-kit/src/types.ts +++ b/packages/ui-extensions-server-kit/src/types.ts @@ -25,10 +25,7 @@ declare global { } interface OutboundPersistEvents { - update: { - extensions?: ({uuid: string} & DeepPartial)[] - app?: DeepPartial - } + update: UpdatePersistPayload } interface DispatchEvents { @@ -65,8 +62,29 @@ declare global { } } -export type DeepPartial = { - [P in keyof T]?: DeepPartial +// `unknown` stays `unknown`: a mapped type would turn it into `{}`, which rejects full payloads. +export type DeepPartial = T extends object + ? { + [P in keyof T]?: DeepPartial + } + : T + +type ExtensionPointUpdate = Pick & DeepPartial + +type ExtensionUpdate = Pick & + DeepPartial> & { + extensionPoints?: string[] | ExtensionPointUpdate[] | null + } + +/** + * The payload of the `update` event that a client persists to the extension server. + * The server merges it into its own payload, so a client sends only the fields that change. + * It finds what to change by the extension's `uuid`, the extension point's `target` and the app's `apiKey`. + * It ignores the whole message when one of them is missing, or when the `apiKey` is not its app's. + */ +export interface UpdatePersistPayload { + extensions?: ExtensionUpdate[] + app?: Pick & DeepPartial } interface ResourceURL { @@ -177,6 +195,9 @@ export interface ExtensionSupportedFeatures { export enum Status { Success = 'success', + Error = 'error', + /** The server sends an empty status when it has nothing to build. */ + None = '', } export interface App { diff --git a/packages/ui-extensions-server-kit/vite.config.mts b/packages/ui-extensions-server-kit/vite.config.mts index 54cd05f9519..0423d1dd175 100644 --- a/packages/ui-extensions-server-kit/vite.config.mts +++ b/packages/ui-extensions-server-kit/vite.config.mts @@ -46,7 +46,9 @@ export default defineConfig({ }, }, plugins: [ - react(), + // The classic runtime imports only `react`, which stays external. The automatic runtime + // bundles React's JSX runtime into `dist/node_modules`, which then breaks module resolution. + react({jsxRuntime: 'classic'}), createEntryFiles({ files: { index: './dist/index',