|
1 |
| -import { OperationOptions } from '@azure/core-client'; |
| 1 | +import { OperationArguments, OperationOptions, OperationSpec } from '@azure/core-client'; |
2 | 2 | import { userAgentPolicyName, setClientRequestIdPolicyName } from '@azure/core-rest-pipeline';
|
3 | 3 | import {
|
4 | 4 | genRequestQueuesPolicy, genCombineGetRequestsPolicy, genErrorFormatterPolicy,
|
5 | 5 | parseBigIntPolicy, genVersionCheckPolicy, genRetryOnFailurePolicy,
|
6 | 6 | } from './utils/autorest';
|
7 | 7 | import { Middleware as MiddlewareApi, MiddlewareOptionalParams, ErrorResponse } from './apis/middleware';
|
| 8 | +import { operationSpecs } from './apis/middleware/middleware'; |
| 9 | +import { IllegalArgumentError, InternalError } from './utils/errors'; |
| 10 | +import { MiddlewarePage, isMiddlewareRawPage } from './utils/MiddlewarePage'; |
8 | 11 |
|
9 | 12 | export default class Middleware extends MiddlewareApi {
|
10 | 13 | /**
|
@@ -51,4 +54,57 @@ export default class Middleware extends MiddlewareApi {
|
51 | 54 | // TODO: use instead our retry policy
|
52 | 55 | this.pipeline.removePolicy({ name: 'defaultRetryPolicy' });
|
53 | 56 | }
|
| 57 | + |
| 58 | + /** |
| 59 | + * Get a middleware response by path instead of a method name and arguments. |
| 60 | + * @param pathWithQuery - a path to request starting with `/v3/` |
| 61 | + */ |
| 62 | + async requestByPath<Response = unknown>(pathWithQuery: string): Promise<Response> { |
| 63 | + const queryPos = pathWithQuery.indexOf('?'); |
| 64 | + const path = pathWithQuery.slice(0, queryPos === -1 ? pathWithQuery.length : queryPos); |
| 65 | + const query = pathWithQuery.slice(queryPos === -1 ? pathWithQuery.length : queryPos + 1); |
| 66 | + |
| 67 | + const operationSpec = operationSpecs.find((os) => { |
| 68 | + let p = path; |
| 69 | + if (os.path == null) return false; |
| 70 | + const groups = os.path.replace(/{\w+}/g, '{param}').split('{param}'); |
| 71 | + while (groups.length > 0) { |
| 72 | + const part = groups.shift(); |
| 73 | + if (part == null) throw new InternalError(`Unexpected operation spec path: ${os.path}`); |
| 74 | + if (!p.startsWith(part)) return false; |
| 75 | + p = p.replace(part, ''); |
| 76 | + if (groups.length > 0) p = p.replace(/^[\w.]+/, ''); |
| 77 | + } |
| 78 | + return p === ''; |
| 79 | + }); |
| 80 | + if (operationSpec == null) { |
| 81 | + throw new IllegalArgumentError(`Can't find operation spec corresponding to ${path}`); |
| 82 | + } |
| 83 | + |
| 84 | + return this.sendOperationRequest({}, { |
| 85 | + ...operationSpec, |
| 86 | + path, |
| 87 | + urlParameters: operationSpec.urlParameters |
| 88 | + ?.filter(({ parameterPath }) => parameterPath === '$host'), |
| 89 | + queryParameters: Array.from(new URLSearchParams(query)).map(([key, value]) => ({ |
| 90 | + parameterPath: ['options', key], |
| 91 | + mapper: { |
| 92 | + defaultValue: value.toString(), |
| 93 | + serializedName: key, |
| 94 | + type: { |
| 95 | + name: 'String', |
| 96 | + }, |
| 97 | + }, |
| 98 | + })), |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + override async sendOperationRequest<T>( |
| 103 | + operationArguments: OperationArguments, |
| 104 | + operationSpec: OperationSpec, |
| 105 | + ): Promise<T> { |
| 106 | + const response = await super.sendOperationRequest(operationArguments, operationSpec); |
| 107 | + if (!isMiddlewareRawPage(response)) return response as T; |
| 108 | + return new MiddlewarePage(response, this) as T; |
| 109 | + } |
54 | 110 | }
|
0 commit comments