Skip to content

Commit 60ad054

Browse files
docs: document useSchematicCreditBalance for React, Vue, and Angular (#362)
Add a "Credit balances" section to the React, Vue, and Angular SDK pages covering the new useSchematicCreditBalance hook/composable and the Angular creditBalance$ observable. Documents the settled default, the remaining/reserved opts.type escape hatch, the returned balance/isLoading shape, and where to find a feature's creditId. Mirrors the SDK READMEs from schematic-js PRs #1389 (React) and #1397 (Vue/Angular). Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 2628613 commit 60ad054

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

fern/docs/pages/developer_resources/sdks/angular.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,46 @@ export class PlanComponent {
233233
}
234234
```
235235

236+
### Credit balances
237+
238+
Use `creditBalance$` to observe a company's credit balance. It is keyed by credit ID and emits as the balance changes over the DataStream, so the number stays accurate while leases open and close:
239+
240+
```typescript
241+
import { Component, inject } from "@angular/core";
242+
import { AsyncPipe } from "@angular/common";
243+
import { SchematicService } from "@schematichq/schematic-angular";
244+
245+
@Component({
246+
selector: "app-credit-meter",
247+
standalone: true,
248+
imports: [AsyncPipe],
249+
template: `
250+
@if (creditBalance$ | async; as credit) {
251+
@if (credit.isLoading) {
252+
<div>Loading…</div>
253+
} @else {
254+
<div>{{ credit.balance }} credits remaining</div>
255+
}
256+
}
257+
`,
258+
})
259+
export class CreditMeterComponent {
260+
private schematic = inject(SchematicService);
261+
creditBalance$ = this.schematic.creditBalance$("credit-id");
262+
}
263+
```
264+
265+
`creditBalance$` emits an object with the following properties:
266+
267+
| Property | Type | Description |
268+
| --- | --- | --- |
269+
| `balance` | `number` | The spendable balance, or `0` while loading or when the company holds no balance in this credit |
270+
| `isLoading` | `boolean` | `true` while the balance is still loading and no value has arrived yet |
271+
272+
By default it surfaces the `settled` balance, which is the spendable number you should show end users. For advanced lease-aware accounting, pass a second argument (`"remaining"` or `"reserved"`) to surface those values instead. The credit ID is available on a feature's entitlement: `entitlement$(key)` emits `creditId` for credit-based features.
273+
274+
*Note: `creditBalance$` requires `@schematichq/schematic-angular` 1.5.0 or later.*
275+
236276
## API Reference
237277

238278
### `provideSchematic(config)`
@@ -252,6 +292,7 @@ Injectable service providing all Schematic functionality:
252292
| `flagValue$(key, fallback?)` | `Observable<boolean>` | Observe a feature flag's boolean value |
253293
| `entitlement$(key, fallback?)` | `Observable<CheckFlagReturn>` | Observe detailed entitlement data |
254294
| `plan$()` | `Observable<CheckPlanReturn \| undefined>` | Observe plan information |
295+
| `creditBalance$(creditId, type?)` | `Observable<SchematicCreditBalance>` | Observe a company's lease-aware credit balance |
255296
| `isPending$()` | `Observable<boolean>` | Observe loading state |
256297

257298
### `SCHEMATIC_CLIENT`

fern/docs/pages/developer_resources/sdks/react.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,41 @@ The hook returns an object with the following properties:
179179
| `trialEndDate` | `Date \| undefined` | The trial end date, if the company has or had a trial |
180180
| `trialStatus` | `"active" \| "expired" \| "converted" \| undefined` | The company's trial status: `active` if the trial is ongoing, `expired` if the trial ended without conversion, `converted` if the company converted to a paid plan, or `undefined` if the company has never trialed |
181181

182+
### Credit balances
183+
184+
To display a company's credit balance, use the `useSchematicCreditBalance` hook. It is keyed by credit ID and updates reactively as the balance changes over the DataStream, so the number stays accurate while leases open and close:
185+
186+
```tsx
187+
import { useSchematicCreditBalance } from "@schematichq/schematic-react";
188+
189+
const CreditMeter = () => {
190+
const { balance, isLoading } = useSchematicCreditBalance("credit-id");
191+
192+
if (isLoading) {
193+
return <div>Loading…</div>;
194+
}
195+
196+
return <div>{balance} credits remaining</div>;
197+
};
198+
```
199+
200+
The hook returns an object with the following properties:
201+
202+
| Property | Type | Description |
203+
| --- | --- | --- |
204+
| `balance` | `number` | The spendable balance, or `0` while loading or when the company holds no balance in this credit |
205+
| `isLoading` | `boolean` | `true` while the balance is still loading and no value has arrived yet |
206+
207+
By default the hook surfaces the `settled` balance, which is the spendable number you should show end users. For advanced lease-aware accounting, pass `opts.type` as `"remaining"` or `"reserved"` to surface those values instead:
208+
209+
```tsx
210+
const { balance } = useSchematicCreditBalance("credit-id", { type: "reserved" });
211+
```
212+
213+
The credit ID is available on a feature's entitlement: `useSchematicEntitlement(key)` returns `creditId` for credit-based features.
214+
215+
*Note: `useSchematicCreditBalance` requires `@schematichq/schematic-react` 1.5.0 or later.*
216+
182217
## Fallback Behavior
183218

184219
The SDK includes built-in fallback behavior you can use to ensure your application continues to function even when unable to reach Schematic (e.g., during service disruptions or network issues).

fern/docs/pages/developer_resources/sdks/vue.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,40 @@ The composable returns an object with the following properties:
177177
| `trialEndDate` | `Date \| undefined` | The trial end date, if the company has or had a trial |
178178
| `trialStatus` | `"active" \| "expired" \| "converted" \| undefined` | The company's trial status: `active` if the trial is ongoing, `expired` if the trial ended without conversion, `converted` if the company converted to a paid plan, or `undefined` if the company has never trialed |
179179

180+
### Credit balances
181+
182+
To display a company's credit balance, use the `useSchematicCreditBalance` composable. It is keyed by credit ID and updates reactively as the balance changes over the DataStream, so the number stays accurate while leases open and close:
183+
184+
```vue
185+
<script setup lang="ts">
186+
import { useSchematicCreditBalance } from "@schematichq/schematic-vue";
187+
188+
const { balance, isLoading } = useSchematicCreditBalance("credit-id");
189+
</script>
190+
191+
<template>
192+
<div v-if="isLoading">Loading…</div>
193+
<div v-else>{{ balance }} credits remaining</div>
194+
</template>
195+
```
196+
197+
The composable returns an object with the following reactive properties:
198+
199+
| Property | Type | Description |
200+
| --- | --- | --- |
201+
| `balance` | `ComputedRef<number>` | The spendable balance, or `0` while loading or when the company holds no balance in this credit |
202+
| `isLoading` | `ComputedRef<boolean>` | `true` while the balance is still loading and no value has arrived yet |
203+
204+
By default the composable surfaces the `settled` balance, which is the spendable number you should show end users. For advanced lease-aware accounting, pass `opts.type` as `"remaining"` or `"reserved"` to surface those values instead:
205+
206+
```ts
207+
const { balance } = useSchematicCreditBalance("credit-id", { type: "reserved" });
208+
```
209+
210+
The credit ID is available on a feature's entitlement: `useSchematicEntitlement(key)` returns `creditId` for credit-based features.
211+
212+
*Note: `useSchematicCreditBalance` requires `@schematichq/schematic-vue` 1.5.0 or later.*
213+
180214
## Fallback Behavior
181215

182216
The SDK includes built-in fallback behavior you can use to ensure your application continues to function even when unable to reach Schematic (e.g., during service disruptions or network issues).

0 commit comments

Comments
 (0)