Skip to content package
@based/solidjs v0.1.4 Wraps the [`@based/client`](https://github.com/atelier-saulx/based/tree/main/packages/client) into SolidJS hooks and signals.
node any 2 deps esm types open source
overview readme
license MIT
deps 2
unpacked 21.7 kB
files 4
node any $ npm install @based/solidjs@0.1.4n npmREADME GitHub-flavored package documentation.
downloads Across all versions.
Download trend unavailable.
compatibility Runtime metadata.
install size Estimated unpacked size across 10 packages.
51.8 MB
Includes dependencies.
dependencies 3 direct dependencies.
versions Latest published Jul 12, 2024
maintainers Registry package owners.
contents @based/solidjs
Wraps the @based/client into SolidJS hooks and
signals.
import { Show } from 'solid-js'
import type { Component } from 'solid-js'
import { useBasedQuery, BasedProvider } from '@based/solidjs'
import based, { BasedClient } from '@based/client'
const client : BasedClient = based ({
env: 'myEnv' ,
org: 'myOrg' ,
project: 'myProject'
})
const
olliv
olli.vanhoja@gmail.com
LoadingData
:
Component
=
()
=>
{
return (
< div > Loading data... </ div >
)
}
const UsersList : Component = () => {
const { data, error, loading } = useBasedQuery (
'counter' ,
{
count: true ,
speed: 3000
}
)
return (
< Show when={ data ().children.length && ! loading () } fallback={ LoadingData }>
< div >
{
data ().children. map (({ id, name }) =>
< p onClick={ () => {
client. call ( 'db:delete' , { id })
} }>{ name }</ p >
)
}
</ div >
</ Show >
)
}
const App : Component = () => {
return (
< BasedProvider client={ client }>
< UsersList />
</ BasedProvider >
)
}
copy
BasedProvider Solid Component that inject the BasedClient context thought the application.
Aliasing < BasedProvider client={ client } liveStatus={ true }>
{ /*slot*/ }
</ BasedProvider > copy or (in deprecation process )
< Provider client={ client } liveStatus={ true }>
{ /*slot*/ }
</ Provider > copy
Props Parameter Type Default Description Required clientBasedClientN/A All the connection information that identifies you in the Based cloud. true liveStatusbooleanfalseOption to display the BasedLiveStatus component either as an overlay or hide it. false
Slots Name Content Required N/A Any component that you want to inject the BasedClient context. true
Emits const App : Component = () => {
return (
< BasedProvider client={ client }>
< UsersList /> // Will receive the BasedClient context injected by the BasedProvider.
</ BasedProvider >
)
} copy
useBasedContext The BasedClient object with the information about the connection with the Based server. You cal also call functions
using the client object.
Aliasing const client = useBasedContext () copy or (in deprecation process )
const client = useClient () copy
Params
Response import type { Component } from 'solid-js'
import { useBasedContext, BasedProvider } from '@based/solidjs'
import based, { BasedClient } from '@based/client'
const client : BasedClient = based ({
env: 'myEnv' ,
org: 'myOrg' ,
project: 'myProject'
})
const doSomething = () : void => {
client. call ( 'doSomething' )
}
const context : BasedClient = useBasedContext ()
const App : Component = () => {
return (
< BasedProvider client={ client }>
< button onClick={ () => doSomething () } />
< p > WebSockets URL: { context.opts.url. toString () }</ p >
</ BasedProvider >
)
} copy
useBasedQuery Subscribe when a component gets mounted / unsubscribes when a comment gets unmounted
const useBasedQuery = < N extends keyof BasedQueryMap >(
db : N ,
payload ?: BasedQueryMap [ N ][ 'payload' ],
opts ?: BasedQueryOptions ,
) : BasedQueryResult < BasedQueryMap [ N ][ 'result' ]> => {
} copy
Aliasing const { data, error, loading } = useBasedQuery ( 'myQueryFunction' ) copy or (in deprecation process )
const { data, error, loading } = useQuery ( 'myQueryFunction' ) copy
Types
BasedQueryMap
type BasedQueryMap = {
db : { payload : any ; result : any }
[key: string] : { payload : any ; result : any }
} copy
BasedQueryOptions
type BasedQueryOptions = {
persistent : boolean
} copy
BasedQueryResult
type BasedQueryResult < T > = {
loading : boolean
data ?: T
error ?: BasedError
checksum ?: number
} copy
BasedError
class BasedError extends Error {
public statusMessage ?: string
public code ?: BasedErrorCode
} copy
Params Parameter Type Default Description Required dbstring/nullN/A The query function name true payloadobjectN/A Filters and other possible mutations that you want from the query false optsobject{ persistent: false }When is true will store the cached result of a query in localStorage on the client-side. Otherwise, the cache is only in volatile memory. false
Response Key Type Always present Description loadingbooleantrueIf the query is still loading. dataanyfalseThe data coming from your filters. errorBasedErrorfalseThe BasedError object containing the statusMessage and code from your error. checksumnumberfalseA calculated value used to verify data integrity and detect errors. Each response has a unique checksum.
Basic example with static parameters:
import type { Component } from 'solid-js'
import { useBasedQuery } from '@based/solidjs'
const ProcessData : Component = () => {
// With static parameters you may destruct the returned object.
// The returned object will contain { data, loading, error, checksum } as reactive signals.
const { data, error, loading } = useBasedQuery ( 'myQueryFunction' )
if ( error ()) {
return error.message
}
return (
< Show when={ data ().text && ! loading () } fallback={< div > Loading data... </ div >}>
< div > data().text </ div >
</ Show >
)
} copy Basic example with dynamic parameters:
import { createMemo } from 'solid-js'
import type { Component } from 'solid-js'
import { useBasedQuery } from '@based/solidjs'
const ProcessData : Component = () => {
// Update these values anywhere in your component.
const [name, setName] = createSignal < string >()
const [payload, setPayload] = createSignal < any >()
// Don't destruct the returned object, will break the reactivity.
// The returned object will contain { data, loading, error, checksum } as reactive signals.
// Use as 'query().data()', 'query().loading()', 'query().error()', 'query().checksum()'.
const query = createMemo (() => useBasedQuery ( name (), payload ()))
if ( error ()) {
return error.message
}
return (
< Show when={ query (). data ().text && ! query (). loading () } fallback={< div > Loading data... </ div >}>
< div > data().text </ div >
</ Show >
)
} copy To persist the result of the query on localStorage on the client-side, pass persistent as true.
const { data: userInfo } = useBasedQuery (
'someUserInfo' ,
{
id: client.authState.userId
},
{
persistent: true
}
) copy Is also possible to pass a null value to the function name. This is useful when you have a query depending on other
data, like Auth.
const { data: userInfo } = useBasedQuery ( 'someUserInfo' , { id: client.authState.userId })
const { data } = useBasedQuery (
userInfo.preferedLanguage ? 'someQueryFunction' : null,
{
preferedLanguage: userInfo.preferedLanguage
}
) copy
useBasedAuth Check the authorization state from the Based client.
Aliasing const auth = useBasedAuth () copy or (in deprecation process )
const auth = useAuthState () copy
Params
Response Key Type Always present Description tokenstringfalseThe connection token userIdstringfalseThe connected userID. refreshTokenstringfalseIf there is a new token provided. errorstringfalseIf the auth fails, an error message will be provided. persistentstringfalseIf the auth values are being stored locally on localStorage. typestringfalseN/A.
import { useBasedAuth } from '@based/solidjs'
const IsUserAuthorized = () => {
// The returned object is a signal.
// Don't destruct the returned object, will break the reactivity.
// Use as 'auth().token', 'auth().userId', 'auth().error'...
const auth = useBasedAuth ()
if ( ! auth ().error || ! auth ().token || ! auth ().userId) {
return 'Not authorized 😭'
}
return 'Authorized! 🎉'
} copy
useBasedStatus Get the connection status from the Based client.
Aliasing const client = useBasedStatus () copy or (in deprecation process )
const client = useStatus () copy
Enums
BasedStatus
enum BasedStatus {
DISCONNECT = 'disconnect' ,
RECONNECT = 'reconnect' ,
CONNECT = 'connect'
} copy
Params
Response Key Type Always present Description connectedbooleantrueIf the connection is established or not. statusBasedStatustrueOne of the three possible status.
import { useBasedStatus } from '@based/solidjs'
const IsBasedConnected = () => {
// The returned object will contain { connected, status } as reactive signals.
const { connected } = useBasedStatus ()
if ( ! connected ()) {
return 'Not connected 😭'
}
return 'Connected! 🎉'
} copy