@typespec/ts-http-runtime 1.0.0-alpha.20240426.4 → 1.0.0-alpha.20240429.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/client/multipart.d.ts +42 -0
- package/dist/browser/client/multipart.d.ts.map +1 -0
- package/dist/browser/client/multipart.js +122 -0
- package/dist/browser/client/multipart.js.map +1 -0
- package/dist/browser/client/sendRequest.d.ts.map +1 -1
- package/dist/browser/client/sendRequest.js +11 -35
- package/dist/browser/client/sendRequest.js.map +1 -1
- package/dist/browser/util/typeGuards.d.ts +1 -0
- package/dist/browser/util/typeGuards.d.ts.map +1 -1
- package/dist/browser/util/typeGuards.js +7 -0
- package/dist/browser/util/typeGuards.js.map +1 -1
- package/dist/commonjs/client/multipart.d.ts +42 -0
- package/dist/commonjs/client/multipart.d.ts.map +1 -0
- package/dist/commonjs/client/multipart.js +127 -0
- package/dist/commonjs/client/multipart.js.map +1 -0
- package/dist/commonjs/client/sendRequest.d.ts.map +1 -1
- package/dist/commonjs/client/sendRequest.js +11 -35
- package/dist/commonjs/client/sendRequest.js.map +1 -1
- package/dist/commonjs/util/typeGuards.d.ts +1 -0
- package/dist/commonjs/util/typeGuards.d.ts.map +1 -1
- package/dist/commonjs/util/typeGuards.js +9 -1
- package/dist/commonjs/util/typeGuards.js.map +1 -1
- package/dist/esm/client/multipart.d.ts +42 -0
- package/dist/esm/client/multipart.d.ts.map +1 -0
- package/dist/esm/client/multipart.js +122 -0
- package/dist/esm/client/multipart.js.map +1 -0
- package/dist/esm/client/sendRequest.d.ts.map +1 -1
- package/dist/esm/client/sendRequest.js +11 -35
- package/dist/esm/client/sendRequest.js.map +1 -1
- package/dist/esm/util/typeGuards.d.ts +1 -0
- package/dist/esm/util/typeGuards.d.ts.map +1 -1
- package/dist/esm/util/typeGuards.js +7 -0
- package/dist/esm/util/typeGuards.js.map +1 -1
- package/dist/react-native/client/multipart.d.ts +42 -0
- package/dist/react-native/client/multipart.d.ts.map +1 -0
- package/dist/react-native/client/multipart.js +122 -0
- package/dist/react-native/client/multipart.js.map +1 -0
- package/dist/react-native/client/sendRequest.d.ts.map +1 -1
- package/dist/react-native/client/sendRequest.js +11 -35
- package/dist/react-native/client/sendRequest.js.map +1 -1
- package/dist/react-native/util/typeGuards.d.ts +1 -0
- package/dist/react-native/util/typeGuards.d.ts.map +1 -1
- package/dist/react-native/util/typeGuards.js +7 -0
- package/dist/react-native/util/typeGuards.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { BodyPart, MultipartRequestBody, RawHttpHeadersInput } from "../interfaces.js";
|
|
2
|
+
/**
|
|
3
|
+
* Describes a single part in a multipart body.
|
|
4
|
+
*/
|
|
5
|
+
export interface PartDescriptor {
|
|
6
|
+
/**
|
|
7
|
+
* Content type of this part. If set, this value will be used to set the Content-Type MIME header for this part, although explicitly
|
|
8
|
+
* setting the Content-Type header in the headers bag will override this value. If set to `null`, no content type will be inferred from
|
|
9
|
+
* the body field. Otherwise, the value of the Content-Type MIME header will be inferred based on the type of the body.
|
|
10
|
+
*/
|
|
11
|
+
contentType?: string | null;
|
|
12
|
+
/**
|
|
13
|
+
* The disposition type of this part (for example, "form-data" for parts making up a multipart/form-data request). If set, this value
|
|
14
|
+
* will be used to set the Content-Disposition MIME header for this part, in addition to the `name` and `filename` properties.
|
|
15
|
+
* If the `name` or `filename` properties are set while `dispositionType` is left undefined, `dispositionType` will default to "form-data".
|
|
16
|
+
*
|
|
17
|
+
* Explicitly setting the Content-Disposition header in the headers bag will override this value.
|
|
18
|
+
*/
|
|
19
|
+
dispositionType?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The field name associated with this part. This value will be used to construct the Content-Disposition header,
|
|
22
|
+
* along with the `dispositionType` and `filename` properties, if the header has not been set in the `headers` bag.
|
|
23
|
+
*/
|
|
24
|
+
name?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The file name of the content if it is a file. This value will be used to construct the Content-Disposition header,
|
|
27
|
+
* along with the `dispositionType` and `name` properties, if the header has not been set in the `headers` bag.
|
|
28
|
+
*/
|
|
29
|
+
filename?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The multipart headers for this part of the multipart body. Values of the Content-Type and Content-Disposition headers set in the headers bag
|
|
32
|
+
* will take precedence over those computed from the request body or the contentType, dispositionType, name, and filename fields on this object.
|
|
33
|
+
*/
|
|
34
|
+
headers?: RawHttpHeadersInput;
|
|
35
|
+
/**
|
|
36
|
+
* The body of this part of the multipart request.
|
|
37
|
+
*/
|
|
38
|
+
body?: unknown;
|
|
39
|
+
}
|
|
40
|
+
export declare function buildBodyPart(descriptor: PartDescriptor): BodyPart;
|
|
41
|
+
export declare function buildMultipartBody(parts: PartDescriptor[]): MultipartRequestBody;
|
|
42
|
+
//# sourceMappingURL=multipart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipart.d.ts","sourceRoot":"","sources":["../../../src/client/multipart.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAMvF;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAE5B;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAE9B;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AA8HD,wBAAgB,aAAa,CAAC,UAAU,EAAE,cAAc,GAAG,QAAQ,CAkBlE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,cAAc,EAAE,GAAG,oBAAoB,CAEhF"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
import { RestError } from "../restError.js";
|
|
4
|
+
import { createHttpHeaders } from "../httpHeaders.js";
|
|
5
|
+
import { stringToUint8Array } from "../util/bytesEncoding.js";
|
|
6
|
+
import { isBinaryBody } from "../util/typeGuards.js";
|
|
7
|
+
/**
|
|
8
|
+
* Get value of a header in the part descriptor ignoring case
|
|
9
|
+
*/
|
|
10
|
+
function getHeaderValue(descriptor, headerName) {
|
|
11
|
+
if (descriptor.headers) {
|
|
12
|
+
const actualHeaderName = Object.keys(descriptor.headers).find((x) => x.toLowerCase() === headerName.toLowerCase());
|
|
13
|
+
if (actualHeaderName) {
|
|
14
|
+
return descriptor.headers[actualHeaderName];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
function getPartContentType(descriptor) {
|
|
20
|
+
const contentTypeHeader = getHeaderValue(descriptor, "content-type");
|
|
21
|
+
if (contentTypeHeader) {
|
|
22
|
+
return contentTypeHeader;
|
|
23
|
+
}
|
|
24
|
+
// Special value of null means content type is to be omitted
|
|
25
|
+
if (descriptor.contentType === null) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
if (descriptor.contentType) {
|
|
29
|
+
return descriptor.contentType;
|
|
30
|
+
}
|
|
31
|
+
const { body } = descriptor;
|
|
32
|
+
if (body === null || body === undefined) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
if (typeof body === "string" || typeof body === "number" || typeof body === "boolean") {
|
|
36
|
+
return "text/plain; charset=UTF-8";
|
|
37
|
+
}
|
|
38
|
+
if (body instanceof Blob) {
|
|
39
|
+
return body.type || "application/octet-stream";
|
|
40
|
+
}
|
|
41
|
+
if (isBinaryBody(body)) {
|
|
42
|
+
return "application/octet-stream";
|
|
43
|
+
}
|
|
44
|
+
// arbitrary non-text object -> generic JSON content type by default. We will try to JSON.stringify the body.
|
|
45
|
+
return "application/json; charset=UTF-8";
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Enclose value in quotes and escape special characters, for use in the Content-Disposition header
|
|
49
|
+
*/
|
|
50
|
+
function escapeDispositionField(value) {
|
|
51
|
+
return JSON.stringify(value);
|
|
52
|
+
}
|
|
53
|
+
function getContentDisposition(descriptor) {
|
|
54
|
+
var _a;
|
|
55
|
+
const contentDispositionHeader = getHeaderValue(descriptor, "content-disposition");
|
|
56
|
+
if (contentDispositionHeader) {
|
|
57
|
+
return contentDispositionHeader;
|
|
58
|
+
}
|
|
59
|
+
if (descriptor.dispositionType === undefined &&
|
|
60
|
+
descriptor.name === undefined &&
|
|
61
|
+
descriptor.filename === undefined) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const dispositionType = (_a = descriptor.dispositionType) !== null && _a !== void 0 ? _a : "form-data";
|
|
65
|
+
let disposition = dispositionType;
|
|
66
|
+
if (descriptor.name) {
|
|
67
|
+
disposition += `; name=${escapeDispositionField(descriptor.name)}`;
|
|
68
|
+
}
|
|
69
|
+
let filename = undefined;
|
|
70
|
+
if (descriptor.filename) {
|
|
71
|
+
filename = descriptor.filename;
|
|
72
|
+
}
|
|
73
|
+
else if (typeof File !== "undefined" && descriptor.body instanceof File) {
|
|
74
|
+
const filenameFromFile = descriptor.body.name;
|
|
75
|
+
if (filenameFromFile !== "") {
|
|
76
|
+
filename = filenameFromFile;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (filename) {
|
|
80
|
+
disposition += `; filename=${escapeDispositionField(filename)}`;
|
|
81
|
+
}
|
|
82
|
+
return disposition;
|
|
83
|
+
}
|
|
84
|
+
function normalizeBody(body, contentType) {
|
|
85
|
+
if (body === undefined) {
|
|
86
|
+
// zero-length body
|
|
87
|
+
return new Uint8Array([]);
|
|
88
|
+
}
|
|
89
|
+
// binary and primitives should go straight on the wire regardless of content type
|
|
90
|
+
if (isBinaryBody(body)) {
|
|
91
|
+
return body;
|
|
92
|
+
}
|
|
93
|
+
if (typeof body === "string" || typeof body === "number" || typeof body === "boolean") {
|
|
94
|
+
return stringToUint8Array(String(body), "utf-8");
|
|
95
|
+
}
|
|
96
|
+
// stringify objects for JSON-ish content types e.g. application/json, application/merge-patch+json, application/vnd.oci.manifest.v1+json, application.json; charset=UTF-8
|
|
97
|
+
if (contentType && /application\/(.+\+)?json(;.+)?/i.test(String(contentType))) {
|
|
98
|
+
return stringToUint8Array(JSON.stringify(body), "utf-8");
|
|
99
|
+
}
|
|
100
|
+
throw new RestError(`Unsupported body/content-type combination: ${body}, ${contentType}`);
|
|
101
|
+
}
|
|
102
|
+
export function buildBodyPart(descriptor) {
|
|
103
|
+
var _a;
|
|
104
|
+
const contentType = getPartContentType(descriptor);
|
|
105
|
+
const contentDisposition = getContentDisposition(descriptor);
|
|
106
|
+
const headers = createHttpHeaders((_a = descriptor.headers) !== null && _a !== void 0 ? _a : {});
|
|
107
|
+
if (contentType) {
|
|
108
|
+
headers.set("content-type", contentType);
|
|
109
|
+
}
|
|
110
|
+
if (contentDisposition) {
|
|
111
|
+
headers.set("content-disposition", contentDisposition);
|
|
112
|
+
}
|
|
113
|
+
const body = normalizeBody(descriptor.body, contentType);
|
|
114
|
+
return {
|
|
115
|
+
headers,
|
|
116
|
+
body,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function buildMultipartBody(parts) {
|
|
120
|
+
return { parts: parts.map(buildBodyPart) };
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=multipart.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipart.js","sourceRoot":"","sources":["../../../src/client/multipart.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,E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AAC,cAAc,EAAE,WAAW,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,kBAAkB,EAAE,CAAC;QACvB,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,kBAAkB,CAAC,CAAC;IACzD,CAAC;IAED,MAAM,IAAI,GAAG,aAAa,CAAC,UAAU,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAEzD,OAAO;QACL,OAAO;QACP,IAAI;KACL,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,KAAuB;IACxD,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,CAAC;AAC7C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { BodyPart, MultipartRequestBody, RawHttpHeadersInput } from \"../interfaces.js\";\nimport { RestError } from \"../restError.js\";\nimport { createHttpHeaders } from \"../httpHeaders.js\";\nimport { stringToUint8Array } from \"../util/bytesEncoding.js\";\nimport { isBinaryBody } from \"../util/typeGuards.js\";\n\n/**\n * Describes a single part in a multipart body.\n */\nexport interface PartDescriptor {\n /**\n * Content type of this part. If set, this value will be used to set the Content-Type MIME header for this part, although explicitly\n * setting the Content-Type header in the headers bag will override this value. If set to `null`, no content type will be inferred from\n * the body field. Otherwise, the value of the Content-Type MIME header will be inferred based on the type of the body.\n */\n contentType?: string | null;\n\n /**\n * The disposition type of this part (for example, \"form-data\" for parts making up a multipart/form-data request). If set, this value\n * will be used to set the Content-Disposition MIME header for this part, in addition to the `name` and `filename` properties.\n * If the `name` or `filename` properties are set while `dispositionType` is left undefined, `dispositionType` will default to \"form-data\".\n *\n * Explicitly setting the Content-Disposition header in the headers bag will override this value.\n */\n dispositionType?: string;\n\n /**\n * The field name associated with this part. This value will be used to construct the Content-Disposition header,\n * along with the `dispositionType` and `filename` properties, if the header has not been set in the `headers` bag.\n */\n name?: string;\n\n /**\n * The file name of the content if it is a file. This value will be used to construct the Content-Disposition header,\n * along with the `dispositionType` and `name` properties, if the header has not been set in the `headers` bag.\n */\n filename?: string;\n\n /**\n * The multipart headers for this part of the multipart body. Values of the Content-Type and Content-Disposition headers set in the headers bag\n * will take precedence over those computed from the request body or the contentType, dispositionType, name, and filename fields on this object.\n */\n headers?: RawHttpHeadersInput;\n\n /**\n * The body of this part of the multipart request.\n */\n body?: unknown;\n}\n\ntype MultipartBodyType = BodyPart[\"body\"];\n\ntype HeaderValue = RawHttpHeadersInput[string];\n\n/**\n * Get value of a header in the part descriptor ignoring case\n */\nfunction getHeaderValue(descriptor: PartDescriptor, headerName: string): HeaderValue | undefined {\n if (descriptor.headers) {\n const actualHeaderName = Object.keys(descriptor.headers).find(\n (x) => x.toLowerCase() === headerName.toLowerCase(),\n );\n if (actualHeaderName) {\n return descriptor.headers[actualHeaderName];\n }\n }\n\n return undefined;\n}\n\nfunction getPartContentType(descriptor: PartDescriptor): HeaderValue | undefined {\n const contentTypeHeader = getHeaderValue(descriptor, \"content-type\");\n if (contentTypeHeader) {\n return contentTypeHeader;\n }\n\n // Special value of null means content type is to be omitted\n if (descriptor.contentType === null) {\n return undefined;\n }\n\n if (descriptor.contentType) {\n return descriptor.contentType;\n }\n\n const { body } = descriptor;\n\n if (body === null || body === undefined) {\n return undefined;\n }\n\n if (typeof body === \"string\" || typeof body === \"number\" || typeof body === \"boolean\") {\n return \"text/plain; charset=UTF-8\";\n }\n\n if (body instanceof Blob) {\n return body.type || \"application/octet-stream\";\n }\n\n if (isBinaryBody(body)) {\n return \"application/octet-stream\";\n }\n\n // arbitrary non-text object -> generic JSON content type by default. We will try to JSON.stringify the body.\n return \"application/json; charset=UTF-8\";\n}\n\n/**\n * Enclose value in quotes and escape special characters, for use in the Content-Disposition header\n */\nfunction escapeDispositionField(value: string): string {\n return JSON.stringify(value);\n}\n\nfunction getContentDisposition(descriptor: PartDescriptor): HeaderValue | undefined {\n const contentDispositionHeader = getHeaderValue(descriptor, \"content-disposition\");\n if (contentDispositionHeader) {\n return contentDispositionHeader;\n }\n\n if (\n descriptor.dispositionType === undefined &&\n descriptor.name === undefined &&\n descriptor.filename === undefined\n ) {\n return undefined;\n }\n\n const dispositionType = descriptor.dispositionType ?? \"form-data\";\n\n let disposition = dispositionType;\n if (descriptor.name) {\n disposition += `; name=${escapeDispositionField(descriptor.name)}`;\n }\n\n let filename: string | undefined = undefined;\n if (descriptor.filename) {\n filename = descriptor.filename;\n } else if (typeof File !== \"undefined\" && descriptor.body instanceof File) {\n const filenameFromFile = (descriptor.body as File).name;\n if (filenameFromFile !== \"\") {\n filename = filenameFromFile;\n }\n }\n\n if (filename) {\n disposition += `; filename=${escapeDispositionField(filename)}`;\n }\n\n return disposition;\n}\n\nfunction normalizeBody(body?: unknown, contentType?: HeaderValue): MultipartBodyType {\n if (body === undefined) {\n // zero-length body\n return new Uint8Array([]);\n }\n\n // binary and primitives should go straight on the wire regardless of content type\n if (isBinaryBody(body)) {\n return body;\n }\n if (typeof body === \"string\" || typeof body === \"number\" || typeof body === \"boolean\") {\n return stringToUint8Array(String(body), \"utf-8\");\n }\n\n // stringify objects for JSON-ish content types e.g. application/json, application/merge-patch+json, application/vnd.oci.manifest.v1+json, application.json; charset=UTF-8\n if (contentType && /application\\/(.+\\+)?json(;.+)?/i.test(String(contentType))) {\n return stringToUint8Array(JSON.stringify(body), \"utf-8\");\n }\n\n throw new RestError(`Unsupported body/content-type combination: ${body}, ${contentType}`);\n}\n\nexport function buildBodyPart(descriptor: PartDescriptor): BodyPart {\n const contentType = getPartContentType(descriptor);\n const contentDisposition = getContentDisposition(descriptor);\n const headers = createHttpHeaders(descriptor.headers ?? {});\n\n if (contentType) {\n headers.set(\"content-type\", contentType);\n }\n if (contentDisposition) {\n headers.set(\"content-disposition\", contentDisposition);\n }\n\n const body = normalizeBody(descriptor.body, contentType);\n\n return {\n headers,\n body,\n };\n}\n\nexport function buildMultipartBody(parts: PartDescriptor[]): MultipartRequestBody {\n return { parts: parts.map(buildBodyPart) };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sendRequest.d.ts","sourceRoot":"","sources":["../../../src/client/sendRequest.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"sendRequest.d.ts","sourceRoot":"","sources":["../../../src/client/sendRequest.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,UAAU,EACV,WAAW,EAKZ,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAK1C,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAG9D;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,WAAW,EACnB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,QAAQ,EAClB,OAAO,GAAE,yBAA8B,EACvC,gBAAgB,CAAC,EAAE,UAAU,GAC5B,OAAO,CAAC,YAAY,CAAC,CAoBvB;AAuCD,MAAM,WAAW,yBAA0B,SAAQ,iBAAiB;IAClE,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B"}
|
|
@@ -5,7 +5,7 @@ import { createHttpHeaders } from "../httpHeaders.js";
|
|
|
5
5
|
import { createPipelineRequest } from "../pipelineRequest.js";
|
|
6
6
|
import { getCachedDefaultHttpsClient } from "./clientHelpers.js";
|
|
7
7
|
import { isReadableStream } from "../util/typeGuards.js";
|
|
8
|
-
import {
|
|
8
|
+
import { buildMultipartBody } from "./multipart.js";
|
|
9
9
|
/**
|
|
10
10
|
* Helper function to send request used by the client
|
|
11
11
|
* @param method - method to use to send the request
|
|
@@ -69,8 +69,8 @@ function getContentType(body) {
|
|
|
69
69
|
function buildPipelineRequest(method, url, options = {}) {
|
|
70
70
|
var _a, _b, _c;
|
|
71
71
|
const requestContentType = getRequestContentType(options);
|
|
72
|
-
const { body,
|
|
73
|
-
const hasContent = body !== undefined ||
|
|
72
|
+
const { body, multipartBody } = getRequestBody(options.body, requestContentType);
|
|
73
|
+
const hasContent = body !== undefined || multipartBody !== undefined;
|
|
74
74
|
const headers = createHttpHeaders(Object.assign(Object.assign(Object.assign({}, (options.headers ? options.headers : {})), { accept: (_c = (_a = options.accept) !== null && _a !== void 0 ? _a : (_b = options.headers) === null || _b === void 0 ? void 0 : _b.accept) !== null && _c !== void 0 ? _c : "application/json" }), (hasContent &&
|
|
75
75
|
requestContentType && {
|
|
76
76
|
"content-type": requestContentType,
|
|
@@ -79,7 +79,7 @@ function buildPipelineRequest(method, url, options = {}) {
|
|
|
79
79
|
url,
|
|
80
80
|
method,
|
|
81
81
|
body,
|
|
82
|
-
|
|
82
|
+
multipartBody,
|
|
83
83
|
headers,
|
|
84
84
|
allowInsecureConnection: options.allowInsecureConnection,
|
|
85
85
|
tracingOptions: options.tracingOptions,
|
|
@@ -100,6 +100,9 @@ function getRequestBody(body, contentType = "") {
|
|
|
100
100
|
if (body === undefined) {
|
|
101
101
|
return { body: undefined };
|
|
102
102
|
}
|
|
103
|
+
if (typeof FormData !== "undefined" && body instanceof FormData) {
|
|
104
|
+
return { body };
|
|
105
|
+
}
|
|
103
106
|
if (isReadableStream(body)) {
|
|
104
107
|
return { body };
|
|
105
108
|
}
|
|
@@ -112,9 +115,10 @@ function getRequestBody(body, contentType = "") {
|
|
|
112
115
|
}
|
|
113
116
|
switch (firstType) {
|
|
114
117
|
case "multipart/form-data":
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
if (Array.isArray(body)) {
|
|
119
|
+
return { multipartBody: buildMultipartBody(body) };
|
|
120
|
+
}
|
|
121
|
+
return { body: JSON.stringify(body) };
|
|
118
122
|
case "text/plain":
|
|
119
123
|
return { body: String(body) };
|
|
120
124
|
default:
|
|
@@ -124,34 +128,6 @@ function getRequestBody(body, contentType = "") {
|
|
|
124
128
|
return { body: JSON.stringify(body) };
|
|
125
129
|
}
|
|
126
130
|
}
|
|
127
|
-
function isRLCFormDataValue(value) {
|
|
128
|
-
return (typeof value === "string" ||
|
|
129
|
-
value instanceof Uint8Array ||
|
|
130
|
-
// We don't do `instanceof Blob` since we should also accept polyfills of e.g. File in Node.
|
|
131
|
-
typeof value.stream === "function");
|
|
132
|
-
}
|
|
133
|
-
function isRLCFormDataInput(body) {
|
|
134
|
-
return (body !== undefined &&
|
|
135
|
-
body instanceof Object &&
|
|
136
|
-
Object.values(body).every((value) => isRLCFormDataValue(value) || (Array.isArray(value) && value.every(isRLCFormDataValue))));
|
|
137
|
-
}
|
|
138
|
-
function processFormDataValue(value) {
|
|
139
|
-
return value instanceof Uint8Array ? createFile(value, "blob") : value;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Checks if binary data is in Uint8Array format, if so wrap it in a Blob
|
|
143
|
-
* to send over the wire
|
|
144
|
-
*/
|
|
145
|
-
function processFormData(formData) {
|
|
146
|
-
const processedFormData = {};
|
|
147
|
-
for (const element in formData) {
|
|
148
|
-
const value = formData[element];
|
|
149
|
-
processedFormData[element] = Array.isArray(value)
|
|
150
|
-
? value.map(processFormDataValue)
|
|
151
|
-
: processFormDataValue(value);
|
|
152
|
-
}
|
|
153
|
-
return processedFormData;
|
|
154
|
-
}
|
|
155
131
|
/**
|
|
156
132
|
* Prepares the response body
|
|
157
133
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sendRequest.js","sourceRoot":"","sources":["../../../src/client/sendRequest.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAWlC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;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AG,MAAA,GAAG,CAAC,IAAI,mCAAI,SAAS,CAAC,WAAW,CAAC;IAClD,OAAO,IAAI,SAAS,CAAC,GAAG,EAAE;QACxB,IAAI,EAAE,OAAO;QACb,UAAU,EAAE,QAAQ,CAAC,MAAM;QAC3B,OAAO,EAAE,QAAQ,CAAC,OAAO;QACzB,QAAQ,EAAE,QAAQ;KACnB,CAAC,CAAC;AACL,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport {\n FormDataMap,\n FormDataValue,\n HttpClient,\n HttpMethods,\n PipelineRequest,\n PipelineResponse,\n RequestBodyType,\n} from \"../interfaces.js\";\nimport { RestError } from \"../restError.js\";\nimport { Pipeline } from \"../pipeline.js\";\nimport { createHttpHeaders } from \"../httpHeaders.js\";\nimport { createPipelineRequest } from \"../pipelineRequest.js\";\nimport { getCachedDefaultHttpsClient } from \"./clientHelpers.js\";\nimport { isReadableStream } from \"../util/typeGuards.js\";\nimport { HttpResponse, RequestParameters } from \"./common.js\";\nimport { createFile } from \"../util/file.js\";\n\n/**\n * Helper function to send request used by the client\n * @param method - method to use to send the request\n * @param url - url to send the request to\n * @param pipeline - pipeline with the policies to run when sending the request\n * @param options - request options\n * @param customHttpClient - a custom HttpClient to use when making the request\n * @returns returns and HttpResponse\n */\nexport async function sendRequest(\n method: HttpMethods,\n url: string,\n pipeline: Pipeline,\n options: InternalRequestParameters = {},\n customHttpClient?: HttpClient,\n): Promise<HttpResponse> {\n const httpClient = customHttpClient ?? getCachedDefaultHttpsClient();\n const request = buildPipelineRequest(method, url, options);\n const response = await pipeline.sendRequest(httpClient, request);\n const headers = response.headers.toJSON();\n const stream = response.readableStreamBody ?? response.browserStreamBody;\n const parsedBody =\n options.responseAsStream || stream !== undefined ? undefined : getResponseBody(response);\n const body = stream ?? parsedBody;\n\n if (options?.onResponse) {\n options.onResponse({ ...response, request, rawHeaders: headers, parsedBody });\n }\n\n return {\n request,\n headers,\n status: `${response.status}`,\n body,\n };\n}\n\n/**\n * Function to determine the request content type\n * @param options - request options InternalRequestParameters\n * @returns returns the content-type\n */\nfunction getRequestContentType(options: InternalRequestParameters = {}): string {\n return (\n options.contentType ??\n (options.headers?.[\"content-type\"] as string) ??\n getContentType(options.body)\n );\n}\n\n/**\n * Function to determine the content-type of a body\n * this is used if an explicit content-type is not provided\n * @param body - body in the request\n * @returns returns the content-type\n */\nfunction getContentType(body: any): string | undefined {\n if (ArrayBuffer.isView(body)) {\n return \"application/octet-stream\";\n }\n\n if (typeof body === \"string\") {\n try {\n JSON.parse(body);\n return \"application/json; charset=UTF-8\";\n } catch (error: any) {\n // If we fail to parse the body, it is not json\n return undefined;\n }\n }\n // By default return json\n return \"application/json; charset=UTF-8\";\n}\n\nexport interface InternalRequestParameters extends RequestParameters {\n responseAsStream?: boolean;\n}\n\nfunction buildPipelineRequest(\n method: HttpMethods,\n url: string,\n options: InternalRequestParameters = {},\n): PipelineRequest {\n const requestContentType = getRequestContentType(options);\n const { body, formData } = getRequestBody(options.body, requestContentType);\n const hasContent = body !== undefined || formData !== undefined;\n\n const headers = createHttpHeaders({\n ...(options.headers ? options.headers : {}),\n accept: options.accept ?? options.headers?.accept ?? \"application/json\",\n ...(hasContent &&\n requestContentType && {\n \"content-type\": requestContentType,\n }),\n });\n\n return createPipelineRequest({\n url,\n method,\n body,\n formData,\n headers,\n allowInsecureConnection: options.allowInsecureConnection,\n tracingOptions: options.tracingOptions,\n abortSignal: options.abortSignal,\n onUploadProgress: options.onUploadProgress,\n onDownloadProgress: options.onDownloadProgress,\n timeout: options.timeout,\n enableBrowserStreams: true,\n streamResponseStatusCodes: options.responseAsStream\n ? new Set([Number.POSITIVE_INFINITY])\n : undefined,\n });\n}\n\ninterface RequestBody {\n body?: RequestBodyType;\n formData?: FormDataMap;\n}\n\n/**\n * Prepares the body before sending the request\n */\nfunction getRequestBody(body?: unknown, contentType: string = \"\"): RequestBody {\n if (body === undefined) {\n return { body: undefined };\n }\n\n if (isReadableStream(body)) {\n return { body };\n }\n\n const firstType = contentType.split(\";\")[0];\n\n if (firstType === \"application/json\") {\n return { body: JSON.stringify(body) };\n }\n\n if (ArrayBuffer.isView(body)) {\n return { body: body instanceof Uint8Array ? body : JSON.stringify(body) };\n }\n\n switch (firstType) {\n case \"multipart/form-data\":\n return isRLCFormDataInput(body)\n ? { formData: processFormData(body) }\n : { body: JSON.stringify(body) };\n case \"text/plain\":\n return { body: String(body) };\n default:\n if (typeof body === \"string\") {\n return { body };\n }\n return { body: JSON.stringify(body) };\n }\n}\n\n/**\n * Union of possible input types for multipart/form-data values that are accepted by RLCs.\n * This extends the default FormDataValue type to include Uint8Array, which is accepted as an input by RLCs.\n */\ntype RLCFormDataValue = FormDataValue | Uint8Array;\n\n/**\n * Input shape for a form data body type as generated by an RLC\n */\ntype RLCFormDataInput = Record<string, RLCFormDataValue | RLCFormDataValue[]>;\n\nfunction isRLCFormDataValue(value: unknown): value is RLCFormDataValue {\n return (\n typeof value === \"string\" ||\n value instanceof Uint8Array ||\n // We don't do `instanceof Blob` since we should also accept polyfills of e.g. File in Node.\n typeof (value as Blob).stream === \"function\"\n );\n}\n\nfunction isRLCFormDataInput(body: unknown): body is RLCFormDataInput {\n return (\n body !== undefined &&\n body instanceof Object &&\n Object.values(body).every(\n (value) =>\n isRLCFormDataValue(value) || (Array.isArray(value) && value.every(isRLCFormDataValue)),\n )\n );\n}\n\nfunction processFormDataValue(value: RLCFormDataValue): FormDataValue {\n return value instanceof Uint8Array ? createFile(value, \"blob\") : value;\n}\n\n/**\n * Checks if binary data is in Uint8Array format, if so wrap it in a Blob\n * to send over the wire\n */\nfunction processFormData(formData: RLCFormDataInput): FormDataMap {\n const processedFormData: FormDataMap = {};\n\n for (const element in formData) {\n const value = formData[element];\n\n processedFormData[element] = Array.isArray(value)\n ? value.map(processFormDataValue)\n : processFormDataValue(value);\n }\n\n return processedFormData;\n}\n\n/**\n * Prepares the response body\n */\nfunction getResponseBody(response: PipelineResponse): RequestBodyType | undefined {\n // Set the default response type\n const contentType = response.headers.get(\"content-type\") ?? \"\";\n const firstType = contentType.split(\";\")[0];\n const bodyToParse: string = response.bodyAsText ?? \"\";\n\n if (firstType === \"text/plain\") {\n return String(bodyToParse);\n }\n // Default to \"application/json\" and fallback to string;\n try {\n return bodyToParse ? JSON.parse(bodyToParse) : undefined;\n } catch (error: any) {\n // If we were supposed to get a JSON object and failed to\n // parse, throw a parse error\n if (firstType === \"application/json\") {\n throw createParseError(response, error);\n }\n\n // We are not sure how to handle the response so we return it as\n // plain text.\n return String(bodyToParse);\n }\n}\n\nfunction createParseError(response: PipelineResponse, err: any): RestError {\n const msg = `Error \"${err}\" occurred while parsing the response body - ${response.bodyAsText}.`;\n const errCode = err.code ?? RestError.PARSE_ERROR;\n return new RestError(msg, {\n code: errCode,\n statusCode: response.status,\n request: response.request,\n response: response,\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"sendRequest.js","sourceRoot":"","sources":["../../../src/client/sendRequest.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAUlC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAkB,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpE;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,MAAmB,EACnB,GAAW,EACX,QAAkB,EAClB,UAAqC,EAAE,EACvC,gBAA6B;;IAE7B,MAAM,UAAU,GAAG,gBAAgB,aAAhB,gBAAgB,cAAhB,gBAAgB,GAAI,2BAA2B,EAAE,CAAC;IACrE,MAAM,OAAO,GAAG,oBAAoB,CAAC,MA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AChG,MAAM,OAAO,GAAG,MAAA,GAAG,CAAC,IAAI,mCAAI,SAAS,CAAC,WAAW,CAAC;IAClD,OAAO,IAAI,SAAS,CAAC,GAAG,EAAE;QACxB,IAAI,EAAE,OAAO;QACb,UAAU,EAAE,QAAQ,CAAC,MAAM;QAC3B,OAAO,EAAE,QAAQ,CAAC,OAAO;QACzB,QAAQ,EAAE,QAAQ;KACnB,CAAC,CAAC;AACL,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport {\n HttpClient,\n HttpMethods,\n MultipartRequestBody,\n PipelineRequest,\n PipelineResponse,\n RequestBodyType,\n} from \"../interfaces.js\";\nimport { RestError } from \"../restError.js\";\nimport { Pipeline } from \"../pipeline.js\";\nimport { createHttpHeaders } from \"../httpHeaders.js\";\nimport { createPipelineRequest } from \"../pipelineRequest.js\";\nimport { getCachedDefaultHttpsClient } from \"./clientHelpers.js\";\nimport { isReadableStream } from \"../util/typeGuards.js\";\nimport { HttpResponse, RequestParameters } from \"./common.js\";\nimport { PartDescriptor, buildMultipartBody } from \"./multipart.js\";\n\n/**\n * Helper function to send request used by the client\n * @param method - method to use to send the request\n * @param url - url to send the request to\n * @param pipeline - pipeline with the policies to run when sending the request\n * @param options - request options\n * @param customHttpClient - a custom HttpClient to use when making the request\n * @returns returns and HttpResponse\n */\nexport async function sendRequest(\n method: HttpMethods,\n url: string,\n pipeline: Pipeline,\n options: InternalRequestParameters = {},\n customHttpClient?: HttpClient,\n): Promise<HttpResponse> {\n const httpClient = customHttpClient ?? getCachedDefaultHttpsClient();\n const request = buildPipelineRequest(method, url, options);\n const response = await pipeline.sendRequest(httpClient, request);\n const headers = response.headers.toJSON();\n const stream = response.readableStreamBody ?? response.browserStreamBody;\n const parsedBody =\n options.responseAsStream || stream !== undefined ? undefined : getResponseBody(response);\n const body = stream ?? parsedBody;\n\n if (options?.onResponse) {\n options.onResponse({ ...response, request, rawHeaders: headers, parsedBody });\n }\n\n return {\n request,\n headers,\n status: `${response.status}`,\n body,\n };\n}\n\n/**\n * Function to determine the request content type\n * @param options - request options InternalRequestParameters\n * @returns returns the content-type\n */\nfunction getRequestContentType(options: InternalRequestParameters = {}): string {\n return (\n options.contentType ??\n (options.headers?.[\"content-type\"] as string) ??\n getContentType(options.body)\n );\n}\n\n/**\n * Function to determine the content-type of a body\n * this is used if an explicit content-type is not provided\n * @param body - body in the request\n * @returns returns the content-type\n */\nfunction getContentType(body: any): string | undefined {\n if (ArrayBuffer.isView(body)) {\n return \"application/octet-stream\";\n }\n\n if (typeof body === \"string\") {\n try {\n JSON.parse(body);\n return \"application/json; charset=UTF-8\";\n } catch (error: any) {\n // If we fail to parse the body, it is not json\n return undefined;\n }\n }\n // By default return json\n return \"application/json; charset=UTF-8\";\n}\n\nexport interface InternalRequestParameters extends RequestParameters {\n responseAsStream?: boolean;\n}\n\nfunction buildPipelineRequest(\n method: HttpMethods,\n url: string,\n options: InternalRequestParameters = {},\n): PipelineRequest {\n const requestContentType = getRequestContentType(options);\n const { body, multipartBody } = getRequestBody(options.body, requestContentType);\n const hasContent = body !== undefined || multipartBody !== undefined;\n\n const headers = createHttpHeaders({\n ...(options.headers ? options.headers : {}),\n accept: options.accept ?? options.headers?.accept ?? \"application/json\",\n ...(hasContent &&\n requestContentType && {\n \"content-type\": requestContentType,\n }),\n });\n\n return createPipelineRequest({\n url,\n method,\n body,\n multipartBody,\n headers,\n allowInsecureConnection: options.allowInsecureConnection,\n tracingOptions: options.tracingOptions,\n abortSignal: options.abortSignal,\n onUploadProgress: options.onUploadProgress,\n onDownloadProgress: options.onDownloadProgress,\n timeout: options.timeout,\n enableBrowserStreams: true,\n streamResponseStatusCodes: options.responseAsStream\n ? new Set([Number.POSITIVE_INFINITY])\n : undefined,\n });\n}\n\ninterface RequestBody {\n body?: RequestBodyType;\n multipartBody?: MultipartRequestBody;\n}\n\n/**\n * Prepares the body before sending the request\n */\nfunction getRequestBody(body?: unknown, contentType: string = \"\"): RequestBody {\n if (body === undefined) {\n return { body: undefined };\n }\n\n if (typeof FormData !== \"undefined\" && body instanceof FormData) {\n return { body };\n }\n\n if (isReadableStream(body)) {\n return { body };\n }\n\n const firstType = contentType.split(\";\")[0];\n\n if (firstType === \"application/json\") {\n return { body: JSON.stringify(body) };\n }\n\n if (ArrayBuffer.isView(body)) {\n return { body: body instanceof Uint8Array ? body : JSON.stringify(body) };\n }\n\n switch (firstType) {\n case \"multipart/form-data\":\n if (Array.isArray(body)) {\n return { multipartBody: buildMultipartBody(body as PartDescriptor[]) };\n }\n return { body: JSON.stringify(body) };\n case \"text/plain\":\n return { body: String(body) };\n default:\n if (typeof body === \"string\") {\n return { body };\n }\n return { body: JSON.stringify(body) };\n }\n}\n\n/**\n * Prepares the response body\n */\nfunction getResponseBody(response: PipelineResponse): RequestBodyType | undefined {\n // Set the default response type\n const contentType = response.headers.get(\"content-type\") ?? \"\";\n const firstType = contentType.split(\";\")[0];\n const bodyToParse: string = response.bodyAsText ?? \"\";\n\n if (firstType === \"text/plain\") {\n return String(bodyToParse);\n }\n // Default to \"application/json\" and fallback to string;\n try {\n return bodyToParse ? JSON.parse(bodyToParse) : undefined;\n } catch (error: any) {\n // If we were supposed to get a JSON object and failed to\n // parse, throw a parse error\n if (firstType === \"application/json\") {\n throw createParseError(response, error);\n }\n\n // We are not sure how to handle the response so we return it as\n // plain text.\n return String(bodyToParse);\n }\n}\n\nfunction createParseError(response: PipelineResponse, err: any): RestError {\n const msg = `Error \"${err}\" occurred while parsing the response body - ${response.bodyAsText}.`;\n const errCode = err.code ?? RestError.PARSE_ERROR;\n return new RestError(msg, {\n code: errCode,\n statusCode: response.status,\n request: response.request,\n response: response,\n });\n}\n"]}
|
|
@@ -18,6 +18,7 @@ export declare function isObjectWithProperties<Thing, PropertyName extends strin
|
|
|
18
18
|
export declare function objectHasProperty<Thing, PropertyName extends string>(thing: Thing, property: PropertyName): thing is Thing & Record<PropertyName, unknown>;
|
|
19
19
|
export declare function isNodeReadableStream(x: unknown): x is NodeJS.ReadableStream;
|
|
20
20
|
export declare function isWebReadableStream(x: unknown): x is ReadableStream;
|
|
21
|
+
export declare function isBinaryBody(body: unknown): body is Uint8Array | NodeJS.ReadableStream | ReadableStream<Uint8Array> | (() => NodeJS.ReadableStream) | (() => ReadableStream<Uint8Array>) | Blob;
|
|
21
22
|
export declare function isReadableStream(x: unknown): x is ReadableStream | NodeJS.ReadableStream;
|
|
22
23
|
export declare function isBlob(x: unknown): x is Blob;
|
|
23
24
|
//# sourceMappingURL=typeGuards.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGuards.d.ts","sourceRoot":"","sources":["../../../src/util/typeGuards.ts"],"names":[],"mappings":";AAGA;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,SAAS,GAAG,IAAI,GAAG,KAAK,IAAI,CAAC,CAEpE;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,YAAY,SAAS,MAAM,EACvE,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,YAAY,EAAE,GACzB,KAAK,IAAI,KAAK,GAAG,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC,CAYhD;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,YAAY,SAAS,MAAM,EAClE,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,YAAY,GACrB,KAAK,IAAI,KAAK,GAAG,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC,CAIhD;AAED,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,cAAc,CAE3E;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,cAAc,CAMnE;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,cAAc,GAAG,MAAM,CAAC,cAAc,CAExF;AAED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IAAI,IAAI,CAE5C"}
|
|
@@ -39,6 +39,13 @@ export function isWebReadableStream(x) {
|
|
|
39
39
|
typeof x.getReader === "function" &&
|
|
40
40
|
typeof x.tee === "function");
|
|
41
41
|
}
|
|
42
|
+
export function isBinaryBody(body) {
|
|
43
|
+
return (body !== undefined &&
|
|
44
|
+
(body instanceof Uint8Array ||
|
|
45
|
+
isReadableStream(body) ||
|
|
46
|
+
typeof body === "function" ||
|
|
47
|
+
body instanceof Blob));
|
|
48
|
+
}
|
|
42
49
|
export function isReadableStream(x) {
|
|
43
50
|
return isNodeReadableStream(x) || isWebReadableStream(x);
|
|
44
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeGuards.js","sourceRoot":"","sources":["../../../src/util/typeGuards.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAI,KAA2B;IACtD,OAAO,OAAO,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,IAAI,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CACpC,KAAY,EACZ,UAA0B;IAE1B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACnD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;QAClC,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,CAAC;YACxC,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAC/B,KAAY,EACZ,QAAsB;IAEtB,OAAO,CACL,SAAS,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,QAAQ,IAAK,KAAiC,CAChG,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,CAAU;IAC7C,OAAO,OAAO,CAAC,CAAC,IAAI,OAAQ,CAA2B,CAAC,MAAM,CAAC,KAAK,UAAU,CAAC,CAAC;AAClF,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,CAAU;IAC5C,OAAO,OAAO,CACZ,CAAC;QACC,OAAQ,CAAoB,CAAC,SAAS,KAAK,UAAU;QACrD,OAAQ,CAAoB,CAAC,GAAG,KAAK,UAAU,CAClD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,CAAU;IACzC,OAAO,oBAAoB,CAAC,CAAC,CAAC,IAAI,mBAAmB,CAAC,CAAC,CAAC,CAAC;AAC3D,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,CAAU;IAC/B,OAAO,OAAQ,CAAU,CAAC,MAAM,KAAK,UAAU,CAAC;AAClD,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Helper TypeGuard that checks if something is defined or not.\n * @param thing - Anything\n */\nexport function isDefined<T>(thing: T | undefined | null): thing is T {\n return typeof thing !== \"undefined\" && thing !== null;\n}\n\n/**\n * Helper TypeGuard that checks if the input is an object with the specified properties.\n * @param thing - Anything.\n * @param properties - The name of the properties that should appear in the object.\n */\nexport function isObjectWithProperties<Thing, PropertyName extends string>(\n thing: Thing,\n properties: PropertyName[],\n): thing is Thing & Record<PropertyName, unknown> {\n if (!isDefined(thing) || typeof thing !== \"object\") {\n return false;\n }\n\n for (const property of properties) {\n if (!objectHasProperty(thing, property)) {\n return false;\n }\n }\n\n return true;\n}\n\n/**\n * Helper TypeGuard that checks if the input is an object with the specified property.\n * @param thing - Any object.\n * @param property - The name of the property that should appear in the object.\n */\nexport function objectHasProperty<Thing, PropertyName extends string>(\n thing: Thing,\n property: PropertyName,\n): thing is Thing & Record<PropertyName, unknown> {\n return (\n isDefined(thing) && typeof thing === \"object\" && property in (thing as Record<string, unknown>)\n );\n}\n\nexport function isNodeReadableStream(x: unknown): x is NodeJS.ReadableStream {\n return Boolean(x && typeof (x as NodeJS.ReadableStream)[\"pipe\"] === \"function\");\n}\n\nexport function isWebReadableStream(x: unknown): x is ReadableStream {\n return Boolean(\n x &&\n typeof (x as ReadableStream).getReader === \"function\" &&\n typeof (x as ReadableStream).tee === \"function\",\n );\n}\n\nexport function isReadableStream(x: unknown): x is ReadableStream | NodeJS.ReadableStream {\n return isNodeReadableStream(x) || isWebReadableStream(x);\n}\n\nexport function isBlob(x: unknown): x is Blob {\n return typeof (x as Blob).stream === \"function\";\n}\n"]}
|
|
1
|
+
{"version":3,"file":"typeGuards.js","sourceRoot":"","sources":["../../../src/util/typeGuards.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAI,KAA2B;IACtD,OAAO,OAAO,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,IAAI,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CACpC,KAAY,EACZ,UAA0B;IAE1B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACnD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;QAClC,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,CAAC;YACxC,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAC/B,KAAY,EACZ,QAAsB;IAEtB,OAAO,CACL,SAAS,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,QAAQ,IAAK,KAAiC,CAChG,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,CAAU;IAC7C,OAAO,OAAO,CAAC,CAAC,IAAI,OAAQ,CAA2B,CAAC,MAAM,CAAC,KAAK,UAAU,CAAC,CAAC;AAClF,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,CAAU;IAC5C,OAAO,OAAO,CACZ,CAAC;QACC,OAAQ,CAAoB,CAAC,SAAS,KAAK,UAAU;QACrD,OAAQ,CAAoB,CAAC,GAAG,KAAK,UAAU,CAClD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,YAAY,CAC1B,IAAa;IAQb,OAAO,CACL,IAAI,KAAK,SAAS;QAClB,CAAC,IAAI,YAAY,UAAU;YACzB,gBAAgB,CAAC,IAAI,CAAC;YACtB,OAAO,IAAI,KAAK,UAAU;YAC1B,IAAI,YAAY,IAAI,CAAC,CACxB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,CAAU;IACzC,OAAO,oBAAoB,CAAC,CAAC,CAAC,IAAI,mBAAmB,CAAC,CAAC,CAAC,CAAC;AAC3D,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,CAAU;IAC/B,OAAO,OAAQ,CAAU,CAAC,MAAM,KAAK,UAAU,CAAC;AAClD,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Helper TypeGuard that checks if something is defined or not.\n * @param thing - Anything\n */\nexport function isDefined<T>(thing: T | undefined | null): thing is T {\n return typeof thing !== \"undefined\" && thing !== null;\n}\n\n/**\n * Helper TypeGuard that checks if the input is an object with the specified properties.\n * @param thing - Anything.\n * @param properties - The name of the properties that should appear in the object.\n */\nexport function isObjectWithProperties<Thing, PropertyName extends string>(\n thing: Thing,\n properties: PropertyName[],\n): thing is Thing & Record<PropertyName, unknown> {\n if (!isDefined(thing) || typeof thing !== \"object\") {\n return false;\n }\n\n for (const property of properties) {\n if (!objectHasProperty(thing, property)) {\n return false;\n }\n }\n\n return true;\n}\n\n/**\n * Helper TypeGuard that checks if the input is an object with the specified property.\n * @param thing - Any object.\n * @param property - The name of the property that should appear in the object.\n */\nexport function objectHasProperty<Thing, PropertyName extends string>(\n thing: Thing,\n property: PropertyName,\n): thing is Thing & Record<PropertyName, unknown> {\n return (\n isDefined(thing) && typeof thing === \"object\" && property in (thing as Record<string, unknown>)\n );\n}\n\nexport function isNodeReadableStream(x: unknown): x is NodeJS.ReadableStream {\n return Boolean(x && typeof (x as NodeJS.ReadableStream)[\"pipe\"] === \"function\");\n}\n\nexport function isWebReadableStream(x: unknown): x is ReadableStream {\n return Boolean(\n x &&\n typeof (x as ReadableStream).getReader === \"function\" &&\n typeof (x as ReadableStream).tee === \"function\",\n );\n}\n\nexport function isBinaryBody(\n body: unknown,\n): body is\n | Uint8Array\n | NodeJS.ReadableStream\n | ReadableStream<Uint8Array>\n | (() => NodeJS.ReadableStream)\n | (() => ReadableStream<Uint8Array>)\n | Blob {\n return (\n body !== undefined &&\n (body instanceof Uint8Array ||\n isReadableStream(body) ||\n typeof body === \"function\" ||\n body instanceof Blob)\n );\n}\n\nexport function isReadableStream(x: unknown): x is ReadableStream | NodeJS.ReadableStream {\n return isNodeReadableStream(x) || isWebReadableStream(x);\n}\n\nexport function isBlob(x: unknown): x is Blob {\n return typeof (x as Blob).stream === \"function\";\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { BodyPart, MultipartRequestBody, RawHttpHeadersInput } from "../interfaces.js";
|
|
2
|
+
/**
|
|
3
|
+
* Describes a single part in a multipart body.
|
|
4
|
+
*/
|
|
5
|
+
export interface PartDescriptor {
|
|
6
|
+
/**
|
|
7
|
+
* Content type of this part. If set, this value will be used to set the Content-Type MIME header for this part, although explicitly
|
|
8
|
+
* setting the Content-Type header in the headers bag will override this value. If set to `null`, no content type will be inferred from
|
|
9
|
+
* the body field. Otherwise, the value of the Content-Type MIME header will be inferred based on the type of the body.
|
|
10
|
+
*/
|
|
11
|
+
contentType?: string | null;
|
|
12
|
+
/**
|
|
13
|
+
* The disposition type of this part (for example, "form-data" for parts making up a multipart/form-data request). If set, this value
|
|
14
|
+
* will be used to set the Content-Disposition MIME header for this part, in addition to the `name` and `filename` properties.
|
|
15
|
+
* If the `name` or `filename` properties are set while `dispositionType` is left undefined, `dispositionType` will default to "form-data".
|
|
16
|
+
*
|
|
17
|
+
* Explicitly setting the Content-Disposition header in the headers bag will override this value.
|
|
18
|
+
*/
|
|
19
|
+
dispositionType?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The field name associated with this part. This value will be used to construct the Content-Disposition header,
|
|
22
|
+
* along with the `dispositionType` and `filename` properties, if the header has not been set in the `headers` bag.
|
|
23
|
+
*/
|
|
24
|
+
name?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The file name of the content if it is a file. This value will be used to construct the Content-Disposition header,
|
|
27
|
+
* along with the `dispositionType` and `name` properties, if the header has not been set in the `headers` bag.
|
|
28
|
+
*/
|
|
29
|
+
filename?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The multipart headers for this part of the multipart body. Values of the Content-Type and Content-Disposition headers set in the headers bag
|
|
32
|
+
* will take precedence over those computed from the request body or the contentType, dispositionType, name, and filename fields on this object.
|
|
33
|
+
*/
|
|
34
|
+
headers?: RawHttpHeadersInput;
|
|
35
|
+
/**
|
|
36
|
+
* The body of this part of the multipart request.
|
|
37
|
+
*/
|
|
38
|
+
body?: unknown;
|
|
39
|
+
}
|
|
40
|
+
export declare function buildBodyPart(descriptor: PartDescriptor): BodyPart;
|
|
41
|
+
export declare function buildMultipartBody(parts: PartDescriptor[]): MultipartRequestBody;
|
|
42
|
+
//# sourceMappingURL=multipart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipart.d.ts","sourceRoot":"","sources":["../../../src/client/multipart.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAMvF;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAE5B;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAE9B;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AA8HD,wBAAgB,aAAa,CAAC,UAAU,EAAE,cAAc,GAAG,QAAQ,CAkBlE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,cAAc,EAAE,GAAG,oBAAoB,CAEhF"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation.
|
|
3
|
+
// Licensed under the MIT license.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.buildMultipartBody = exports.buildBodyPart = void 0;
|
|
6
|
+
const restError_js_1 = require("../restError.js");
|
|
7
|
+
const httpHeaders_js_1 = require("../httpHeaders.js");
|
|
8
|
+
const bytesEncoding_js_1 = require("../util/bytesEncoding.js");
|
|
9
|
+
const typeGuards_js_1 = require("../util/typeGuards.js");
|
|
10
|
+
/**
|
|
11
|
+
* Get value of a header in the part descriptor ignoring case
|
|
12
|
+
*/
|
|
13
|
+
function getHeaderValue(descriptor, headerName) {
|
|
14
|
+
if (descriptor.headers) {
|
|
15
|
+
const actualHeaderName = Object.keys(descriptor.headers).find((x) => x.toLowerCase() === headerName.toLowerCase());
|
|
16
|
+
if (actualHeaderName) {
|
|
17
|
+
return descriptor.headers[actualHeaderName];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
function getPartContentType(descriptor) {
|
|
23
|
+
const contentTypeHeader = getHeaderValue(descriptor, "content-type");
|
|
24
|
+
if (contentTypeHeader) {
|
|
25
|
+
return contentTypeHeader;
|
|
26
|
+
}
|
|
27
|
+
// Special value of null means content type is to be omitted
|
|
28
|
+
if (descriptor.contentType === null) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
if (descriptor.contentType) {
|
|
32
|
+
return descriptor.contentType;
|
|
33
|
+
}
|
|
34
|
+
const { body } = descriptor;
|
|
35
|
+
if (body === null || body === undefined) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
if (typeof body === "string" || typeof body === "number" || typeof body === "boolean") {
|
|
39
|
+
return "text/plain; charset=UTF-8";
|
|
40
|
+
}
|
|
41
|
+
if (body instanceof Blob) {
|
|
42
|
+
return body.type || "application/octet-stream";
|
|
43
|
+
}
|
|
44
|
+
if ((0, typeGuards_js_1.isBinaryBody)(body)) {
|
|
45
|
+
return "application/octet-stream";
|
|
46
|
+
}
|
|
47
|
+
// arbitrary non-text object -> generic JSON content type by default. We will try to JSON.stringify the body.
|
|
48
|
+
return "application/json; charset=UTF-8";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Enclose value in quotes and escape special characters, for use in the Content-Disposition header
|
|
52
|
+
*/
|
|
53
|
+
function escapeDispositionField(value) {
|
|
54
|
+
return JSON.stringify(value);
|
|
55
|
+
}
|
|
56
|
+
function getContentDisposition(descriptor) {
|
|
57
|
+
var _a;
|
|
58
|
+
const contentDispositionHeader = getHeaderValue(descriptor, "content-disposition");
|
|
59
|
+
if (contentDispositionHeader) {
|
|
60
|
+
return contentDispositionHeader;
|
|
61
|
+
}
|
|
62
|
+
if (descriptor.dispositionType === undefined &&
|
|
63
|
+
descriptor.name === undefined &&
|
|
64
|
+
descriptor.filename === undefined) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const dispositionType = (_a = descriptor.dispositionType) !== null && _a !== void 0 ? _a : "form-data";
|
|
68
|
+
let disposition = dispositionType;
|
|
69
|
+
if (descriptor.name) {
|
|
70
|
+
disposition += `; name=${escapeDispositionField(descriptor.name)}`;
|
|
71
|
+
}
|
|
72
|
+
let filename = undefined;
|
|
73
|
+
if (descriptor.filename) {
|
|
74
|
+
filename = descriptor.filename;
|
|
75
|
+
}
|
|
76
|
+
else if (typeof File !== "undefined" && descriptor.body instanceof File) {
|
|
77
|
+
const filenameFromFile = descriptor.body.name;
|
|
78
|
+
if (filenameFromFile !== "") {
|
|
79
|
+
filename = filenameFromFile;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (filename) {
|
|
83
|
+
disposition += `; filename=${escapeDispositionField(filename)}`;
|
|
84
|
+
}
|
|
85
|
+
return disposition;
|
|
86
|
+
}
|
|
87
|
+
function normalizeBody(body, contentType) {
|
|
88
|
+
if (body === undefined) {
|
|
89
|
+
// zero-length body
|
|
90
|
+
return new Uint8Array([]);
|
|
91
|
+
}
|
|
92
|
+
// binary and primitives should go straight on the wire regardless of content type
|
|
93
|
+
if ((0, typeGuards_js_1.isBinaryBody)(body)) {
|
|
94
|
+
return body;
|
|
95
|
+
}
|
|
96
|
+
if (typeof body === "string" || typeof body === "number" || typeof body === "boolean") {
|
|
97
|
+
return (0, bytesEncoding_js_1.stringToUint8Array)(String(body), "utf-8");
|
|
98
|
+
}
|
|
99
|
+
// stringify objects for JSON-ish content types e.g. application/json, application/merge-patch+json, application/vnd.oci.manifest.v1+json, application.json; charset=UTF-8
|
|
100
|
+
if (contentType && /application\/(.+\+)?json(;.+)?/i.test(String(contentType))) {
|
|
101
|
+
return (0, bytesEncoding_js_1.stringToUint8Array)(JSON.stringify(body), "utf-8");
|
|
102
|
+
}
|
|
103
|
+
throw new restError_js_1.RestError(`Unsupported body/content-type combination: ${body}, ${contentType}`);
|
|
104
|
+
}
|
|
105
|
+
function buildBodyPart(descriptor) {
|
|
106
|
+
var _a;
|
|
107
|
+
const contentType = getPartContentType(descriptor);
|
|
108
|
+
const contentDisposition = getContentDisposition(descriptor);
|
|
109
|
+
const headers = (0, httpHeaders_js_1.createHttpHeaders)((_a = descriptor.headers) !== null && _a !== void 0 ? _a : {});
|
|
110
|
+
if (contentType) {
|
|
111
|
+
headers.set("content-type", contentType);
|
|
112
|
+
}
|
|
113
|
+
if (contentDisposition) {
|
|
114
|
+
headers.set("content-disposition", contentDisposition);
|
|
115
|
+
}
|
|
116
|
+
const body = normalizeBody(descriptor.body, contentType);
|
|
117
|
+
return {
|
|
118
|
+
headers,
|
|
119
|
+
body,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
exports.buildBodyPart = buildBodyPart;
|
|
123
|
+
function buildMultipartBody(parts) {
|
|
124
|
+
return { parts: parts.map(buildBodyPart) };
|
|
125
|
+
}
|
|
126
|
+
exports.buildMultipartBody = buildMultipartBody;
|
|
127
|
+
//# sourceMappingURL=multipart.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipart.js","sourceRoot":"","sources":["../../../src/client/multipart.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAGlC,kDAA4C;AAC5C,sDAAsD;AACtD,+DAA8D;AAC9D,yDAAqD;AAkDrD;;GAEG;AACH,SAAS,cAAc,CAAC,UAA0B,EAAE,UAAkB;IACpE,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;QACvB,MAAM,gBAAgB,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,IAAI,CAC3D,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,UAAU,CAAC,WAAW,EAAE,CACpD,CAAC;QACF,IAAI,gBAAgB,EAAE,CAAC;YACrB,OAAO,UAAU,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;QAC9C,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,kBAAkB,CAAC,UAA0B;IACpD,MAAM,iBAAiB,GAAG,cAAc,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IACrE,IAAI,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AAC;IACnD,CAAC;IAED,0KAA0K;IAC1K,IAAI,WAAW,IAAI,iCAAiC,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC;QAC/E,OAAO,IAAA,qCAAkB,EAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC;IAC3D,CAAC;IAED,MAAM,IAAI,wBAAS,CAAC,8CAA8C,IAAI,KAAK,WAAW,EAAE,CAAC,CAAC;AAC5F,CAAC;AAED,SAAgB,aAAa,CAAC,UAA0B;;IACtD,MAAM,WAAW,GAAG,kBAAkB,CAAC,UAAU,CAAC,CAAC;IACnD,MAAM,kBAAkB,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAC;IAC7D,MAAM,OAAO,GAAG,IAAA,kCAAiB,EAAC,MAAA,UAAU,CAAC,OAAO,mCAAI,EAAE,CAAC,CAAC;IAE5D,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,kBAAkB,EAAE,CAAC;QACvB,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,kBAAkB,CAAC,CAAC;IACzD,CAAC;IAED,MAAM,IAAI,GAAG,aAAa,CAAC,UAAU,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAEzD,OAAO;QACL,OAAO;QACP,IAAI;KACL,CAAC;AACJ,CAAC;AAlBD,sCAkBC;AAED,SAAgB,kBAAkB,CAAC,KAAuB;IACxD,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,CAAC;AAC7C,CAAC;AAFD,gDAEC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { BodyPart, MultipartRequestBody, RawHttpHeadersInput } from \"../interfaces.js\";\nimport { RestError } from \"../restError.js\";\nimport { createHttpHeaders } from \"../httpHeaders.js\";\nimport { stringToUint8Array } from \"../util/bytesEncoding.js\";\nimport { isBinaryBody } from \"../util/typeGuards.js\";\n\n/**\n * Describes a single part in a multipart body.\n */\nexport interface PartDescriptor {\n /**\n * Content type of this part. If set, this value will be used to set the Content-Type MIME header for this part, although explicitly\n * setting the Content-Type header in the headers bag will override this value. If set to `null`, no content type will be inferred from\n * the body field. Otherwise, the value of the Content-Type MIME header will be inferred based on the type of the body.\n */\n contentType?: string | null;\n\n /**\n * The disposition type of this part (for example, \"form-data\" for parts making up a multipart/form-data request). If set, this value\n * will be used to set the Content-Disposition MIME header for this part, in addition to the `name` and `filename` properties.\n * If the `name` or `filename` properties are set while `dispositionType` is left undefined, `dispositionType` will default to \"form-data\".\n *\n * Explicitly setting the Content-Disposition header in the headers bag will override this value.\n */\n dispositionType?: string;\n\n /**\n * The field name associated with this part. This value will be used to construct the Content-Disposition header,\n * along with the `dispositionType` and `filename` properties, if the header has not been set in the `headers` bag.\n */\n name?: string;\n\n /**\n * The file name of the content if it is a file. This value will be used to construct the Content-Disposition header,\n * along with the `dispositionType` and `name` properties, if the header has not been set in the `headers` bag.\n */\n filename?: string;\n\n /**\n * The multipart headers for this part of the multipart body. Values of the Content-Type and Content-Disposition headers set in the headers bag\n * will take precedence over those computed from the request body or the contentType, dispositionType, name, and filename fields on this object.\n */\n headers?: RawHttpHeadersInput;\n\n /**\n * The body of this part of the multipart request.\n */\n body?: unknown;\n}\n\ntype MultipartBodyType = BodyPart[\"body\"];\n\ntype HeaderValue = RawHttpHeadersInput[string];\n\n/**\n * Get value of a header in the part descriptor ignoring case\n */\nfunction getHeaderValue(descriptor: PartDescriptor, headerName: string): HeaderValue | undefined {\n if (descriptor.headers) {\n const actualHeaderName = Object.keys(descriptor.headers).find(\n (x) => x.toLowerCase() === headerName.toLowerCase(),\n );\n if (actualHeaderName) {\n return descriptor.headers[actualHeaderName];\n }\n }\n\n return undefined;\n}\n\nfunction getPartContentType(descriptor: PartDescriptor): HeaderValue | undefined {\n const contentTypeHeader = getHeaderValue(descriptor, \"content-type\");\n if (contentTypeHeader) {\n return contentTypeHeader;\n }\n\n // Special value of null means content type is to be omitted\n if (descriptor.contentType === null) {\n return undefined;\n }\n\n if (descriptor.contentType) {\n return descriptor.contentType;\n }\n\n const { body } = descriptor;\n\n if (body === null || body === undefined) {\n return undefined;\n }\n\n if (typeof body === \"string\" || typeof body === \"number\" || typeof body === \"boolean\") {\n return \"text/plain; charset=UTF-8\";\n }\n\n if (body instanceof Blob) {\n return body.type || \"application/octet-stream\";\n }\n\n if (isBinaryBody(body)) {\n return \"application/octet-stream\";\n }\n\n // arbitrary non-text object -> generic JSON content type by default. We will try to JSON.stringify the body.\n return \"application/json; charset=UTF-8\";\n}\n\n/**\n * Enclose value in quotes and escape special characters, for use in the Content-Disposition header\n */\nfunction escapeDispositionField(value: string): string {\n return JSON.stringify(value);\n}\n\nfunction getContentDisposition(descriptor: PartDescriptor): HeaderValue | undefined {\n const contentDispositionHeader = getHeaderValue(descriptor, \"content-disposition\");\n if (contentDispositionHeader) {\n return contentDispositionHeader;\n }\n\n if (\n descriptor.dispositionType === undefined &&\n descriptor.name === undefined &&\n descriptor.filename === undefined\n ) {\n return undefined;\n }\n\n const dispositionType = descriptor.dispositionType ?? \"form-data\";\n\n let disposition = dispositionType;\n if (descriptor.name) {\n disposition += `; name=${escapeDispositionField(descriptor.name)}`;\n }\n\n let filename: string | undefined = undefined;\n if (descriptor.filename) {\n filename = descriptor.filename;\n } else if (typeof File !== \"undefined\" && descriptor.body instanceof File) {\n const filenameFromFile = (descriptor.body as File).name;\n if (filenameFromFile !== \"\") {\n filename = filenameFromFile;\n }\n }\n\n if (filename) {\n disposition += `; filename=${escapeDispositionField(filename)}`;\n }\n\n return disposition;\n}\n\nfunction normalizeBody(body?: unknown, contentType?: HeaderValue): MultipartBodyType {\n if (body === undefined) {\n // zero-length body\n return new Uint8Array([]);\n }\n\n // binary and primitives should go straight on the wire regardless of content type\n if (isBinaryBody(body)) {\n return body;\n }\n if (typeof body === \"string\" || typeof body === \"number\" || typeof body === \"boolean\") {\n return stringToUint8Array(String(body), \"utf-8\");\n }\n\n // stringify objects for JSON-ish content types e.g. application/json, application/merge-patch+json, application/vnd.oci.manifest.v1+json, application.json; charset=UTF-8\n if (contentType && /application\\/(.+\\+)?json(;.+)?/i.test(String(contentType))) {\n return stringToUint8Array(JSON.stringify(body), \"utf-8\");\n }\n\n throw new RestError(`Unsupported body/content-type combination: ${body}, ${contentType}`);\n}\n\nexport function buildBodyPart(descriptor: PartDescriptor): BodyPart {\n const contentType = getPartContentType(descriptor);\n const contentDisposition = getContentDisposition(descriptor);\n const headers = createHttpHeaders(descriptor.headers ?? {});\n\n if (contentType) {\n headers.set(\"content-type\", contentType);\n }\n if (contentDisposition) {\n headers.set(\"content-disposition\", contentDisposition);\n }\n\n const body = normalizeBody(descriptor.body, contentType);\n\n return {\n headers,\n body,\n };\n}\n\nexport function buildMultipartBody(parts: PartDescriptor[]): MultipartRequestBody {\n return { parts: parts.map(buildBodyPart) };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sendRequest.d.ts","sourceRoot":"","sources":["../../../src/client/sendRequest.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"sendRequest.d.ts","sourceRoot":"","sources":["../../../src/client/sendRequest.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,UAAU,EACV,WAAW,EAKZ,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAK1C,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAG9D;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,WAAW,EACnB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,QAAQ,EAClB,OAAO,GAAE,yBAA8B,EACvC,gBAAgB,CAAC,EAAE,UAAU,GAC5B,OAAO,CAAC,YAAY,CAAC,CAoBvB;AAuCD,MAAM,WAAW,yBAA0B,SAAQ,iBAAiB;IAClE,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B"}
|