This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 658
/
Copy pathClientRequest.ts
131 lines (117 loc) · 3.04 KB
/
ClientRequest.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
PartialServerQueryRequest,
ServerQueryResponse,
} from "../common/bridges/ServerBridge";
import {LocalCommand, localCommands} from "./commands";
import Client from "./Client";
import {consumeUnknown} from "@internal/consume";
import review from "./review";
import {DIAGNOSTIC_CATEGORIES} from "@internal/diagnostics";
import SilentClientError from "./SilentClientError";
import {ClientQueryResponse} from "../common/types/client";
import {isBridgeEndDiagnosticsError} from "@internal/events";
export type ClientRequestType = "local" | "server";
export default class ClientRequest {
constructor(
client: Client,
type: ClientRequestType = "local",
query: PartialServerQueryRequest,
) {
this.client = client;
this.type = type;
this.query = query;
}
public query: PartialServerQueryRequest;
public type: ClientRequestType;
public client: Client;
public fork(query: PartialServerQueryRequest): ClientRequest {
return new ClientRequest(this.client, this.type, query);
}
public async init(): Promise<ClientQueryResponse> {
const {requestFlags} = this.query;
if (requestFlags?.review) {
return await this.initReview();
} else {
return await this.initCommand();
}
}
private async initReview(): Promise<ClientQueryResponse> {
return review(this);
}
public async initCommand(): Promise<ClientQueryResponse> {
const localCommand = localCommands.get(this.query.commandName);
if (this.type === "server" || localCommand === undefined) {
return this.initFromServer();
} else {
return this.initFromLocal(localCommand);
}
}
private async initFromLocal(
// rome-ignore lint/ts/noExplicitAny: future cleanup
localCommand: LocalCommand<any>,
): Promise<ClientQueryResponse> {
const {query} = this;
let flags;
if (localCommand.defineFlags !== undefined) {
flags = localCommand.defineFlags(
consumeUnknown(
query.commandFlags,
DIAGNOSTIC_CATEGORIES["flags/invalid"],
),
);
}
try {
const res = await localCommand.callback(this, flags);
if (res === true) {
return {
type: "SUCCESS",
data: undefined,
hasData: false,
markers: [],
files: {},
};
} else if (res === false) {
return {
type: "CLIENT_ERROR",
message: "Command return",
markers: [],
};
} else {
return res;
}
} catch (err) {
if (err instanceof SilentClientError) {
return {
type: "CLIENT_ERROR",
message: err.message,
markers: [],
};
} else {
throw err;
}
}
}
private async initFromServer(): Promise<ServerQueryResponse> {
const {client} = this;
try {
const bridge = await client.findOrStartServer();
return await bridge.events.query.call(this.query);
} catch (err) {
if (isBridgeEndDiagnosticsError(err)) {
return {
type: "CANCELLED",
markers: [],
reason: err.message,
};
} else {
throw err;
}
}
}
}