Skip to content

Expose FetchFactory #62011

@bpetex

Description

@bpetex

Which @angular/* package(s) are relevant/related to the feature request?

common

Description

I would like to be able to provide my own implementation of FetchFactory.

Currently the angular codebase already has an internal solution used for testing only:
https://github.com/angular/angular/blob/main/packages/common/http/src/fetch.ts#L339

The use case where this is relevant is with cloudflare workers under the same account and http request between them is forbidden and you can only use it through service binding to directly call the worker's fetch function.

At the moment I'm "forced" to copy paste the whole FetchBackend class to be able to modify the fetch implementation.

Proposed solution

{
  provide: FetchFactory,
  useClass: CustomFetchFactory,
}

Example implementation

export class CustomFetchFactory implements FetchFactory {
  private bindings  = inject<Bindings>(REQUEST_CONTEXT);

  fetch(url: string, init?: RequestInit): Promise<Response> {
    if (this.bindings?.CF_WORKER && url.includes('cf-worker-under-same-account')) {
      return this.bindings.CF_WORKER.fetch(new Request(url, init));
    }

    return globalThis.fetch(url, init);
  }

}

Alternatives considered

none

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: common/httpIssues related to HTTP and HTTP ClientfeatureIssue that requests a new feature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy