Skip to content

Commit 00f73c1

Browse files
chore(simplify): simplify recent changes in qurl-typescript (#17)
## Summary Simplification sweep targeting the most complex file in the codebase (`src/client.ts`). ### `src/client.ts` - **`list()` param building**: Replaced 5 duplicated `!== null && !== undefined` checks with a single `Object.entries` loop using `!= null` (loose equality covers both null and undefined) - **`rawRequest()` error handling**: Extracted `classifyFetchError()` private method to reduce nesting depth in the catch block from 5 levels to 3 ### Files reviewed but not changed - **`src/errors.ts`**: Error subclass hierarchy is standard boilerplate needed for the public API (`instanceof` checks). No meaningful simplification without changing behavior. - **`src/types.ts`**: Pure type definitions, no logic. - **`src/index.ts`**: Re-exports only. All 46 tests pass. No behavior changes. ## Test plan - [x] `npm test` — all 46 tests pass - [x] `npm run format:check` — passes - [x] Verified no behavior changes (only structural refactoring) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 07781a2 commit 00f73c1

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/client.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ export class QURLClient {
133133
*/
134134
async list(input: ListInput = {}): Promise<ListOutput> {
135135
const params = new URLSearchParams();
136-
if (input.limit !== null && input.limit !== undefined) params.set("limit", String(input.limit));
137-
if (input.cursor !== null && input.cursor !== undefined) params.set("cursor", input.cursor);
138-
if (input.status !== null && input.status !== undefined) params.set("status", input.status);
139-
if (input.q !== null && input.q !== undefined) params.set("q", input.q);
140-
if (input.sort !== null && input.sort !== undefined) params.set("sort", input.sort);
136+
for (const [key, value] of Object.entries(input)) {
137+
if (value !== null && value !== undefined) params.set(key, String(value));
138+
}
141139

142140
const query = params.toString();
143141
const path = query ? `/v1/qurls?${query}` : "/v1/qurls";
@@ -255,16 +253,13 @@ export class QURLClient {
255253
signal: AbortSignal.timeout(this.timeout),
256254
});
257255
} catch (err) {
258-
const isTimeout = err instanceof DOMException && err.name === "TimeoutError";
259-
if (isTimeout) {
260-
lastError = new TimeoutError("Request timed out", { cause: err });
261-
} else {
262-
const cause = err instanceof Error ? err : undefined;
263-
lastError = new NetworkError(cause?.message ?? String(err), { cause });
264-
}
265-
this.log(`${method} ${url} ${isTimeout ? "timed out" : "network error"}`, {
266-
error: lastError.message,
267-
});
256+
lastError = this.classifyFetchError(err);
257+
this.log(
258+
`${method} ${url} ${lastError instanceof TimeoutError ? "timed out" : "network error"}`,
259+
{
260+
error: lastError.message,
261+
},
262+
);
268263
if (attempt < this.maxRetries) {
269264
continue;
270265
}
@@ -337,4 +332,12 @@ export class QURLClient {
337332
const jitter = Math.random() * base * 0.5;
338333
return Math.min(base + jitter, RETRY_MAX_DELAY_MS);
339334
}
335+
336+
private classifyFetchError(err: unknown): TimeoutError | NetworkError {
337+
if (err instanceof DOMException && err.name === "TimeoutError") {
338+
return new TimeoutError("Request timed out", { cause: err });
339+
}
340+
const cause = err instanceof Error ? err : undefined;
341+
return new NetworkError(cause?.message ?? String(err), { cause });
342+
}
340343
}

0 commit comments

Comments
 (0)