-
Notifications
You must be signed in to change notification settings - Fork 285
[http-client-js] Improve documentation #6940
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: internal | ||
packages: | ||
- "@typespec/http-client-js" | ||
--- | ||
|
||
Add code documentation |
33 changes: 33 additions & 0 deletions
33
packages/http-client-js/src/components/client-context/client-context-declaration.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 20 additions & 1 deletion
21
packages/http-client-js/src/components/client-context/client-context.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/http-client-js/src/components/encoding-provider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,41 @@ | ||
/** | ||
* This file defines the EncodingProvider component which wraps its children with an encoding context. | ||
* This context includes default values for encoding configurations such as "bytes" and "datetime". | ||
* The component is useful when you need consistent encoding options across different parts of the emitter | ||
* or when you want to override the default settings with custom values. | ||
*/ | ||
|
||
import { Children } from "@alloy-js/core"; | ||
import { EncodingContext } from "../context/encoding/encoding-context.jsx"; | ||
import { EncodingDefaults } from "../context/encoding/types.js"; | ||
|
||
/** | ||
* Interface for the properties accepted by the EncodingProvider component. | ||
* | ||
* @property {EncodingDefaults} [defaults] - Optional custom default encoding values that override built-in defaults. | ||
* @property {Children} [children] - Nested elements/components that will have access to the encoding context. | ||
*/ | ||
export interface EncodingProviderProps { | ||
defaults?: EncodingDefaults; | ||
children?: Children; | ||
} | ||
|
||
/** | ||
* The EncodingProvider component wraps its children with a context that supplies encoding defaults. | ||
* It merges user-supplied defaults with built-in default values. If no user defaults are provided, the | ||
* encoding context will use "none" for 'bytes' and "rfc3339" for 'datetime'. | ||
* | ||
* @param {EncodingProviderProps} props - The props for the provider, including potential default values and children. | ||
* @returns A JSX element that provides the encoding context to its children. | ||
*/ | ||
export function EncodingProvider(props: EncodingProviderProps) { | ||
// Create a merged defaults object: built-in values are used unless overridden by props.defaults. | ||
const defaults: EncodingDefaults = { | ||
bytes: "none", | ||
datetime: "rfc3339", | ||
...props.defaults, | ||
}; | ||
|
||
// Wrap children with the EncodingContext.Provider to ensure they receive the defined defaults. | ||
return <EncodingContext.Provider value={defaults}>{props.children}</EncodingContext.Provider>; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/http-client-js/src/components/external-packages/ts-http-runtime.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/http-client-js/src/components/external-packages/uri-template.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
/** | ||
* This file contains helper components to generate HTTP responses handling. | ||
* The code processes responses by status code and content type, and throws custom REST errors when the response | ||
* does not match any in the spec. | ||
* It uses the Alloy JS framework and Typespec HTTP utilities to flatten and process HTTP responses. | ||
*/ | ||
|
||
import { Children, code, For, List, Refkey } from "@alloy-js/core"; | ||
import { isVoidType } from "@typespec/compiler"; | ||
import { $ } from "@typespec/compiler/experimental/typekit"; | ||
import { HttpOperation } from "@typespec/http"; | ||
import { getCreateRestErrorRefkey } from "./static-helpers/rest-error.jsx"; | ||
import { ContentTypeEncodingProvider } from "./transforms/content-type-encoding-provider.jsx"; | ||
import { JsonTransform } from "./transforms/json/json-transform.jsx"; | ||
|
||
export interface HttpResponseProps { | ||
httpOperation: HttpOperation; | ||
responseRefkey: Refkey; | ||
children?: Children; | ||
} | ||
|
||
/** | ||
* HttpResponse component that renders a list of HTTP response handling code. | ||
* It includes conditional handling for the success responses and a fallback error throwing. | ||
* | ||
* @param props - Properties including the HTTP operation and a reference key for the response. | ||
*/ | ||
export function HttpResponse(props: HttpResponseProps) { | ||
return ( | ||
<List hardline> | ||
|
@@ -25,20 +39,34 @@ export interface HttpResponsesProps { | |
children?: Children; | ||
} | ||
|
||
/** | ||
* HttpResponses component that iterates over HTTP responses extracted from the httpOperation. | ||
* It generates conditional checks for each non-error response based on status code and content type. | ||
* | ||
* @param props - Properties containing the HttpOperation to be processed. | ||
*/ | ||
export function HttpResponses(props: HttpResponsesProps) { | ||
// Handle response by status code and content type | ||
// Obtain and flatten the HTTP responses from the operation, filtering by non-error responses. | ||
const responses = $.httpOperation.flattenResponses(props.httpOperation); | ||
|
||
return ( | ||
<For each={responses.filter((r) => !$.httpResponse.isErrorResponse(r))}> | ||
{({ statusCode, contentType, responseContent, type }) => { | ||
// Extract the body from the response content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this also auto generated by AI? how did it know which lines to add and which not? |
||
const body = responseContent.body; | ||
|
||
// Default expression to return if no valid body processing is applicable. | ||
let expression: Children = code`return;`; | ||
|
||
// Build a content type check condition: | ||
// If a body exists, check if the response headers include the expected content type. | ||
// Otherwise, ensure that the response has no body. | ||
const contentTypeCheck = body | ||
? ` && response.headers["content-type"]?.includes("${contentType}")` | ||
: " && !response.body"; | ||
|
||
// If there is a response body and (it's single (aka not multipart nor file) or the type is not void), | ||
// apply JSON transformation wrapped with the content-type encoding provider. | ||
if (body && (body.bodyKind === "single" || (type && !isVoidType(type)))) { | ||
expression = ( | ||
<ContentTypeEncodingProvider contentType={contentType}> | ||
|
@@ -49,6 +77,7 @@ export function HttpResponses(props: HttpResponsesProps) { | |
); | ||
} | ||
|
||
// If the status code represents a single status value, generate an if-statement to match it. | ||
if ($.httpResponse.statusCode.isSingle(statusCode)) { | ||
return code` | ||
if (+response.status === ${statusCode}${contentTypeCheck}) { | ||
|
@@ -57,6 +86,7 @@ export function HttpResponses(props: HttpResponsesProps) { | |
`; | ||
} | ||
|
||
// If the status code represents a range, generate an if-statement to match the range. | ||
if ($.httpResponse.statusCode.isRange(statusCode)) { | ||
return code` | ||
if (+response.status >= ${statusCode.start} && +response.status <= ${statusCode.end} ${contentTypeCheck}) { | ||
|
@@ -65,6 +95,7 @@ export function HttpResponses(props: HttpResponsesProps) { | |
`; | ||
} | ||
|
||
// Return null if the statusCode type is not handled explicitly. | ||
return null; | ||
}} | ||
</For> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel like this would be more useful if we could add the typekit description to it?
"Get the responses for the given operation. This function will return an array of responses grouped by status code and content type."