All files / core/src/http proxy.ts

100% Statements 69/69
100% Branches 1/1
100% Functions 1/1
100% Lines 69/69

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 701x 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 1x 1x 1x 1x 1x  
import { Observable } from 'rxjs';
import { Provide, provide } from '../super/provide';
import { Http } from './http';
 
/**
 * Abstract **Proxy** base class to implement {@link Http.Request} interceptors,
 * on the client side. By extending this abstract base class and providing the
 * extending class to the {@link Linker}, e.g., by {@link Target}ing it, the
 * class's {@link handle} method will be called with the {@link Http.Request}
 * details (which could have been modified by a previous **Proxy**) and the next
 * {@link Http.Handler}, whenever a request is fired through the {@link Http}
 * class.
 *
 * @decorator {@link Provide}
 *
 * @example
 * Simple **Proxy** intercepting `file:` requests:
 * ```ts
 * import { type Http, Provider, type Proxy, Target } from '@sgrud/core';
 * import { type Observable, of } from 'rxjs';
 * import { file } from './file';
 *
 * ⁠@Target()
 * export class FileProxy
 *   extends Provider<typeof Proxy>('sgrud.core.Proxy') {
 *
 *   public override handle(
 *     request: Http.Request,
 *     handler: Http.Handler
 *   ): Observable<Http.Response> {
 *     if (request.url.startsWith('file:')) {
 *       return of<Http.Response>(file);
 *     }
 *
 *     return handler.handle(request);
 *   }
 *
 * }
 * ```
 *
 * @see {@link Http}
 */
@Provide()
export abstract class Proxy {
 
  /**
   * Magic string by which this class is {@link provide}d.
   *
   * @see {@link provide}
   */
  public static readonly [provide]: 'sgrud.core.Proxy' = 'sgrud.core.Proxy';
 
  /**
   * The **handle** method of linked classes extending the {@link Proxy} base
   * class is called whenever an {@link Http.Request} is fired. The extending
   * class can either pass the `request` to the next `handler`, with or without
   * modifying it, or an interceptor can chose to completely handle a `request`
   * by itself through returning an {@link Observable} {@link Http.Response}.
   *
   * @param request - The {@link Http.Request} to be **handle**d.
   * @param handler - The next {@link Http.Handler} to **handle** the `request`.
   * @returns An {@link Observable} of the **handle**d {@link Http.Response}.
   */
  public abstract handle(
    request: Http.Request,
    handler: Http.Handler
  ): Observable<Http.Response>;
 
}