@rest-vir/define-service 0.8.3 → 0.9.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.
@@ -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 type RestVirApi<SpecificService extends ServiceDefinition> = {
18
- endpoints: {
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
- webSockets: {
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, { endpointFetch, webSocketConnect }?: Readonly<GenerateApiOptions>): RestVirApi<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, { endpointFetch, webSocketConnect } = {}) {
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rest-vir/define-service",
3
- "version": "0.8.3",
3
+ "version": "0.9.0",
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.0",
44
- "@augment-vir/common": "^31.10.0",
43
+ "@augment-vir/assert": "^31.10.1",
44
+ "@augment-vir/common": "^31.10.1",
45
45
  "date-vir": "^7.3.1",
46
46
  "type-fest": "^4.37.0",
47
47
  "url-vir": "^2.1.3"
48
48
  },
49
49
  "devDependencies": {
50
- "@augment-vir/test": "^31.10.0",
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.27.9",
59
+ "typedoc": "^0.28.0",
60
60
  "ws": "^8.18.1"
61
61
  },
62
62
  "peerDependencies": {