@typespec/ts-http-runtime 1.0.0-alpha.20231110.2 → 1.0.0-alpha.20231115.1

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.
Files changed (35) hide show
  1. package/dist/index.js +500 -250
  2. package/dist/index.js.map +1 -1
  3. package/dist-esm/src/client/sendRequest.js +1 -1
  4. package/dist-esm/src/client/sendRequest.js.map +1 -1
  5. package/dist-esm/src/createPipelineFromOptions.js +5 -0
  6. package/dist-esm/src/createPipelineFromOptions.js.map +1 -1
  7. package/dist-esm/src/fetchHttpClient.js +1 -14
  8. package/dist-esm/src/fetchHttpClient.js.map +1 -1
  9. package/dist-esm/src/index.js +2 -0
  10. package/dist-esm/src/index.js.map +1 -1
  11. package/dist-esm/src/interfaces.js.map +1 -1
  12. package/dist-esm/src/pipelineRequest.js +1 -0
  13. package/dist-esm/src/pipelineRequest.js.map +1 -1
  14. package/dist-esm/src/policies/formDataPolicy.js +34 -32
  15. package/dist-esm/src/policies/formDataPolicy.js.map +1 -1
  16. package/dist-esm/src/policies/multipartPolicy.js +112 -0
  17. package/dist-esm/src/policies/multipartPolicy.js.map +1 -0
  18. package/dist-esm/src/util/file.js +51 -0
  19. package/dist-esm/src/util/file.js.map +1 -0
  20. package/dist-esm/src/util/stream.browser.js +64 -0
  21. package/dist-esm/src/util/stream.browser.js.map +1 -0
  22. package/dist-esm/src/util/stream.js +81 -0
  23. package/dist-esm/src/util/stream.js.map +1 -0
  24. package/dist-esm/src/util/typeGuards.js +14 -0
  25. package/dist-esm/src/util/typeGuards.js.map +1 -1
  26. package/dist-esm/src/xhrHttpClient.js +2 -12
  27. package/dist-esm/src/xhrHttpClient.js.map +1 -1
  28. package/package.json +2 -3
  29. package/types/ts-http-runtime.d.ts +116 -2
  30. package/dist-esm/src/client/helpers/isReadableStream.browser.js +0 -12
  31. package/dist-esm/src/client/helpers/isReadableStream.browser.js.map +0 -1
  32. package/dist-esm/src/client/helpers/isReadableStream.js +0 -10
  33. package/dist-esm/src/client/helpers/isReadableStream.js.map +0 -1
  34. package/dist-esm/src/policies/formDataPolicy.browser.js +0 -43
  35. package/dist-esm/src/policies/formDataPolicy.browser.js.map +0 -1
@@ -246,6 +246,20 @@ export declare interface BearerTokenAuthenticationPolicyOptions {
246
246
  logger?: TypeSpecRuntimeLogger;
247
247
  }
248
248
 
249
+ /**
250
+ * A part of the request body in a multipart request.
251
+ */
252
+ export declare interface BodyPart {
253
+ /**
254
+ * The headers for this part of the multipart request.
255
+ */
256
+ headers: HttpHeaders;
257
+ /**
258
+ * The body of this part of the multipart request.
259
+ */
260
+ body: ((() => ReadableStream<Uint8Array>) | (() => NodeJS.ReadableStream)) | ReadableStream<Uint8Array> | NodeJS.ReadableStream | Uint8Array | Blob;
261
+ }
262
+
249
263
  /**
250
264
  * promise.race() wrapper that aborts rest of promises as soon as the first promise settles.
251
265
  */
@@ -380,6 +394,70 @@ export declare function createDefaultHttpClient(): HttpClient;
380
394
  */
381
395
  export declare function createEmptyPipeline(): Pipeline;
382
396
 
