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 11 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
34 changes: 24 additions & 10 deletions common/config/rush/pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion sdk/agrifood/agrifood-farming-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"dependencies": {
"@azure/core-auth": "^1.3.0",
"@azure-rest/core-client-paging": "1.0.0-beta.1",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"@azure-rest/core-client-lro": "1.0.0-beta.1",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"autoPublish": false,
"dependencies": {
"@azure/core-auth": "^1.3.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client-lro-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
"dependencies": {
"@azure/core-lro": "^2.2.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"tslib": "^2.2.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client-paging-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"dependencies": {
"@azure/core-paging": "^1.2.0",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"tslib": "^2.2.0"
},
"devDependencies": {
Expand Down
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 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 encode the path or query parameters */
qiaozha marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 12 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,16 @@ describe("urlHelpers", () => {

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+`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"autoPublish": false,
"dependencies": {
"@azure/core-auth": "^1.3.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
Expand Down
2 changes: 1 addition & 1 deletion sdk/purview/purview-account-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"dependencies": {
"@azure-rest/core-client-paging": "1.0.0-beta.1",
"@azure/core-auth": "^1.3.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
Expand Down
2 changes: 1 addition & 1 deletion sdk/purview/purview-administration-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"dependencies": {
"@azure/core-paging": "^1.1.1",
"@azure/core-auth": "^1.3.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
Expand Down
2 changes: 1 addition & 1 deletion sdk/purview/purview-catalog-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"dependencies": {
"@azure/core-lro": "^2.2.0",
"@azure/core-auth": "^1.3.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
Expand Down
2 changes: 1 addition & 1 deletion sdk/purview/purview-scanning-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"autoPublish": false,
"dependencies": {
"@azure/core-auth": "^1.3.0",
"@azure-rest/core-client": "1.0.0-beta.7",
"@azure-rest/core-client": "1.0.0-beta.8",
"@azure/core-paging": "^1.1.1",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/logger": "^1.0.0",
Expand Down