Gathering detailed insights and metrics for @parli/sdk
Gathering detailed insights and metrics for @parli/sdk
Gathering detailed insights and metrics for @parli/sdk
Gathering detailed insights and metrics for @parli/sdk
npm install @parli/sdk
Typescript
Module System
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
This exposes the Lustre API as a series of React hooks in a type-safe way. It will automatically account for routing and request batching, and users of this SDK should avoid trying to manually optimize requests. If anything appears not optimal, please file a bug rather than attempting to work around it.
This package follows semantic versioning practices.
Anything not exported from the root-level package IS NOT considered part of the public API, and may be unstable. Attempting to "reach in" deeper to the package is not supported in any way, and has no guarantees about following SemVer.
Additionally, the type
field on identifiers is considered a private implementation detail and is also not covered by SemVer.
See the section on identifers below.
In order to set up the necessary React Context, the Provider must be registered:
1// index.tsx or somewhere else near the package root 2import React from 'react' 3import ReactDOM from 'react-dom' 4import { ApiProvider } from '@parli/sdk' // Add this 5import App from './App' 6 7// Replace this 8// ReactDOM.render(<App />, document.getElementById('root')) 9// With this: 10ReactDOM.render(<ApiProvider><App /></ApiProvider>, document.getElementById('root'))
The API hooks WILL NOT FUNCTION without this provider registered into the rendering tree.
Additionally, an API key SHOULD be set for applications that must use one:
1import { setApiKey } from '@parli/sdk' 2setApiKey('your-access-token')
Important:
Any function in the package starting with use
is a React Hook, and its usage is strictly required to follow the Rules of Hooks (RoH).
As they are the primary interaction with this package, developers should be familiar with the RoH and are strongly encouraged to set up the RoH linters.
In general, avoid trying to be clever about request management or other batching. This library will optimize request handling, and if you find yourself trying to outsmart it, it's probably a bug - please file one!
import { queries } from '@parli/sdk'
All of the functions in this structure correspond to the various Queries in the API. They are all type-safe, and parameterized in a way to avoid type juggling. Queries will be routed automatically to the correct back-end service, and batched appropriately.
The names do not necessarily correspond 1:1 with the API names, especially for "either-or" queries (e.g. graph_search
accepts question
or context
but not both, so two methods will be exposed to handle those cases)
The queries
functions return the body to be passed to the useQuery
hook, not the actual response.
The formatted outbound request is not intended for use in any other place.
See its documentation below.
useEntity
Takes an entity identifer and returns the entity, or undefined
if the entity has not been loaded yet.
A request to load the entity will automatically run.
Generally, the identifer will be the return type of something else - either a query response or the property of some other entity.
useEntities
Takes a list of entity identifiers and returns a list of loaded entities.
Any entities not yet loaded will be requested.
For convenience with the mechanics of useQuery
's not loaded state, this method also accepts undefined
.
Note:
If multiple entity Id types are passed (e.g. a Brand Id and a Spec Id), the return type will infer that the entities can be any of those corresponding entities (e.g. Array<Entities.Brand | Entities.Spec>
).
In most circumstances, this is an anti-pattern and should be avoided.
useQuery
Takes a typed Query (i.e. the return value from one of the queries
functions) and returns the response from the API.
This will deal with the client hashes via the input parameters, so you don't need to think about that.
Entities and many queries work with identifers.
Due to limitations of Javascript (and therefore typescript), type information of identifers needs to be manually propagated - this is done as a tiny object of { id: string, type: string }
.
Day to day, this can be mostly ignored, as the hooks accept this structure and know what to do with it.
You SHOULD use identifers as React component props, and pass them around as a whole object:
1import React from 'react' 2import { Entities } from '@parli/sdk' 3 4const ParentComponent: React.FC = () => { 5 const brandId = ... // query result, field from property, etc 6 return ( 7 <ChildComponent brandId={brandId} /> 8 ) 9} 10 11interface ChildProps { 12 brandId: Entities.Brand['id'] 13} 14const ChildComponent: React.FC<ChildProps> = ({ brandId }) => { 15 // const brand = useEntity(brandId) 16 // ... 17}
However, when dealing with lists and keys, it will be necessary to interact with the internals:
1// in a React FC 2const entities: Entities.Brand[] = [] // e.g. result of a query or useEntities 3return ( 4 <ul> 5 {entities.map(brand => <li key={brand.id.id}>{brand.name}</li>)} 6 </ul> 7)
If you find yourself inspecting the contents of the Id
structure anywhere else, you are probably doing something wrong.
1import React from 'react' 2 3import { 4 Entities, 5 queries, 6 useEntities, 7 useEntity, 8 useQuery, 9} from '@parli/sdk' 10 11const MyComponent: React.FC = () => { 12 const familyIds = useQuery(queries.uncategorizedFamiliesQueue()) 13 const families = useEntities(familyIds) 14 15 return ( 16 <ul> 17 {families.map(fam => <li key={fam.id.id}>{fam.name}</li>)} 18 </ul> 19 ) 20} 21 22export default MyComponent
The type information for Entities is explicitly exported, for the common use-case of adding type annotations to a component.
1import React from 'react' 2import { Entities } from '@parli/sdk' 3 4interface Props { 5 brandId: Entities.Brand['id'] 6} 7const MyComponent: React.FC<Props> = ({ brandId }) => { 8 // ... 9}
At this time, query responses are not directly exported for similar use.
However, type information will still be attached to the return value of useQuery
.
No vulnerabilities found.
No security vulnerabilities found.