Skip to content

Documentation · ARDOR

Binding keys ​

CoreBindings defines the eleven keys used in the ARDOR container for application instances, options, providers, and features. LocalStorageKeys defines the keys used in browser storage for authentication state.

ts
import { CoreBindings, useInjectable, type IDataProvider } from '@venizia/ardor';

export const useRestDataProvider = () => {
  return useInjectable<IDataProvider>({
    key: CoreBindings.DEFAULT_REST_DATA_PROVIDER,
  });
};

CoreBindings ​

CoreBindings is defined in packages/kernel/src/common/keys.ts.

ConstantValueBound byRead byStatus
APPLICATION_INSTANCE@app/application/instanceFramework (preConfigure())Services, configurations, componentsAlways bound
APPLICATION_INFO@app/application/infoFramework (preConfigure())DefaultRestDataProvider, network request tracingAlways bound
DEFAULT_AUTH_PROVIDER@app/application/auth/defaultApplication (@provide)ArdorApplicationOptional
DEFAULT_I18N_PROVIDER@app/application/i18n/defaultApplication (@provide)ArdorApplicationRequired
DEFAULT_REST_DATA_PROVIDER@app/application/data/rest/defaultApplication (@provide)ArdorApplication, DefaultAuthProvider, useRequestHeaderLocaleRequired
AUTH_PROVIDER_OPTIONS@app/application/options/authApplication (@provide)DefaultAuthProviderRequired when DEFAULT_AUTH_PROVIDER is bound
DEFAULT_AUTH_SERVICE@app/application/service/auth/defaultApplication (@provide)DefaultAuthProvider; DefaultRestDataProvider (optional)Required when DEFAULT_AUTH_PROVIDER is bound
REST_DATA_PROVIDER_OPTIONS@app/application/options/rest/dataApplication (@provide)DefaultRestDataProviderRequired by DefaultRestDataProvider
I18N_PROVIDER_OPTIONS@app/application/options/i18nApplication (@provide)DefaultI18nProviderRequired by DefaultI18nProvider
FEATURES@app/application/featuresFramework (preConfigure())ArdorApplication, DefaultI18nProviderAlways bound
PERMISSION_PROVIDER@app/application/permission/providerApplication (@provide)DefaultAuthProvider, canAccessOptional

For binding order in the application lifecycle, see The bind order in Configuration and Application.

LocalStorageKeys ​

LocalStorageKeys (defined in packages/kernel/src/common/keys.ts) provides keys for browser local storage.

ConstantValueRole
KEY_AUTH_TOKEN@app/auth/tokenSerialized authentication token record (IAuthTokenRecord)
KEY_AUTH_IDENTITY@app/auth/identityStored user identity
KEY_AUTH_PERMISSION@app/auth/permissionStored user roles, a JSON array of names
PREFIX_AUTH@app/auth/Prefix of the session keys; DefaultAuthService.cleanUp removes everything under it
PREFIX_STORE@app/store/The namespace for the data of the signed-in user; sign-out removes everything under it. Keep what must outlive a sign-out (a theme) under @app/prefs/<name>, which no constant names

Each key belongs to LocalStorageKeys.SCHEME_SET and can be verified with LocalStorageKeys.isValid(value).

ts
import { LocalStorageKeys } from '@venizia/ardor';

export const readStoredToken = (): string | null => {
  return localStorage.getItem(LocalStorageKeys.KEY_AUTH_TOKEN);
};

Two applications on one origin ​

Browser storage is shared by every page of an origin. Two ARDOR applications served from one origin, say under /shop and /blog, would share one session and one @app/store/ namespace. Give each an id in its application info (IApplicationInfo.id, letters, digits, ., _ and -):

ts
const info = { name: 'Shop', version: '1.0.0', description: 'The shop', id: 'shop' };

LocalStorageKeys.scoped({ id }) answers the keys of one application: with the id shop the session is @app/auth/shop/token, @app/auth/shop/identity and @app/auth/shop/permission, and the store prefix is @app/store/shop/. DefaultAuthService takes the id from CoreBindings.APPLICATION_INFO and exposes the keys as authService.keys; its cleanUp, the auth provider's sign-out sweep and the request service's token all use them, so a sign-out in one application leaves the other's session and data alone. Name your own store keys with authService.keys.storePrefix. Without an id the keys are the ones in the table above, so an application that is alone on its origin changes nothing. An application without an id on a shared origin still sweeps all of @app/auth/ and @app/store/, the others' keys included: give every application of an origin an id. The session keys all stay under @app/auth/, which BrowserStorage refuses to write.

Key resolution in useInjectable ​

useInjectable accepts keys from the TUseInjectableKeys union:

  • TUseInjectableKeysDefault: union of all string values on CoreBindings.
  • IUseInjectableKeysOverrides: interface augmented to register custom keys.
  • TUseInjectableKeys: TUseInjectableKeysDefault | keyof IUseInjectableKeysOverrides.

To resolve custom keys such as service namespaces (services.<ClassName>), augment IUseInjectableKeysOverrides:

tsx
import { useInjectable } from '@venizia/ardor';

declare module '@venizia/ardor' {
  interface IUseInjectableKeysOverrides {
    'services.UserService': true;
  }
}

class UserService {
  list() {
    return [] as Array<string>;
  }
}

export const useUserService = () => {
  return useInjectable<UserService>({ key: 'services.UserService' });
};