Skip to content

Commit

Permalink
Make core rest client work with null and encoding (#18381)
Browse files Browse the repository at this point in the history
* make core rest client work with null and add encoding options

* adding test

* adding test

* use enableUrlEncoding instead

* change to skipUrlEncoding

* add change log

* update dependencies

* upate format

* update according comments

* update format

* change back dependencies

* update pnpm-lock

* resolve ci failure
  • Loading branch information
qiaozha committed Nov 3, 2021
1 parent 8d46ea0 commit 591adb4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 19 deletions.
44 changes: 31 additions & 13 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 (Unreleased)

### Features Added

- Add options skipUrlEncoding to support skip path 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;
skipUrlEncoding?: 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 skip encoding the path parameters */
skipUrlEncoding?: boolean;
};

/**
Expand Down
10 changes: 9 additions & 1 deletion 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);
let value = pathParam;
if (!options.skipUrlEncoding) {
value = encodeURIComponent(pathParam);
}

path = path.replace(/{([^/]+)}/, value);
}

const url = new URL(`${baseUrl}/${path}`);
Expand All @@ -34,6 +39,9 @@ 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`);
}
Expand Down
20 changes: 19 additions & 1 deletion sdk/core/core-client-rest/test/urlHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,25 @@ describe("urlHelpers", () => {

it("should handle full urls as path", () => {
const result = buildRequestUrl(mockBaseUrl, "https://example2.org", []);

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

it("should encode url when enable path parameter encoding", () => {
const result = buildRequestUrl(mockBaseUrl, "/foo bar", []);
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= " },
});
assert.equal(result, `https://example.org/foo?foo=+aaaa&bar=b%3D+`);
});

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

0 comments on commit 591adb4

Please sign in to comment.