Skip to content

πŸ“„ usePluginApi – Plugin API Access Composable ​

The usePluginApi composable is the entry point to all plugin APIs.

It provides access to:

  • $data β†’ backend communication
  • $store β†’ state management
  • $socket β†’ real-time events
  • $eventbus β†’ internal/global communication
  • $log β†’ structured logging

🧠 Core Concept ​

ts
const { $data, $store, $socket, $eventbus, $log } = usePluginApi()
  • Automatically resolves the current plugin context
  • Returns scoped API instances
  • Can access other plugins
  • Allows custom query identification via actionId

πŸ”§ Basic Usage ​

typescript
import { usePluginApi } from "@raclettejs/core/orchestrator/composables"
ts
const { $data } = usePluginApi()

πŸ” Multiple Instances ​

ts
const { $data } = usePluginApi()
const { $data: $globalData } = usePluginApi("raclette__core")

🌍 Accessing Other Plugins ​

ts
const { $data: $globalData } = usePluginApi("raclette__core")

$globalData.tag.getAll({
  params: { isDeleted: false },
})

Behavior ​

  • Uses API of another plugin
  • Keeps your $log context
  • Prevents unintended cross-plugin side effects

βš™οΈ Parameters ​

ts
usePluginApi(params)

Type ​

typescript
type PluginApiParams = {
  actionId?: string
  $data?: boolean
  $store?: boolean
  $eventbus?: boolean
  $socket?: boolean
  pluginKey?: string
}

🧩 Parameter Usage ​

pluginKey ​

If you only provide a string as param to usePluginApi, it will be interpreted as a pluginKey

ts
usePluginApi("raclette__core")

or

ts
usePluginApi({ pluginKey: "raclette__core" })

πŸ†” actionId – Query Identification (Part 1) ​

The actionId defines a base identifier for all $data queries executed from this API instance.


πŸ” Default Query ID ​

Without actionId, a query ID looks like:

plugin-pacifico__example-todo-116-todo-getAll-b84N

Structure:

plugin-<pluginKey>-<instanceId>-<model>-<action>-<nanoId>

Example:

plugin-raclette__core-116-tag-getAll-U5Rs

✏️ With actionId ​

ts
const { $data } = usePluginApi({
  actionId: "test",
})

Result:

plugin-pacifico__example-todo-test-p5EF

What changes? ​

  • Replaces:

    • instanceId
    • model
    • action
  • Becomes a custom base identifier

  • Still appends a random nanoId


πŸ†” id in $data Calls – Query Identification (Part 2) ​

To fully control the query ID, you must also set id in the $data call.

ts
$data.example.getAll({
  params: {...},
  id: "myFixedId",
})

🎯 Final Query ID Behavior ​

SetupResult
no actionId, no idfully auto-generated
actionId onlycustom base + random nanoId
actionId + idfully controlled query ID

βœ… Fully Controlled Example ​

ts
const { $data } = usePluginApi({
  actionId: "todoList",
})

$data.todo.getAll({
  params: { isDeleted: true },
  id: "archive",
})

Result:

plugin-pacifico__example-todo-todoList-archive

🎯 Why This Matters ​

Stable Query IDs ​

  • predictable store entries
  • easier debugging

Shared Queries ​

Multiple components can reference the same query


Cache Control ​

Avoid duplicate or fragmented cache entries


Debugging ​

Readable and meaningful identifiers:

plugin-myPlugin-todoList-main

⚠️ Important Notes ​

  • actionId affects all queries from this API instance
  • id overrides the final nanoId
  • Without id, randomness still exists
  • Use both together for full control

🧠 Mental Model ​

  • usePluginApi β†’ defines query namespace
  • actionId β†’ defines query group
  • id β†’ defines specific query instance

πŸ” Typical Patterns ​

Default usage ​

ts
const { $data } = usePluginApi()

Stable queries ​

ts
const { $data } = usePluginApi({
  actionId: "todoList",
})

$data.todo.getAll({
  id: "main",
})

Cross-plugin with control ​

ts
const { $data } = usePluginApi({
  pluginKey: "raclette__core",
  actionId: "coreTags",
})

$data.tag.getAll({
  id: "all",
})

βš–οΈ When to Use actionId + id ​

Use them when you need:

  • stable query identifiers
  • shared queries across components
  • precise cache control
  • better debugging

🚫 Important Constraints ​

  • Must be used inside setup()
  • Overusing fixed IDs can cause collisions
  • Default behavior is fine for most cases

🧠 Key Takeaway ​

usePluginApi gives you access to all plugin APIs

πŸ‘‰ actionId + id give you full control over how your queries are identified, cached, and shared