@tstdl/base 0.90.88 → 0.90.89

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.
@@ -1,5 +1,6 @@
1
1
  import { type CancellationSignal } from '../../cancellation/index.js';
2
2
  import { dispose, type Disposable } from '../../disposable/index.js';
3
+ import type { OneOrMany } from '../../schema/index.js';
3
4
  import type { Record, TypedOmit, UndefinableJson, UndefinableJsonObject } from '../../types.js';
4
5
  import { HttpForm, type HttpFormObject } from '../http-form.js';
5
6
  import { HttpHeaders, type HttpHeadersObject } from '../http-headers.js';
@@ -22,14 +23,17 @@ export type HttpRequestAuthorization = {
22
23
  bearer?: string;
23
24
  token?: string;
24
25
  };
26
+ export type HttpFormDataObjectValue = string | number | boolean | Uint8Array | Blob;
27
+ export type HttpFormDataObject = Record<string, OneOrMany<HttpFormDataObjectValue>>;
25
28
  export type HttpClientRequestOptions = Partial<TypedOmit<HttpClientRequest, 'url' | 'method' | 'abortSignal' | 'abort' | 'headers' | 'query' | 'body'>> & {
26
29
  urlParameter?: HttpUrlParametersObject | HttpUrlParameters;
27
30
  headers?: HttpHeadersObject | HttpHeaders;
28
31
  query?: HttpQueryObject | HttpQuery;
29
32
  credentials?: HttpRequestCredentials;
30
33
  authorization?: HttpRequestAuthorization;
31
- body?: TypedOmit<HttpRequestBody, 'form'> & {
34
+ body?: TypedOmit<HttpRequestBody, 'form' | 'formData'> & {
32
35
  form?: HttpFormObject | HttpForm;
36
+ formData?: HttpFormDataObject | FormData;
33
37
  };
34
38
  abortSignal?: CancellationSignal;
35
39
  };
@@ -1,7 +1,8 @@
1
1
  import { CancellationToken } from '../../cancellation/index.js';
2
2
  import { dispose } from '../../disposable/index.js';
3
3
  import { clone } from '../../utils/clone.js';
4
- import { isDefined, isString, isUndefined } from '../../utils/type-guards.js';
4
+ import { objectEntries } from '../../utils/object/object.js';
5
+ import { isArray, isBlob, isDefined, isString, isUint8Array, isUndefined } from '../../utils/type-guards.js';
5
6
  import { HttpForm } from '../http-form.js';
6
7
  import { HttpHeaders } from '../http-headers.js';
7
8
  import { HttpQuery } from '../http-query.js';
@@ -149,5 +150,28 @@ function normalizeBody(body) {
149
150
  if (isDefined(normalizedBody.form)) {
150
151
  normalizedBody.form = new HttpForm(normalizedBody.form);
151
152
  }
153
+ if (isDefined(normalizedBody.formData) && !(normalizedBody.formData instanceof FormData)) {
154
+ const formData = new FormData();
155
+ for (const [key, value] of objectEntries(normalizedBody.formData)) {
156
+ if (isArray(value)) {
157
+ for (const item of value) {
158
+ formData.append(key, convertFormDataObjectValue(item));
159
+ }
160
+ }
161
+ else {
162
+ formData.set(key, convertFormDataObjectValue(value));
163
+ }
164
+ }
165
+ normalizedBody.formData = formData;
166
+ }
152
167
  return normalizedBody;
153
168
  }
169
+ function convertFormDataObjectValue(value) {
170
+ if (isString(value) || isBlob(value)) {
171
+ return value;
172
+ }
173
+ if (isUint8Array(value)) {
174
+ return new Blob([value]);
175
+ }
176
+ return String(value);
177
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.90.88",
3
+ "version": "0.90.89",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"