Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/ui-extensions-server-kit-package-contents.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion packages/ui-extensions-server-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -46,10 +46,7 @@ export namespace ExtensionServer {
}

export interface OutboundPersistEvents {
update: {
extensions?: ExtensionPayload[]
app?: App
}
update: UpdatePersistPayload
}

export interface DispatchEvents {
Expand Down
41 changes: 41 additions & 0 deletions packages/ui-extensions-server-kit/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -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<UpdatePersistPayload>()
})

test('rejects an app update without the API key, which the server ignores', () => {
expectTypeOf<{app: {title: string}}>().not.toExtend<UpdatePersistPayload>()
})

test('accepts a partial extension update that names the extension', () => {
expectTypeOf<{extensions: [{uuid: string; version: string}]}>().toExtend<UpdatePersistPayload>()
expectTypeOf<{extensions: [{version: string}]}>().not.toExtend<UpdatePersistPayload>()
})

test('accepts a partial extension point update that names the target', () => {
expectTypeOf<{
extensions: [{uuid: string; extensionPoints: [{target: string; resource: {url: string}}]}]
}>().toExtend<UpdatePersistPayload>()
expectTypeOf<{
extensions: [{uuid: string; extensionPoints: [{resource: {url: string}}]}]
}>().not.toExtend<UpdatePersistPayload>()
})

test('accepts whole extension payloads and a whole app', () => {
expectTypeOf<{app: App; extensions: ExtensionPayload[]}>().toExtend<UpdatePersistPayload>()
})

test('accepts the string and null forms of extension points', () => {
expectTypeOf<{extensions: [{uuid: string; extensionPoints: string[]}]}>().toExtend<UpdatePersistPayload>()
expectTypeOf<{extensions: [{uuid: string; extensionPoints: null}]}>().toExtend<UpdatePersistPayload>()
})
})
33 changes: 27 additions & 6 deletions packages/ui-extensions-server-kit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ declare global {
}

interface OutboundPersistEvents {
update: {
extensions?: ({uuid: string} & DeepPartial<ExtensionPayload>)[]
app?: DeepPartial<App>
}
update: UpdatePersistPayload
}

interface DispatchEvents {
Expand Down Expand Up @@ -65,8 +62,29 @@ declare global {
}
}

export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>
// `unknown` stays `unknown`: a mapped type would turn it into `{}`, which rejects full payloads.
export type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>
}
: T

type ExtensionPointUpdate = Pick<ExtensionPoint, 'target'> & DeepPartial<ExtensionPoint>

type ExtensionUpdate = Pick<ExtensionPayload, 'uuid'> &
DeepPartial<Omit<ExtensionPayload, 'extensionPoints'>> & {
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<App, 'apiKey'> & DeepPartial<App>
}

interface ResourceURL {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion packages/ui-extensions-server-kit/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading