Pular para o conteúdo principal

StarlightClient <D>

The Starlight client is the main entry point for any requests you might want to make to Starlight’s APIs.

There’s two ways of interacting with a client: using the default client exported by the SDK or creating a new client instance using the makeStarlightClient function. Follow the provided links to learn how to use each method.

Either way you choose to interact, all clients expose the same methods and accessors that provide ways to request data. These methods and accessors return Selector or Instance objects, which are documented in the sections of the same name found in this API’s sidebar.

Index

Selector Accessors

Instance Methods

Other Methods

Selector Accessors

collections

media

  • Returns a MediaSelector.

    This is an accessor, which means that it should be used just like a common object parameter. For instance:

    import Starlight from '@starlightcms/js-sdk'

    const response = await Starlight.media.list()

    See MediaSelector for more info.


    Returns MediaSelector

models

search

  • Returns a SearchSelector.

    This is an accessor, which means that it should be used just like a common object parameter. For instance:

    import Starlight from '@starlightcms/js-sdk'

    const response = await Starlight.search.entries()

    See SearchSelector for more info.


    Returns SearchSelector

singletons

  • Returns a SingletonSelector.

    This is an accessor, which means that it should be used just like a common object parameter. For instance:

    import Starlight from '@starlightcms/js-sdk'

    const response = await Starlight.singletons.list()

    See SingletonSelector for more info.


    Returns SingletonSelector

Instance Methods

collection

  • Returns a CollectionInstance. For instance:

    import Starlight from '@starlightcms/js-sdk'

    const response = await Starlight.collection('featured-posts').items()

    See CollectionInstance for more info.

    @example

    Typing the returned CollectionInstance.

    import Starlight, { MediaObject } from '@starlightcms/js-sdk'

    const response = await Starlight.collection<MediaObject>('carousel-images').items()

    Type parameters

    • T: unknown

      The type of entity this Collection holds. You can pass this type parameter to correctly type the response of the CollectionInstance.items method.

    Parameters

    • slug: string | number

    Returns CollectionInstance<T>

model

Other Methods

configure

  • Updates the client configuration. See StarlightConfig to see all the available options.

    If you want to use the default SDK client, you need to set its workspace using this method in your application. You should run this method only once, and as soon as possible in your application lifecycle. For instance, when using React:

    // src/index.js
    import { createRoot } from "react-dom/client"
    import Starlight from '@starlightcms/js-sdk'
    import MyApp from './MyApp.js'

    // We only need to run this once. In this case, we're
    // running it before initializing our React app.
    Starlight.configure({
    workspace: '1234567890'
    })

    const rootElement = document.getElementById("root")
    const root = createRoot(rootElement)

    root.render(<MyApp />)

    Parameters

    Returns void

get

  • get<T>(path: string, params?: Record<string, undefined | string | number | boolean>, options?: RequestInit): Promise<T>
  • Returns a Promise that results in a response or throws a StarlightError. The response will be the parsed JSON data sent by the API.

    This method is used internally by all Selectors and Instances to request data to Starlight’s Query API, but it can also be used standalone if desired.

    This method automatically prefixes the given path with the API URL, but it doesn’t include a trailing slash, so be sure to include one at the start of your path.

    @throws

    StarlightError on any errors.


    Type parameters

    Parameters

    • path: string

      The path to GET. Will be prefixed with the API URL. Must start with a forward slash (/).

    • optionalparams: Record<string, undefined | string | number | boolean>

      An optional object of values that will be converted to a string and passed as GET params.

    • optionaloptions: RequestInit

      An optional configuration object passed to the fetch method. Check MDN documentation on fetch to see the available options.

    Returns Promise<T>