Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export interface ParametrizedHostContext extends Client {
/** The API version to use for this operation. */
/** Known values of {@link KnownVersions} that the service accepts. */
apiVersion: string;
host?: string;
subdomain?: string;
sufix?: string;
}

/** Optional parameters for the client. */
Expand Down Expand Up @@ -61,5 +64,11 @@ export function createParametrizedHost(
return next(req);
},
});
return { ...clientContext, apiVersion } as ParametrizedHostContext;
return {
...clientContext,
apiVersion,
host: options.host,
subdomain: options.subdomain,
sufix: options.sufix,
Comment thread
qiaozha marked this conversation as resolved.
Outdated
} as ParametrizedHostContext;
}
103 changes: 77 additions & 26 deletions packages/typespec-ts/src/modular/buildClientContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,60 @@ export function buildClientContext(
getClientContextPath(clientMap, emitterOptions)
);

// Get all client parameters (both required and optional) for the interface
const requiredInterfaceProperties = getClientParameters(client, dpgContext, {
onClientOnly: false,
requiredOnly: true,
apiVersionAsRequired: true
})
.filter((p) => {
const clientParamName = getClientParameterName(p);
return (
clientParamName !== "endpointParam" && clientParamName !== "credential"
);
})
.map((p) => {
return {
name: getClientParameterName(p),
type: getTypeExpression(dpgContext, p.type),
hasQuestionToken: false,
docs: getDocsWithKnownVersion(dpgContext, p)
};
});

// Collect names of required properties to avoid duplicates
const requiredPropertyNames = new Set(
requiredInterfaceProperties.map((p) => p.name)
);

const optionalInterfaceProperties = getClientParameters(client, dpgContext, {
onClientOnly: false,
optionalOnly: true
})
.filter((p) => {
const clientParamName = getClientParameterName(p);
return (
clientParamName !== "endpointParam" &&
clientParamName !== "credential" &&
clientParamName !== "endpoint" &&
!requiredPropertyNames.has(clientParamName) // Avoid duplicating required properties
);
})
.map((p) => {
return {
name: getClientParameterName(p),
type: getTypeExpression(dpgContext, p.type),
hasQuestionToken: true,
docs: getDocsWithKnownVersion(dpgContext, p)
};
});

clientContextFile.addInterface({
isExported: true,
name: `${rlcClientName}`,
extends: [resolveReference(dependencies.Client)],
docs: getDocsFromDescription(client.doc),
properties: getClientParameters(client, dpgContext, {
onClientOnly: false,
requiredOnly: true,
apiVersionAsRequired: true
})
.filter((p) => {
const clientParamName = getClientParameterName(p);
return (
clientParamName !== "endpointParam" &&
clientParamName !== "credential"
);
})
.map((p) => {
return {
name: getClientParameterName(p),
type: getTypeExpression(dpgContext, p.type),
hasQuestionToken: false,
docs: getDocsWithKnownVersion(dpgContext, p)
};
})
properties: [...requiredInterfaceProperties, ...optionalInterfaceProperties]
});

const propertiesInOptions = getClientParameters(client, dpgContext, {
Expand Down Expand Up @@ -262,13 +291,35 @@ export function buildClientContext(
p.name !== "credential" &&
p.name !== "options"
);
if (contextRequiredParam.length) {

// Collect names of required parameters to avoid duplicates
const requiredParamNames = new Set(contextRequiredParam.map((p) => p.name));

// Also include optional parameters from clientInitialization that should be passed through
const contextOptionalParams = getClientParameters(client, dpgContext, {
optionalOnly: true,
onClientOnly: false
}).filter((p) => {
const clientParamName = getClientParameterName(p);
return (
clientParamName !== "endpointParam" &&
clientParamName !== "credential" &&
clientParamName !== "endpoint" &&
!requiredParamNames.has(clientParamName) // Avoid duplicating required parameters
);
});

const allContextParams = [
...contextRequiredParam.map((p) => p.name),
...contextOptionalParams.map(
(p) =>
`${getClientParameterName(p)}: options.${getClientParameterName(p)}`
)
];

if (allContextParams.length) {
factoryFunction.addStatements(
`return { ...clientContext, ${contextRequiredParam
.map((p) => {
return p.name;
})
.join(", ")}} as ${rlcClientName};`
`return { ...clientContext, ${allContextParams.join(", ")}} as ${rlcClientName};`
);
} else {
factoryFunction.addStatements(`return clientContext;`);
Expand Down
Loading
Loading