@hyperse-hub/hps-plugin-cmdk
Vendure plugin that feeds admin command-palette (cmdk) data through a single Shop API query. The list is built from pluggable search strategies, cached per channel and language, and refreshed on a schedule or on demand.
Intent
- Give the storefront (or any Shop API client) a stable, typed
cmdkActions query for the admin UI’s command palette: grouped sections (e.g. catalog, products) with paths, titles, subtitles, keywords, and optional icons.
- Let projects extend what appears in cmdk without changing core plugin code: register
CmdkSearchStrategy implementations that return rows for their section.
- Avoid recomputing on every request by caching aggregated results; invalidate only when a scheduled job or an admin mutation runs.
Design
Strategy aggregation
HpsPluginCmdkOptions.cmdkSearchStrategies is an array of CmdkSearchStrategy instances.
- Each strategy has a
section label, optional priority (lower runs first), init(injector) for Vendure services, and getActions(ctx, channelId, languageCode).
aggregateCmdkActions sorts by priority, runs each strategy, and merges into [{ section, actions }], matching the GraphQL CmdkAction shape.
- Use
mergeCmdkSearchStrategies(...) to compose defaults with custom strategies. Reference catalog + product strategies are not exported from the package; copy them from the Examples section below (or from src/examples in this repo).
Caching and refresh
CmdkService caches the aggregated list per (channelId, languageCode) with no TTL; invalidation is explicit.
- Scheduled task (
refreshCmdkActionsTask): daily at 00:00, enqueues a refresh job for every channel × availableLanguageCodes.
- Admin mutation
refreshCmdkActions(channelId, languageCode?): enqueues a background job for one channel (language defaults to the caller’s context).
- Refresh jobs rebuild an admin
RequestContext scoped to the channel (by id), clear the cache key, and repopulate.
Bootstrap
HpsPluginCmdk.init() merges options; on application bootstrap, each strategy’s init() runs with a Vendure Injector.
GraphQL
Shop
Query.cmdkActions — [CmdkAction!]! for the current channel and language (from request context).
Admin
Mutation.refreshCmdkActions(channelId: ID!, languageCode: String) — queues a cache rebuild; returns true when the job is accepted (not when work finishes).
Installation
yarn add @hyperse-hub/hps-plugin-cmdk
Peer dependencies: @vendure/core (and Nest/GraphQL stack as in your Vendure app). See package.json for exact ranges.
Usage
Import what the package exports (HpsPluginCmdk, mergeCmdkSearchStrategies, AbstractCmdkSearchStrategy, etc.) and pass your own strategy instances. Wire in the reference examples by copying the files into your codebase (they are not published from @hyperse-hub/hps-plugin-cmdk).
import {
HpsPluginCmdk,
mergeCmdkSearchStrategies,
} from '@hyperse-hub/hps-plugin-cmdk';
import { exampleCmdkSearchStrategies } from './cmdk/example-cmdk-search-strategies.js';
HpsPluginCmdk.init({
cmdkSearchStrategies: exampleCmdkSearchStrategies(),
});
Implement CmdkSearchStrategy or extend AbstractCmdkSearchStrategy for custom sections. For multi-channel shops, align ctx with channelId/languageCode in getActions so cached data matches the refresh path.
Examples
The snippets below mirror src/examples in this repository. They depend on optional Hyperse plugins (@hyperse-hub/hps-plugin-storage, @hyperse-hub/hps-plugin-smart-search, etc.); adjust services to match your stack.
cmdk-search-catalog.example.ts
import { AssetHydrateService } from '@hyperse-hub/hps-plugin-storage';
import type { Collection, ID } from '@vendure/core';
import { CollectionService, type RequestContext } from '@vendure/core';
import type { CmdkSectionActions } from '@hyperse-hub/hps-plugin-cmdk';
import { AbstractCmdkSearchStrategy } from '@hyperse-hub/hps-plugin-cmdk';
export class ExampleCmdkSearchCatalog extends AbstractCmdkSearchStrategy {
readonly section = 'category';
readonly priority = 10;
getActions(
ctx: RequestContext,
_channelId: ID,
_languageCode: string
): Promise<CmdkSectionActions[]> {
return this.constructActions(ctx);
}
private async constructActions(
ctx: RequestContext
): Promise<CmdkSectionActions[]> {
const collectionService = this.injector.get(CollectionService);
const assetHydrateService = this.injector.get(AssetHydrateService);
const catalogItems: Collection[] = [];
while (true) {
const { items } = await collectionService.findAll(ctx, {
skip: catalogItems.length,
take: 1000,
});
if (items.length === 0) {
break;
}
catalogItems.push(...items);
}
const result: CmdkSectionActions[] = [];
for (const item of catalogItems) {
if (item.featuredAsset) {
await assetHydrateService.hydrateAsset(ctx, item.featuredAsset);
}
const metaTags = item.customFields?.['metaTags'];
result.push({
path: `/category/${item.slug}`,
title: item.name,
subTitle: item.description,
keywords: !metaTags
? []
: Array.isArray(metaTags)
? metaTags
: [metaTags],
icon: item.featuredAsset?.preview,
});
}
return result;
}
}
cmdk-search-product.example.ts
import { FulltextSearchService } from '@hyperse-hub/hps-plugin-smart-search';
import { AssetHydrateService } from '@hyperse-hub/hps-plugin-storage';
import type { Asset, ID, RequestContext } from '@vendure/core';
import type { CmdkSectionActions } from '@hyperse-hub/hps-plugin-cmdk';
import { AbstractCmdkSearchStrategy } from '@hyperse-hub/hps-plugin-cmdk';
export class ExampleCmdkSearchProduct extends AbstractCmdkSearchStrategy {
readonly section = 'products';
readonly priority = 20;
getActions(
ctx: RequestContext,
_channelId: ID,
_languageCode: string
): Promise<CmdkSectionActions[]> {
return this.constructActions(ctx);
}
private async constructActions(
ctx: RequestContext
): Promise<CmdkSectionActions[]> {
const searchService = this.injector.get(FulltextSearchService);
const assetHydrateService = this.injector.get(AssetHydrateService);
const { items } = await searchService.search(
ctx,
{
groupByProduct: false,
take: 100000,
},
true
);
const result: CmdkSectionActions[] = [];
for (const item of items) {
if (item.productAsset) {
await assetHydrateService.hydrateAsset(ctx, item.productAsset as Asset);
}
result.push({
path: `/product/${item.slug}`,
title: item.sku,
subTitle: item.productName,
icon: item.productAsset?.preview,
keywords: [],
});
}
return result;
}
}
example-cmdk-search-strategies.ts
import type { CmdkSearchStrategy } from '@hyperse-hub/hps-plugin-cmdk';
import { ExampleCmdkSearchCatalog } from './cmdk-search-catalog.example.js';
import { ExampleCmdkSearchProduct } from './cmdk-search-product.example.js';
export function exampleCmdkSearchStrategies(): CmdkSearchStrategy[] {
return [new ExampleCmdkSearchCatalog(), new ExampleCmdkSearchProduct()];
}
Compatibility
- Vendure plugin
compatibility: >=3.0.0
- Node:
>=20.0.0
License
GPL-3.0-only — see package.json.