CEXT-6160: store commerce instance on association and expose retrieval helpers#511
CEXT-6160: store commerce instance on association and expose retrieval helpers#511vinayrao2000 wants to merge 24 commits into
Conversation
Drop the Associated prefix from the public helpers per Daniel's review feedback - cleaner, more discoverable API.
… CEXT-6160-commerce-system-config-on-association
Per Ivan's review feedback - 'not associated' is exceptional state, throwing keeps happy-path code clean without forcing null checks at every call site.
Per Daniel's review feedback - puts SDK-managed config under a system.* namespace, cleanly separated from user-defined configuration.* keys.
lib-config now exposes generic system config primitives (setSystemConfigByKey/getSystemConfigByKey), and lib-app has the typed association wrappers on top. Drops the delete operation - clearing is done by setting null.
Typed association helpers (setAssociationData, getAssociationData, clearAssociationData) live in lib-app per the layered design, not lib-config. lib-config only exposes the generic system config primitives.
lib-config provides persistence via lib-files with lib-state as a performance cache. Cache expiry triggers automatic re-fetch from files, so no TTL refresh logic is needed in our code.
Adds setSystemConfigByKey / getSystemConfigByKey primitives under modules/configuration/system/. Uses lib-files for persistent storage with lib-state as a performance cache. Passing null to set clears the entry. Exposed via the new @adobe/aio-commerce-lib-config/system subpath export. Domain-agnostic - operates purely on opaque keys and values. Will be used by lib-app's association module via the public subpath export.
- AppNotAssociatedError class (extends CommerceSdkErrorBase) - Internal association module: setAssociationData / getAssociationData / clearAssociationData (calls into lib-config system submodule) - Public root entrypoint: getCommerceInstance and getCommerceClient helpers, both throw AppNotAssociatedError when no data is stored - Add root export to package.json and tsdown.config.ts
- New association runtime action with POST / and DELETE / handlers,
protected with require-adobe-auth: true
- POST / accepts { commerceBaseUrl, commerceEnv } and stores it via
the internal association module
- DELETE / clears the stored data
- Add association.js.template for app scaffolding
- Update buildAppManagementExtConfig to deploy the association action
alongside app-config (always deployed, no feature gating)
- Update OpenAPI spec with the new POST /association and DELETE /association
routes, bump info.version to 1.1.0
- Update test fixtures and config test expectations
- Unit tests for AppNotAssociatedError, association repository, public helpers (getCommerceInstance / getCommerceClient), and the association runtime action handlers (POST / and DELETE /) - changesets for minor bumps in lib-app and lib-config - Document the new helpers in lib-app usage.md
🦋 Changeset detectedLatest commit: 305860a The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| const logger = getLogger("@adobe/aio-commerce-lib-config:system-repository"); | ||
| try { | ||
| const state = await getSharedState(); | ||
| await state.put(key, payload); |
There was a problem hiding this comment.
Shouldn't we add a TTL here? As I know a default value is 24 hours
| "enum": ["saas", "paas"] | ||
| } | ||
| }, | ||
| "required": ["baseUrl", "env"] |
There was a problem hiding this comment.
Is there a reason why the response body is different from the request body? I expected to see commerceBaseUrl or baseUrl in both.
| "title": "App Management API", | ||
| "description": "REST API for managing Adobe Commerce App Builder apps: resolving app configuration, managing configuration values, running the installation and uninstallation lifecycle, exposing the Admin UI SDK registration, and managing the scope tree.", | ||
| "version": "1.1.0", | ||
| "version": "1.1.1", |
There was a problem hiding this comment.
should be minor instead of patch
| export async function main(params) { | ||
| try { | ||
| const client = await getCommerceClient(params); | ||
| return { |
There was a problem hiding this comment.
use preset ok() from the sdk
| }; | ||
| } catch (error) { | ||
| if (error instanceof AppNotAssociatedError) { | ||
| return { |
There was a problem hiding this comment.
use preset badRequest from the sdk
| "The 'commerceBaseUrl' field must be a valid absolute URL (e.g., 'https://my-store.example.com')", | ||
| ), | ||
| ), | ||
| commerceEnv: v.picklist(["saas", "paas"]), |
There was a problem hiding this comment.
didn't we used flavour in other parts of the sdk? we should align this
There was a problem hiding this comment.
can you check if createCombinedStore can be used instead of implementing your own cache
Description
Adds a new feature that stores the Commerce instance details (Base URL + deployment type) when an app is associated with a Commerce instance, and exposes typed async helpers so any runtime action can call Commerce APIs without custom storage setup.
Two new helpers in
@adobe/aio-commerce-lib-app:getCommerceInstance(params){ baseUrl, env }getCommerceClient(params)AdobeCommerceHttpClient(ready to call APIs)Both throw
AppNotAssociatedErrorif the app isn't currently associated.Storage (via new
./systemsubmodule in@adobe/aio-commerce-lib-config):getSystemConfigByKey/setSystemConfigByKeyAPI — reusable for other "system-level" config beyond associationsystem.associationfor this featureNew runtime action auto-scaffolded into apps:
POST /association— store{ commerceBaseUrl, commerceEnv }(called by App Management UI on associate)DELETE /association— clear stored data (called on unassociate)Related Issue
CEXT-6160
Motivation and Context
Before this change, every action that wanted to call Commerce APIs had to:
paramsAdobeCommerceHttpClientThis pushed boilerplate and inconsistent storage patterns into every app. Customers had no standard way to know which Commerce instance their app was associated with at runtime.
After this change, one line in any action gets a configured client:
The SDK owns where association data is stored (state + files), how it's read, and how it propagates from the association event to runtime actions.
How Has This Been Tested?
Unit tests (~1,865 tests passing across the monorepo):
lib-config:system-repository.test.tscovers set/get/clear, state-as-cache + files-as-source-of-truth, null clears, type safetylib-app:app-not-associated-error.test.ts— class, message, optionsassociation-repository.test.ts— round-trip via system configactions/association.test.ts— router (POST/DELETE), validation, logger middlewareindex.test.ts—getCommerceInstanceandgetCommerceClienthappy + error pathsTypecheck: clean across all 13 packages (
pnpm typecheck)Lint: clean (
pnpm check:ci)E2E (in
commerce-app-purchase-approvaldemo app):POST .../association→ 200 OKcommerce-config-demoaction →{ associated: true, instance: { baseUrl, env }, clientType: "AdobeCommerceHttpClient" }DELETE .../association→ 204 No Content{ associated: false, ... }Both helpers verified working end-to-end through deployed actions.
Types of changes
Checklist:
Changesets
aio-commerce-lib-app— minor (new helpers + new action; additive only)aio-commerce-lib-config— minor (new./systemsubpath export; additive only)Affected packages
@adobe/aio-commerce-lib-appgetCommerceInstance,getCommerceClient,AppNotAssociatedError, association runtime action + scaffolding template@adobe/aio-commerce-lib-config./systemsubpath export withgetSystemConfigByKey/setSystemConfigByKey