@hyperse-hub/hps-plugin-core
Vendure plugin providing shared core capabilities for Hyperse Hub plugins, including:
- reCAPTCHA v3 verification helper
- Facet/FacetValue helpers with caching
- Next.js SSG cache revalidation helper (Admin API)
- Admin helper to query customer session token (SuperAdmin)
- i18n translation resource registration
- Exchange rates (OpenExchangeRates.org), persisted in Vendure Settings Store, daily scheduled sync, and Admin mutation to refresh manually
Install
yarn add @hyperse-hub/hps-plugin-core
Usage
Register the plugin in your Vendure config:
import { HpsPluginCore } from '@hyperse-hub/hps-plugin-core';
export const config = {
plugins: [
HpsPluginCore.init({
devMode: process.env.NODE_ENV !== 'production',
reCaptchaV3SecretKey: process.env.RECAPTCHA_V3_SECRET_KEY ?? '',
exchangeRateOptions: {
exchangeRateAppId: process.env.OPENEXCHANGERATES_APP_ID ?? '',
},
ttlCache: {
variantFacets: 5 * 60 * 1000,
},
nextCacheOptions: {
nextFrontEndPoint:
process.env.NEXT_FRONTEND_ENDPOINT ?? 'https://www.hyperse.net',
nextCleanSSGCacheToken: process.env.NEXT_REVALIDATE_TOKEN ?? '',
},
cacheType: 'memory',
cacheOptions: {
cacheSize: 10_000,
namespace: 'hyperse',
},
}),
],
};
Configuration
HpsPluginCore.init() accepts (see PluginInitOptions in src/types.ts):
| Option | Description |
|---|
devMode | When true: reCAPTCHA verification is skipped (mock success); scheduled exchange rate sync is skipped. Manual Admin refreshExchangeRate still runs. |
reCaptchaV3SecretKey | Server-side secret for reCAPTCHA v3. |
exchangeRateOptions.exchangeRateAppId | OpenExchangeRates App ID. Required for fetching rates; leave empty only if you do not use exchange features. |
ttlCache.variantFacets | Cache TTL (ms) for variant facet values lookup. |
nextCacheOptions | Base URL and token for Next.js POST /api/revalidate. |
cacheType | 'memory' | 'redis' | 'sql'. When or , the plugin registers the matching Vendure cache plugin. |
Settings Store registration
The plugin merges into VendureConfig.settingsStoreFields a global, read-only (for GraphQL) field:
- Namespace:
hpsCore
- Field:
currencyExchangeRates
- Full key:
hpsCore.currencyExchangeRates (also exported as SETTINGS_STORE_CURRENCY_EXCHANGE_RATES_KEY)
Programmatic updates use SettingsStoreService.set() (for example inside ExchangeRatesService); the Admin Settings Store UI treats the field as read-only.
If your app already defines settingsStoreFields.hpsCore, this plugin merges its field and avoids duplicating currencyExchangeRates.
Scheduler
On load, the plugin pushes a scheduled task into config.schedulerOptions.tasks:
| Property | Value |
|---|
| id | refresh-exchange-rate-task |
| schedule | Daily at 00:00 (server timezone) |
| behavior | Calls ExchangeRatesService.syncSiteCurrencyRate with non-forced sync, so devMode: true skips the job. |
Vendure’s scheduler can run duplicate work on multiple server instances; see vendure#1425 if you need stronger single-flight behavior.
What this plugin provides
GraphQL extensions
Both Admin API and Shop API are extended with:
RecaptchaInput: token: String!, action: String
This package only provides the shared input type; actual usage depends on downstream resolvers/mutations.
Admin API is additionally extended with:
- Query:
queryCustomerSessionToken(email: String, customerId: ID): String
- Requires
Permission.SuperAdmin
- Returns the user session token (if any) for the given customer
- Mutation:
revalidateSlug(slug: String!): Boolean!
- Requires
Permission.UpdateCatalog
- Calls the Next.js revalidation endpoint configured via
nextCacheOptions
- Mutation:
refreshExchangeRate: Boolean!
- Requires
Permission.UpdateCatalog
- Fetches latest rates from OpenExchangeRates and writes the payload to Settings Store (
hpsCore.currencyExchangeRates)
- Uses a forced sync path, so it still runs when
devMode is true (useful for local testing)
Exchange rates
- Source: OpenExchangeRates.org
latest.json (USD-based by default).
- Persistence: JSON payload stored under
hpsCore.currencyExchangeRates via Vendure SettingsStoreService.
- In-memory cache: Responses are cached through Vendure
CacheService (TTL ~12h per plugin setup) to limit API calls.
- Legacy: Older deployments may have stored the same data on the
Setting entity with key currency_exchange_rates; that is not read by this plugin. Migrate the value into Settings Store if you still rely on historical rows.
ExchangeRatesService (DI):
syncSiteCurrencyRate(ctx, forceUpdate?): fetch and persist. When forceUpdate is false and devMode is true, returns early without calling the API.
getCurrentExchangeRates(ctx): returns ExchangeRateApiResult (typed in src/types.ts). Prefers the Settings Store snapshot from the last successful sync (hpsCore.currencyExchangeRates); if none is stored or the payload is invalid, falls back to getLatestRates() (OpenExchange + in-memory cache).
getLatestRates(base?, refresh?): returns ExchangeRateApiResult from OpenExchangeRates only (USD base by default). Uses the plugin HTTP cache unless is .
Public constant SETTINGS_STORE_CURRENCY_EXCHANGE_RATES_KEY is re-exported from the package entry for custom tooling or direct reads via SettingsStoreService.get().
Services
All services are available via Nest/Vendure DI when the plugin is installed.
GoogleRecaptchaService
verify(token: string, secretKey?: string): verifies a client token via Google reCAPTCHA v3.
- Uses
secretKey parameter if provided, otherwise reCaptchaV3SecretKey from plugin options.
- When
devMode: true, it returns a successful mock response (and logs a debug message).
- Uses a request timeout (10s).
CacheRevalidateService
revalidateNextCache(ctx, issueSlug, includeAllSites?): calls Next.js POST /api/revalidate
- Sends
x-next-purge-token header using nextCleanSSGCacheToken
- When failing and
devMode: false, it logs a structured HTTP error summary
- Uses a request timeout (10s)
FacetValueHelpService
Helpers around product/variant facet values:
productVariantFacetValues(ctx, productVariantId): returns merged facet values (product + variant) with caching (channel-aware cache key).
refreshedProductVariantFacetValues(ctx, productVariantId): same but without cache.
orderLineHasFacetValues(ctx, orderLine, facetValueIds): checks if order line contains all facet values.
orderLineAgainstAnyOfFacetValues(ctx, orderLine, facetValueIds): checks if order line contains any facet value; empty list returns false.
orderLineContainAnyOfFacetValues(ctx, orderLine, facetValueIds): checks if order line contains any facet value; empty list returns true.
- Facet relation hydration is done in a single batched query (avoids N+1).
i18n resources
I18nTranslationService is used on bootstrap to register i18nResources (currently en and zh).
Development
From repo root:
yarn workspace @hyperse-hub/hps-plugin-core build
yarn workspace @hyperse-hub/hps-plugin-core test
yarn workspace @hyperse-hub/hps-plugin-core lint