Skip to content

CEXT-6160: store commerce instance on association and expose retrieval helpers#511

Open
vinayrao2000 wants to merge 24 commits into
mainfrom
CEXT-6160-commerce-system-config-on-association
Open

CEXT-6160: store commerce instance on association and expose retrieval helpers#511
vinayrao2000 wants to merge 24 commits into
mainfrom
CEXT-6160-commerce-system-config-on-association

Conversation

@vinayrao2000

Copy link
Copy Markdown
Contributor

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:

Helper Returns Use when
getCommerceInstance(params) { baseUrl, env } You need the raw stored values (URL/env)
getCommerceClient(params) AdobeCommerceHttpClient (ready to call APIs) You want to call Commerce APIs directly

Both throw AppNotAssociatedError if the app isn't currently associated.

Storage (via new ./system submodule in @adobe/aio-commerce-lib-config):

  • Two-layer pattern: state (cache) → files (persistent source of truth)
  • Generic getSystemConfigByKey / setSystemConfigByKey API — reusable for other "system-level" config beyond association
  • Key namespace: system.association for this feature

New 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:

  1. Read auth params from params
  2. Read the base URL from env vars or a custom-managed config
  3. Manually construct AdobeCommerceHttpClient
  4. Handle missing config in every action

This 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:

const client = await getCommerceClient(params)

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.ts covers set/get/clear, state-as-cache + files-as-source-of-truth, null clears, type safety
  • lib-app:
    • app-not-associated-error.test.ts — class, message, options
    • association-repository.test.ts — round-trip via system config
    • actions/association.test.ts — router (POST/DELETE), validation, logger middleware
    • index.test.tsgetCommerceInstance and getCommerceClient happy + error paths

Typecheck: clean across all 13 packages (pnpm typecheck)

Lint: clean (pnpm check:ci)

E2E (in commerce-app-purchase-approval demo app):

  • Associate via App Management UI → POST .../association → 200 OK
  • Invoke commerce-config-demo action → { associated: true, instance: { baseUrl, env }, clientType: "AdobeCommerceHttpClient" }
  • Unassociate via App Management UI → DELETE .../association → 204 No Content
  • Invoke demo action again → { associated: false, ... }
  • Re-associate after unassociate → flow works cleanly

Both helpers verified working end-to-end through deployed actions.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have read the DEVELOPMENT document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Changesets

  • aio-commerce-lib-app — minor (new helpers + new action; additive only)
  • aio-commerce-lib-config — minor (new ./system subpath export; additive only)

Affected packages

Package Change type
@adobe/aio-commerce-lib-app new public API: getCommerceInstance, getCommerceClient, AppNotAssociatedError, association runtime action + scaffolding template
@adobe/aio-commerce-lib-config new public API: ./system subpath export with getSystemConfigByKey / setSystemConfigByKey

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-bot

changeset-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 305860a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@adobe/aio-commerce-lib-app Minor
@adobe/aio-commerce-lib-config Minor

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

@github-actions github-actions Bot added with-changeset The PR contains a Changeset file. pkg: aio-commerce-lib-config Includes changes in `packages/aio-commerce-lib-config` pkg: aio-commerce-lib-app Includes changes in `packages/aio-commerce-lib-app` labels Jun 9, 2026
Comment thread packages/aio-commerce-lib-app/docs/usage.md Outdated
Comment thread packages/aio-commerce-lib-app/source/actions/association/schema.ts Outdated
const logger = getLogger("@adobe/aio-commerce-lib-config:system-repository");
try {
const state = await getSharedState();
await state.put(key, payload);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add a TTL here? As I know a default value is 24 hours

"enum": ["saas", "paas"]
}
},
"required": ["baseUrl", "env"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be minor instead of patch

export async function main(params) {
try {
const client = await getCommerceClient(params);
return {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use preset ok() from the sdk

};
} catch (error) {
if (error instanceof AppNotAssociatedError) {
return {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't we used flavour in other parts of the sdk? we should align this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check if createCombinedStore can be used instead of implementing your own cache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: aio-commerce-lib-app Includes changes in `packages/aio-commerce-lib-app` pkg: aio-commerce-lib-config Includes changes in `packages/aio-commerce-lib-config` with-changeset The PR contains a Changeset file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants