This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operationsGroup.handlebars
205 lines (193 loc) · 7.64 KB
/
operationsGroup.handlebars
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
|______||______||______||______||______||______||______||______||______||______||______|
___ _ _____ _ _
/ _ \ | | | __ \ | | | |
/ /_\ \ _ _ | |_ ___ | | \/ ___ _ __ ___ _ __ __ _ | |_ ___ __| |
| _ || | | || __|/ _ \ | | __ / _ \| '_ \ / _ \| '__|/ _` || __|/ _ \ / _` |
| | | || |_| || |_| (_) | | |_\ \| __/| | | || __/| | | (_| || |_| __/| (_| |
\_| |_/ \__,_| \__|\___/ \____/ \___||_| |_| \___||_| \__,_| \__|\___| \__,_|
______ _ _ _ ___ ___ _ _ __
| _ \ | \ | | | | | \/ | | |(_) / _|
| | | | ___ | \| | ___ | |_ | . . | ___ __| | _ | |_ _ _
| | | |/ _ \ | . ` | / _ \ | __| | |\/| | / _ \ / _` || || _|| | | |
| |/ /| (_) | | |\ || (_) || |_ | | | || (_) || (_| || || | | |_| |
|___/ \___/ \_| \_/ \___/ \__| \_| |_/ \___/ \__,_||_||_| \__, |
__/ |
|___/
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
|______||______||______||______||______||______||______||______||______||______||______|
Client is generated from swagger.json file
*/
import * as superAgent from "superagent";
import { TOKEN_COOKIE_NAME } from '../common/authentication';
import { TypedEvent } from '../common/TypedEvent';
import { retreiveCookieValue } from '../common/cookieUtils';
{{#if imports.types}}
import {
{{#joinList imports.types}}
{{this}}{{/joinList}}
} from "{{imports.path}}"
{{/if}}
{{#if tag}}
{{#each tag}}
{{this}}
{{/each}}
{{/if}}
export default class {{operationGroup.operationsGroupName}} {
private _previousToken:string | undefined | null = null;
private _tokenChangedEvent = new TypedEvent<string|undefined>();
/**
* An event that is fired when the app token associated with this client
* has changed.
*
* @readonly
* @type {TypedEvent<string|undefined>}
* @memberof Client
*/
public get onTokenChanged() : TypedEvent<string|undefined> {
return this._tokenChangedEvent;
}
/**
* A hook to allow errors occured to be processed further before being thrown
* out of the api client. This is useful for modifying validation errors etc.
*
* @memberof Client
*/
public errorProcessor: (error:any) => Error = (e)=>e;
constructor(private _agent:superAgent.SuperAgent<any> = superAgent.agent()){
}
/**
* Returns the underlying SuperAgent instance being used for requests
*
* @readonly
* @memberof Client
*/
get agent() {
return this._agent;
}
/**
* Hook responsible for extracting the value out of the response
*
* @protected
* @template T
* @param {superAgent.Response} response
* @returns {T}
* @memberof Client
*/
protected handleResponse<T>(response:superAgent.Response):T {
return response.body as T;
}
/**
* Ensures that a application token currently exists and fetches a new one
* if the old one has expired or is not present.
*
* @protected
* @returns {Promise<void>}
* @memberof Client
*/
protected async ensureToken(): Promise<void> {
let token = retreiveCookieValue(TOKEN_COOKIE_NAME, this.agent);
// if there is no token, we will go out and retreive one
if (token == undefined) {
try{
console.log('Fetching new token');
await this.GetToken();
}catch(e){
console.error("Couldn't fetch token",e);
}
}
}
/**
* Takes a token and handles emitting events if the token has changed
*
* @protected
* @param {string} [tokenString]
* @memberof Client
*/
protected handleNewToken(token?:string){
if(token !== this._previousToken){
this._previousToken = token;
this.onTokenChanged.emit(token);
}
}
/**
* All operations in the client are routed through this method which
* is responsible for issuing and handling responses in a way which
* errors can be captured and processed within the client.
* This method also ensures that a client token exists before issuing the
* request.
*
* @protected
* @template T
* @param {() => Promise<superAgent.Response>} worker
* @returns {Promise<T>}
* @memberof Client
*/
protected async tryRequest<T>(worker: () => Promise<superAgent.Response>) : Promise<T> {
try {
await this.ensureToken();
const response = await worker();
return this.handleResponse(response);
} catch (error) {
if (this.errorProcessor) {
throw this.errorProcessor(error);
} else {
throw error;
}
}
}
{{#each operationGroup.operations}}
public async {{operationName}}({{#joinList operationParams}} {{paramDisplayName}}:{{paramType}} {{/joinList}}):Promise<{{responsesType}}>{
{{#if operationParams}}
{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}
const params = {
{{#joinList this ",\n" "op=>(!op.inBody && !op.inPath)"}}
"{{paramName}}":{{paramDisplayName}}{{/joinList}}
};
{{/some}}
{{/if}}
{{#equal operationName "GetToken"}}
// For getting the token, we need to bypass the tryRequest as
// it will ensure token which will call this method again
try{
const response: superAgent.Response = await this.agent.{{httpVerb}}(`{{{toInterpolatedString url}}}`)
{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}
.query(params)
{{/some}}
{{#filterList operationParams "op=>op.inBody"}}
.send({{paramDisplayName}})
{{/filterList}}
const { token:tokenString } = this.handleResponse<{ token: string }>(response);
this.handleNewToken(tokenString);
return tokenString;
}catch(e){
this.handleNewToken();
throw e;
}
{{else}}
{{#equal operationName "Logout"}}
await this.agent.{{httpVerb}}(`{{{toInterpolatedString url}}}`)
{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}
.query(params)
{{/some}}
{{#filterList operationParams "op=>op.inBody"}}
.send({{paramDisplayName}})
{{/filterList}}
this.handleNewToken();
{{else}}
return this.tryRequest<{{responsesType}}>(async () => {
const response: superAgent.Response = await this.agent.{{httpVerb}}(`{{{toInterpolatedString url}}}`)
{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}
.query(params)
{{/some}}
{{#filterList operationParams "op=>op.inBody"}}
.send({{paramDisplayName}})
{{/filterList}}
return response;
});
{{/equal}}
{{/equal}}
}
{{/each}}
}