397
+ /**
398
+ * Create an object that implements the File interface. This object is intended to be
399
+ * passed into RequestBodyType.formData, and is not guaranteed to work as expected in
400
+ * other situations.
401
+ *
402
+ * Use this function create a File object for use in RequestBodyType.formData in environments where the global File object is unavailable.
403
+ *
404
+ * @param content - the content of the file as a Uint8Array in memory.
405
+ * @param name - the name of the file.
406
+ * @param options - optional metadata about the file, e.g. file name, file size, MIME type.
407
+ */
408
+ export declare function createFile(content: Uint8Array, name: string, options?: CreateFileOptions): File;
409
+
410
+ /**
411
+ * Create an object that implements the File interface. This object is intended to be
412
+ * passed into RequestBodyType.formData, and is not guaranteed to work as expected in
413
+ * other situations.
414
+ *
415
+ * Use this function to:
416
+ * - Create a File object for use in RequestBodyType.formData in environments where the
417
+ * global File object is unavailable.
418
+ * - Create a File-like object from a readable stream without reading the stream into memory.
419
+ *
420
+ * @param stream - the content of the file as a callback returning a stream. When a File object made using createFile is
421
+ * passed in a request's form data map, the stream will not be read into memory
422
+ * and instead will be streamed when the request is made. In the event of a retry, the
423
+ * stream needs to be read again, so this callback SHOULD return a fresh stream if possible.
424
+ * @param name - the name of the file.
425
+ * @param options - optional metadata about the file, e.g. file name, file size, MIME type.
426
+ */
427
+ export declare function createFileFromStream(stream: () => ReadableStream<Uint8Array> | NodeJS.ReadableStream, name: string, options?: CreateFileFromStreamOptions): File;
428
+
429
+ /**
430
+ * Extra options for createFile when a stream is being passed in.
431
+ */
432
+ export declare interface CreateFileFromStreamOptions extends CreateFileOptions {
433
+ /**
434
+ * Size of the file represented by the stream in bytes.
435
+ *
436
+ * This will be used by the pipeline when calculating the Content-Length header
437
+ * for the overall request.
438
+ */
439
+ size?: number;
440
+ }
441
+
442
+ /**
443
+ * Options passed into createFile specifying metadata about the file.
444
+ */
445
+ export declare interface CreateFileOptions {
446
+ /**
447
+ * The MIME type of the file.
448
+ */
449
+ type?: string;
450
+ /**
451
+ * Last modified time of the file as a UNIX timestamp.
452
+ * This will default to the current date.
453
+ */
454
+ lastModified?: number;
455
+ /**
456
+ * relative path of this file when uploading a directory.
457
+ */
458
+ webkitRelativePath?: string;
459
+ }
460
+
383
461
  /**
384
462
  * Creates an object that satisfies the `HttpHeaders` interface.
385
463
  * @param rawHeaders - A simple object representing initial headers
@@ -524,9 +602,10 @@ export declare function formDataPolicy(): PipelinePolicy;
524
602
  export declare const formDataPolicyName = "formDataPolicy";
525
603
 
526
604
  /**
527
- * Each form data entry can be a string or (in the browser) a Blob.
605
+ * Each form data entry can be a string, Blob, or a File. If you wish to pass a file with a name but do not have
606
+ * access to the File class, you can use the createFile helper to create one.
528
607
  */
529
- export declare type FormDataValue = string | Blob;
608
+ export declare type FormDataValue = string | Blob | File;
530
609
 
