-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.ts
More file actions
441 lines (392 loc) · 11.4 KB
/
api.ts
File metadata and controls
441 lines (392 loc) · 11.4 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { Fetch } from "@effect/platform/FetchHttpClient";
import { FileSystem } from "@effect/platform/FileSystem";
import * as Effect from "effect/Effect";
import { v7 as uuid } from "uuid";
import {
AuthenticationError,
type CliError,
NetworkError,
ValidationError,
} from "../effect/errors";
import { fileExists } from "../effect/fs-utils";
import type { Keychain } from "../effect/services/keychain";
import { getTokenInfoEffect } from "./auth";
import { type Environment, envGetEffect, getApiUrl } from "./environment";
// Minimum seconds before expiry to consider token valid for a request
const TOKEN_EXPIRY_BUFFER_SECONDS = 30;
// Header names (lowercased) that must be redacted from debug output and
// the --include envelope to prevent leaking tokens, cookies, or secrets.
const SENSITIVE_HEADER_PARTS = [
"authorization",
"cookie",
"set-cookie",
"token",
"secret",
"api-key",
"apikey",
"x-auth",
] as const;
function isSensitiveHeader(headerName: string): boolean {
const lower = headerName.toLowerCase();
return SENSITIVE_HEADER_PARTS.some((part) => lower.includes(part));
}
/**
* Return a copy of headers with sensitive values replaced by "[REDACTED]".
*/
export { sanitizeHeaders as sanitizeResponseHeaders };
function sanitizeHeaders(
headers: Record<string, string>,
): Record<string, string> {
const sanitized: Record<string, string> = {};
for (const [key, value] of Object.entries(headers)) {
sanitized[key] = isSensitiveHeader(key) ? "[REDACTED]" : value;
}
return sanitized;
}
/**
* Redact values whose keys look like they contain secrets.
*/
function redactSensitiveBodyFields(body: string): string {
try {
const parsed = JSON.parse(body);
if (typeof parsed !== "object" || parsed === null) return body;
const redacted: Record<string, unknown> = {};
for (const [key, value] of Object.entries(parsed)) {
const lower = key.toLowerCase();
const isSensitive = SENSITIVE_HEADER_PARTS.some((part) =>
lower.includes(part),
) || lower.includes("password") || lower.includes("credential");
redacted[key] = isSensitive ? "[REDACTED]" : value;
}
return JSON.stringify(redacted);
} catch {
return body;
}
}
export type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
export interface ApiRequestOptions {
endpoint: string;
method?: HttpMethod;
fields?: Record<string, string>;
body?: string;
headers?: Record<string, string>;
debug?: boolean;
}
export interface ApiResponse {
status: number;
statusText: string;
headers: Record<string, string>;
data: unknown;
}
/**
* Parse field arguments into an object.
* Fields are in the format "key=value".
*/
export function parseFieldsEffect(
fields: string[],
): Effect.Effect<Record<string, string>, ValidationError, never> {
return Effect.gen(function* () {
const result: Record<string, string> = {};
for (const field of fields) {
const eqIndex = field.indexOf("=");
if (eqIndex === -1) {
return yield* Effect.fail(
new ValidationError({
message: `Invalid field format: ${field}`,
userMessage: `Invalid field format: "${field}". Expected "key=value".`,
}),
);
}
const key = field.slice(0, eqIndex);
const value = field.slice(eqIndex + 1);
if (!key) {
return yield* Effect.fail(
new ValidationError({
message: `Empty field key: ${field}`,
userMessage: `Empty field key in: "${field}"`,
}),
);
}
result[key] = value;
}
return result;
});
}
/**
* Parse header arguments into an object.
* Headers are in the format "Key: Value".
*/
export function parseHeadersEffect(
headers: string[],
): Effect.Effect<Record<string, string>, ValidationError, never> {
return Effect.gen(function* () {
const result: Record<string, string> = {};
for (const header of headers) {
const colonIndex = header.indexOf(":");
if (colonIndex === -1) {
return yield* Effect.fail(
new ValidationError({
message: `Invalid header format: ${header}`,
userMessage: `Invalid header format: "${header}". Expected "Key: Value".`,
}),
);
}
const key = header.slice(0, colonIndex).trim();
const value = header.slice(colonIndex + 1).trim();
if (!key) {
return yield* Effect.fail(
new ValidationError({
message: `Empty header key: ${header}`,
userMessage: `Empty header key in: "${header}"`,
}),
);
}
result[key] = value;
}
return result;
});
}
/**
* Read JSON body from file.
*/
export function readBodyFromFileEffect(
filePath: string,
): Effect.Effect<string, ValidationError, FileSystem> {
return Effect.gen(function* () {
const fs = yield* FileSystem;
const exists = yield* fileExists(filePath);
if (!exists) {
return yield* Effect.fail(
new ValidationError({
message: `File not found: ${filePath}`,
userMessage: `File not found: ${filePath}`,
}),
);
}
const content = yield* fs.readFileString(filePath).pipe(
Effect.mapError(
(error) =>
new ValidationError({
message: `Failed to read file: ${error.message}`,
userMessage: `Could not read file: ${filePath}`,
}),
),
);
// Validate it's valid JSON
try {
JSON.parse(content);
} catch {
return yield* Effect.fail(
new ValidationError({
message: `Invalid JSON in file: ${filePath}`,
userMessage: `File does not contain valid JSON: ${filePath}`,
}),
);
}
return content;
});
}
/**
* Build full URL from endpoint using the current environment.
*/
function buildUrlEffect(
endpoint: string,
): Effect.Effect<string, CliError, FileSystem> {
return Effect.gen(function* () {
// Reject full URLs - only relative paths are allowed
if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
return yield* Effect.fail(
new ValidationError({
message: "Full URLs are not allowed",
userMessage:
"Only relative endpoints are allowed (e.g., /v1/domains). Full URLs are not permitted.",
}),
);
}
// Get base URL from environment
const env: Environment = yield* envGetEffect();
const baseUrl = getApiUrl(env);
// Ensure endpoint starts with /
const normalizedEndpoint = endpoint.startsWith("/")
? endpoint
: `/${endpoint}`;
return `${baseUrl}${normalizedEndpoint}`;
});
}
/**
* Make an authenticated request to the GoDaddy API.
*/
export function apiRequestEffect(
options: ApiRequestOptions,
): Effect.Effect<ApiResponse, CliError, FileSystem | Keychain | Fetch> {
return Effect.gen(function* () {
const {
endpoint,
method = "GET",
fields,
body,
headers = {},
debug,
} = options;
// Get access token with expiry info
const tokenInfo = yield* getTokenInfoEffect().pipe(
Effect.mapError(
(err) =>
new AuthenticationError({
message: `Failed to access token from keychain: ${err.message}`,
userMessage:
"Unable to access secure credentials. Unlock your keychain and try again.",
}),
),
);
if (!tokenInfo) {
return yield* Effect.fail(
new AuthenticationError({
message: "No valid access token found",
userMessage: "Not authenticated. Run 'godaddy auth login' first.",
}),
);
}
// Check if token is about to expire
if (tokenInfo.expiresInSeconds < TOKEN_EXPIRY_BUFFER_SECONDS) {
return yield* Effect.fail(
new AuthenticationError({
message: "Access token is about to expire",
userMessage: `Token expires in ${tokenInfo.expiresInSeconds}s. Run 'godaddy auth login' to refresh.`,
}),
);
}
const accessToken = tokenInfo.accessToken;
// Build URL
const url = yield* buildUrlEffect(endpoint);
// Build headers
const requestHeaders: Record<string, string> = {
Authorization: `Bearer ${accessToken}`,
"X-Request-ID": uuid(),
...headers,
};
// Build body
let requestBody: string | undefined;
if (body) {
requestBody = body;
if (!requestHeaders["Content-Type"]) {
requestHeaders["Content-Type"] = "application/json";
}
} else if (fields && Object.keys(fields).length > 0) {
requestBody = JSON.stringify(fields);
if (!requestHeaders["Content-Type"]) {
requestHeaders["Content-Type"] = "application/json";
}
}
if (debug) {
console.error(`> ${method} ${url}`);
const sanitizedRequestHeaders = sanitizeHeaders(requestHeaders);
for (const [key, value] of Object.entries(sanitizedRequestHeaders)) {
console.error(`> ${key}: ${value}`);
}
if (requestBody) {
console.error(`> Body: ${redactSensitiveBodyFields(requestBody)}`);
}
console.error("");
}
// Get the HTTP client from the service context
const fetch = yield* Fetch;
const response = yield* Effect.tryPromise({
try: () =>
fetch(url, {
method,
headers: requestHeaders,
body: requestBody,
}),
catch: (err) =>
new NetworkError({
message: `Network request failed: ${err instanceof Error ? err.message : String(err)}`,
userMessage:
"Network request failed. Check your connection and try again.",
}),
});
// Parse response headers
const responseHeaders: Record<string, string> = {};
response.headers.forEach((value, key) => {
responseHeaders[key] = value;
});
if (debug) {
console.error(`< ${response.status} ${response.statusText}`);
const sanitizedResponseHeaders = sanitizeHeaders(responseHeaders);
for (const [key, value] of Object.entries(sanitizedResponseHeaders)) {
console.error(`< ${key}: ${value}`);
}
console.error("");
}
// Parse response body
let data: unknown;
const contentType = response.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
const text = yield* Effect.tryPromise({
try: () => response.text(),
catch: (err) =>
new NetworkError({
message: `Failed to read response body: ${err}`,
userMessage: "Failed to read API response.",
}),
});
if (text) {
try {
data = JSON.parse(text);
} catch {
data = text;
}
}
} else {
data = yield* Effect.tryPromise({
try: () => response.text(),
catch: (err) =>
new NetworkError({
message: `Failed to read response body: ${err}`,
userMessage: "Failed to read API response.",
}),
});
}
// Check for error status codes
if (!response.ok) {
// Internal message includes the raw server payload for debugging;
// userMessage is a safe, generic description shown to users/agents.
const internalDetail =
typeof data === "object" && data !== null
? JSON.stringify(data)
: String(data || response.statusText);
// Handle 401 Unauthorized specifically - token may be revoked or invalid
if (response.status === 401) {
return yield* Effect.fail(
new AuthenticationError({
message: `Authentication failed (401): ${internalDetail}`,
userMessage:
"Your session has expired or is invalid. Run 'godaddy auth login' to re-authenticate.",
}),
);
}
// Handle 403 Forbidden - insufficient permissions
if (response.status === 403) {
return yield* Effect.fail(
new AuthenticationError({
message: `Access denied (403): ${internalDetail}`,
userMessage:
"You don't have permission to access this resource. Check your account permissions.",
}),
);
}
return yield* Effect.fail(
new NetworkError({
message: `API error (${response.status}): ${internalDetail}`,
userMessage: `API request failed with status ${response.status}: ${response.statusText}`,
}),
);
}
return {
status: response.status,
statusText: response.statusText,
headers: responseHeaders,
data,
};
});
}