node-vtex-api icon indicating copy to clipboard operation
node-vtex-api copied to clipboard

Feature/idaas 805/tenants client alternative

Open rafarubim opened this issue 3 months ago • 0 comments

What is the purpose of this pull request?

Add LM and Catalog client methods to enable applications to replace requests to the Tenants systems (the Tenants client should be marked as deprecated from now on).

The new API client methods reflect the API interface directly, without converting data or applying business logic. Therefore, any code that depends on the Tenants client or API should adapt to the new data structure interface.

Data fetch and CRUD operations should be replaced like this:
  • Requests to /api/tenant/bindings routes should use /api/license-manager/binding routes instead (you can use this PR's new client methods as reference).
  • Requests to /api/tenant/tenants route should be replaced by inferred data or data sought from different places:
    • The id and metadata properties returned from the Tenants client were incorrectly typed, because the API did not actually return these fields.
    • slug and title correspond to the requested tenant name.
    • I (@rafarubim) am uncertain of how to replace the edition and infra fields, but I found out that these fields are mostly hard-coded by the Tenants system to return 'vtex:tier-1339' and 'VTEX'.
    • The bindings field should be sought from the License Manager API: /api/license-manager/binding/site/{tenant}. In the new client, you should use context.clients.licenseManager.listBindings({ tenant, adminUserAuthToken }) instead (more details below).
    • The defaultCurrency and defaultLocale fields should be sought from the Tenant's first sales channel, from the Catalog API: /api/catalog_system/pub/saleschannel/1. In the new client, you should use context.clients.catalog.getSalesChannel(1) (more details below).
Result from Tenants client should be replaced like this:
  • defaultLocale -> CultureInfo from context.clients.catalog.getSalesChannel(1).
  • defaultCurrency -> CurrencyCode from context.clients.catalog.getSalesChannel(1).
  • bindings[x].id -> Id from context.clients.licenseManager.listBindings().
  • bindings[x].canonicalBaseAddress -> result of concatenation:
`${Addresses[i].Host}/${Addresses[i].BasePath}`

Where Addresses is from context.clients.licenseManager.listBindings() and i corresponds to the address with Addresses[i].IsCanonical equal to true.

  • bindings[x].alternateBaseAddresses -> list with concatenation results:
`${Addresses[i].Host}/${Addresses[i].BasePath}`

Where Addresses is from context.clients.licenseManager.listBindings() and i corresponds to all addresses with Adresses[i].IsCanonical equal to false.

  • bindings[x].defaultLocale -> DefaultLocale from context.clients.licenseManager.listBindings().
  • bindings[x].supportedLocales -> from SupportedLocales from context.clients.licenseManager.listBindings().
  • bindings[x].defaultCurrency -> CurrencyCode from context.clients.catalog.getSalesChannel(1).
  • bindings[x].supportedCurrencies -> I (@rafarubim) am uncertain of a replacement, but [CurrencyCode] seems acceptable.
  • bindings[x].extraContext -> I (@rafarubim) am uncertain of a replacement, but I believe it could be related to DefaultSalesChannelId from context.clients.licenseManager.listBindings().
  • bindings[x].targetProduct -> This value should be inferred from the bindings[x].canonicalBaseAddress and bindings[x].alternateBaseAddresses fields, following this logic:
const isAdmin = canonicalBaseAddress.endsWith('myvtex.com/admin') || alternateBaseAddresses.some(addr => addr.endsWith('myvtex.com/admin'))

const targetProduct = isAdmin ? 'vtex-admin' : 'vtex-storefront'

How should this be manually tested?

This PR's changes have been locally linked, then have been added to a test branch in the vtex/auth-admin repository.

This branch adds GraphQL queries and server resolvers that correspond to a node-vtex-api client call, like this:

graphql/schema.graphql image

node/index.ts image

To test these GraphQL queries, go into this GraphiQL workspace.

You can check out the following queries/mutations to test the application:

Get Sales Channel image

List Bindings image

Get Binding image

Create Binding

⚠️ Ensure you pass valid parameters (e.g., use a valid host registered in the tenant, don't start path with a leading slash /) to avoid a 4xx response ⚠️ Wait a few minutes after deletion, as License Manager has a durable cache. image

Delete Binding

⚠️ Wait a few minutes after deletion, as License Manager has a durable cache. image

Update Binding

⚠️ Wait a few minutes after updating, as License Manager has a durable cache. image

Types of changes

  • [ ] Bug fix (a non-breaking change which fixes an issue)
  • [X] New feature (a non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Requires change to documentation, which has been updated accordingly.

rafarubim avatar Oct 28 '25 00:10 rafarubim