Skip to content

Commit 2474ec1

Browse files
authored
[core] core-client-rest: depend on @typespec/ts-http-runtime (Azure#33982)
1 parent c47ecce commit 2474ec1

20 files changed

+1047
-2302
lines changed

sdk/core/core-client-rest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"@azure/core-auth": "^1.3.0",
8080
"@azure/core-rest-pipeline": "^1.5.0",
8181
"@azure/core-tracing": "^1.0.1",
82-
"@azure/core-util": "^1.0.0",
82+
"@typespec/ts-http-runtime": "^0.2.2",
8383
"tslib": "^2.6.2"
8484
},
8585
"devDependencies": {

sdk/core/core-client-rest/review/core-client.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { PipelineResponse } from '@azure/core-rest-pipeline';
1717
import type { RawHttpHeaders } from '@azure/core-rest-pipeline';
1818
import type { RawHttpHeadersInput } from '@azure/core-rest-pipeline';
1919
import type { RequestBodyType } from '@azure/core-rest-pipeline';
20-
import { RestError } from '@azure/core-rest-pipeline';
20+
import type { RestError } from '@azure/core-rest-pipeline';
2121
import type { TokenCredential } from '@azure/core-auth';
2222
import type { TransferProgressEvent } from '@azure/core-rest-pipeline';
2323

sdk/core/core-client-rest/src/getClient.ts

Lines changed: 35 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33

44
import type { KeyCredential, TokenCredential } from "@azure/core-auth";
55
import { isKeyCredential, isTokenCredential } from "@azure/core-auth";
6-
import type { HttpClient, HttpMethods, Pipeline, PipelineOptions } from "@azure/core-rest-pipeline";
6+
import { type PipelineOptions } from "@azure/core-rest-pipeline";
77
import { createDefaultPipeline } from "./clientHelpers.js";
8-
import type {
9-
Client,
10-
ClientOptions,
11-
HttpBrowserStreamResponse,
12-
HttpNodeStreamResponse,
13-
RequestParameters,
14-
StreamableMethod,
15-
} from "./common.js";
16-
import { sendRequest } from "./sendRequest.js";
17-
import { buildRequestUrl } from "./urlHelpers.js";
18-
import { isNodeLike } from "@azure/core-util";
8+
import type { Client, ClientOptions, RequestParameters, StreamableMethod } from "./common.js";
9+
import {
10+
getClient as tspGetClient,
11+
type ClientOptions as TspClientOptions,
12+
} from "@typespec/ts-http-runtime";
13+
14+
/**
15+
* Function to wrap RequestParameters so that we get the legacy onResponse behavior in core-client-rest
16+
*/
17+
function wrapRequestParameters(parameters: RequestParameters): RequestParameters {
18+
if (parameters.onResponse) {
19+
return {
20+
...parameters,
21+
onResponse(rawResponse, error) {
22+
parameters.onResponse?.(rawResponse, error, error);
23+
},
24+
};
25+
}
26+
27+
return parameters;
28+
}
1929

2030
/**
2131
* Creates a client with a default pipeline
@@ -49,163 +59,44 @@ export function getClient(
4959
}
5060

5161
const pipeline = createDefaultPipeline(endpoint, credentials, clientOptions);
52-
if (clientOptions.additionalPolicies?.length) {
53-
for (const { policy, position } of clientOptions.additionalPolicies) {
54-
// Sign happens after Retry and is commonly needed to occur
55-
// before policies that intercept post-retry.
56-
const afterPhase = position === "perRetry" ? "Sign" : undefined;
57-
pipeline.addPolicy(policy, {
58-
afterPhase,
59-
});
60-
}
61-
}
62+
const tspClient = tspGetClient(endpoint, {
63+
...clientOptions,
64+
pipeline,
65+
} as TspClientOptions) as Client;
6266

63-
const { allowInsecureConnection, httpClient } = clientOptions;
64-
const endpointUrl = clientOptions.endpoint ?? endpoint;
6567
const client = (path: string, ...args: Array<any>) => {
66-
const getUrl = (requestOptions: RequestParameters) =>
67-
buildRequestUrl(endpointUrl, path, args, { allowInsecureConnection, ...requestOptions });
68-
6968
return {
7069
get: (requestOptions: RequestParameters = {}): StreamableMethod => {
71-
return buildOperation(
72-
"GET",
73-
getUrl(requestOptions),
74-
pipeline,
75-
requestOptions,
76-
allowInsecureConnection,
77-
httpClient,
78-
);
70+
return tspClient.path(path, ...args).get(wrapRequestParameters(requestOptions));
7971
},
8072
post: (requestOptions: RequestParameters = {}): StreamableMethod => {
81-
return buildOperation(
82-
"POST",
83-
getUrl(requestOptions),
84-
pipeline,
85-
requestOptions,
86-
allowInsecureConnection,
87-
httpClient,
88-
);
73+
return tspClient.path(path, ...args).post(wrapRequestParameters(requestOptions));
8974
},
9075
put: (requestOptions: RequestParameters = {}): StreamableMethod => {
91-
return buildOperation(
92-
"PUT",
93-
getUrl(requestOptions),
94-
pipeline,
95-
requestOptions,
96-
allowInsecureConnection,
97-
httpClient,
98-
);
76+
return tspClient.path(path, ...args).put(wrapRequestParameters(requestOptions));
9977
},
10078
patch: (requestOptions: RequestParameters = {}): StreamableMethod => {
101-
return buildOperation(
102-
"PATCH",
103-
getUrl(requestOptions),
104-
pipeline,
105-
requestOptions,
106-
allowInsecureConnection,
107-
httpClient,
108-
);
79+
return tspClient.path(path, ...args).patch(wrapRequestParameters(requestOptions));
10980
},
11081
delete: (requestOptions: RequestParameters = {}): StreamableMethod => {
111-
return buildOperation(
112-
"DELETE",
113-
getUrl(requestOptions),
114-
pipeline,
115-
requestOptions,
116-
allowInsecureConnection,
117-
httpClient,
118-
);
82+
return tspClient.path(path, ...args).delete(wrapRequestParameters(requestOptions));
11983
},
12084
head: (requestOptions: RequestParameters = {}): StreamableMethod => {
121-
return buildOperation(
122-
"HEAD",
123-
getUrl(requestOptions),
124-
pipeline,
125-
requestOptions,
126-
allowInsecureConnection,
127-
httpClient,
128-
);
85+
return tspClient.path(path, ...args).head(wrapRequestParameters(requestOptions));
12986
},
13087
options: (requestOptions: RequestParameters = {}): StreamableMethod => {
131-
return buildOperation(
132-
"OPTIONS",
133-
getUrl(requestOptions),
134-
pipeline,
135-
requestOptions,
136-
allowInsecureConnection,
137-
httpClient,
138-
);
88+
return tspClient.path(path, ...args).options(wrapRequestParameters(requestOptions));
13989
},
14090
trace: (requestOptions: RequestParameters = {}): StreamableMethod => {
141-
return buildOperation(
142-
"TRACE",
143-
getUrl(requestOptions),
144-
pipeline,
145-
requestOptions,
146-
allowInsecureConnection,
147-
httpClient,
148-
);
91+
return tspClient.path(path, ...args).trace(wrapRequestParameters(requestOptions));
14992
},
15093
};
15194
};
15295

15396
return {
15497
path: client,
15598
pathUnchecked: client,
156-
pipeline,
157-
};
158-
}
159-
160-
function buildOperation(
161-
method: HttpMethods,
162-
url: string,
163-
pipeline: Pipeline,
164-
options: RequestParameters,
165-
allowInsecureConnection?: boolean,
166-
httpClient?: HttpClient,
167-
): StreamableMethod {
168-
allowInsecureConnection = options.allowInsecureConnection ?? allowInsecureConnection;
169-
return {
170-
then: function (onFulfilled, onrejected) {
171-
return sendRequest(
172-
method,
173-
url,
174-
pipeline,
175-
{ ...options, allowInsecureConnection },
176-
httpClient,
177-
).then(onFulfilled, onrejected);
178-
},
179-
async asBrowserStream() {
180-
if (isNodeLike) {
181-
throw new Error(
182-
"`asBrowserStream` is supported only in the browser environment. Use `asNodeStream` instead to obtain the response body stream. If you require a Web stream of the response in Node, consider using `Readable.toWeb` on the result of `asNodeStream`.",
183-
);
184-
} else {
185-
return sendRequest(
186-
method,
187-
url,
188-
pipeline,
189-
{ ...options, allowInsecureConnection, responseAsStream: true },
190-
httpClient,
191-
) as Promise<HttpBrowserStreamResponse>;
192-
}
193-
},
194-
async asNodeStream() {
195-
if (isNodeLike) {
196-
return sendRequest(
197-
method,
198-
url,
199-
pipeline,
200-
{ ...options, allowInsecureConnection, responseAsStream: true },
201-
httpClient,
202-
) as Promise<HttpNodeStreamResponse>;
203-
} else {
204-
throw new Error(
205-
"`isNodeStream` is not supported in the browser environment. Use `asBrowserStream` to obtain the response body stream.",
206-
);
207-
}
208-
},
99+
pipeline: tspClient.pipeline,
209100
};
210101
}
211102

sdk/core/core-client-rest/src/helpers/isBinaryBody.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

sdk/core/core-client-rest/src/helpers/isReadableStream-browser.mts

Lines changed: 0 additions & 14 deletions
This file was deleted.

sdk/core/core-client-rest/src/helpers/isReadableStream.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)