@travetto/web-rpc 6.0.0-rc.3 → 6.0.0-rc.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/web-rpc",
3
- "version": "6.0.0-rc.3",
3
+ "version": "6.0.0-rc.5",
4
4
  "description": "RPC support for a Web Application",
5
5
  "keywords": [
6
6
  "web",
@@ -28,7 +28,7 @@
28
28
  "dependencies": {
29
29
  "@travetto/config": "^6.0.0-rc.2",
30
30
  "@travetto/schema": "^6.0.0-rc.2",
31
- "@travetto/web": "^6.0.0-rc.3"
31
+ "@travetto/web": "^6.0.0-rc.4"
32
32
  },
33
33
  "travetto": {
34
34
  "displayName": "Web RPC Support"
package/src/controller.ts CHANGED
@@ -3,13 +3,18 @@ import { Any, AppError, Util } from '@travetto/runtime';
3
3
  import {
4
4
  HeaderParam, Controller, Undocumented, ExcludeInterceptors, ControllerRegistry,
5
5
  WebAsyncContext, Body, EndpointUtil, BodyParseInterceptor, Post, WebCommonUtil,
6
- RespondInterceptor
6
+ RespondInterceptor, DecompressInterceptor
7
7
  } from '@travetto/web';
8
8
 
9
9
  @Controller('/rpc')
10
- @ExcludeInterceptors(val => !(val instanceof BodyParseInterceptor || val instanceof RespondInterceptor || val.category === 'global'))
10
+ @ExcludeInterceptors(val => !(
11
+ val instanceof DecompressInterceptor ||
12
+ val instanceof BodyParseInterceptor ||
13
+ val instanceof RespondInterceptor ||
14
+ val.category === 'global'
15
+ ))
11
16
  @Undocumented()
12
- export class WebRpController {
17
+ export class WebRpcController {
13
18
 
14
19
  @Inject()
15
20
  ctx: WebAsyncContext;
@@ -21,12 +26,10 @@ export class WebRpController {
21
26
  async onRequest(target: string, @HeaderParam('X-TRV-RPC-INPUTS') paramInput?: string, @Body() body?: Any): Promise<unknown> {
22
27
  const endpoint = ControllerRegistry.getEndpointById(target);
23
28
 
24
- if (!endpoint) {
29
+ if (!endpoint || !endpoint.filter) {
25
30
  throw new AppError('Unknown endpoint', { category: 'notfound' });
26
31
  }
27
32
 
28
- const bodyParamIdx = endpoint.params.findIndex((x) => x.location === 'body');
29
-
30
33
  const { request } = this.ctx;
31
34
 
32
35
  let params: unknown[];
@@ -36,6 +39,8 @@ export class WebRpController {
36
39
  params = Util.decodeSafeJSON(paramInput)!;
37
40
  } else if (Array.isArray(body)) { // Params passed via body
38
41
  params = body;
42
+
43
+ const bodyParamIdx = endpoint.params.findIndex((x) => x.location === 'body');
39
44
  if (bodyParamIdx >= 0) { // Re-assign body
40
45
  request.body = params[bodyParamIdx];
41
46
  }
@@ -49,6 +54,6 @@ export class WebRpController {
49
54
  WebCommonUtil.setRequestParams(request, final);
50
55
 
51
56
  // Dispatch
52
- return await endpoint.filter!({ request: this.ctx.request });
57
+ return endpoint.filter({ request });
53
58
  }
54
59
  }
package/src/service.ts CHANGED
@@ -30,6 +30,7 @@ export class WebRpcClientGeneratorService {
30
30
  const entry = RuntimeIndex.getEntry(Runtime.getSourceFile(x));
31
31
  return entry && entry.role === 'std';
32
32
  })
33
+ .filter(x => ControllerRegistry.get(x).documented !== false)
33
34
  .map(x => {
34
35
  const imp = ManifestModuleUtil.withOutputExtension(Runtime.getImport(x));
35
36
  const base = Runtime.workspaceRelative(RuntimeIndex.manifest.build.typesFolder);
@@ -49,7 +50,7 @@ export class WebRpcClientGeneratorService {
49
50
  const clientOutputFile = path.resolve(config.output, path.basename(clientSourceFile));
50
51
  const clientSourceContents = await fs.readFile(clientSourceFile, 'utf8');
51
52
 
52
- const flavorSourceFile = RuntimeIndex.getFromImport(`@travetto/web-rpc/support/client/rpc-${config.type}.ts`)!.sourceFile;
53
+ const flavorSourceFile = RuntimeIndex.getFromImport(`@travetto/web-rpc/support/client/rpc-${config.type}.ts`)?.sourceFile ?? '<unknown>';
53
54
  const flavorOutputFile = path.resolve(config.output, path.basename(flavorSourceFile));
54
55
  const flavorSourceContents = (await fs.readFile(flavorSourceFile, 'utf8').catch(() => ''))
55
56
  .replaceAll(/^\s*\/\/\s*@ts-ignore[^\n]*\n/gsm, '')
@@ -57,7 +58,7 @@ export class WebRpcClientGeneratorService {
57
58
 
58
59
  const factoryOutputFile = path.resolve(config.output, 'factory.ts');
59
60
  const factorySourceContents = [
60
- `import { ${clientFactory.name} } from './rpc.ts';`,
61
+ `import { ${clientFactory.name} } from './rpc';`,
61
62
  ...classes.map((n) => `import type { ${n.name} } from '${n.import}';`),
62
63
  '',
63
64
  `export const factory = ${clientFactory.name}<{`,
@@ -11,6 +11,12 @@ const isBlobMap = (x: any): x is Record<string, Blob> => x && typeof x === 'obje
11
11
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
12
  const isBlobLike = (x: any): x is Record<string, Blob> | Blob => x instanceof Blob || isBlobMap(x);
13
13
 
14
+ const extendHeaders = (base: RequestInit['headers'], toAdd: Record<string, string>): Headers => {
15
+ const headers = new Headers(base);
16
+ for (const [k, v] of Object.entries(toAdd)) { headers.set(k, v); }
17
+ return headers;
18
+ };
19
+
14
20
  export type PreRequestHandler = (item: RequestInit) => Promise<RequestInit | undefined | void>;
15
21
  export type PostResponseHandler = (item: Response) => Promise<Response | undefined | void>;
16
22
 
@@ -66,11 +72,7 @@ function buildRequest<T extends RequestInit>(base: T, controller: string, endpoi
66
72
  return {
67
73
  ...base,
68
74
  method: 'POST',
69
- path: `${controller}:${endpoint}`,
70
- headers: {
71
- ...base.headers,
72
- 'Content-Type': 'application/json',
73
- }
75
+ path: `${controller}:${endpoint}`
74
76
  };
75
77
  }
76
78
 
@@ -147,10 +149,7 @@ export async function invokeFetch<T>(request: RpcRequest, ...params: unknown[]):
147
149
  try {
148
150
  const { body, headers } = getBody(params);
149
151
  core.body = body;
150
- core.headers = {
151
- ...core.headers ?? {},
152
- ...headers
153
- };
152
+ core.headers = extendHeaders(core.headers, headers);
154
153
 
155
154
  for (const fn of request.preRequestHandlers ?? []) {
156
155
  const computed = await fn(core);
@@ -175,13 +174,14 @@ export async function invokeFetch<T>(request: RpcRequest, ...params: unknown[]):
175
174
  core.signal = AbortSignal.any(signals);
176
175
  }
177
176
 
177
+ const url = typeof request.url === 'string' ? new URL(request.url) : request.url;
178
+ if (request.core?.path) {
179
+ url.pathname = `${url.pathname}/${request.core.path}`.replaceAll('//', '/');
180
+ }
181
+
178
182
  let resolved: Response | undefined;
179
183
  for (let i = 0; i <= (core.retriesOnConnectFailure ?? 0); i += 1) {
180
184
  try {
181
- const url = typeof request.url === 'string' ? new URL(request.url) : request.url;
182
- if (request.core?.path) {
183
- url.pathname = `${url.pathname}/${request.core.path}`.replaceAll('//', '/');
184
- }
185
185
  resolved = await fetch(url, core);
186
186
  break;
187
187
  } catch (err) {