PalmyraStoreFactory#

@palmyralabs/palmyra-wire · PalmyraStoreFactory

Overview#

Single entry point. Configure a baseUrl, an errorHandlerFactory, and shared StoreOptions once; use the factory everywhere else to get per-endpoint stores. Each getter merges the factory’s storeOptions with the per-call options — per-call wins.

Constructor#

interface PalmyraStoreFactoryArg {
  baseUrl?:             string;                      // default '/palmyra'
  errorHandlerFactory?: APIErrorHandlerFactory;
  storeOptions?:        StoreOptions;
}

new PalmyraStoreFactory(props: PalmyraStoreFactoryArg)

Methods#

Method Signature
getGridStore getGridStore(options, endPoint, idProperty?) → GridStore<any>
getFormStore getFormStore(options, endPoint, idProperty?) → DataStore<any>
getLookupStore getLookupStore(options, endPoint, idProperty) → LookupStore<any>
getChartStore getChartStore(options, endPoint, idProperty?) → ChartStore<any>
getTreeStore getTreeStore(options, endPoint) → TreeQueryStore<any, any>

Argument types (shared across all five):

  • options: StoreOptionsendPointOptions + axiosCustomizer; merged with factory-level options.
  • endPoint: IEndPoint — a string (used for every verb) or a MultiEndPoint mapping per-verb URLs.
  • idProperty?: strings — single attribute name or tuple of attribute names for composite keys.

Example#

// src/wire/StoreFactory.ts
import { PalmyraStoreFactory } from '@palmyralabs/palmyra-wire';
import { toast } from 'react-toastify';

const errorHandler = () => (error: any) => {
  const status = error?.response?.status;
  if (status === 401) window.location.assign('/login');
  if (status === 403) toast.error("You don't have permission for this action.");
  return true;  // error consumed — do not rethrow
};

const AppStoreFactory = new PalmyraStoreFactory({
  baseUrl: '/api/palmyra',
  errorHandlerFactory: errorHandler,
});

export default AppStoreFactory;

Inject it once:

import { StoreFactoryContext } from '@palmyralabs/palmyra-wire';

<StoreFactoryContext.Provider value={AppStoreFactory}>
  <App />
</StoreFactoryContext.Provider>

Consume it per page — either through the injected context (preferred inside templates like SummaryGrid) or directly:

const users      = AppStoreFactory.getGridStore({}, '/user');
const userForm   = AppStoreFactory.getFormStore({}, { get: '/user/{id}', put: '/user/{id}', post: '/user' }, 'id');
const categories = AppStoreFactory.getLookupStore({}, '/mstCategory', 'id');