Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make core rest client work with null and encoding #18381

Merged
merged 19 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions sdk/core/core-client-rest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Release History
## 1.0.0-beta.8 (2021-10-27)
qiaozha marked this conversation as resolved.
Show resolved Hide resolved

### Features Added

- Add options to support enable path parameter encoding and query parameter encoding.

## 1.0.0-beta.7 (2021-09-02)

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client-rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-rest/core-client",
"version": "1.0.0-beta.7",
"version": "1.0.0-beta.8",
"description": "Core library for interfacing with AutoRest rest level generated code",
"sdk-type": "client",
"main": "dist/index.js",
Expand Down
6 changes: 3 additions & 3 deletions sdk/core/core-client-rest/review/core-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ export type RequestParameters = {
queryParameters?: Record<string, unknown>;
contentType?: string;
allowInsecureConnection?: boolean;
enableUrlEncoding?: boolean;
};

// @public
export type RouteParams<TRoute extends string> = TRoute extends `${infer _Head}/{${infer _Param}}${infer Tail}` ? [
pathParam: string,
...pathParams: RouteParams<Tail>
pathParam: string,
...pathParams: RouteParams<Tail>
] : [
];


```
2 changes: 2 additions & 0 deletions sdk/core/core-client-rest/src/pathClientTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type RequestParameters = {
contentType?: string;
/** Set to true if the request is sent over HTTP instead of HTTPS */
allowInsecureConnection?: boolean;
/** Set to true if you want to encode the path or query parameters */
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
enableUrlEncoding?: boolean;
};

/**
Expand Down
18 changes: 16 additions & 2 deletions sdk/core/core-client-rest/src/urlHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export function buildRequestUrl(
}

for (const pathParam of pathParameters) {
path = path.replace(/{([^/]+)}/, pathParam);
if (options.enableUrlEncoding) {
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
const encodeParam = encodeURIComponent(pathParam);
path = path.replace(/{([^/]+)}/, encodeParam);
} else {
path = path.replace(/{([^/]+)}/, pathParam);
}
}

const url = new URL(`${baseUrl}/${path}`);
Expand All @@ -34,11 +39,20 @@ export function buildRequestUrl(
const queryParams = options.queryParameters;
for (const key of Object.keys(queryParams)) {
const param = queryParams[key] as any;
if (param === undefined || param === null) {
continue;
}
if (!param.toString || typeof param.toString !== "function") {
throw new Error(`Query parameters must be able to be represented as string, ${key} can't`);
}
const value = param.toISOString !== undefined ? param.toISOString() : param.toString();
url.searchParams.append(key, value);
if (options.enableUrlEncoding) {
const encodeParam = encodeURIComponent(value);
url.searchParams.append(key, encodeParam);
} else {
url.searchParams.append(key, value);
}

qiaozha marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
15 changes: 15 additions & 0 deletions sdk/core/core-client-rest/test/urlHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ describe("urlHelpers", () => {

assert.equal(result, `https://example2.org`);
});

it("should encode url when enable path parameter encoding", () => {
const result = buildRequestUrl(mockBaseUrl, "/foo bar", [], {
enableUrlEncoding: true
});
assert.equal(result, `https://example.org/foo%20bar`);
});

it("should encode url when enable query parameter encoding", () => {
const result = buildRequestUrl(mockBaseUrl, "/foo", [], {
queryParameters: { foo: ' aaaa', bar: 'b= ' },
enableUrlEncoding: true
});
assert.equal(result, `https://example.org/foo?foo=%2520aaaa&bar=b%253D%2520`);
});
});