|
3 | 3 |
|
4 | 4 | import type { KeyCredential, TokenCredential } from "@azure/core-auth";
|
5 | 5 | 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"; |
7 | 7 | 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 | +} |
19 | 29 |
|
20 | 30 | /**
|
21 | 31 | * Creates a client with a default pipeline
|
@@ -49,163 +59,44 @@ export function getClient(
|
49 | 59 | }
|
50 | 60 |
|
51 | 61 | 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; |
62 | 66 |
|
63 |
| - const { allowInsecureConnection, httpClient } = clientOptions; |
64 |
| - const endpointUrl = clientOptions.endpoint ?? endpoint; |
65 | 67 | const client = (path: string, ...args: Array<any>) => {
|
66 |
| - const getUrl = (requestOptions: RequestParameters) => |
67 |
| - buildRequestUrl(endpointUrl, path, args, { allowInsecureConnection, ...requestOptions }); |
68 |
| - |
69 | 68 | return {
|
70 | 69 | 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)); |
79 | 71 | },
|
80 | 72 | 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)); |
89 | 74 | },
|
90 | 75 | 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)); |
99 | 77 | },
|
100 | 78 | 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)); |
109 | 80 | },
|
110 | 81 | 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)); |
119 | 83 | },
|
120 | 84 | 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)); |
129 | 86 | },
|
130 | 87 | 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)); |
139 | 89 | },
|
140 | 90 | 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)); |
149 | 92 | },
|
150 | 93 | };
|
151 | 94 | };
|
152 | 95 |
|
153 | 96 | return {
|
154 | 97 | path: client,
|
155 | 98 | 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, |
209 | 100 | };
|
210 | 101 | }
|
211 | 102 |
|
|
0 commit comments