Spoosh
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

PropertyTypeDescription
trigger(options) => PromiseExecute the mutation
dataSignal<TData | undefined>Response data
errorSignal<TError | undefined>Error if request failed
loadingSignal<boolean>True while mutation is in progress
abort() => voidAbort current request
inputSignal<TriggerInput | undefined>The last trigger input
metaSignal<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

OptionTypeDescription
transform(data: TData) => TOutTransform response data (from transform plugin)
+ plugin options-Additional options from installed plugins

Trigger Options

OptionTypeDescription
bodyTBodyRequest body
queryTQueryQuery parameters
paramsRecord<string, string | number>Path parameters
+ plugin options-Options from installed plugins

On this page