Injects
injectWrite
Trigger mutations with loading and error states
The callback calls the API method with parentheses.
Basic Usage
@Component({
selector: "app-create-user",
template: `
<form (ngSubmit)="handleSubmit()">
<button [disabled]="createUser.loading()">
{{ createUser.loading() ? "Creating..." : "Create User" }}
</button>
</form>
`,
})
export class CreateUserComponent {
createUser = injectWrite((api) => api("users").POST());
async handleSubmit() {
const result = await this.createUser.trigger({ body: formData });
if (result.data) {
console.log("Created:", result.data);
}
}
}With Invalidation
createPost = injectWrite((api) => api("posts").POST());
await createPost.trigger({
body: { title: "New Post", content: "..." },
invalidate: ["posts"],
});Returns
| Property | Type | Description |
|---|---|---|
trigger | (options) => Promise | Execute the mutation |
data | Signal<TData | undefined> | Response data |
error | Signal<TError | undefined> | Error if request failed |
loading | Signal<boolean> | True while mutation is in progress |
abort | () => void | Abort current request |
input | Signal<TriggerInput | undefined> | The last trigger input |
meta | Signal<PluginResults> | Plugin-provided metadata |
Hook-Level Options
injectWrite accepts a second argument for hook-level options like transform:
createPost = injectWrite((api) => api("posts").POST(), {
transform: (post) => ({
success: true,
postId: post.id,
createdAt: new Date(post.timestamp),
}),
});Available Hook Options
| Option | Type | Description |
|---|---|---|
transform | (data: TData) => TOut | Transform response data (from transform plugin) |
| + plugin options | - | Additional options from installed plugins |
Trigger Options
| Option | Type | Description |
|---|---|---|
body | TBody | Request body |
query | TQuery | Query parameters |
params | Record<string, string | number> | Path parameters |
| + plugin options | - | Options from installed plugins |