@rest-vir/define-service 1.3.0 → 1.3.2
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.
|
@@ -98,6 +98,8 @@ export declare const endpointInitShape: Shape<{
|
|
|
98
98
|
* - Any other set value overrides the service's origin requirement (if it has any).
|
|
99
99
|
*/
|
|
100
100
|
requiredClientOrigin: Shape<import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<(import("@sinclair/typebox").TUnsafe<{
|
|
101
|
+
allOrigins: boolean;
|
|
102
|
+
}> | import("@sinclair/typebox").TUnsafe<{
|
|
101
103
|
anyOrigin: boolean;
|
|
102
104
|
}> | import("@sinclair/typebox").TUnsafe<RegExp> | import("@sinclair/typebox").TString | import("@sinclair/typebox").TUndefined | import("@sinclair/typebox").TUnsafe<() => void> | import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<(import("@sinclair/typebox").TUnsafe<{
|
|
103
105
|
anyOrigin: boolean;
|
|
@@ -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,
|
package/dist/util/origin.d.ts
CHANGED
|
@@ -82,6 +82,8 @@ export type OriginRequirement = undefined | string | RegExp | AnyOrigin | AllOri
|
|
|
82
82
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
83
83
|
*/
|
|
84
84
|
export declare const originRequirementShape: import("object-shape-tester").Shape<import("object-shape-tester").Shape<import("@sinclair/typebox").TUnion<(import("@sinclair/typebox").TUnsafe<{
|
|
85
|
+
allOrigins: boolean;
|
|
86
|
+
}> | import("@sinclair/typebox").TUnsafe<{
|
|
85
87
|
anyOrigin: boolean;
|
|
86
88
|
}> | import("@sinclair/typebox").TUnsafe<RegExp> | import("@sinclair/typebox").TString | import("@sinclair/typebox").TUndefined | import("@sinclair/typebox").TUnsafe<() => void> | import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<(import("@sinclair/typebox").TUnsafe<{
|
|
87
89
|
anyOrigin: boolean;
|
package/dist/util/origin.js
CHANGED
|
@@ -50,7 +50,7 @@ export function isAllOrigins(input) {
|
|
|
50
50
|
* @category Package : @rest-vir/define-service
|
|
51
51
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
52
52
|
*/
|
|
53
|
-
export const originRequirementShape = defineShape(unionShape(undefined, '', exactShape(AnyOrigin), classShape(RegExp), () => { }, [
|
|
53
|
+
export const originRequirementShape = defineShape(unionShape(undefined, '', exactShape(AllOrigins), exactShape(AnyOrigin), classShape(RegExp), () => { }, [
|
|
54
54
|
unionShape('', exactShape(AnyOrigin), classShape(RegExp), () => { }),
|
|
55
55
|
]));
|
|
56
56
|
/**
|
|
@@ -137,6 +137,8 @@ export declare const webSocketInitShape: Shape<{
|
|
|
137
137
|
* - Any other set value overrides the service's origin requirement (if it has any).
|
|
138
138
|
*/
|
|
139
139
|
requiredClientOrigin: Shape<import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<(import("@sinclair/typebox").TUnsafe<{
|
|
140
|
+
allOrigins: boolean;
|
|
141
|
+
}> | import("@sinclair/typebox").TUnsafe<{
|
|
140
142
|
anyOrigin: boolean;
|
|
141
143
|
}> | import("@sinclair/typebox").TUnsafe<RegExp> | import("@sinclair/typebox").TString | import("@sinclair/typebox").TUndefined | import("@sinclair/typebox").TUnsafe<() => void> | import("@sinclair/typebox").TArray<import("@sinclair/typebox").TUnion<(import("@sinclair/typebox").TUnsafe<{
|
|
142
144
|
anyOrigin: boolean;
|