/
operation-details.ts
238 lines (191 loc) · 8.22 KB
/
operation-details.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { Representation } from "./../../../../../models/representation";
import * as ko from "knockout";
import template from "./operation-details.html";
import { Router } from "@paperbits/common/routing";
import { Component, RuntimeComponent, OnMounted, OnDestroyed } from "@paperbits/common/ko/decorators";
import { Api } from "../../../../../models/api";
import { Operation } from "../../../../../models/operation";
import { ApiService } from "../../../../../services/apiService";
import { TypeDefinition } from "../../../../../models/typeDefinition";
import { RouteHelper } from "../../../../../routing/routeHelper";
import { TenantService } from "../../../../../services/tenantService";
import { SwaggerObject } from "./../../../../../contracts/swaggerObject";
import { Utils } from "../../../../../utils";
@RuntimeComponent({ selector: "operation-details" })
@Component({
selector: "operation-details",
template: template,
injectable: "operationDetails"
})
export class OperationDetails {
private definitions: ko.ObservableArray<TypeDefinition>;
public readonly selectedApiName: ko.Observable<string>;
public readonly selectedOperationName: ko.Observable<string>;
public readonly consoleIsOpen: ko.Observable<boolean>;
public readonly api: ko.Observable<Api>;
public readonly schemas: ko.ObservableArray<string>;
public readonly tags: ko.ObservableArray<string>;
public readonly operation: ko.Observable<Operation>;
public readonly requestUrlSample: ko.Computed<string>;
public readonly primaryHostname: ko.Observable<string>;
public readonly hostnames: ko.Observable<string[]>;
public readonly working: ko.Observable<boolean>;
constructor(
private readonly apiService: ApiService,
private readonly tenantService: TenantService,
private readonly router: Router,
private readonly routeHelper: RouteHelper
) {
this.working = ko.observable(false);
this.primaryHostname = ko.observable();
this.hostnames = ko.observable();
this.api = ko.observable();
this.schemas = ko.observableArray([]);
this.tags = ko.observableArray([]);
this.operation = ko.observable();
this.selectedApiName = ko.observable();
this.selectedOperationName = ko.observable();
this.consoleIsOpen = ko.observable();
this.definitions = ko.observableArray<TypeDefinition>();
this.requestUrlSample = ko.computed(() => {
if (!this.api() || !this.operation()) {
return null;
}
const api = this.api();
const operation = this.operation();
const hostname = this.primaryHostname();
const apiPath = api.versionedPath;
const operationPath = operation.displayUrlTemplate;
return `https://${hostname}/${apiPath}${operationPath}`;
});
}
@OnMounted()
public async initialize(): Promise<void> {
await this.loadGatewayInfo();
const apiName = this.routeHelper.getApiName();
const operationName = this.routeHelper.getOperationName();
this.router.addRouteChangeListener(this.onRouteChange.bind(this));
if (!apiName || !operationName) {
return;
}
this.selectedApiName(apiName);
this.selectedOperationName(operationName);
await this.loadApi(apiName);
await this.loadOperation(apiName, operationName);
}
private async onRouteChange(): Promise<void> {
const apiName = this.routeHelper.getApiName();
const operationName = this.routeHelper.getOperationName();
if (apiName !== this.selectedApiName()) {
this.selectedApiName(apiName);
this.loadApi(apiName);
}
if (apiName !== this.selectedApiName() || operationName !== this.selectedOperationName()) {
this.operation(null);
if (operationName) {
this.selectedOperationName(operationName);
await this.loadOperation(apiName, operationName);
}
}
}
public async loadApi(apiName: string): Promise<void> {
const api = await this.apiService.getApi(`apis/${apiName}`);
this.api(api);
}
public async loadOperation(apiName: string, operationName: string): Promise<void> {
this.working(true);
const operation = await this.apiService.getOperation(`apis/${apiName}/operations/${operationName}`);
if (operation) {
await this.loadDefinitions(operation);
this.operation(operation);
}
else {
this.cleanSelection();
}
const operationTags = await this.apiService.getOperationTags(`apis/${apiName}/operations/${operationName}`);
this.tags(operationTags.map(tag => tag.name));
this.working(false);
}
public async loadDefinitions(operation: Operation): Promise<void> {
const schemaIds = [];
const apiId = `apis/${this.selectedApiName()}/schemas`;
const prepresentations = operation.responses
.map(response => response.representations)
.concat(operation.request.representations)
.flat();
prepresentations
.map(representation => representation.schemaId)
.filter(schemaId => !!schemaId)
.forEach(schemaId => {
if (!schemaIds.includes(schemaId)) {
schemaIds.push(schemaId);
}
});
const schemasPromises = schemaIds.map(schemaId => this.apiService.getApiSchema(`${apiId}/${schemaId}`));
const schemas = await Promise.all(schemasPromises);
const definitions = schemas.map(x => x.definitions).flat();
this.definitions(definitions);
}
public async loadGatewayInfo(): Promise<void> {
let hostnames = await this.tenantService.getProxyHostnames();
if (hostnames.length === 0) {
// TODO: Remove once setting backend serving the setting gets deployed.
hostnames = await this.getProxyHostnames();
}
if (hostnames.length === 0) {
throw new Error(`Unable to fetch gateway hostnames.`);
}
this.primaryHostname(hostnames[0]);
this.hostnames(hostnames);
}
private cleanSelection(): void {
this.operation(null);
this.selectedOperationName(null);
this.closeConsole();
}
public openConsole(): void {
this.consoleIsOpen(true);
}
public closeConsole(): void {
this.consoleIsOpen(false);
}
public getDefinitionForRepresentation(representation: Representation): TypeDefinition {
const definition = this.definitions().find(x => x.name === representation.typeName);
if (!definition) {
// Fallback for the case when type is referenced, but not defined in schema.
return new TypeDefinition(representation.typeName, {});
}
if (!definition.name) {
definition.name = representation.typeName;
}
if (representation.sample) {
definition.example = representation.sample;
if (representation.contentType.contains("/xml")) {
definition.example = Utils.formatXml(representation.sample);
definition.exampleFormat = "xml";
}
if (representation.contentType.contains("/json")) {
definition.example = Utils.formatJson(representation.sample);
definition.exampleFormat = "json";
}
}
return definition;
}
public getDefinitionReferenceUrl(definition: TypeDefinition): string {
const apiName = this.selectedApiName();
const operationName = this.selectedOperationName();
return this.routeHelper.getDefinitionAnchor(apiName, operationName, definition.name);
}
private async getProxyHostnames(): Promise<string[]> {
const apiName = this.routeHelper.getApiName();
if (!apiName) {
return [];
}
const apiDefinition: SwaggerObject = await this.apiService.exportApi(`apis/${apiName}`, "swagger");
return [apiDefinition.host];
}
@OnDestroyed()
public dispose(): void {
this.router.removeRouteChangeListener(this.onRouteChange);
}
}