531
610
  /**
532
611
  * Wrapper object for http request and response. Deserialized object is stored in
@@ -910,6 +989,33 @@ export declare interface LogPolicyOptions {
910
989
  logger?: Debugger;
911
990
  }
912
991
 
992
+ /**
993
+ * Pipeline policy for multipart requests
994
+ */
995
+ export declare function multipartPolicy(): PipelinePolicy;
996
+
997
+ /**
998
+ * Name of multipart policy
999
+ */
1000
+ export declare const multipartPolicyName = "multipartPolicy";
1001
+
1002
+ /**
1003
+ * A request body consisting of multiple parts.
1004
+ */
1005
+ export declare interface MultipartRequestBody {
1006
+ /**
1007
+ * The parts of the request body.
1008
+ */
1009
+ parts: BodyPart[];
1010
+ /**
1011
+ * The boundary separating each part of the request body.
1012
+ * If not specified, a random boundary will be generated.
1013
+ *
1014
+ * When specified, '--' will be prepended to the boundary in the request to ensure the boundary follows the specification.
1015
+ */
1016
+ boundary?: string;
1017
+ }
1018
+
913
1019
  /**
914
1020
  * Helper TypeGuard that checks if the input is an object with the specified property.
915
1021
  * @param thing - Any object.
@@ -1152,6 +1258,10 @@ export declare interface PipelineRequest {
1152
1258
  * The HTTP body content (if any)
1153
1259
  */
1154
1260
  body?: RequestBodyType;
1261
+ /**
1262
+ * Body for a multipart request.
1263
+ */
1264
+ multipartBody?: MultipartRequestBody;
1155
1265
  /**
1156
1266
  * To simulate a browser form post
1157
1267
  */
