@rest-vir/define-service 0.8.3 → 0.9.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.
- package/dist/endpoint/endpoint.d.ts +5 -1
- package/dist/endpoint/endpoint.js +1 -1
- package/dist/frontend-connect/generate-api.d.ts +25 -5
- package/dist/frontend-connect/generate-api.js +68 -25
- package/dist/util/origin.d.ts +5 -1
- package/dist/util/origin.js +4 -4
- package/dist/web-socket/web-socket-definition.d.ts +5 -1
- package/dist/web-socket/web-socket-definition.js +1 -1
- package/package.json +6 -6
|
@@ -93,7 +93,11 @@ export declare const endpointInitShape: ShapeDefinition<{
|
|
|
93
93
|
* service's origin requirement).
|
|
94
94
|
* - Any other set value overrides the service's origin requirement (if it has any).
|
|
95
95
|
*/
|
|
96
|
-
requiredClientOrigin: import("object-shape-tester").
|
|
96
|
+
requiredClientOrigin: import("object-shape-tester").ShapeOptional<ShapeDefinition<import("object-shape-tester").ShapeOr<[undefined, string, import("object-shape-tester").ShapeExact<readonly [{
|
|
97
|
+
anyOrigin: boolean;
|
|
98
|
+
}]>, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void, import("object-shape-tester").ShapeOr<[string, import("object-shape-tester").ShapeExact<readonly [{
|
|
99
|
+
anyOrigin: boolean;
|
|
100
|
+
}]>, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void]>[]]>, false>>;
|
|
97
101
|
methods: import("object-shape-tester").ShapeIndexedKeys<[{
|
|
98
102
|
keys: import("object-shape-tester").ShapeEnum<readonly [typeof HttpMethod]>;
|
|
99
103
|
values: boolean;
|
|
@@ -22,7 +22,7 @@ export const endpointInitShape = defineShape({
|
|
|
22
22
|
* service's origin requirement).
|
|
23
23
|
* - Any other set value overrides the service's origin requirement (if it has any).
|
|
24
24
|
*/
|
|
25
|
-
requiredClientOrigin: originRequirementShape,
|
|
25
|
+
requiredClientOrigin: optional(originRequirementShape),
|
|
26
26
|
methods: indexedKeys({
|
|
27
27
|
keys: enumShape(HttpMethod),
|
|
28
28
|
values: false,
|
|
@@ -14,20 +14,38 @@ import { CollapsedFetchEndpointParams, FetchEndpointOutput, GenericFetchEndpoint
|
|
|
14
14
|
* @category Package : @rest-vir/define-service
|
|
15
15
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
18
|
-
|
|
17
|
+
export declare class RestVirApi<SpecificService extends ServiceDefinition> {
|
|
18
|
+
/**
|
|
19
|
+
* All supported endpoints within this API. Use `.fetch()` on an individual endpoint to send
|
|
20
|
+
* requests to it.
|
|
21
|
+
*/
|
|
22
|
+
readonly endpoints: {
|
|
19
23
|
[EndpointPath in keyof SpecificService['endpoints']]: SpecificService['endpoints'][EndpointPath] extends GenericEndpointDefinition ? SpecificService['endpoints'][EndpointPath] & {
|
|
20
24
|
/** Send a fetch request to this endpoint. */
|
|
21
25
|
fetch(...params: CollapsedFetchEndpointParams<SpecificService['endpoints'][EndpointPath]>): Promise<FetchEndpointOutput<SpecificService['endpoints'][EndpointPath]>>;
|
|
22
26
|
} : never;
|
|
23
27
|
};
|
|
24
|
-
|
|
28
|
+
/**
|
|
29
|
+
* All supported WebSockets within this API. Use `.connect()` on an individual WebSocket to open
|
|
30
|
+
* up connections to it.
|
|
31
|
+
*/
|
|
32
|
+
readonly webSockets: {
|
|
25
33
|
[WebSocketPath in keyof SpecificService['webSockets']]: SpecificService['webSockets'][WebSocketPath] extends WebSocketDefinition ? SpecificService['webSockets'][WebSocketPath] & {
|
|
26
34
|
/** Connect to this WebSocket. */
|
|
27
35
|
connect(...params: CollapsedConnectWebSocketParams<SpecificService['webSockets'][WebSocketPath], false>): Promise<ClientWebSocket<SpecificService['webSockets'][WebSocketPath]>>;
|
|
28
36
|
} : never;
|
|
29
37
|
};
|
|
30
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Where this API is hosted. This can be freely modified at any time and it will immediately
|
|
40
|
+
* affect all `endpoint.fetch()` and `webSocket.connect()` methods.
|
|
41
|
+
*
|
|
42
|
+
* @example 'https://example.com' 'http://localhost:3000'
|
|
43
|
+
*
|
|
44
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Location for help on which part of the URL is the origin (if necessary).
|
|
45
|
+
*/
|
|
46
|
+
serviceOrigin: string;
|
|
47
|
+
constructor(service: SpecificService, { endpointFetch, webSocketConnect, serviceOrigin }?: Readonly<GenerateApiOptions>);
|
|
48
|
+
}
|
|
31
49
|
/**
|
|
32
50
|
* Options for {@link generateApi}.
|
|
33
51
|
*
|
|
@@ -38,6 +56,8 @@ export type RestVirApi<SpecificService extends ServiceDefinition> = {
|
|
|
38
56
|
export type GenerateApiOptions = Readonly<PartialWithUndefined<{
|
|
39
57
|
endpointFetch: Readonly<Pick<GenericFetchEndpointParams, 'options' | 'fetch'>>;
|
|
40
58
|
webSocketConnect: Readonly<Pick<GenericConnectWebSocketParams<CommonWebSocket>, 'listeners' | 'protocols' | 'webSocketConstructor'>>;
|
|
59
|
+
/** Override the service definition's service origin. */
|
|
60
|
+
serviceOrigin: string;
|
|
41
61
|
}>>;
|
|
42
62
|
/**
|
|
43
63
|
* Creates an API from the given service definition, with automatically generated endpoint fetch and
|
|
@@ -63,7 +83,7 @@ export type GenerateApiOptions = Readonly<PartialWithUndefined<{
|
|
|
63
83
|
*
|
|
64
84
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
65
85
|
*/
|
|
66
|
-
export declare function generateApi<const SpecificService extends ServiceDefinition>(service: SpecificService,
|
|
86
|
+
export declare function generateApi<const SpecificService extends ServiceDefinition>(service: SpecificService, options?: Readonly<GenerateApiOptions>): RestVirApi<SpecificService>;
|
|
67
87
|
/**
|
|
68
88
|
* Mock inputs for {@link makeMockApi}.
|
|
69
89
|
*
|
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
import { mapObjectValues, } from '@augment-vir/common';
|
|
2
2
|
import { connectWebSocket } from './connect-web-socket.js';
|
|
3
3
|
import { fetchEndpoint, } from './fetch-endpoint.js';
|
|
4
|
+
/**
|
|
5
|
+
* An API generated by {@link generateApi}. This can be easily mocked by wrapping this API in
|
|
6
|
+
* {@link makeMockApi}.
|
|
7
|
+
*
|
|
8
|
+
* @category Internal
|
|
9
|
+
* @category Package : @rest-vir/define-service
|
|
10
|
+
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
11
|
+
*/
|
|
12
|
+
export class RestVirApi {
|
|
13
|
+
/**
|
|
14
|
+
* All supported endpoints within this API. Use `.fetch()` on an individual endpoint to send
|
|
15
|
+
* requests to it.
|
|
16
|
+
*/
|
|
17
|
+
endpoints;
|
|
18
|
+
/**
|
|
19
|
+
* All supported WebSockets within this API. Use `.connect()` on an individual WebSocket to open
|
|
20
|
+
* up connections to it.
|
|
21
|
+
*/
|
|
22
|
+
webSockets;
|
|
23
|
+
/**
|
|
24
|
+
* Where this API is hosted. This can be freely modified at any time and it will immediately
|
|
25
|
+
* affect all `endpoint.fetch()` and `webSocket.connect()` methods.
|
|
26
|
+
*
|
|
27
|
+
* @example 'https://example.com' 'http://localhost:3000'
|
|
28
|
+
*
|
|
29
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Location for help on which part of the URL is the origin (if necessary).
|
|
30
|
+
*/
|
|
31
|
+
serviceOrigin;
|
|
32
|
+
constructor(service, { endpointFetch, webSocketConnect, serviceOrigin } = {}) {
|
|
33
|
+
this.serviceOrigin = serviceOrigin || service.serviceOrigin;
|
|
34
|
+
this.endpoints = mapObjectValues(service.endpoints, (endpointPath, endpointDefinition) => {
|
|
35
|
+
return {
|
|
36
|
+
...endpointDefinition,
|
|
37
|
+
fetch: (...params) => {
|
|
38
|
+
return fetchEndpoint({
|
|
39
|
+
...endpointDefinition,
|
|
40
|
+
service: {
|
|
41
|
+
...endpointDefinition.service,
|
|
42
|
+
serviceOrigin: this.serviceOrigin,
|
|
43
|
+
},
|
|
44
|
+
}, {
|
|
45
|
+
...endpointFetch,
|
|
46
|
+
...params[0],
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
this.webSockets = mapObjectValues(service.webSockets, (webSocketPath, webSocketDefinition) => {
|
|
52
|
+
return {
|
|
53
|
+
...webSocketDefinition,
|
|
54
|
+
connect: (...params) => {
|
|
55
|
+
return connectWebSocket({
|
|
56
|
+
...webSocketDefinition,
|
|
57
|
+
service: {
|
|
58
|
+
...webSocketDefinition.service,
|
|
59
|
+
serviceOrigin: this.serviceOrigin,
|
|
60
|
+
},
|
|
61
|
+
}, {
|
|
62
|
+
...webSocketConnect,
|
|
63
|
+
...params[0],
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
4
70
|
/**
|
|
5
71
|
* Creates an API from the given service definition, with automatically generated endpoint fetch and
|
|
6
72
|
* WebSocket connect methods.
|
|
@@ -25,31 +91,8 @@ import { fetchEndpoint, } from './fetch-endpoint.js';
|
|
|
25
91
|
*
|
|
26
92
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
27
93
|
*/
|
|
28
|
-
export function generateApi(service,
|
|
29
|
-
return
|
|
30
|
-
endpoints: mapObjectValues(service.endpoints, (endpointPath, endpointDefinition) => {
|
|
31
|
-
return {
|
|
32
|
-
...endpointDefinition,
|
|
33
|
-
fetch: (...params) => {
|
|
34
|
-
return fetchEndpoint(endpointDefinition, {
|
|
35
|
-
...endpointFetch,
|
|
36
|
-
...params[0],
|
|
37
|
-
});
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}),
|
|
41
|
-
webSockets: mapObjectValues(service.webSockets, (webSocketPath, webSocketDefinition) => {
|
|
42
|
-
return {
|
|
43
|
-
...webSocketDefinition,
|
|
44
|
-
connect: (...params) => {
|
|
45
|
-
return connectWebSocket(webSocketDefinition, {
|
|
46
|
-
...webSocketConnect,
|
|
47
|
-
...params[0],
|
|
48
|
-
});
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
}),
|
|
52
|
-
};
|
|
94
|
+
export function generateApi(service, options = {}) {
|
|
95
|
+
return new RestVirApi(service, options);
|
|
53
96
|
}
|
|
54
97
|
/**
|
|
55
98
|
* Overwrites a generated API's fetch and WebSocket connect methods with a mocked fetch function and
|
package/dist/util/origin.d.ts
CHANGED
|
@@ -51,4 +51,8 @@ export type OriginRequirement = undefined | string | RegExp | AnyOrigin | (((ori
|
|
|
51
51
|
* @category Package : @rest-vir/define-service
|
|
52
52
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
53
53
|
*/
|
|
54
|
-
export declare const originRequirementShape: import("object-shape-tester").
|
|
54
|
+
export declare const originRequirementShape: import("object-shape-tester").ShapeDefinition<import("object-shape-tester").ShapeOr<[undefined, string, import("object-shape-tester").ShapeExact<readonly [{
|
|
55
|
+
anyOrigin: boolean;
|
|
56
|
+
}]>, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void, import("object-shape-tester").ShapeOr<[string, import("object-shape-tester").ShapeExact<readonly [{
|
|
57
|
+
anyOrigin: boolean;
|
|
58
|
+
}]>, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void]>[]]>, false>;
|
package/dist/util/origin.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { check } from '@augment-vir/assert';
|
|
2
|
-
import { classShape, or } from 'object-shape-tester';
|
|
2
|
+
import { classShape, defineShape, exact, or } from 'object-shape-tester';
|
|
3
3
|
/**
|
|
4
4
|
* Explicity denotes that any origin is allowed. Use {@link isAnyOrigin} to check if something is
|
|
5
5
|
* equal to this.
|
|
@@ -28,6 +28,6 @@ export function isAnyOrigin(input) {
|
|
|
28
28
|
* @category Package : @rest-vir/define-service
|
|
29
29
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
30
30
|
*/
|
|
31
|
-
export const originRequirementShape = or(undefined, '', classShape(RegExp), () => { }, [
|
|
32
|
-
or('', classShape(RegExp), () => { }),
|
|
33
|
-
]);
|
|
31
|
+
export const originRequirementShape = defineShape(or(undefined, '', exact(AnyOrigin), classShape(RegExp), () => { }, [
|
|
32
|
+
or('', exact(AnyOrigin), classShape(RegExp), () => { }),
|
|
33
|
+
]));
|
|
@@ -137,7 +137,11 @@ export declare const webSocketInitShape: ShapeDefinition<{
|
|
|
137
137
|
* service's origin requirement).
|
|
138
138
|
* - Any other set value overrides the service's origin requirement (if it has any).
|
|
139
139
|
*/
|
|
140
|
-
requiredClientOrigin: import("object-shape-tester").
|
|
140
|
+
requiredClientOrigin: import("object-shape-tester").ShapeOptional<ShapeDefinition<import("object-shape-tester").ShapeOr<[undefined, string, import("object-shape-tester").ShapeExact<readonly [{
|
|
141
|
+
anyOrigin: boolean;
|
|
142
|
+
}]>, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void, import("object-shape-tester").ShapeOr<[string, import("object-shape-tester").ShapeExact<readonly [{
|
|
143
|
+
anyOrigin: boolean;
|
|
144
|
+
}]>, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void]>[]]>, false>>;
|
|
141
145
|
customProps: import("object-shape-tester").ShapeOptional<import("object-shape-tester").ShapeOr<[undefined, import("object-shape-tester").ShapeIndexedKeys<[{
|
|
142
146
|
keys: import("object-shape-tester").ShapeUnknown<[unknown]>;
|
|
143
147
|
values: import("object-shape-tester").ShapeUnknown<[unknown]>;
|
|
@@ -21,7 +21,7 @@ export const webSocketInitShape = defineShape({
|
|
|
21
21
|
* service's origin requirement).
|
|
22
22
|
* - Any other set value overrides the service's origin requirement (if it has any).
|
|
23
23
|
*/
|
|
24
|
-
requiredClientOrigin: originRequirementShape,
|
|
24
|
+
requiredClientOrigin: optional(originRequirementShape),
|
|
25
25
|
customProps: optional(or(undefined, indexedKeys({
|
|
26
26
|
keys: unknownShape(),
|
|
27
27
|
values: unknownShape(),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rest-vir/define-service",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
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.10.
|
|
44
|
-
"@augment-vir/common": "^31.10.
|
|
43
|
+
"@augment-vir/assert": "^31.10.1",
|
|
44
|
+
"@augment-vir/common": "^31.10.1",
|
|
45
45
|
"date-vir": "^7.3.1",
|
|
46
|
-
"type-fest": "^4.
|
|
46
|
+
"type-fest": "^4.38.0",
|
|
47
47
|
"url-vir": "^2.1.3"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@augment-vir/test": "^31.10.
|
|
50
|
+
"@augment-vir/test": "^31.10.1",
|
|
51
51
|
"@web/dev-server-esbuild": "^1.0.4",
|
|
52
52
|
"@web/test-runner": "^0.20.0",
|
|
53
53
|
"@web/test-runner-commands": "^0.9.0",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"istanbul-smart-text-reporter": "^1.1.5",
|
|
57
57
|
"markdown-code-example-inserter": "^3.0.3",
|
|
58
58
|
"object-shape-tester": "^5.1.5",
|
|
59
|
-
"typedoc": "^0.
|
|
59
|
+
"typedoc": "^0.28.1",
|
|
60
60
|
"ws": "^8.18.1"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|