Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion sdk/core/core-client-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src",
"execute:samples": "echo skipped",
"extract-api": "tsc -p . && api-extractor run --local",
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
Expand Down
4 changes: 3 additions & 1 deletion sdk/core/core-client-rest/review/core-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { KeyCredential } from '@azure/core-auth';
import { Pipeline } from '@azure/core-rest-pipeline';
import { PipelineOptions } from '@azure/core-rest-pipeline';
import { PipelineRequest } from '@azure/core-rest-pipeline';
import { RawHttpHeaders } from '@azure/core-rest-pipeline';
import { RestError } from '@azure/core-rest-pipeline';
import { TokenCredential } from '@azure/core-auth';

Expand Down Expand Up @@ -73,6 +72,9 @@ export type PathUncheckedResponse = HttpResponse & {
body: any;
};

// @public (undocumented)
export type RawHttpHeaders = Record<string, string | number | boolean>;

// @public
export type RequestParameters = {
headers?: RawHttpHeaders;
Expand Down
4 changes: 3 additions & 1 deletion sdk/core/core-client-rest/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { PipelineOptions, RawHttpHeaders, PipelineRequest } from "@azure/core-rest-pipeline";
import { PipelineOptions, PipelineRequest } from "@azure/core-rest-pipeline";

/**
* General options that a Rest Level Client can take
Expand Down Expand Up @@ -34,6 +34,8 @@ export type ClientOptions = PipelineOptions & {
allowInsecureConnection?: boolean;
};

export type RawHttpHeaders = Record<string, string | number | boolean>;

/**
* Represents the shape of an HttpResponse
*/
Expand Down
20 changes: 20 additions & 0 deletions sdk/core/core-client-rest/src/headersHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { RawHttpHeaders } from "./common";
import { RawHttpHeaders as CoreRawHeaders } from "@azure/core-rest-pipeline";

/**
* Converts a REST client headers to core-rest-pipeline compatible headers
* REST clients allow headers to be number, boolean or strings, this function
* stringifies these headers
* @returns all headers values converted to strings
Comment thread
deyaaeldeen marked this conversation as resolved.
* @internal
*/
export function toCoreRawHeaders(headers: RawHttpHeaders = {}): CoreRawHeaders {
const coreHeaders: CoreRawHeaders = {};
for (const [name, value] of Object.entries(headers)) {
coreHeaders[name] = String(value);
}
return coreHeaders;
}
2 changes: 1 addition & 1 deletion sdk/core/core-client-rest/src/pathClientTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { RawHttpHeaders } from "@azure/core-rest-pipeline";
import { RawHttpHeaders } from "./common";

/**
* Shape of the default request parameters, this may be overriden by the specific
Expand Down
4 changes: 3 additions & 1 deletion sdk/core/core-client-rest/src/restError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { PathUncheckedResponse } from "./getClient";
import { RestError, PipelineResponse, createHttpHeaders } from "@azure/core-rest-pipeline";
import { toCoreRawHeaders } from "./headersHelper";

/**
* Creates a rest error from a PathUnchecked response
Expand All @@ -16,8 +17,9 @@ export function createRestError(message: string, response: PathUncheckedResponse
}

function toPipelineResponse(response: PathUncheckedResponse): PipelineResponse {
const headers = toCoreRawHeaders(response.headers);
return {
headers: createHttpHeaders(response.headers),
headers: createHttpHeaders(headers),
request: response.request,
status: statusCodeToNumber(response.status) ?? -1,
};
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client-rest/src/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
createPipelineRequest,
HttpMethods,
Pipeline,
RawHttpHeaders,
} from "@azure/core-rest-pipeline";
import { RawHttpHeaders } from "./common";
import { getCachedDefaultHttpsClient } from "./clientHelpers";
import { RequestParameters } from "./pathClientTypes";
import { HttpResponse } from "./common";
Expand Down
20 changes: 20 additions & 0 deletions sdk/core/core-client-rest/test/sendRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ describe("sendRequest", () => {
await sendRequest("POST", mockBaseUrl, mockPipeline, { headers: { foo: "foo" } });
});

it("should set a boolean header", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
assert.equal(request.headers.get("foo"), "true");
return { headers: createHttpHeaders() } as PipelineResponse;
};

await sendRequest("POST", mockBaseUrl, mockPipeline, { headers: { foo: true } });
});

it("should set a number header", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
assert.equal(request.headers.get("foo"), "123");
return { headers: createHttpHeaders() } as PipelineResponse;
};

await sendRequest("POST", mockBaseUrl, mockPipeline, { headers: { foo: 123 } });
});

it("should set octet-stream when binary body", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
Expand Down