draft: structure-first refactor for TypeScript request flow (experimental)#11
draft: structure-first refactor for TypeScript request flow (experimental)#11kujung-conalog wants to merge 2 commits intomainfrom
Conversation
Summary of ChangesHello @kujung-conalog, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request proposes an experimental refactor of the TypeScript client's request flow, aiming to enhance readability and maintainability. The changes restructure the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This is an excellent refactoring that greatly improves the structure and readability of the TypeScript client's request flow. By breaking down the monolithic request method into smaller, single-purpose 'atomic' functions, the code is now much easier to understand, maintain, and test. The new tests for case-insensitive headers and authorization prefix handling are also valuable additions. I have one suggestion to further enhance the purity of a helper function, which I believe aligns well with the structure-first approach of this refactor.
clients/typescript/src/client.ts
Outdated
| function buildRequestBody(input: RequestInput, headers: Record<string, string>): BodyInit | undefined { | ||
| if (input.formData) { | ||
| delete headers["Content-Type"]; | ||
| return input.formData as unknown as BodyInit; | ||
| } | ||
|
|
||
| return headers; | ||
| if (input.body === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| headers["Content-Type"] = "application/json"; | ||
| return JSON.stringify(input.body); | ||
| } |
There was a problem hiding this comment.
The buildRequestBody function currently has a side effect of modifying the headers object. For better separation of concerns and to create a pure 'atomic' function as intended by this refactor, this function should only be responsible for creating the request body. The logic for managing the Content-Type header can be handled within the orchestrating request method.
By making buildRequestBody a pure function, its behavior becomes more predictable and easier to test in isolation.
After applying the suggested change, you will need to update the request method as follows:
// in PatchClientV3.request()
const body = buildRequestBody(input);
if (input.formData) {
delete headers["Content-Type"];
} else if (input.body !== undefined) {
headers["Content-Type"] = "application/json";
}
const response = await this.fetchFn(requestUrl, { method, headers, body });function buildRequestBody(input: RequestInput): BodyInit | undefined {
if (input.formData) {
return input.formData as unknown as BodyInit;
}
if (input.body === undefined) {
return undefined;
}
return JSON.stringify(input.body);
}There was a problem hiding this comment.
Great point. I updated the draft refactor to make buildRequestBody pure and moved header mutation to applyRequestBodyHeaders in the request orchestrator (commit a7a445a). Verified with npm run build && npm test && npm run typecheck.
EXPERIMENTAL DRAFT
This PR is an experimental structure-improvement proposal (not intended for immediate merge as-is).
Intent
Make the TypeScript client request success path readable in one top-down pass, with minimal boundaries and role-fixed atoms.
Primary Flow (structure-first)
Boundaries
fetchcall inPatchClientV3.requestbuildAuthHeaders,hasBearerPrefix,isJsonContentTypebuildRequestUrl,appendQueryParams,buildRequestBody,parseResponsePayloadAtoms (name + I/O)
buildRequestUrl(baseUrl, path, query) -> stringappendQueryParams(url, query) -> voidbuildRequestHeaders(defaultHeaders, accessToken, accountType, options) -> headersbuildAuthHeaders(options, accessToken, accountType) -> headersbuildRequestBody(input, headers) -> body | undefinedparseResponsePayload(response) -> payloadisJsonContentType(contentType) -> booleanhasBearerPrefix(token) -> booleanChanges
Tests
cd clients/typescript && npm run build && npm test && npm run typecheck(pass)Application/JSONas JSONbearerprefix without duplicationBearerapplication/problem+jsonerror payload as objectPrimary Flow: URL build -> header build -> body build -> fetch -> payload parse -> error/return
Boundaries: request I/O (
fetch), auth/media-type decisions, URL/query/body transformsTests: contract-focused cases for media-type parsing and authorization header invariants
Refactor Check: parameter growth avoided (atoms use only required inputs), decision owners centralized per atom, no legacy parallel path kept