Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js-client): handle type definitions for request body params #6018

Merged
merged 4 commits into from
Jan 16, 2025

Conversation

CalebBarnes
Copy link
Contributor

@CalebBarnes CalebBarnes commented Jan 16, 2025

Summary

This PR adds type definitions for the body parameter in methods that accept a request body, while maintaining proper handling for optional and void parameters. It ensures that methods with optional or no parameters are typed correctly.

Changes

  • Reference Documentation and Example:

  • Request Body Types:

    • Added types for body parameters for methods with application/json or application/octet-stream request bodies.
    • Typed application/json bodies as JSON objects or functions returning JSON objects (with type definitions derived from the @netlify/open-api schema).
    • Typed application/octet-stream bodies as Node.js ReadStream or functions returning a ReadStream.
  • Optional Parameters:

    • Introduced IsRequestBodyOptional to check if all properties in the request body are optional.
    • Enhanced the OperationParams type to conditionally include the body parameter only when applicable and correctly handle optional request bodies.
    • Refactored to ensure that methods with no required parameters (including request body) can be called without passing any arguments (void).
  • Helper Types:

    • Added helper types to simplify logic and improve readability:
      • HasRequestBody: Determines if a method supports a request body.
      • RequestBody: Extracts the request body type based on the operation.
      • IsParamsOrRequestBodyRequired: Determines if any path, query, or request body parameters are required.
      • CombinedParamsAndRequestBody: Combines path, query, and request body parameters into a single type.

How it looks in editor

  • operation createSite with request body type application/json using types derived from @netlify/open-api:
    image
    image

  • operation uploadDeployFile with request body type application/octet-stream:
    image
    image


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures
    we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or
    something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures
    your code follows our style guide and passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

@CalebBarnes CalebBarnes changed the base branch from main to fix/js-client-types-optional-params January 16, 2025 00:11
Base automatically changed from fix/js-client-types-optional-params to main January 16, 2025 18:14
Handling type definitions for requestBody in API schema while still
taking into consideration optional / void params if all path, query, and
requestBody keys are optional.
@CalebBarnes CalebBarnes force-pushed the fix/js-client-types-request-body branch from a168c12 to 7cacefc Compare January 16, 2025 18:58
…PI client definition comment

Improve DX by adding links to docs and small code sample.
- combined the request body type with the Params type
- handle cases where request body properties are optional
- handle cases where all params and request body are optional
- allow 'application/json' body to be a JSON object or a function
returning one
- allow 'application/octet-stream' body to be a Node.js ReadStream or a
function returning one
- simplify by extracting repetitive logic to helper types like
`IsParamsOrRequestBodyRequired` and `IsRequestBodyOptional` etc.
@CalebBarnes CalebBarnes marked this pull request as ready for review January 16, 2025 20:59
@CalebBarnes CalebBarnes requested a review from a team as a code owner January 16, 2025 20:59
YujohnNattrass
YujohnNattrass previously approved these changes Jan 16, 2025
DanielSLew
DanielSLew previously approved these changes Jan 16, 2025
…dy for clarity

These types are responsible for adding detailed annotations to the
`body` parameter of API methods, such as describing its usage and
examples for `application/json` and `application/octet-stream`. The new
names make their purpose more explicit and improve readability for
future maintenance and usage.
@kodiakhq kodiakhq bot merged commit e2e9114 into main Jan 16, 2025
33 checks passed
@kodiakhq kodiakhq bot deleted the fix/js-client-types-request-body branch January 16, 2025 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants