@mmstack/resource 19.7.1 → 19.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2510,6 +2510,57 @@ function injectMutationResourceOptions(injector) {
2510
2510
  ? injector.get(MUTATION_RESOURCE_OPTIONS)
2511
2511
  : inject(MUTATION_RESOURCE_OPTIONS);
2512
2512
  }
2513
+ /**
2514
+ * Creates a resource for performing mutations (e.g., POST, PUT, PATCH, DELETE requests).
2515
+ * Unlike `queryResource`, `mutationResource` is designed for one-off operations that change data.
2516
+ * It does *not* cache responses and does not provide a `value` signal. Instead, it focuses on
2517
+ * managing the mutation lifecycle (pending, error, success) and provides callbacks for handling
2518
+ * these states.
2519
+ *
2520
+ * @param request A function that returns the base `HttpResourceRequest` to be made. This function is called reactively. The parameter is the mutation value provided by the `mutate` method.
2521
+ * @param options Configuration options for the mutation resource. This includes callbacks
2522
+ * for `onMutate`, `onError`, `onSuccess`, and `onSettled`.
2523
+ * @typeParam TResult - The type of the expected result from the mutation.
2524
+ * @typeParam TRaw - The raw response type from the HTTP request (defaults to TResult).
2525
+ * @typeParam TMutation - The type of the mutation value (the request body).
2526
+ * @typeParam TICTX - The type of the initial context value passed to `onMutate`.
2527
+ * @typeParam TCTX - The type of the context value returned by `onMutate`.
2528
+ * @returns A `MutationResourceRef` instance, which provides methods for triggering the mutation
2529
+ * and observing its status.
2530
+ *
2531
+ * @example
2532
+ * ```ts
2533
+ * // Basic PATCH mutation
2534
+ * const updateUser = mutationResource<User, User, Partial<User>>(
2535
+ * (body) => ({ url: `/api/users/${userId()}`, method: 'PATCH', body }),
2536
+ * {
2537
+ * onSuccess: (saved) => toast.success(`Updated ${saved.name}`),
2538
+ * onError: (err) => toast.error(err),
2539
+ * },
2540
+ * );
2541
+ *
2542
+ * updateUser.mutate({ name: 'Alice' });
2543
+ * ```
2544
+ *
2545
+ * @example
2546
+ * ```ts
2547
+ * // Optimistic update with rollback via the `ctx` returned from `onMutate`
2548
+ * const updateUser = mutationResource<User, User, Partial<User>, { prev: User | null }>(
2549
+ * (body) => ({ url: `/api/users/${userId()}`, method: 'PATCH', body }),
2550
+ * {
2551
+ * onMutate: (patch) => {
2552
+ * const prev = current();
2553
+ * current.update((u) => (u ? { ...u, ...patch } : u));
2554
+ * return { prev };
2555
+ * },
2556
+ * onError: (_err, { prev }) => current.set(prev),
2557
+ * },
2558
+ * );
2559
+ * ```
2560
+ */
2561
+ // The request callback maps the mutation value into an HTTP request. `body` is a
2562
+ // free-form request field (Angular's `body?: unknown`), independent of `TMutation`
2563
+ // and optional — so bodyless POSTs and transforms (e.g. params → `FormData`) are fine.
2513
2564
  function mutationResource(request, options0 = {}) {
2514
2565
  // Two-layer option injection: per-call > provideMutationResourceOptions > provideResourceOptions.
2515
2566
  const globalOpts = injectResourceOptions(options0.injector);