Skip to content

draft: structure-first refactor for TypeScript request flow (experimental)#11

Draft
kujung-conalog wants to merge 2 commits intomainfrom
codex/structure-first-ts-draft
Draft

draft: structure-first refactor for TypeScript request flow (experimental)#11
kujung-conalog wants to merge 2 commits intomainfrom
codex/structure-first-ts-draft

Conversation

@kujung-conalog
Copy link
Copy Markdown

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)

  1. Build URL from base/path/query.
  2. Build headers from defaults + auth + per-request override.
  3. Build request body and set content-type policy.
  4. Execute fetch at the I/O boundary.
  5. Parse payload by response contract.
  6. Throw typed client error for non-2xx, else return payload.

Boundaries

  • I/O boundary: fetch call in PatchClientV3.request
  • Domain decision atoms: buildAuthHeaders, hasBearerPrefix, isJsonContentType
  • Transform atoms: buildRequestUrl, appendQueryParams, buildRequestBody, parseResponsePayload

Atoms (name + I/O)

  • buildRequestUrl(baseUrl, path, query) -> string
  • appendQueryParams(url, query) -> void
  • buildRequestHeaders(defaultHeaders, accessToken, accountType, options) -> headers
  • buildAuthHeaders(options, accessToken, accountType) -> headers
  • buildRequestBody(input, headers) -> body | undefined
  • parseResponsePayload(response) -> payload
  • isJsonContentType(contentType) -> boolean
  • hasBearerPrefix(token) -> boolean

Changes

  • keep public API surface unchanged
  • refactor request orchestration into one composition point + explicit atoms
  • keep side effects at boundaries and keep decision ownership local to atom
  • add contract tests for auth prefix normalization and case-insensitive JSON media-type parsing

Tests

  • cd clients/typescript && npm run build && npm test && npm run typecheck (pass)
  • new contracts covered:
    • parses Application/JSON as JSON
    • preserves lowercase bearer prefix without duplication
    • still prefixes raw token with Bearer
    • parses application/problem+json error payload as object

Primary Flow: URL build -> header build -> body build -> fetch -> payload parse -> error/return
Boundaries: request I/O (fetch), auth/media-type decisions, URL/query/body transforms
Tests: 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

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 request method into a clear, top-down sequence of operations, delegating specific responsibilities like URL construction, header assembly, body preparation, and response parsing to dedicated, atomic functions. This approach centralizes decision ownership and isolates side effects, making the request lifecycle easier to understand and test.

Highlights

  • Request Orchestration Refactor: Refactored the core request orchestration logic in the TypeScript client's request method to improve readability and maintainability.
  • New Helper Functions: Introduced new, focused helper functions for building URLs, constructing headers, preparing request bodies, and parsing response payloads.
  • Improved Content-Type Handling: Enhanced response payload parsing to correctly handle case-insensitive JSON content types.
  • Authentication Header Logic: Improved authentication header logic to ensure correct management and prefixing of 'Bearer' tokens.
  • New Contract Tests: Added comprehensive contract tests to validate the new parsing behaviors and authentication header invariants.

🧠 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
  • clients/typescript/src/client.ts
    • Refactored the request method to delegate responsibilities to new helper functions, and introduced several new utility functions for URL building, header construction, body preparation, and response parsing.
  • clients/typescript/test/client-error-parsing.test.js
    • Added a readHeader utility function and new tests to validate case-insensitive JSON content type parsing and correct handling of 'Bearer' authorization header prefixes.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 335 to 347
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);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants