- Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathHTTPClient.ts
91 lines (84 loc) · 2.79 KB
/
HTTPClient.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module HTTPClient
*/
import{Context}from"./IContext";
import{Middleware}from"./middleware/IMiddleware";
/**
* @class
* Class representing HTTPClient
*/
exportclassHTTPClient{
/**
* @private
* A member holding first middleware of the middleware chain
*/
privatemiddleware: Middleware;
/**
* @public
* @constructor
* Creates an instance of a HTTPClient
* @param {...Middleware} middleware - The first middleware of the middleware chain or a sequence of all the Middleware handlers
*/
publicconstructor(...middleware: Middleware[]){
if(!middleware||!middleware.length){
consterror=newError();
error.name="InvalidMiddlewareChain";
error.message="Please provide a default middleware chain or custom middleware chain";
throwerror;
}
this.setMiddleware(...middleware);
}
/**
* @private
* Processes the middleware parameter passed to set this.middleware property
* The calling function should validate if middleware is not undefined or not empty.
* @param {...Middleware} middleware - The middleware passed
* @returns Nothing
*/
privatesetMiddleware(...middleware: Middleware[]): void{
if(middleware.length>1){
this.parseMiddleWareArray(middleware);
}else{
this.middleware=middleware[0];
}
}
/**
* @private
* Processes the middleware array to construct the chain
* and sets this.middleware property to the first middleware handler of the array
* The calling function should validate if middleware is not undefined or not empty
* @param {Middleware[]} middlewareArray - The array of middleware handlers
* @returns Nothing
*/
privateparseMiddleWareArray(middlewareArray: Middleware[]){
middlewareArray.forEach((element,index)=>{
if(index<middlewareArray.length-1){
element.setNext(middlewareArray[index+1]);
}
});
this.middleware=middlewareArray[0];
}
/**
* @public
* @async
* To send the request through the middleware chain
* @param {Context} context - The context of a request
* @returns A promise that resolves to the Context
*/
publicasyncsendRequest(context: Context): Promise<Context>{
if(typeofcontext.request==="string"&&context.options===undefined){
consterror=newError();
error.name="InvalidRequestOptions";
error.message="Unable to execute the middleware, Please provide valid options for a request";
throwerror;
}
awaitthis.middleware.execute(context);
returncontext;
}
}