@shipengine/alchemy 5.0.12 → 5.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Alchemy
|
|
2
|
+
|
|
3
|
+
`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](https://shipengine.github.io/shipengine-openapi/).
|
|
4
|
+
|
|
5
|
+
The library makes opinionated choices about API access, state management, language translations[^2] and UI foundations.
|
|
6
|
+
|
|
7
|
+
## Features:
|
|
8
|
+
|
|
9
|
+
- Available to all descendants of the `AlchemyProvider`:
|
|
10
|
+
|
|
11
|
+
- A batteries-included client for accessing ShipeEngine API endpoints with a shared cache and lifecycle management via hooks from [@shipengine/react-api](https://www.npmjs.com/package/@shipengine/react-api).
|
|
12
|
+
- A [ShipEngine Giger](https://www.shipengine.com/docs/elements/elements-guide/#theming) theme, which can be re-defined at instantiation via the `themeConfig` prop and accessible on the Emotion `css` callback prop, and icons via Giger's `Icon` component.
|
|
13
|
+
|
|
14
|
+
- Available to components created via the `alchemy.createElement` factory:
|
|
15
|
+
- Fully isolated styles. CSS styles are reset at the boundary of each Element, ensuring that they are self-contained and unaffected by the host application’s styles.
|
|
16
|
+
- Fully isolated language translations. A non-global instance of `i18next` is provided for each Element. Individual translations can be overriden at the Element's point-of-use via the optional `resources` prop.
|
|
17
|
+
- A configurable `ErrorBoundary`.
|
|
18
|
+
|
|
19
|
+
## Basic Usage
|
|
20
|
+
|
|
21
|
+
[!IMPORTANT]
|
|
22
|
+
In addition to `@shipengine/alchemy`, you _must_ also add `@shipengine/react-api` and `@shipengine/js-api` to your project's dependencies.
|
|
23
|
+
|
|
24
|
+
- A single `AlchemyProvider` should be rendered near the top of the application's component hiearchy
|
|
25
|
+
- The `createElement` factory is used to create stylistically isolated components that will utilize the AlchemyProvider's API client and theme.
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import alchemy, { AlchemyProvider, useListShipments } from "@shipengine/alchemy";
|
|
29
|
+
import { themeConfig } from "../themeConfig";
|
|
30
|
+
|
|
31
|
+
type MyComponentProps = {
|
|
32
|
+
myProp: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const MyComponent = ({ myProp }: MyComponentProps) => {
|
|
36
|
+
const { data, isLoading, error } = useListShipments();
|
|
37
|
+
|
|
38
|
+
if (isLoading) return <div>Loading...</div>;
|
|
39
|
+
if (error) return <div>{error.message}</div>;
|
|
40
|
+
|
|
41
|
+
console.log(data);
|
|
42
|
+
// > an object matching the response schema of https://shipengine.github.io/shipengine-openapi/#operation/list_shipments
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<div
|
|
46
|
+
css={(theme) => ({
|
|
47
|
+
/* ... */
|
|
48
|
+
})}
|
|
49
|
+
>
|
|
50
|
+
Retrieved {data.shipments.length} shipments
|
|
51
|
+
</div>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Your own component that the ErrorBoundary will display when an error occurs
|
|
56
|
+
const MyErrorFallback = ({ error }) => <div>Whoops...</div>;
|
|
57
|
+
|
|
58
|
+
const MyElement = alchemy.createElement(
|
|
59
|
+
MyComponent,
|
|
60
|
+
MyErrorFallback,
|
|
61
|
+
// optional configuration object
|
|
62
|
+
{
|
|
63
|
+
// applied to the container
|
|
64
|
+
css: (theme) => ({
|
|
65
|
+
// ...
|
|
66
|
+
}),
|
|
67
|
+
|
|
68
|
+
// i18next translations
|
|
69
|
+
resources: {
|
|
70
|
+
// ...
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const MyApp = () => {
|
|
76
|
+
return (
|
|
77
|
+
<AlchemyProvider getToken={() => "your-platform-token"} themeConfig={themeConfig}>
|
|
78
|
+
<section>
|
|
79
|
+
Your pre-existing application
|
|
80
|
+
<div>
|
|
81
|
+
Locate the Element wherever you like
|
|
82
|
+
<MyElement
|
|
83
|
+
myProp="just a regular prop"
|
|
84
|
+
// optional translations override prop added by the factory
|
|
85
|
+
resources={{}}
|
|
86
|
+
/>
|
|
87
|
+
</div>
|
|
88
|
+
</section>
|
|
89
|
+
</AlchemyProvider>
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
```
|