Alchemy
shipengine/alchemy is a toolkit for building re-usable, fully-featured, data-connected and cross-compatible React components (“Elements”)[^1] for use in applications that utilize the ShipEngine API.
The library makes opinionated choices about API access, state management, language translations[^2] and UI foundations.
Features:
Basic Usage
[!IMPORTANT]
In addition to @shipengine/alchemy, you must also add @shipengine/react-api and @shipengine/js-api to your project's dependencies.
- A single
AlchemyProvider should be rendered near the top of the application's component hiearchy
- The
createElement factory is used to create stylistically isolated components that will utilize the AlchemyProvider's API client and theme.
import alchemy, { AlchemyProvider, useListShipments } from "@shipengine/alchemy";
import { themeConfig } from "../themeConfig";
type MyComponentProps = {
myProp: string;
};
const MyComponent = ({ myProp }: MyComponentProps) => {
const { data, isLoading, error } = useListShipments();
if (isLoading) return <div>Loading...</div>;
if (error) return <div>{error.message}</div>;
console.log(data);
return (
<div
css={(theme) => ({
/* ... */
})}
>
Retrieved {data.shipments.length} shipments
</div>
);
};
const MyErrorFallback = ({ error }) => <div>Whoops...</div>;
const MyElement = alchemy.createElement(
MyComponent,
MyErrorFallback,
{
css: (theme) => ({
}),
resources: {
},
}
);
const MyApp = () => {
return (
<AlchemyProvider getToken={() => "your-platform-token"} themeConfig={themeConfig}>
<section>
Your pre-existing application
<div>
Locate the Element wherever you like
<MyElement
myProp="just a regular prop"
// optional translations override prop added by the factory
resources={{}}
/>
</div>
</section>
</AlchemyProvider>
);
};