All files / state/src/effect fetch.ts

100% Statements 79/79
83.33% Branches 5/6
100% Functions 2/2
100% Lines 79/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 801x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 2x 3x 3x 2x 1x 1x  
import { StateWorker } from '../handler/handler';
import { Implant } from '../handler/implant';
import { Store } from '../store/store';
import { Effect } from './effect';
 
declare global {
  namespace sgrud.state.effects {
 
    /**
     * {@link Implant}ed {@link FetchEffect} providing convenient access to the
     * {@link globalThis.fetch} method within {@link Store.Action}s. Prefer the
     * usage of this {@link Effect} over the {@link globalThis.fetch} method
     * when {@link StateWorker.dispatch}ing {@link Store.Action}s.
     *
     * @param requestInfo - The {@link RequestInfo} or {@link URL} to **fetch**.
     * @param requestInit - An optional {@link RequestInit} object.
     * @returns A {@link Promise} of the **fetch**ed {@link Response}.
     *
     * @example
     * Invoke **fetch** within an {@link Store.Action}:
     * ```ts
     * import { Stateful, Store } from '@sgrud/state';
     *
     * ⁠@Stateful('io.github.sgrud.store.example', { response: undefined })
     * export class ExampleStore extends Store<ExampleStore> {
     *
     *   public readonly response?: unknown;
     *
     *   public async getResponse(url: string): Promise<Store.State<this>> {
     *     const request = await sgrud.state.effects.fetch(url);
     *     const response = await request.json();
     *
     *     if (!request.ok) {
     *       throw response;
     *     }
     *
     *     return { ...this, response };
     *   }
     *
     * }
     * ```
     *
     * @see {@link FetchEffect}
     */
    function fetch(
      requestInfo: RequestInfo | URL,
      requestInit?: RequestInit
    ): Promise<Response>;
 
  }
}
 
/**
 * Built-in **FetchEffect** extending the abstract {@link Effect} base class.
 * This **FetchEffect** is automatically {@link StateWorker.implant}ed when the
 * `@sgrud/state` module is imported and can therefore be always used in
 * {@link Store.Action}s.
 *
 * @decorator {@link Implant}
 *
 * @see {@link Effect}
 */
@Implant('fetch')
export class FetchEffect extends Effect {
 
  /**
   * Overridden **function** binding the {@link FetchEffect} to the polymorphic
   * `this` of the {@link StateWorker}.
   *
   * @param this - The explicit polymorphic `this` parameter.
   * @returns This {@link FetchEffect} bound to the {@link StateWorker}.
   */
  public override function(this: StateWorker): Store.Effects['fetch'] {
    return async(requestInfo, requestInit) => {
      return await fetch(requestInfo, requestInit);
    };
  }
 
}