forked from vercel-labs/emulate
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.ts
More file actions
173 lines (148 loc) · 5.58 KB
/
Copy pathclient.ts
File metadata and controls
173 lines (148 loc) · 5.58 KB
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
// Typed client for the /_emulate control plane. One handle works against any
// emulator wherever it runs — a local createEmulator() process, another
// process on the same machine, or a hosted instance on emulators.dev — so
// consumers never hand-roll fetch calls or re-cast response shapes the server
// already types.
import type { CredentialRequest, IssuedCredential } from "./control-plane.js";
import type { ArmedFault, FaultArmInput } from "./faults.js";
import type { LedgerEntry } from "./ledger.js";
import type {
EmulatorInstanceInfo,
OperationCoverage,
ResolvedConnection,
ServiceManifest,
SpecCoverage,
SpecKind,
SpecManifest,
SurfaceManifest,
} from "./manifest.js";
import type { StoreSnapshot } from "./store.js";
import type { WebhookDelivery } from "./webhooks.js";
export interface ManifestResponse {
manifest: ServiceManifest;
instance: EmulatorInstanceInfo;
connections: ResolvedConnection[];
}
export interface SpecsResponse {
specs: SpecManifest[];
surfaces: SurfaceManifest[];
}
export interface CoverageResponse {
operations: OperationCoverage[];
summary: Record<SpecCoverage, number>;
specs: Array<{ kind: SpecKind; title: string; coverage: SpecCoverage; operationCount: number }>;
}
export interface LogsResponse {
webhooks: WebhookDelivery[];
requests: LedgerEntry[];
}
export interface ConnectionsQuery {
token?: string;
clientId?: string;
clientSecret?: string;
}
export class EmulatorControlError extends Error {
constructor(
readonly method: string,
readonly url: string,
readonly status: number,
readonly body: string,
) {
super(`${method} ${url} -> ${status}${body ? `: ${body.slice(0, 300)}` : ""}`);
this.name = "EmulatorControlError";
}
}
/** fetch-compatible function, injectable for tests (e.g. a Hono app.request). */
export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
export class EmulatorClient {
/** Provider base URL — what an SDK or app under test points at. */
readonly baseUrl: string;
readonly #fetch: FetchLike;
constructor(baseUrl: string, options?: { fetch?: FetchLike }) {
this.baseUrl = baseUrl.replace(/\/+$/, "");
this.#fetch = options?.fetch ?? ((input, init) => fetch(input, init));
}
/** The /_emulate/openapi URL — feed it to anything that ingests a spec. */
get openapiUrl(): string {
return `${this.baseUrl}/_emulate/openapi`;
}
async manifest(): Promise<ManifestResponse> {
return this.#json("GET", "/_emulate/manifest");
}
async quickstart(): Promise<string> {
return this.#text("GET", "/_emulate/quickstart");
}
async specs(): Promise<SpecsResponse> {
return this.#json("GET", "/_emulate/specs");
}
async coverage(): Promise<CoverageResponse> {
return this.#json("GET", "/_emulate/coverage");
}
async connections(query?: ConnectionsQuery): Promise<ResolvedConnection[]> {
const params = new URLSearchParams();
if (query?.token) params.set("token", query.token);
if (query?.clientId) params.set("client_id", query.clientId);
if (query?.clientSecret) params.set("client_secret", query.clientSecret);
const qs = params.size > 0 ? `?${params}` : "";
const body = await this.#json<{ connections: ResolvedConnection[] }>("GET", `/_emulate/connections${qs}`);
return body.connections;
}
async state(): Promise<StoreSnapshot> {
return this.#json("GET", "/_emulate/state");
}
async logs(): Promise<LogsResponse> {
return this.#json("GET", "/_emulate/logs");
}
readonly ledger = {
list: async (limit?: number): Promise<LedgerEntry[]> => {
const qs = limit !== undefined ? `?limit=${limit}` : "";
const body = await this.#json<{ entries: LedgerEntry[] }>("GET", `/_emulate/ledger${qs}`);
return body.entries;
},
clear: async (): Promise<void> => {
await this.#json("DELETE", "/_emulate/ledger");
},
};
readonly faults = {
arm: async (request: FaultArmInput): Promise<ArmedFault> => {
const body = await this.#json<{ fault: ArmedFault }>("POST", "/_emulate/faults", request);
return body.fault;
},
list: async (): Promise<ArmedFault[]> => {
const body = await this.#json<{ faults: ArmedFault[] }>("GET", "/_emulate/faults");
return body.faults;
},
clear: async (id?: string): Promise<void> => {
await this.#json("DELETE", id ? `/_emulate/faults/${encodeURIComponent(id)}` : "/_emulate/faults");
},
};
readonly credentials = {
mint: async (request: CredentialRequest = {}): Promise<IssuedCredential> => {
const body = await this.#json<{ credential: IssuedCredential }>("POST", "/_emulate/credentials", request);
return body.credential;
},
};
async seed(seed: unknown): Promise<void> {
await this.#json("POST", "/_emulate/seed", seed);
}
async reset(): Promise<void> {
await this.#json("POST", "/_emulate/reset", {});
}
async #request(method: string, path: string, body?: unknown): Promise<Response> {
const url = `${this.baseUrl}${path}`;
const response = await this.#fetch(url, {
method,
...(body === undefined ? {} : { headers: { "content-type": "application/json" }, body: JSON.stringify(body) }),
});
if (!response.ok) {
throw new EmulatorControlError(method, url, response.status, await response.text());
}
return response;
}
async #json<T>(method: string, path: string, body?: unknown): Promise<T> {
return (await this.#request(method, path, body)).json() as Promise<T>;
}
async #text(method: string, path: string, body?: unknown): Promise<string> {
return (await this.#request(method, path, body)).text();
}
}