@rest-vir/define-service 1.3.1 → 1.3.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.
|
@@ -55,9 +55,9 @@ export type ConnectWebSocketParams<WebSocketToConnect extends Readonly<SelectFro
|
|
|
55
55
|
} : {
|
|
56
56
|
protocols: WebSocketToConnect['ProtocolsType'];
|
|
57
57
|
}) & (WebSocketToConnect['searchParamsShape'] extends undefined ? {
|
|
58
|
-
searchParams?:
|
|
58
|
+
searchParams?: Record<string, string[]>;
|
|
59
59
|
} : {
|
|
60
|
-
searchParams: WebSocketToConnect['SearchParamsType']
|
|
60
|
+
searchParams: WebSocketToConnect['SearchParamsType'] & Record<string, string[]>;
|
|
61
61
|
});
|
|
62
62
|
/**
|
|
63
63
|
* Collapsed version of {@link ConnectWebSocketParams} for {@link connectWebSocket} that only
|
|
@@ -45,9 +45,9 @@ export type FetchEndpointParams<EndpointToFetch extends SelectFrom<EndpointDefin
|
|
|
45
45
|
responseDataShape: true;
|
|
46
46
|
methods: true;
|
|
47
47
|
}>, AllowFetchMock extends boolean = true> = EndpointToFetch extends EndpointDefinition ? Readonly<ConstructPathParams<EndpointToFetch['path']> & (EndpointToFetch['SearchParamsType'] extends undefined ? {
|
|
48
|
-
searchParams?:
|
|
48
|
+
searchParams?: Record<string, string[]> | undefined;
|
|
49
49
|
} : {
|
|
50
|
-
searchParams: EndpointToFetch['SearchParamsType']
|
|
50
|
+
searchParams: EndpointToFetch['SearchParamsType'] & Record<string, string[]>;
|
|
51
51
|
}) & (EndpointExecutorData<EndpointToFetch>['request'] extends undefined ? {
|
|
52
52
|
/**
|
|
53
53
|
* This endpoint does not accept any request data, so there is none to be
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { check } from '@augment-vir/assert';
|
|
2
|
-
import { addPrefix, filterMap, getObjectTypedEntries, HttpMethod, } from '@augment-vir/common';
|
|
2
|
+
import { addPrefix, filterMap, getObjectTypedEntries, HttpMethod, mapObject, } from '@augment-vir/common';
|
|
3
3
|
import { assertValidShape } from 'object-shape-tester';
|
|
4
4
|
import { buildUrl } from 'url-vir';
|
|
5
5
|
import { parseJsonWithUndefined } from '../augments/json.js';
|
|
@@ -94,13 +94,17 @@ export async function fetchEndpoint(endpoint, ...params) {
|
|
|
94
94
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
95
95
|
*/
|
|
96
96
|
export function buildEndpointRequestInit(endpoint, ...[{ method, options = {}, pathParams, requestData, searchParams, wildcard } = {},]) {
|
|
97
|
-
const headers = options.headers instanceof Headers
|
|
97
|
+
const headers = mapObject(options.headers instanceof Headers
|
|
98
98
|
? Object.fromEntries(options.headers.entries())
|
|
99
99
|
: check.isArray(options.headers)
|
|
100
100
|
? Object.fromEntries(options.headers)
|
|
101
|
-
: options.headers || {}
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
: options.headers || {}, (key, value) => {
|
|
102
|
+
return {
|
|
103
|
+
key: key.toLowerCase(),
|
|
104
|
+
value,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
if (!headers['content-type']) {
|
|
104
108
|
if (requestData instanceof FormData) {
|
|
105
109
|
/**
|
|
106
110
|
* Do not set `content-type` manually when submitting form data because the browser will
|
|
@@ -112,6 +116,7 @@ export function buildEndpointRequestInit(endpoint, ...[{ method, options = {}, p
|
|
|
112
116
|
headers['content-type'] = 'application/json';
|
|
113
117
|
}
|
|
114
118
|
}
|
|
119
|
+
const shouldStringify = !!headers['content-type']?.match(/\bjson\b/i);
|
|
115
120
|
const url = buildEndpointUrl(endpoint, {
|
|
116
121
|
pathParams,
|
|
117
122
|
searchParams,
|
|
@@ -121,15 +126,15 @@ export function buildEndpointRequestInit(endpoint, ...[{ method, options = {}, p
|
|
|
121
126
|
...options,
|
|
122
127
|
headers,
|
|
123
128
|
method: filterToValidMethod(endpoint, method),
|
|
124
|
-
...(requestData
|
|
125
|
-
?
|
|
126
|
-
body: requestData,
|
|
127
|
-
}
|
|
128
|
-
: requestData
|
|
129
|
+
...(requestData
|
|
130
|
+
? shouldStringify
|
|
129
131
|
? {
|
|
130
132
|
body: JSON.stringify(requestData),
|
|
131
133
|
}
|
|
132
|
-
: {
|
|
134
|
+
: {
|
|
135
|
+
body: requestData,
|
|
136
|
+
}
|
|
137
|
+
: {}),
|
|
133
138
|
};
|
|
134
139
|
return {
|
|
135
140
|
url,
|
|
@@ -87,7 +87,7 @@ export type WithFinalWebSocketProps<Init, WebSocketPath extends EndpointPathBase
|
|
|
87
87
|
MessageFromHostType: Init['messageFromHostShape'] extends NoParam ? any : undefined extends Init['messageFromHostShape'] ? undefined : ShapeInitType<Init['messageFromHostShape']>;
|
|
88
88
|
protocolsShape: 'protocolsShape' extends keyof Init ? undefined extends Init['protocolsShape'] ? undefined : Shape<Init['protocolsShape']> | undefined : undefined;
|
|
89
89
|
ProtocolsType: undefined extends Init['protocolsShape'] ? string[] : ShapeInitType<Init['protocolsShape']>;
|
|
90
|
-
searchParamsShape: 'searchParamsShape' extends keyof Init ?
|
|
90
|
+
searchParamsShape: 'searchParamsShape' extends keyof Init ? Shape<Init['searchParamsShape']> | undefined : undefined;
|
|
91
91
|
SearchParamsType: 'searchParamsShape' extends keyof Init ? ShapeInitType<Init['searchParamsShape']> : undefined;
|
|
92
92
|
customProps: 'customProps' extends keyof Init ? Init['customProps'] : undefined;
|
|
93
93
|
}> : never) & {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rest-vir/define-service",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "Define an connect to a declarative and type safe REST and WebSocket service.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rest",
|
|
@@ -40,14 +40,14 @@
|
|
|
40
40
|
"test:update": "npm test update"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@augment-vir/assert": "^31.
|
|
44
|
-
"@augment-vir/common": "^31.
|
|
43
|
+
"@augment-vir/assert": "^31.51.1",
|
|
44
|
+
"@augment-vir/common": "^31.51.1",
|
|
45
45
|
"date-vir": "^8.0.0",
|
|
46
46
|
"type-fest": "^5.2.0",
|
|
47
47
|
"url-vir": "^2.1.6"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@augment-vir/test": "^31.
|
|
50
|
+
"@augment-vir/test": "^31.51.1",
|
|
51
51
|
"@web/dev-server-esbuild": "^1.0.4",
|
|
52
52
|
"@web/test-runner": "^0.20.2",
|
|
53
53
|
"@web/test-runner-commands": "^0.9.0",
|