import type { Children, Refkey } from "@alloy-js/core";
import { code, List, refkey, StatementList } from "@alloy-js/core";
import * as ts from "@alloy-js/typescript";
import { Reference } from "@alloy-js/typescript";
import type { HttpOperation } from "@typespec/http";
import { EncodingProvider } from "./encoding-provider.jsx";
import { uriTemplateLib } from "./external-packages/uri-template.js";
import { HttpRequestOptions } from "./http-request-options.js";
import { HttpRequestParametersExpression } from "./http-request-parameters-expression.js";
export interface HttpRequestProps {
httpOperation: HttpOperation;
operationOptionsParamRefkey: Refkey;
responseRefkey?: Refkey;
}
export function HttpRequest(props: HttpRequestProps) {
const operationUrlRefkey = refkey();
const requestOptionsVarRefkey = refkey();
const httpResponseRefkey = props.responseRefkey ?? refkey();
const verb = props.httpOperation.verb;
return (
{code`
await client.pathUnchecked(${()}).${verb}(${()})
`}
{code`
if (typeof options?.operationOptions?.onResponse === "function") {
options?.operationOptions?.onResponse(response);
}`}
);
}
export interface HttpUrlProps {
httpOperation: HttpOperation;
requestOptionsParamRefkey: Refkey;
pathVarRefkey?: Refkey;
children?: Children;
}
HttpRequest.Url = function HttpUrlDeclaration(props: HttpUrlProps) {
const urlTemplate = props.httpOperation.uriTemplate;
const urlParameters = props.httpOperation.parameters.properties.filter(
(p) => p.kind === "path" || p.kind === "query",
);
return (
{uriTemplateLib.parse}({JSON.stringify(urlTemplate)}).expand(
{
}
)
);
};