@rest-vir/define-service 0.6.0 → 0.8.0

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.
@@ -97,19 +97,28 @@ export function buildEndpointRequestInit(endpoint, ...[{ method, options = {}, p
97
97
  ? Object.fromEntries(options.headers)
98
98
  : options.headers || {};
99
99
  const hasContentTypeHeader = Object.keys(headers).some((headerKey) => headerKey.toLowerCase() === 'content-type');
100
- if (!hasContentTypeHeader && check.isObject(requestData)) {
101
- headers['content-type'] = 'application/json';
100
+ if (!hasContentTypeHeader) {
101
+ if (requestData instanceof FormData) {
102
+ headers['content-type'] = 'multipart/form-data';
103
+ }
104
+ else if (check.isObject(requestData)) {
105
+ headers['content-type'] = 'application/json';
106
+ }
102
107
  }
103
108
  const url = buildEndpointUrl(endpoint, { pathParams });
104
109
  const requestInit = {
105
110
  ...options,
106
111
  headers,
107
112
  method: filterToValidMethod(endpoint, method),
108
- ...(requestData
113
+ ...(requestData instanceof FormData
109
114
  ? {
110
- body: JSON.stringify(requestData),
115
+ body: requestData,
111
116
  }
112
- : {}),
117
+ : requestData
118
+ ? {
119
+ body: JSON.stringify(requestData),
120
+ }
121
+ : {}),
113
122
  };
114
123
  return {
115
124
  url,
package/dist/index.d.ts CHANGED
@@ -13,6 +13,7 @@ export * from './service/match-url.js';
13
13
  export * from './service/minimal-service.js';
14
14
  export * from './service/service-definition.error.js';
15
15
  export * from './service/service-definition.js';
16
+ export * from './util/custom-shapes.js';
16
17
  export * from './util/mock-fetch.js';
17
18
  export * from './util/no-param.js';
18
19
  export * from './util/origin.js';
package/dist/index.js CHANGED
@@ -13,6 +13,7 @@ export * from './service/match-url.js';
13
13
  export * from './service/minimal-service.js';
14
14
  export * from './service/service-definition.error.js';
15
15
  export * from './service/service-definition.js';
16
+ export * from './util/custom-shapes.js';
16
17
  export * from './util/mock-fetch.js';
17
18
  export * from './util/no-param.js';
18
19
  export * from './util/origin.js';
@@ -0,0 +1,37 @@
1
+ /**
2
+ * A custom shape definition for requests that require `FormData` as the body.
3
+ *
4
+ * @category Define Service
5
+ * @category Package : @rest-vir/define-service
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import {defineService, formDataShape, AnyOrigin, HttpMethod} from '@rest-vir/define-shape';
10
+ *
11
+ * export const myService = defineService({
12
+ * serviceName: 'my-service',
13
+ * serviceOrigin: 'https://example.com',
14
+ * requiredClientOrigin: AnyOrigin,
15
+ * endpoints: {
16
+ * '/my-endpoint': {
17
+ * methods: {
18
+ * [HttpMethod.Post]: true,
19
+ * },
20
+ * requestDataShape: formDataShape,
21
+ * responseDataShape: undefined,
22
+ * },
23
+ * },
24
+ * });
25
+ * ```
26
+ *
27
+ * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
28
+ */
29
+ export declare const formDataShape: import("object-shape-tester").CustomSpecifier<FormData>;
30
+ /**
31
+ * Check if the input is a shape definition for {@link formDataShape}.
32
+ *
33
+ * @category Internal
34
+ * @category Package : @rest-vir/define-service
35
+ * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
36
+ */
37
+ export declare function isFormDataShape(shape: unknown): boolean;
@@ -0,0 +1,54 @@
1
+ import { customShape, isCustomSpecifier, isShapeDefinition } from 'object-shape-tester';
2
+ /**
3
+ * A custom shape definition for requests that require `FormData` as the body.
4
+ *
5
+ * @category Define Service
6
+ * @category Package : @rest-vir/define-service
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {defineService, formDataShape, AnyOrigin, HttpMethod} from '@rest-vir/define-shape';
11
+ *
12
+ * export const myService = defineService({
13
+ * serviceName: 'my-service',
14
+ * serviceOrigin: 'https://example.com',
15
+ * requiredClientOrigin: AnyOrigin,
16
+ * endpoints: {
17
+ * '/my-endpoint': {
18
+ * methods: {
19
+ * [HttpMethod.Post]: true,
20
+ * },
21
+ * requestDataShape: formDataShape,
22
+ * responseDataShape: undefined,
23
+ * },
24
+ * },
25
+ * });
26
+ * ```
27
+ *
28
+ * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
29
+ */
30
+ export const formDataShape = customShape({
31
+ customName: 'FormData',
32
+ checker(value) {
33
+ return value instanceof FormData;
34
+ },
35
+ defaultValue: new FormData(),
36
+ });
37
+ /**
38
+ * Check if the input is a shape definition for {@link formDataShape}.
39
+ *
40
+ * @category Internal
41
+ * @category Package : @rest-vir/define-service
42
+ * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
43
+ */
44
+ export function isFormDataShape(shape) {
45
+ if (isShapeDefinition(shape)) {
46
+ return isFormDataShape(shape.shape);
47
+ }
48
+ else if (isCustomSpecifier(shape)) {
49
+ return shape.customName === formDataShape.customName;
50
+ }
51
+ else {
52
+ return false;
53
+ }
54
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rest-vir/define-service",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Define an connect to a declarative and type safe REST and WebSocket service.",
5
5
  "keywords": [
6
6
  "rest",