@@ -1241,6 +1351,10 @@ export declare interface PipelineRequestOptions {
1241
1351
  * The HTTP body content (if any)
1242
1352
  */
1243
1353
  body?: RequestBodyType;
1354
+ /**
1355
+ * Body for a multipart request.
1356
+ */
1357
+ multipartBody?: MultipartRequestBody;
1244
1358
  /**
1245
1359
  * To simulate a browser form post
1246
1360
  */
@@ -1,12 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- /**
4
- * Checks if the body is a ReadableStream supported by browsers
5
- * @internal
6
- */
7
- export function isReadableStream(body) {
8
- return Boolean(body &&
9
- typeof body.getReader === "function" &&
10
- typeof body.tee === "function");
11
- }
12
- //# sourceMappingURL=isReadableStream.browser.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"isReadableStream.browser.js","sourceRoot":"","sources":["../../../../src/client/helpers/isReadableStream.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAa;IAC5C,OAAO,OAAO,CACZ,IAAI;QACF,OAAQ,IAAuB,CAAC,SAAS,KAAK,UAAU;QACxD,OAAQ,IAAuB,CAAC,GAAG,KAAK,UAAU,CACrD,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Checks if the body is a ReadableStream supported by browsers\n * @internal\n */\nexport function isReadableStream(body: unknown): body is ReadableStream {\n return Boolean(\n body &&\n typeof (body as ReadableStream).getReader === \"function\" &&\n typeof (body as ReadableStream).tee === \"function\"\n );\n}\n"]}
@@ -1,10 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- /**
4
- * Checks if the body is a ReadableStream supported by Node
5
- * @internal
6
- */
7
- export function isReadableStream(body) {
8
- return Boolean(body) && typeof body.pipe === "function";
9
- }
10
- //# sourceMappingURL=isReadableStream.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"isReadableStream.js","sourceRoot":"","sources":["../../../../src/client/helpers/isReadableStream.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAa;IAC5C,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,OAAQ,IAAY,CAAC,IAAI,KAAK,UAAU,CAAC;AACnE,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Checks if the body is a ReadableStream supported by Node\n * @internal\n */\nexport function isReadableStream(body: unknown): body is NodeJS.ReadableStream {\n return Boolean(body) && typeof (body as any).pipe === \"function\";\n}\n"]}
@@ -1,43 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- /**
4
- * The programmatic identifier of the formDataPolicy.
5
- */
6
- export const formDataPolicyName = "formDataPolicy";
7
- /**
8
- * A policy that encodes FormData on the request into the body.
9
- */
10
- export function formDataPolicy() {
11
- return {
12
- name: formDataPolicyName,
13
- async sendRequest(request, next) {
14
- if (request.formData) {
15
- const formData = request.formData;
16
- const requestForm = new FormData();
17
- for (const formKey of Object.keys(formData)) {
18
- const formValue = formData[formKey];
19
- if (Array.isArray(formValue)) {
20
- for (const subValue of formValue) {
21
- requestForm.append(formKey, subValue);
22
- }
23
- }
24
- else {
25
- requestForm.append(formKey, formValue);
26
- }
27
- }
28
- request.body = requestForm;
29
- request.formData = undefined;
30
- const contentType = request.headers.get("Content-Type");
31
- if (contentType && contentType.indexOf("application/x-www-form-urlencoded") !== -1) {
32
- request.body = new URLSearchParams(requestForm).toString();
33
- }
34
- else if (contentType && contentType.indexOf("multipart/form-data") !== -1) {
35
- // browser will automatically apply a suitable content-type header
36
- request.headers.delete("Content-Type");
37
- }
38
- }
39
- return next(request);
40
- },
41
- };
42
- }
43
- //# sourceMappingURL=formDataPolicy.browser.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"formDataPolicy.browser.js","sourceRoot":"","sources":["../../../src/policies/formDataPolicy.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAKlC;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,gBAAgB,CAAC;AAEnD;;GAEG;AACH,MAAM,UAAU,cAAc;IAC5B,OAAO;QACL,IAAI,EAAE,kBAAkB;QACxB,KAAK,CAAC,WAAW,CAAC,OAAwB,EAAE,IAAiB;YAC3D,IAAI,OAAO,CAAC,QAAQ,EAAE;gBACpB,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;gBAClC,MAAM,WAAW,GAAG,IAAI,QAAQ,EAAE,CAAC;gBACnC,KAAK,MAAM,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;oBAC3C,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;oBACpC,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;wBAC5B,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE;4BAChC,WAAW,CAAC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;yBACvC;qBACF;yBAAM;wBACL,WAAW,CAAC,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;qBACxC;iBACF;gBAED,OAAO,CAAC,IAAI,GAAG,WAAW,CAAC;gBAC3B,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;gBAC7B,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;gBACxD,IAAI,WAAW,IAAI,WAAW,CAAC,OAAO,CAAC,mCAAmC,CAAC,KAAK,CAAC,CAAC,EAAE;oBAClF,OAAO,CAAC,IAAI,GAAG,IAAI,eAAe,CAAC,WAAkB,CAAC,CAAC,QAAQ,EAAE,CAAC;iBACnE;qBAAM,IAAI,WAAW,IAAI,WAAW,CAAC,OAAO,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAC,EAAE;oBAC3E,kEAAkE;oBAClE,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;iBACxC;aACF;YACD,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { PipelineRequest, PipelineResponse, SendRequest } from \"../interfaces\";\nimport { PipelinePolicy } from \"../pipeline\";\n\n/**\n * The programmatic identifier of the formDataPolicy.\n */\nexport const formDataPolicyName = \"formDataPolicy\";\n\n/**\n * A policy that encodes FormData on the request into the body.\n */\nexport function formDataPolicy(): PipelinePolicy {\n return {\n name: formDataPolicyName,\n async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {\n if (request.formData) {\n const formData = request.formData;\n const requestForm = new FormData();\n for (const formKey of Object.keys(formData)) {\n const formValue = formData[formKey];\n if (Array.isArray(formValue)) {\n for (const subValue of formValue) {\n requestForm.append(formKey, subValue);\n }\n } else {\n requestForm.append(formKey, formValue);\n }\n }\n\n request.body = requestForm;\n request.formData = undefined;\n const contentType = request.headers.get(\"Content-Type\");\n if (contentType && contentType.indexOf(\"application/x-www-form-urlencoded\") !== -1) {\n request.body = new URLSearchParams(requestForm as any).toString();\n } else if (contentType && contentType.indexOf(\"multipart/form-data\") !== -1) {\n // browser will automatically apply a suitable content-type header\n request.headers.delete(\"Content-Type\");\n }\n }\n return next(request);\n },\n };\n}\n"]}