Spoosh
Injects

injectWrite

Trigger mutations with loading and error states

Trigger mutations with loading and error states. The callback selects the API method (no 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
inputTriggerOptions | undefinedThe last trigger input
metaSignal<PluginResults>Plugin-provided metadata

Trigger Options

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

On this page