@izara_frontend/service-schemas
v1.0.6
Published
Quick guide — initialize the schema tools and use the provided hooks (e.g. `getObjectLinks`).
Downloads
310
Readme
@izara_frontend/service-schemas
Quick guide — initialize the schema tools and use the provided hooks (e.g. getObjectLinks).
Requirement
- react: "^18.3.1",
- react-dom: "^18.3.1"
Purpose
- This package exports helpers and hook-like functions to fetch and work with service object schemas (links, object schemas, validators, etc.).
- It expects you to initialize a small runtime integration once (injecting reducers and the posts API) and then use the exported functions inside components or utility modules.
Install
- This package is intended to be consumed from your monorepo packages. Example (if published):
npm install @izara_frontend/service-schemasInitialization (required once, before using hooks)
- Call
initGetSchemaToolsin a parent component or application bootstrap code. TheinitGetSchemaToolscall persists across components and only needs to be done once per app lifetime.
Example (typical place: app entry or parent module):
import { initGetSchemaTools } from "@izara_frontend/service-schemas";
// These values come from your app: an injection helper, the Redux store, and your RTK Query postsApi factory.
initGetSchemaTools({
injectReducer: injectReducerV2,
store,
createPostsApi: createPostsApiV2
});injectReducershould be a function that knows how to inject a reducer into your store (your app's implementation). If you use a helperinjectReducerV2in your repo, pass it.storeis the Redux store instance used by your app.createPostsApiis the factory that creates the postsApi used by the package; pass your app variant if you have one.
Using exported helpers (example: getObjectLinks)
- Each helper follows a small pattern: when called it returns a tuple and a callable object you can use directly.
Typical usage inside a component:
import React, { useEffect } from 'react';
import { getObjectLinks } from '@izara_frontend/service-schemas';
export default function MyComponent() {
// call at top-level of component
const [objLinksResults, getObjectLinksFn, objLinksOption] = getObjectLinks();
// objLinksResults and objLinksOption are React state objects managed by the hook
useEffect(() => {
async function load() {
// "normal" call: the helper will update objLinksResults and objLinksOption state
const [schema, errorsFound] = await getObjectLinksFn({ serviceTag: 'xx', objectType: 'xx' });
// schema and errorsFound are the results of this call
}
load();
}, []);
return <div>...render using objLinksResults...</div>;
}Direct-trigger usage via .initiate (RTK-style)
- Some helper callables expose an
.initiate(params)method. This returns a promise with the response and does NOT automatically push results into the returned React state tuple — it gives the result directly.
// using initiate to get the value directly
const [schema, errorsFound] = await getObjectLinksFn().initiate({ serviceTag: 'xx', objectType: 'xx' });
// This returns the fetched value immediately; it does not mutate objLinksResults / objLinksOption state.Difference between "normal" and .initiate()
- Normal call (calling
getObjectLinksFn(params)):- Updates the returned
objLinksResultsandobjLinksOptionReact state (so components re-render and you can read them directly). - Useful when you want to keep the result in component state and react to changes.
- Updates the returned
.initiate(params)call:- Returns the response directly (a promise). It is useful when you want a one-off result (for example inside an async handler) and don't need the returned hook state to update.
- That call is usually useful for programmatic flows or server-side usage where you only need the returned value.
Return shapes / tuple explained
- The pattern is typically:
const [resultsState, actionFn, optionsState] = someGetFunction();resultsState— React state managed by the helper: contains last response (schema, data) and flags.actionFn— a callable function you can call with params to fetch. Also may expose.initiate()for direct returns.optionsState— optional object with metadata such as isLoading, isError, status, etc.
Examples
- Normal usage (keeps result in state):
const [objLinksResults, getObjectLinksFn, objLinksOption] = getObjectLinks();
useEffect(() => {
getObjectLinksFn({ serviceTag: 'shop', objectType: 'product' });
}, []);
// read results from objLinksResults- One-off result via
.initiate(direct):
const [, getObjectLinksFn] = getObjectLinks();
async function fetchNow() {
const [schema, errorsFound] = await getObjectLinksFn().initiate({ serviceTag: 'shop', objectType: 'product' });
// use schema directly
}Notes & best practices
- Initialize
initGetSchemaToolsbefore using any of the exported helpers. A safe place is your app root or a parent component that mounts early. - Place the
getObjectXcall (thesomeGetFunction()call) at top-level of your React component (not inside conditionals) so hooks rules are respected. - Many helpers accept different params — check the helper signature you use. The package provides multiple fetch helpers (links, object schema, validators, etc.) each with their own param set.
.initiate()will not update the hook-managed state; use it when you need a single direct result.
Troubleshooting
- If results are not appearing in
resultsStateafter a normal call:- Ensure
initGetSchemaToolswas called with the correctstoreandcreatePostsApi. - Ensure the helper is used inside a component where the hook can run.
- Ensure
- If you get multiple or stale values, check cache keys and parameters passed to the helper.
