@travetto/web-rpc 6.0.1 → 6.0.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.
- package/README.md +1 -1
- package/package.json +4 -4
- package/src/controller.ts +9 -1
- package/support/client/rpc.ts +18 -4
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @travetto/web-rpc
|
|
|
13
13
|
yarn add @travetto/web-rpc
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
This module allows for a highly focused scenario, of supporting RPC operations within a [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative
|
|
16
|
+
This module allows for a highly focused scenario, of supporting RPC operations within a [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") application. The module takes care of producing the appropriate handler for the RPC commands along with the ability to generate the appropriate client to be used to interact with the RPC functionality. The generated client uses Proxy-based objects, along with [Typescript](https://typescriptlang.org) magic to create a dynamic client that does not rely on generating a lot of code.
|
|
17
17
|
|
|
18
18
|
## CLI - web:rpc-client
|
|
19
19
|
The library will create the RPC client in one of three flavors: fetch, fetch + node, angular.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/web-rpc",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.3",
|
|
4
4
|
"description": "RPC support for a Web Application",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web",
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"directory": "module/web-rpc"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/config": "^6.0.
|
|
30
|
-
"@travetto/schema": "^6.0.
|
|
31
|
-
"@travetto/web": "^6.0.
|
|
29
|
+
"@travetto/config": "^6.0.1",
|
|
30
|
+
"@travetto/schema": "^6.0.1",
|
|
31
|
+
"@travetto/web": "^6.0.3"
|
|
32
32
|
},
|
|
33
33
|
"travetto": {
|
|
34
34
|
"displayName": "Web RPC Support"
|
package/src/controller.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Any, AppError, Util } from '@travetto/runtime';
|
|
|
3
3
|
import {
|
|
4
4
|
HeaderParam, Controller, Undocumented, ExcludeInterceptors, ControllerRegistry,
|
|
5
5
|
WebAsyncContext, Body, EndpointUtil, BodyInterceptor, Post, WebCommonUtil,
|
|
6
|
-
RespondInterceptor, DecompressInterceptor
|
|
6
|
+
RespondInterceptor, DecompressInterceptor, Get
|
|
7
7
|
} from '@travetto/web';
|
|
8
8
|
|
|
9
9
|
@Controller('/rpc')
|
|
@@ -19,6 +19,14 @@ export class WebRpcController {
|
|
|
19
19
|
@Inject()
|
|
20
20
|
ctx: WebAsyncContext;
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Allow for get-based requests
|
|
24
|
+
*/
|
|
25
|
+
@Get('/:target')
|
|
26
|
+
async onGetRequest(target: string, @HeaderParam('X-TRV-RPC-INPUTS') paramInput?: string): Promise<unknown> {
|
|
27
|
+
return this.onRequest(target, paramInput);
|
|
28
|
+
}
|
|
29
|
+
|
|
22
30
|
/**
|
|
23
31
|
* RPC main entrypoint
|
|
24
32
|
*/
|
package/support/client/rpc.ts
CHANGED
|
@@ -25,6 +25,8 @@ export type RpcRequest = {
|
|
|
25
25
|
timeout?: number;
|
|
26
26
|
retriesOnConnectFailure?: number;
|
|
27
27
|
path?: string;
|
|
28
|
+
controller?: string;
|
|
29
|
+
endpoint?: string;
|
|
28
30
|
};
|
|
29
31
|
url: URL | string;
|
|
30
32
|
consumeJSON?: <T>(text?: unknown) => (T | Promise<T>);
|
|
@@ -72,11 +74,21 @@ function buildRequest<T extends RequestInit>(base: T, controller: string, endpoi
|
|
|
72
74
|
return {
|
|
73
75
|
...base,
|
|
74
76
|
method: 'POST',
|
|
75
|
-
path: `${controller}:${endpoint}
|
|
77
|
+
path: `${controller}:${endpoint}`,
|
|
78
|
+
controller,
|
|
79
|
+
endpoint
|
|
76
80
|
};
|
|
77
81
|
}
|
|
78
82
|
|
|
79
|
-
export function getBody(inputs: unknown[]): { body: FormData | string, headers: Record<string, string> } {
|
|
83
|
+
export function getBody(inputs: unknown[], isBodyRequest: boolean): { body: FormData | string | undefined, headers: Record<string, string> } {
|
|
84
|
+
if (!isBodyRequest) {
|
|
85
|
+
return {
|
|
86
|
+
body: undefined,
|
|
87
|
+
headers: {
|
|
88
|
+
'X-TRV-RPC-INPUTS': btoa(encodeURIComponent(JSON.stringify(inputs)))
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
80
92
|
// If we do not have a blob, simple output
|
|
81
93
|
if (!inputs.some(isBlobLike)) {
|
|
82
94
|
return {
|
|
@@ -147,8 +159,10 @@ export async function invokeFetch<T>(request: RpcRequest, ...params: unknown[]):
|
|
|
147
159
|
let core = request.core!;
|
|
148
160
|
|
|
149
161
|
try {
|
|
150
|
-
const { body, headers } = getBody(params);
|
|
151
|
-
|
|
162
|
+
const { body, headers } = getBody(params, /^(post|put|patch)$/i.test(request.core?.method ?? 'POST'));
|
|
163
|
+
if (body) {
|
|
164
|
+
core.body = body;
|
|
165
|
+
}
|
|
152
166
|
core.headers = extendHeaders(core.headers, headers);
|
|
153
167
|
|
|
154
168
|
for (const fn of request.preRequestHandlers ?? []) {
|