You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WhenyourAPIresponseincludesrelatedresources (usingthe`include`queryparameter), `jsonapi-react-tools`providestypesafety for the `included` array. This requires defining a central registry of all your resource types.
2.**CreateYourApplication's Type Registry:** Implement the `JsonApiTypeRegistry` interface, mapping the exact `type` string returned by your API to the corresponding attribute interface.
161
+
162
+
```ts
163
+
// types/Registry.ts
164
+
import { JsonApiTypeRegistry } from "@intility/jsonapi-react-tools";
165
+
import { CompanyAttributes } from "./Company";
166
+
import { LocationAttributes } from "./Location";
167
+
168
+
// Add all resource types that can appear in 'data' or 'included'
// Add checks for other included types if necessary
210
+
});
211
+
212
+
return (
213
+
<ul>
214
+
{response.data.map((company) => (
215
+
<li key={company.id}>
216
+
{company.attributes.companyName}
217
+
{/* You can now safely look up and display related data */}
218
+
</li>
219
+
))}
220
+
</ul>
221
+
);
222
+
};
223
+
```
224
+
225
+
Bydefiningthe`AppTypeRegistry`andusingthe`isResourceType`typepredicate, you gain full type safety when working with related resources included in your API responses.
0 commit comments