@rest-vir/define-service 0.9.0 → 0.10.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.
@@ -85,8 +85,8 @@ export type FindPortOptions = Pick<GenericFetchEndpointParams, 'fetch'> & Partia
85
85
  * const {origin} = await findDevServicePort(myService);
86
86
  * ```
87
87
  *
88
- * @throws Error if no valid starting port can be found or if the max scan distance has been reached
89
- * without finding a valid port.
88
+ * @returns `undefined` if the given service has no port in its service origin.
89
+ * @throws Error If the max scan distance has been reached without finding a valid port.
90
90
  * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
91
91
  */
92
92
  export declare function findDevServicePort(service: Readonly<SelectFrom<ServiceDefinition, {
@@ -96,18 +96,18 @@ export declare function findDevServicePort(service: Readonly<SelectFrom<ServiceD
96
96
  }>>, options?: Readonly<Omit<FindPortOptions, 'isValidResponse'>>): Promise<{
97
97
  port: number;
98
98
  origin: string;
99
- }>;
99
+ } | undefined>;
100
100
  /**
101
101
  * Find the first port, starting with the port in the given origin, that, with the given path, is
102
102
  * alive and matches, if provided, `isValidResponse`.
103
103
  *
104
104
  * @category Internal
105
105
  * @category Package : @rest-vir/define-service
106
- * @throws Error if no valid starting port can be found or if the max scan distance has been reached
107
- * without finding a valid port.
106
+ * @returns `undefined` if the given origin has no port number to start with
107
+ * @throws Error if the max scan distance has been reached without finding a valid port.
108
108
  * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
109
109
  */
110
- export declare function findLivePort(originWithStartingPort: string, pathToCheck: EndpointPathBase, { fetch, maxScanDistance, isValidResponse, timeout, }?: Readonly<Omit<FindPortOptions, 'overwriteOrigin'>>): Promise<number>;
110
+ export declare function findLivePort(originWithStartingPort: string, pathToCheck: EndpointPathBase, { fetch, maxScanDistance, isValidResponse, timeout, }?: Readonly<Omit<FindPortOptions, 'overwriteOrigin'>>): Promise<number | undefined>;
111
111
  /**
112
112
  * Creates a copy of a service definition (without mutating the original service definition) that
113
113
  * maps the service's origin port to find the live port that's actually running.
@@ -125,6 +125,7 @@ export declare function findLivePort(originWithStartingPort: string, pathToCheck
125
125
  * const mappedService = await mapServiceDevPort(myServiceDefinition);
126
126
  * ```
127
127
  *
128
+ * @returns The service unchanged if it does not have a port number in its service origin.
128
129
  * @throws Error if no valid starting port can be found or if the max scan distance has been reached
129
130
  * without finding a valid port.
130
131
  * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
@@ -48,8 +48,8 @@ export const restVirServiceNameHeader = 'rest-vir-service';
48
48
  * const {origin} = await findDevServicePort(myService);
49
49
  * ```
50
50
  *
51
- * @throws Error if no valid starting port can be found or if the max scan distance has been reached
52
- * without finding a valid port.
51
+ * @returns `undefined` if the given service has no port in its service origin.
52
+ * @throws Error If the max scan distance has been reached without finding a valid port.
53
53
  * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
54
54
  */
55
55
  export async function findDevServicePort(service, options = {}) {
@@ -59,14 +59,22 @@ export async function findDevServicePort(service, options = {}) {
59
59
  if (!endpoint) {
60
60
  throw new Error(`Service has no endpoints.`);
61
61
  }
62
- const port = await waitUntil.isNumber(() => findLivePort(startingOrigin, endpoint.path, {
63
- ...options,
64
- isValidResponse(response) {
65
- return (response.headers.get(restVirServiceNameHeader) === service.serviceName);
66
- },
67
- }), {
62
+ const { port } = await waitUntil.isDefined(async () => {
63
+ return {
64
+ port: await findLivePort(startingOrigin, endpoint.path, {
65
+ ...options,
66
+ isValidResponse(response) {
67
+ return (response.headers.get(restVirServiceNameHeader) ===
68
+ service.serviceName);
69
+ },
70
+ }),
71
+ };
72
+ }, {
68
73
  timeout: options.timeout,
69
74
  });
75
+ if (!port) {
76
+ return undefined;
77
+ }
70
78
  const { origin } = buildUrl(startingOrigin, {
71
79
  port,
72
80
  });
@@ -85,14 +93,14 @@ export async function findDevServicePort(service, options = {}) {
85
93
  *
86
94
  * @category Internal
87
95
  * @category Package : @rest-vir/define-service
88
- * @throws Error if no valid starting port can be found or if the max scan distance has been reached
89
- * without finding a valid port.
96
+ * @returns `undefined` if the given origin has no port number to start with
97
+ * @throws Error if the max scan distance has been reached without finding a valid port.
90
98
  * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
91
99
  */
92
100
  export async function findLivePort(originWithStartingPort, pathToCheck, { fetch = globalThis.fetch, maxScanDistance = 100, isValidResponse, timeout = { seconds: 10 }, } = {}) {
93
101
  const { port: originalPort } = parseUrl(originWithStartingPort);
94
102
  if (!originalPort) {
95
- throw new Error(`Given origin doesn't use a port.`);
103
+ return undefined;
96
104
  }
97
105
  const startingPort = Number(originalPort);
98
106
  assert.isNumber(startingPort, `Given origin doesn't have a valid port.`);
@@ -143,12 +151,16 @@ export async function findLivePort(originWithStartingPort, pathToCheck, { fetch
143
151
  * const mappedService = await mapServiceDevPort(myServiceDefinition);
144
152
  * ```
145
153
  *
154
+ * @returns The service unchanged if it does not have a port number in its service origin.
146
155
  * @throws Error if no valid starting port can be found or if the max scan distance has been reached
147
156
  * without finding a valid port.
148
157
  * @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
149
158
  */
150
159
  export async function mapServiceDevPort(service, options = {}) {
151
- const { origin } = await findDevServicePort(service, options);
160
+ const { origin } = (await findDevServicePort(service, options)) || {};
161
+ if (!origin) {
162
+ return service;
163
+ }
152
164
  return defineService({
153
165
  ...service.init,
154
166
  serviceOrigin: origin,
@@ -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").ShapeOr<[undefined, string, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void, import("object-shape-tester").ShapeOr<[string, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void]>[]]>;
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,
@@ -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").ShapeOr<[undefined, string, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void, import("object-shape-tester").ShapeOr<[string, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void]>[]]>;
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>;
@@ -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").ShapeOr<[undefined, string, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void, import("object-shape-tester").ShapeOr<[string, import("object-shape-tester").ShapeClass<[RegExpConstructor]>, () => void]>[]]>;
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.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Define an connect to a declarative and type safe REST and WebSocket service.",
5
5
  "keywords": [
6
6
  "rest",
@@ -43,7 +43,7 @@
43
43
  "@augment-vir/assert": "^31.10.1",
44
44
  "@augment-vir/common": "^31.10.1",
45
45
  "date-vir": "^7.3.1",
46
- "type-fest": "^4.37.0",
46
+ "type-fest": "^4.39.0",
47
47
  "url-vir": "^2.1.3"
48
48
  },
49
49
  "devDependencies": {
@@ -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.28.0",
59
+ "typedoc": "^0.28.1",
60
60
  "ws": "^8.18.1"
61
61
  },
62
62
  "peerDependencies": {