@compassdigital/sdk.typescript 3.0.0-beta.10 → 3.0.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gen.ts +11 -4
- package/lib/base.d.ts +86 -6
- package/lib/base.d.ts.map +1 -1
- package/lib/base.js +120 -28
- package/lib/base.js.map +1 -1
- package/lib/index.d.ts +236 -236
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/order.d.ts +4 -4
- package/lib/order.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/base.ts +120 -15
- package/src/index.ts +273 -238
- package/src/order.ts +6 -6
- package/template.ejs +2 -2
- package/test/client.test.ts +3 -3
- package/test/gen.test.ts +22 -0
package/gen.ts
CHANGED
|
@@ -60,10 +60,11 @@ const operationIDs : Record<string, string> = {
|
|
|
60
60
|
"get /promo": "get_promos",
|
|
61
61
|
"get /schedule": "get_schedules",
|
|
62
62
|
"get /brand": "get_brands",
|
|
63
|
-
"get /announcement/resource": "get_resources"
|
|
63
|
+
"get /announcement/resource": "get_resources",
|
|
64
|
+
"get /order/location/brand/{id}": "get_order_location_brand",
|
|
64
65
|
};
|
|
65
66
|
|
|
66
|
-
class CodeGenerator {
|
|
67
|
+
export class CodeGenerator {
|
|
67
68
|
|
|
68
69
|
private imports: ClientImport[] = [];
|
|
69
70
|
private methods: ClientMethod[] = [];
|
|
@@ -249,7 +250,7 @@ class CodeGenerator {
|
|
|
249
250
|
* Infer the operation name from the method and parameter.
|
|
250
251
|
* These are a bunch of dirty hacks to make the generated names consistent.
|
|
251
252
|
*/
|
|
252
|
-
|
|
253
|
+
inferName(op: OperationDetails, service: ManifestService): OperationName {
|
|
253
254
|
let { method, path } = op;
|
|
254
255
|
let path_segments: string[] = [];
|
|
255
256
|
// use the operationId from our collection if we have one.
|
|
@@ -350,4 +351,10 @@ async function main() {
|
|
|
350
351
|
await gen.generate(manifest);
|
|
351
352
|
}
|
|
352
353
|
|
|
353
|
-
|
|
354
|
+
/**
|
|
355
|
+
* Only generate if --run flag was passed
|
|
356
|
+
*/
|
|
357
|
+
const args = process.argv.splice(2);
|
|
358
|
+
if (args[0] === "--run") {
|
|
359
|
+
main();
|
|
360
|
+
}
|
package/lib/base.d.ts
CHANGED
|
@@ -7,6 +7,10 @@ export interface RequestOptions {
|
|
|
7
7
|
base_url?: string;
|
|
8
8
|
intercept?: InterceptFn;
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* The request data provided to intercept
|
|
12
|
+
* functions.
|
|
13
|
+
*/
|
|
10
14
|
export interface RequestData {
|
|
11
15
|
name: string;
|
|
12
16
|
service: string;
|
|
@@ -15,41 +19,117 @@ export interface RequestData {
|
|
|
15
19
|
headers: Record<string, string>;
|
|
16
20
|
body?: string;
|
|
17
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* The response data expected from
|
|
24
|
+
* intercept functions.
|
|
25
|
+
*/
|
|
18
26
|
export interface ResponseData {
|
|
19
27
|
ok: boolean;
|
|
20
28
|
status: number;
|
|
21
29
|
body: string;
|
|
22
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* A function that performs an https request.
|
|
33
|
+
*/
|
|
23
34
|
export declare type FetchFn = (req: RequestData) => Promise<ResponseData>;
|
|
35
|
+
/**
|
|
36
|
+
* A function that intercepts an http request and returns a response.
|
|
37
|
+
*/
|
|
24
38
|
export declare type InterceptFn = (req: RequestData, fetch: FetchFn) => Promise<ResponseData>;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
/**
|
|
40
|
+
* We have to re-implement the promise methods instead of just existing
|
|
41
|
+
* the existing Promise class due to: https://github.com/microsoft/TypeScript/issues/15202
|
|
42
|
+
*/
|
|
43
|
+
export declare class ResponsePromise<T> implements Promise<T> {
|
|
44
|
+
private promise;
|
|
45
|
+
/**
|
|
46
|
+
* Implements Promise interface.
|
|
47
|
+
*/
|
|
48
|
+
[Symbol.toStringTag]: string;
|
|
49
|
+
constructor(promise: Promise<T>);
|
|
50
|
+
/**
|
|
51
|
+
* Implements Promise interface.
|
|
52
|
+
*/
|
|
53
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): ResponsePromise<TResult1 | TResult2>;
|
|
54
|
+
/**
|
|
55
|
+
* Implements Promise interface.
|
|
56
|
+
*/
|
|
57
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): ResponsePromise<T | TResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Implements Promise interface.
|
|
60
|
+
*/
|
|
61
|
+
finally(onfinally?: (() => void) | null): ResponsePromise<T>;
|
|
62
|
+
/**
|
|
63
|
+
* Returns a promise that resolves to null if http status code
|
|
64
|
+
* or service error code matches one of the provided codes.
|
|
65
|
+
* If no codes are specified, all codes are ignored.
|
|
66
|
+
*/
|
|
67
|
+
ignore(...codes: number[]): ResponsePromise<T | null>;
|
|
29
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* An error returned from an api service.
|
|
71
|
+
*/
|
|
30
72
|
export declare class ServiceError extends Error {
|
|
31
73
|
status: number;
|
|
32
74
|
code: number;
|
|
33
75
|
constructor(status: number, // http status
|
|
34
76
|
code: number, // service error code
|
|
35
77
|
message: string);
|
|
78
|
+
/**
|
|
79
|
+
* Returns the http status code from err.
|
|
80
|
+
* If err isn't an instance of ServiceError, -1 is returned.
|
|
81
|
+
*/
|
|
36
82
|
static status(err: any): number;
|
|
83
|
+
/**
|
|
84
|
+
* Returns the service error code from err.
|
|
85
|
+
* If err isn't an instance of ServiceError, -1 is returned.
|
|
86
|
+
*/
|
|
37
87
|
static code(err: any): number;
|
|
88
|
+
/**
|
|
89
|
+
* Returns a promise that resolves to null if http status code
|
|
90
|
+
* or service error code matches one of the provided codes.
|
|
91
|
+
* If no codes are specified, all codes are ignored.
|
|
92
|
+
*/
|
|
38
93
|
static ignore<T>(codes: number[], response: Promise<T>): Promise<T | null>;
|
|
94
|
+
/**
|
|
95
|
+
* Constructs a ServiceError from the provided response data.
|
|
96
|
+
*/
|
|
39
97
|
static from_res(res: ResponseData): ServiceError;
|
|
40
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* BaseServiceClient contains the logic for executing http requests.
|
|
101
|
+
* This class is meant to be extended by the generated code.
|
|
102
|
+
*/
|
|
41
103
|
export declare abstract class BaseServiceClient {
|
|
42
104
|
private options;
|
|
43
|
-
constructor(options?:
|
|
105
|
+
constructor(options?: RequestOptions);
|
|
44
106
|
/**
|
|
45
107
|
* Combine the route and query parameters into a full url.
|
|
46
108
|
*/
|
|
47
109
|
private build_url;
|
|
110
|
+
/**
|
|
111
|
+
* Perform the http request or pass it to the intercept function
|
|
112
|
+
* if one was configured.
|
|
113
|
+
*/
|
|
48
114
|
private fetch;
|
|
115
|
+
/**
|
|
116
|
+
* Returns true if the response status was a 4xx error.
|
|
117
|
+
*/
|
|
49
118
|
private is_4xx;
|
|
119
|
+
/**
|
|
120
|
+
* Returns the merged options.
|
|
121
|
+
*/
|
|
50
122
|
private get_options;
|
|
123
|
+
/**
|
|
124
|
+
* A delegate to _request that wraps the response in a ResponsePromise.
|
|
125
|
+
*/
|
|
51
126
|
protected request(service: string, name: string, method: string, route: string, body: any, options?: {
|
|
52
127
|
query?: any;
|
|
53
|
-
} & RequestOptions):
|
|
128
|
+
} & RequestOptions): ResponsePromise<any>;
|
|
129
|
+
/**
|
|
130
|
+
* The main logic for fulfilling a request as perscribed by
|
|
131
|
+
* the request options.
|
|
132
|
+
*/
|
|
133
|
+
private _request;
|
|
54
134
|
}
|
|
55
135
|
//# sourceMappingURL=base.d.ts.map
|
package/lib/base.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,cA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AC,OAAO,CACf,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,GAAG,EACT,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,GAAG,CAAA;KAAE,GAAG,cAAmB,GAC7C,eAAe,CAAC,GAAG,CAAC;IAKvB;;;OAGG;YACW,QAAQ;CAyCvB"}
|
package/lib/base.js
CHANGED
|
@@ -64,9 +64,59 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
64
64
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
65
65
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
66
66
|
};
|
|
67
|
+
var _a;
|
|
67
68
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
68
|
-
exports.BaseServiceClient = exports.ServiceError = void 0;
|
|
69
|
+
exports.BaseServiceClient = exports.ServiceError = exports.ResponsePromise = void 0;
|
|
69
70
|
var cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
71
|
+
/**
|
|
72
|
+
* We have to re-implement the promise methods instead of just existing
|
|
73
|
+
* the existing Promise class due to: https://github.com/microsoft/TypeScript/issues/15202
|
|
74
|
+
*/
|
|
75
|
+
var ResponsePromise = /** @class */ (function () {
|
|
76
|
+
function ResponsePromise(promise) {
|
|
77
|
+
this.promise = promise;
|
|
78
|
+
/**
|
|
79
|
+
* Implements Promise interface.
|
|
80
|
+
*/
|
|
81
|
+
this[_a] = "[object ResponsePromise]";
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Implements Promise interface.
|
|
85
|
+
*/
|
|
86
|
+
ResponsePromise.prototype.then = function (onfulfilled, onrejected) {
|
|
87
|
+
return new ResponsePromise(this.promise.then(onfulfilled, onrejected));
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Implements Promise interface.
|
|
91
|
+
*/
|
|
92
|
+
ResponsePromise.prototype.catch = function (onrejected) {
|
|
93
|
+
return new ResponsePromise(this.promise.catch(onrejected));
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Implements Promise interface.
|
|
97
|
+
*/
|
|
98
|
+
ResponsePromise.prototype.finally = function (onfinally) {
|
|
99
|
+
return new ResponsePromise(this.promise.finally(onfinally));
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Returns a promise that resolves to null if http status code
|
|
103
|
+
* or service error code matches one of the provided codes.
|
|
104
|
+
* If no codes are specified, all codes are ignored.
|
|
105
|
+
*/
|
|
106
|
+
ResponsePromise.prototype.ignore = function () {
|
|
107
|
+
var codes = [];
|
|
108
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
109
|
+
codes[_i] = arguments[_i];
|
|
110
|
+
}
|
|
111
|
+
return new ResponsePromise(ServiceError.ignore(codes, this.promise));
|
|
112
|
+
};
|
|
113
|
+
return ResponsePromise;
|
|
114
|
+
}());
|
|
115
|
+
exports.ResponsePromise = ResponsePromise;
|
|
116
|
+
_a = Symbol.toStringTag;
|
|
117
|
+
/**
|
|
118
|
+
* An error returned from an api service.
|
|
119
|
+
*/
|
|
70
120
|
var ServiceError = /** @class */ (function (_super) {
|
|
71
121
|
__extends(ServiceError, _super);
|
|
72
122
|
function ServiceError(status, // http status
|
|
@@ -81,30 +131,46 @@ var ServiceError = /** @class */ (function (_super) {
|
|
|
81
131
|
Object.setPrototypeOf(_this, ServiceError.prototype);
|
|
82
132
|
return _this;
|
|
83
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Returns the http status code from err.
|
|
136
|
+
* If err isn't an instance of ServiceError, -1 is returned.
|
|
137
|
+
*/
|
|
84
138
|
ServiceError.status = function (err) {
|
|
85
139
|
if (err instanceof ServiceError) {
|
|
86
140
|
return err.status;
|
|
87
141
|
}
|
|
88
142
|
return -1;
|
|
89
143
|
};
|
|
144
|
+
/**
|
|
145
|
+
* Returns the service error code from err.
|
|
146
|
+
* If err isn't an instance of ServiceError, -1 is returned.
|
|
147
|
+
*/
|
|
90
148
|
ServiceError.code = function (err) {
|
|
91
149
|
if (err instanceof ServiceError) {
|
|
92
150
|
return err.code;
|
|
93
151
|
}
|
|
94
152
|
return -1;
|
|
95
153
|
};
|
|
154
|
+
/**
|
|
155
|
+
* Returns a promise that resolves to null if http status code
|
|
156
|
+
* or service error code matches one of the provided codes.
|
|
157
|
+
* If no codes are specified, all codes are ignored.
|
|
158
|
+
*/
|
|
96
159
|
ServiceError.ignore = function (codes, response) {
|
|
97
160
|
return __awaiter(this, void 0, void 0, function () {
|
|
98
161
|
var err_1, _i, codes_1, code;
|
|
99
|
-
return __generator(this, function (
|
|
100
|
-
switch (
|
|
162
|
+
return __generator(this, function (_b) {
|
|
163
|
+
switch (_b.label) {
|
|
101
164
|
case 0:
|
|
102
|
-
|
|
165
|
+
_b.trys.push([0, 2, , 3]);
|
|
103
166
|
return [4 /*yield*/, response];
|
|
104
|
-
case 1: return [2 /*return*/,
|
|
167
|
+
case 1: return [2 /*return*/, _b.sent()];
|
|
105
168
|
case 2:
|
|
106
|
-
err_1 =
|
|
169
|
+
err_1 = _b.sent();
|
|
107
170
|
if (err_1 instanceof ServiceError) {
|
|
171
|
+
if (codes.length === 0) {
|
|
172
|
+
return [2 /*return*/, null];
|
|
173
|
+
}
|
|
108
174
|
for (_i = 0, codes_1 = codes; _i < codes_1.length; _i++) {
|
|
109
175
|
code = codes_1[_i];
|
|
110
176
|
if (err_1.status === code || err_1.code === code) {
|
|
@@ -118,6 +184,9 @@ var ServiceError = /** @class */ (function (_super) {
|
|
|
118
184
|
});
|
|
119
185
|
});
|
|
120
186
|
};
|
|
187
|
+
/**
|
|
188
|
+
* Constructs a ServiceError from the provided response data.
|
|
189
|
+
*/
|
|
121
190
|
ServiceError.from_res = function (res) {
|
|
122
191
|
var message = res.body;
|
|
123
192
|
var code = res.status;
|
|
@@ -134,6 +203,10 @@ var ServiceError = /** @class */ (function (_super) {
|
|
|
134
203
|
return ServiceError;
|
|
135
204
|
}(Error));
|
|
136
205
|
exports.ServiceError = ServiceError;
|
|
206
|
+
/**
|
|
207
|
+
* BaseServiceClient contains the logic for executing http requests.
|
|
208
|
+
* This class is meant to be extended by the generated code.
|
|
209
|
+
*/
|
|
137
210
|
var BaseServiceClient = /** @class */ (function () {
|
|
138
211
|
function BaseServiceClient(options) {
|
|
139
212
|
this.options = options !== null && options !== void 0 ? options : {};
|
|
@@ -141,8 +214,8 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
141
214
|
/**
|
|
142
215
|
* Combine the route and query parameters into a full url.
|
|
143
216
|
*/
|
|
144
|
-
BaseServiceClient.prototype.build_url = function (route,
|
|
145
|
-
var base_url =
|
|
217
|
+
BaseServiceClient.prototype.build_url = function (route, _b, params) {
|
|
218
|
+
var base_url = _b.base_url, _c = _b.stage, stage = _c === void 0 ? "dev" : _c;
|
|
146
219
|
var url = "https://api.compassdigital.org/" + stage + route;
|
|
147
220
|
// use the base url if one was provided.
|
|
148
221
|
if (base_url) {
|
|
@@ -153,8 +226,8 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
153
226
|
}
|
|
154
227
|
// add query parameters.
|
|
155
228
|
var query = [];
|
|
156
|
-
for (var _i = 0,
|
|
157
|
-
var
|
|
229
|
+
for (var _i = 0, _d = Object.entries(params); _i < _d.length; _i++) {
|
|
230
|
+
var _e = _d[_i], name_1 = _e[0], value = _e[1];
|
|
158
231
|
query.push(encodeURIComponent(name_1) + "=" + encodeURIComponent(value));
|
|
159
232
|
}
|
|
160
233
|
if (query.length > 0) {
|
|
@@ -162,12 +235,16 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
162
235
|
}
|
|
163
236
|
return url;
|
|
164
237
|
};
|
|
238
|
+
/**
|
|
239
|
+
* Perform the http request or pass it to the intercept function
|
|
240
|
+
* if one was configured.
|
|
241
|
+
*/
|
|
165
242
|
BaseServiceClient.prototype.fetch = function (req, options) {
|
|
166
243
|
return __awaiter(this, void 0, void 0, function () {
|
|
167
244
|
var res;
|
|
168
|
-
var
|
|
169
|
-
return __generator(this, function (
|
|
170
|
-
switch (
|
|
245
|
+
var _b;
|
|
246
|
+
return __generator(this, function (_c) {
|
|
247
|
+
switch (_c.label) {
|
|
171
248
|
case 0:
|
|
172
249
|
if (options === null || options === void 0 ? void 0 : options.intercept) {
|
|
173
250
|
return [2 /*return*/, options.intercept(req, this.fetch.bind(this))];
|
|
@@ -178,37 +255,52 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
178
255
|
headers: req.headers,
|
|
179
256
|
})];
|
|
180
257
|
case 1:
|
|
181
|
-
res =
|
|
182
|
-
|
|
258
|
+
res = _c.sent();
|
|
259
|
+
_b = {
|
|
183
260
|
ok: res.ok,
|
|
184
261
|
status: res.status
|
|
185
262
|
};
|
|
186
263
|
return [4 /*yield*/, res.text()];
|
|
187
|
-
case 2: return [2 /*return*/, (
|
|
188
|
-
|
|
264
|
+
case 2: return [2 /*return*/, (_b.body = _c.sent(),
|
|
265
|
+
_b)];
|
|
189
266
|
}
|
|
190
267
|
});
|
|
191
268
|
});
|
|
192
269
|
};
|
|
270
|
+
/**
|
|
271
|
+
* Returns true if the response status was a 4xx error.
|
|
272
|
+
*/
|
|
193
273
|
BaseServiceClient.prototype.is_4xx = function (res) {
|
|
194
274
|
return 400 <= res.status && res.status < 500;
|
|
195
275
|
};
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
return __assign(__assign(__assign(
|
|
276
|
+
/**
|
|
277
|
+
* Returns the merged options.
|
|
278
|
+
*/
|
|
279
|
+
BaseServiceClient.prototype.get_options = function (options) {
|
|
280
|
+
return __assign(__assign(__assign({}, this.options), options), { headers: __assign(__assign({}, this.options.headers), options.headers) });
|
|
201
281
|
};
|
|
282
|
+
/**
|
|
283
|
+
* A delegate to _request that wraps the response in a ResponsePromise.
|
|
284
|
+
*/
|
|
202
285
|
BaseServiceClient.prototype.request = function (service, name, method, route, body, options) {
|
|
203
|
-
|
|
286
|
+
if (options === void 0) { options = {}; }
|
|
287
|
+
var promise = this._request(service, name, method, route, body, options);
|
|
288
|
+
return new ResponsePromise(promise);
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* The main logic for fulfilling a request as perscribed by
|
|
292
|
+
* the request options.
|
|
293
|
+
*/
|
|
294
|
+
BaseServiceClient.prototype._request = function (service, name, method, route, body, options) {
|
|
295
|
+
var _b;
|
|
204
296
|
if (options === void 0) { options = {}; }
|
|
205
297
|
return __awaiter(this, void 0, void 0, function () {
|
|
206
298
|
var url, headers, req, res, err, data;
|
|
207
|
-
return __generator(this, function (
|
|
208
|
-
switch (
|
|
299
|
+
return __generator(this, function (_c) {
|
|
300
|
+
switch (_c.label) {
|
|
209
301
|
case 0:
|
|
210
|
-
options = this.get_options(
|
|
211
|
-
url = this.build_url(route, options, (
|
|
302
|
+
options = this.get_options(options);
|
|
303
|
+
url = this.build_url(route, options, (_b = options.query) !== null && _b !== void 0 ? _b : {});
|
|
212
304
|
headers = Object.assign({
|
|
213
305
|
"User-Agent": "CDL/ServiceClient"
|
|
214
306
|
}, options.headers);
|
|
@@ -224,7 +316,7 @@ var BaseServiceClient = /** @class */ (function () {
|
|
|
224
316
|
}
|
|
225
317
|
return [4 /*yield*/, this.fetch(req, options)];
|
|
226
318
|
case 1:
|
|
227
|
-
res =
|
|
319
|
+
res = _c.sent();
|
|
228
320
|
if (!res.ok) {
|
|
229
321
|
err = ServiceError.from_res(res);
|
|
230
322
|
if (options.debug) {
|
package/lib/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,4DAAgC;AAqDhC;;;GAGG;AACH;IAOE,yBAAoB,OAAm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