Skip to content

Commit 484d1ec

Browse files
authored
feat(xhr-http-handler): add static factory method for XHR handler (#5534)
1 parent a813615 commit 484d1ec

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

packages/xhr-http-handler/src/xhr-http-handler.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ export class XhrHttpHandler extends EventEmitter implements HttpHandler<XhrHttpH
7373
private config?: XhrHttpHandlerOptions;
7474
private configProvider: Promise<XhrHttpHandlerOptions>;
7575

76+
/**
77+
* @returns the input if it is an HttpHandler of any class,
78+
* or instantiates a new instance of this handler.
79+
*/
80+
public static create(instanceOrOptions?: HttpHandler<any> | XhrHttpHandlerOptions) {
81+
if (typeof (instanceOrOptions as any)?.handle === "function") {
82+
// is already an instance of HttpHandler.
83+
return instanceOrOptions as HttpHandler<any>;
84+
}
85+
// input is ctor options or undefined.
86+
return new XhrHttpHandler(instanceOrOptions as XhrHttpHandlerOptions);
87+
}
88+
7689
public constructor(options?: XhrHttpHandlerOptions | Provider<XhrHttpHandlerOptions>) {
7790
super();
7891
if (typeof options === "function") {

0 commit comments

Comments
 (0)