From 484d1ecd8a7d794fd57692612506c14a186e462a Mon Sep 17 00:00:00 2001 From: George Fu Date: Fri, 1 Dec 2023 12:45:48 -0500 Subject: [PATCH] feat(xhr-http-handler): add static factory method for XHR handler (#5534) --- packages/xhr-http-handler/src/xhr-http-handler.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/xhr-http-handler/src/xhr-http-handler.ts b/packages/xhr-http-handler/src/xhr-http-handler.ts index 9bfc015e45f0..a5637056a4e0 100644 --- a/packages/xhr-http-handler/src/xhr-http-handler.ts +++ b/packages/xhr-http-handler/src/xhr-http-handler.ts @@ -73,6 +73,19 @@ export class XhrHttpHandler extends EventEmitter implements HttpHandler; + /** + * @returns the input if it is an HttpHandler of any class, + * or instantiates a new instance of this handler. + */ + public static create(instanceOrOptions?: HttpHandler | XhrHttpHandlerOptions) { + if (typeof (instanceOrOptions as any)?.handle === "function") { + // is already an instance of HttpHandler. + return instanceOrOptions as HttpHandler; + } + // input is ctor options or undefined. + return new XhrHttpHandler(instanceOrOptions as XhrHttpHandlerOptions); + } + public constructor(options?: XhrHttpHandlerOptions | Provider) { super(); if (typeof options === "function") {