@rest-vir/define-service 0.7.0 → 0.8.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.
|
@@ -97,19 +97,32 @@ 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
|
|
101
|
-
|
|
100
|
+
if (!hasContentTypeHeader) {
|
|
101
|
+
if (requestData instanceof FormData) {
|
|
102
|
+
/**
|
|
103
|
+
* Do not set `content-type` manually when submitting form data because the browser will
|
|
104
|
+
* set it automatically _and_ include a boundary in the content type, which is needed
|
|
105
|
+
* for reading the form data properly.
|
|
106
|
+
*/
|
|
107
|
+
}
|
|
108
|
+
else if (check.isObject(requestData)) {
|
|
109
|
+
headers['content-type'] = 'application/json';
|
|
110
|
+
}
|
|
102
111
|
}
|
|
103
112
|
const url = buildEndpointUrl(endpoint, { pathParams });
|
|
104
113
|
const requestInit = {
|
|
105
114
|
...options,
|
|
106
115
|
headers,
|
|
107
116
|
method: filterToValidMethod(endpoint, method),
|
|
108
|
-
...(requestData
|
|
117
|
+
...(requestData instanceof FormData
|
|
109
118
|
? {
|
|
110
|
-
body:
|
|
119
|
+
body: requestData,
|
|
111
120
|
}
|
|
112
|
-
:
|
|
121
|
+
: requestData
|
|
122
|
+
? {
|
|
123
|
+
body: JSON.stringify(requestData),
|
|
124
|
+
}
|
|
125
|
+
: {}),
|
|
113
126
|
};
|
|
114
127
|
return {
|
|
115
128
|
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
|
+
}
|