@api-client/core 0.7.8 → 0.7.11

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.
Files changed (55) hide show
  1. package/build/browser.d.ts +5 -1
  2. package/build/browser.js.map +1 -1
  3. package/build/index.d.ts +6 -1
  4. package/build/index.js +8 -0
  5. package/build/index.js.map +1 -1
  6. package/build/src/events/Events.d.ts +3 -3
  7. package/build/src/events/transport/TransportEvents.d.ts +10 -36
  8. package/build/src/events/transport/TransportEvents.js +9 -23
  9. package/build/src/events/transport/TransportEvents.js.map +1 -1
  10. package/build/src/models/AppProject.d.ts +10 -0
  11. package/build/src/models/AppProject.js +14 -0
  12. package/build/src/models/AppProject.js.map +1 -1
  13. package/build/src/proxy/AppProjectProxy.d.ts +28 -0
  14. package/build/src/proxy/AppProjectProxy.js +107 -0
  15. package/build/src/proxy/AppProjectProxy.js.map +1 -0
  16. package/build/src/proxy/HttpProjectProxy.d.ts +24 -0
  17. package/build/src/proxy/HttpProjectProxy.js +99 -0
  18. package/build/src/proxy/HttpProjectProxy.js.map +1 -0
  19. package/build/src/proxy/Proxy.d.ts +27 -0
  20. package/build/src/proxy/Proxy.js +7 -0
  21. package/build/src/proxy/Proxy.js.map +1 -0
  22. package/build/src/proxy/ProxyService.d.ts +33 -0
  23. package/build/src/proxy/ProxyService.js +43 -0
  24. package/build/src/proxy/ProxyService.js.map +1 -0
  25. package/build/src/proxy/RequestProxy.d.ts +42 -0
  26. package/build/src/proxy/RequestProxy.js +53 -0
  27. package/build/src/proxy/RequestProxy.js.map +1 -0
  28. package/build/src/runtime/http-runner/HttpRequestRunner.d.ts +5 -0
  29. package/build/src/runtime/http-runner/HttpRequestRunner.js +18 -0
  30. package/build/src/runtime/http-runner/HttpRequestRunner.js.map +1 -1
  31. package/build/src/runtime/node/InteropInterfaces.d.ts +2 -1
  32. package/build/src/runtime/node/ProjectParallelRunner.d.ts +3 -2
  33. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
  34. package/build/src/runtime/node/ProjectRequestRunner.d.ts +0 -5
  35. package/build/src/runtime/node/ProjectRequestRunner.js +1 -18
  36. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  37. package/build/src/runtime/node/ProjectRunner.d.ts +3 -3
  38. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  39. package/build/src/runtime/node/ProjectRunnerWorker.js +23 -5
  40. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
  41. package/package.json +4 -1
  42. package/src/events/transport/TransportEvents.ts +13 -53
  43. package/src/models/AppProject.ts +16 -0
  44. package/src/proxy/AppProjectProxy.ts +126 -0
  45. package/src/proxy/HttpProjectProxy.ts +114 -0
  46. package/src/proxy/Proxy.ts +30 -0
  47. package/src/proxy/ProxyService.ts +48 -0
  48. package/src/proxy/RequestProxy.ts +89 -0
  49. package/src/proxy/readme.md +9 -0
  50. package/src/runtime/http-runner/HttpRequestRunner.ts +19 -0
  51. package/src/runtime/node/InteropInterfaces.ts +2 -1
  52. package/src/runtime/node/ProjectParallelRunner.ts +3 -2
  53. package/src/runtime/node/ProjectRequestRunner.ts +2 -20
  54. package/src/runtime/node/ProjectRunner.ts +3 -3
  55. package/src/runtime/node/ProjectRunnerWorker.ts +21 -5
@@ -0,0 +1,48 @@
1
+ import { IProjectExecutionLog } from "../runtime/reporters/Reporter.js";
2
+ import { IRequestLog } from "../models/RequestLog.js";
3
+ import AppProjectProxy, { IAppProjectProxyInit } from "./AppProjectProxy.js";
4
+ import HttpProjectProxy, { IHttpProjectProxyInit } from "./HttpProjectProxy.js";
5
+ import { IProxyResult } from "./Proxy.js";
6
+ import RequestProxy, { IRequestProxyInit } from "./RequestProxy.js";
7
+
8
+ export default class ProxyService {
9
+ /**
10
+ * Executes a single HTTP request.
11
+ *
12
+ * @param init The request to execute with additional configuration.
13
+ * @returns The execution log for the request.
14
+ */
15
+ async proxyRequest(init: IRequestProxyInit): Promise<IProxyResult<IRequestLog>> {
16
+ const proxy = new RequestProxy();
17
+ await proxy.configure(init);
18
+ return proxy.execute();
19
+ }
20
+
21
+ /**
22
+ * Executes an HttpProject.
23
+ *
24
+ * @param token The user access token to read data from the store.
25
+ * @param storeUri The store's base URI.
26
+ * @param init The project run configuration.
27
+ * @returns The key under which the proxy is cached.
28
+ */
29
+ async proxyHttpProject(token: string, storeUri: string, init: IHttpProjectProxyInit): Promise<IProxyResult<IProjectExecutionLog>> {
30
+ const proxy = new HttpProjectProxy();
31
+ await proxy.configure(init, token, storeUri);
32
+ return proxy.execute();
33
+ }
34
+
35
+ /**
36
+ * Executes an AppProject.
37
+ *
38
+ * @param token The user access token to read data from the store.
39
+ * @param storeUri The store's base URI.
40
+ * @param init The project run configuration.
41
+ * @returns The key under which the proxy is cached.
42
+ */
43
+ async proxyAppProject(token: string, storeUri: string, init: IAppProjectProxyInit): Promise<IProxyResult<IProjectExecutionLog>> {
44
+ const proxy = new AppProjectProxy();
45
+ await proxy.configure(init, token, storeUri);
46
+ return proxy.execute() as Promise<IProxyResult<IProjectExecutionLog>>;
47
+ }
48
+ }
@@ -0,0 +1,89 @@
1
+ import { IHttpRequest, Kind as HttpRequestKind } from '../models/HttpRequest.js';
2
+ import { IRequestAuthorization } from '../models/RequestAuthorization.js';
3
+ import { IRequestConfig } from '../models/RequestConfig.js';
4
+ import Proxy, { IProxyResult } from "./Proxy.js";
5
+ import { HttpCertificate } from '../models/ClientCertificate.js';
6
+ import { IHttpActionFlow } from '../models/http-actions/HttpActions.js';
7
+ import { ApiError } from '../runtime/store/Errors.js';
8
+ import { DummyLogger } from '../lib/logging/DummyLogger.js';
9
+ import { HttpRequestRunner } from '../runtime/http-runner/HttpRequestRunner.js';
10
+ import { IRequestLog } from "../models/RequestLog.js";
11
+
12
+ export interface IRequestProxyInit {
13
+ kind: typeof HttpRequestKind;
14
+ /**
15
+ * The request to execute.
16
+ */
17
+ request: IHttpRequest;
18
+ /**
19
+ * The authorization data to apply.
20
+ */
21
+ authorization?: IRequestAuthorization[];
22
+ /**
23
+ * The request configuration.
24
+ */
25
+ config?: IRequestConfig;
26
+ /**
27
+ * The list of execution variables to use with the request.
28
+ */
29
+ variables?: Record<string, string>;
30
+ /**
31
+ * The certificate data to use with the request.
32
+ */
33
+ certificate?: HttpCertificate;
34
+ /**
35
+ * The request flows to execute with the request.
36
+ */
37
+ flows?: IHttpActionFlow[];
38
+ }
39
+
40
+ /**
41
+ * Proxies a single HTTP request
42
+ */
43
+ export default class RequestProxy extends Proxy {
44
+ init?: IRequestProxyInit;
45
+
46
+ async configure(init: IRequestProxyInit): Promise<void> {
47
+ const { request } = init;
48
+ if (!request) {
49
+ throw new ApiError({
50
+ error: true,
51
+ message: 'Invalid request',
52
+ detail: 'The "request" parameter is required.',
53
+ code: 400,
54
+ });
55
+ }
56
+ if (!request.url) {
57
+ throw new ApiError({
58
+ error: true,
59
+ message: 'Invalid request',
60
+ detail: 'The "request.url" parameter is required.',
61
+ code: 400,
62
+ });
63
+ }
64
+ this.init = init;
65
+ }
66
+
67
+ async execute(): Promise<IProxyResult<IRequestLog>> {
68
+ const data = this.init as IRequestProxyInit;
69
+ const { request, authorization, certificate, config={}, variables={}, flows } = data;
70
+ const factory = new HttpRequestRunner();
71
+ factory.variables = variables;
72
+ factory.logger = new DummyLogger();
73
+ factory.config = { ...config, enabled: true } as IRequestConfig;
74
+ if (Array.isArray(authorization) && authorization.length) {
75
+ factory.authorization = authorization;
76
+ }
77
+ if (Array.isArray(flows) && flows.length) {
78
+ factory.flows = flows;
79
+ }
80
+ if (certificate) {
81
+ factory.certificates = [certificate];
82
+ }
83
+ const result = await factory.run(request);
84
+ return {
85
+ result: result,
86
+ variables,
87
+ };
88
+ }
89
+ }
@@ -0,0 +1,9 @@
1
+ # HTTP Proxy
2
+
3
+ The libraries to create an HTTP/Socket proxy for API things (projects, requests).
4
+
5
+ ## Libraries
6
+
7
+ - `AppProjectProxy` - a class that runs request from an `AppProject`
8
+ - `HttpProjectProxy` - a class that runs request from an `HttpProject`
9
+ - `RequestProxy` - a class that runs a single request as `HttpRequest`
@@ -166,6 +166,7 @@ export class HttpRequestRunner {
166
166
  const { variables, variablesProcessor } = this;
167
167
  let copy: IHttpRequest = { ...request };
168
168
  if (variables) {
169
+ copy.url = this.prepareRequestUrl(copy.url, variables);
169
170
  copy = await variablesProcessor.evaluateVariablesWithContext(copy, variables);
170
171
  }
171
172
  return copy;
@@ -350,4 +351,22 @@ export class HttpRequestRunner {
350
351
  }
351
352
  return !config.ignoreSessionCookies;
352
353
  }
354
+
355
+ /**
356
+ * When defined it applies the serve's base URI to relative URLs.
357
+ * @param currentUrl The URL to process.
358
+ */
359
+ prepareRequestUrl(currentUrl: string, variables: Record<string, string>): string {
360
+ const { baseUri } = variables;
361
+ if (!baseUri) {
362
+ return currentUrl;
363
+ }
364
+ if (currentUrl.startsWith('http:') || currentUrl.startsWith('https:')) {
365
+ return currentUrl;
366
+ }
367
+ if (currentUrl.startsWith('/')) {
368
+ return `${baseUri}${currentUrl}`;
369
+ }
370
+ return `${baseUri}/${currentUrl}`;
371
+ }
353
372
  }
@@ -4,6 +4,7 @@ import { Environment } from '../../models/Environment.js';
4
4
  import { Logger } from '../../lib/logging/Logger.js';
5
5
  import { IRequestLog } from '../../models/RequestLog.js';
6
6
  import { CookieJar } from '../../cookies/CookieJar.js';
7
+ import { IAppProject } from 'browser.js';
7
8
 
8
9
  export interface IRequestRunnerOptions {
9
10
  /**
@@ -71,7 +72,7 @@ export interface IProjectParallelRunnerOptions extends IProjectRunnerOptions {
71
72
  }
72
73
 
73
74
  export interface IProjectParallelWorkerOptions extends IProjectRunnerOptions {
74
- project: IHttpProject;
75
+ project: IHttpProject | IAppProject;
75
76
  }
76
77
 
77
78
  export interface IProjectRunnerOptions {
@@ -3,6 +3,7 @@ import { cpus } from 'os';
3
3
  import { dirname, join } from 'path';
4
4
  import { fileURLToPath } from 'url';
5
5
  import { HttpProject } from '../../models/HttpProject.js';
6
+ import { AppProject } from '../../models/AppProject.js';
6
7
  import { IProjectExecutionLog, IProjectExecutionIteration } from '../reporters/Reporter.js';
7
8
  import { BaseRunner } from './BaseRunner.js';
8
9
  import { State } from './enums.js';
@@ -66,13 +67,13 @@ export interface ProjectParallelRunner {
66
67
  * change. This event can be user to refresh the UI to reflect the newest state.
67
68
  */
68
69
  export class ProjectParallelRunner extends BaseRunner {
69
- project: HttpProject;
70
+ project: HttpProject | AppProject;
70
71
  options: IProjectParallelRunnerOptions;
71
72
  workers: WorkerInfoInternal[] = [];
72
73
  private mainResolver?: (report: IProjectExecutionLog) => void;
73
74
  private mainRejecter?: (err: Error) => void;
74
75
 
75
- constructor(project: HttpProject, opts: IProjectParallelRunnerOptions = {}) {
76
+ constructor(project: HttpProject | AppProject, opts: IProjectParallelRunnerOptions = {}) {
76
77
  super();
77
78
  this.project = project;
78
79
  this.options = opts;
@@ -252,11 +252,11 @@ export class ProjectRequestRunner extends EventEmitter {
252
252
  key: request.key,
253
253
  };
254
254
  const requestData = request.expects.toJSON();
255
- requestData.url = this.prepareRequestUrl(requestData.url, variables);
256
-
255
+
257
256
  try {
258
257
  // Below replaces the single call to the `run()` function of the factory to
259
258
  // report via the events a request object that has evaluated with the Jexl library.
259
+ requestData.url = factory.prepareRequestUrl(requestData.url, variables);
260
260
  const requestCopy = await factory.applyVariables(requestData);
261
261
  await factory.applyAuthorization(requestCopy);
262
262
  await factory.applyCookies(requestCopy);
@@ -333,22 +333,4 @@ export class ProjectRequestRunner extends EventEmitter {
333
333
  ctx.baseUri = baseUri || '';
334
334
  return this.variablesProcessor.buildContext(ctx);
335
335
  }
336
-
337
- /**
338
- * When defined it applies the serve's base URI to relative URLs.
339
- * @param currentUrl The URL to process.
340
- */
341
- protected prepareRequestUrl(currentUrl: string, variables: Record<string, string>): string {
342
- const { baseUri } = variables;
343
- if (!baseUri) {
344
- return currentUrl;
345
- }
346
- if (currentUrl.startsWith('http:') || currentUrl.startsWith('https:')) {
347
- return currentUrl;
348
- }
349
- if (currentUrl.startsWith('/')) {
350
- return `${baseUri}${currentUrl}`;
351
- }
352
- return `${baseUri}/${currentUrl}`;
353
- }
354
336
  }
@@ -11,9 +11,9 @@ import { pathExists, readJson } from '../../lib/fs/Fs.js';
11
11
  import { BaseRunner } from './BaseRunner.js';
12
12
  import { IProjectRunnerOptions, IRequestRunnerOptions } from './InteropInterfaces.js';
13
13
  import { State } from './enums.js';
14
- import { AppProject } from '../../models/AppProject.js';
14
+ import { AppProject, AppProjectFolder } from '../../models/AppProject.js';
15
15
 
16
- type ProjectParent = HttpProject | ProjectFolder;
16
+ type ProjectParent = HttpProject | ProjectFolder | AppProject | AppProjectFolder;
17
17
 
18
18
  export interface ProjectRunner {
19
19
  /**
@@ -173,7 +173,7 @@ export abstract class ProjectRunner extends BaseRunner {
173
173
  * A required step before running the project.
174
174
  * It configures the execution context. It may throw an error when configuration is not valid.
175
175
  */
176
- async configure(project: HttpProject, opts: IProjectRunnerOptions = {}): Promise<void> {
176
+ async configure(project: HttpProject | AppProject, opts: IProjectRunnerOptions = {}): Promise<void> {
177
177
  this.project = project;
178
178
  this.options = opts || {};
179
179
  if (typeof this.options.iterations === 'number' && this.options.iterations >= 0) {
@@ -2,7 +2,8 @@
2
2
  /* eslint-disable no-unused-vars */
3
3
  import process from 'process';
4
4
  import cluster from 'cluster';
5
- import { HttpProject } from '../../models/HttpProject.js';
5
+ import { HttpProject, Kind as HttpProjectKind } from '../../models/HttpProject.js';
6
+ import { AppProject, AppProjectKind } from '../../models/AppProject.js';
6
7
  import { IProjectExecutionLog } from '../reporters/Reporter.js';
7
8
  import { IWorkerMessage } from './ProjectParallelRunner.js';
8
9
  import { IProjectParallelWorkerOptions } from './InteropInterfaces.js';
@@ -16,7 +17,9 @@ class ProjectExeWorker extends ProjectRunner {
16
17
  if (cluster.isPrimary) {
17
18
  throw new Error(`This file should not be called directly.`);
18
19
  }
19
- process.send!({ cmd: 'online' });
20
+ if (process.send) {
21
+ process.send({ cmd: 'online' });
22
+ }
20
23
  process.on('message', this.messageHandler.bind(this));
21
24
  }
22
25
 
@@ -29,11 +32,22 @@ class ProjectExeWorker extends ProjectRunner {
29
32
  async run(options: IProjectParallelWorkerOptions): Promise<void> {
30
33
  options.cookies = new InMemoryCookieJar();
31
34
  try {
32
- await this.configure(new HttpProject(options.project), options);
35
+ const schema = options.project;
36
+ let project: HttpProject | AppProject;
37
+ if (schema.kind === HttpProjectKind) {
38
+ project = new HttpProject(schema);
39
+ } else if (schema.kind === AppProjectKind) {
40
+ project = new AppProject(schema);
41
+ } else {
42
+ throw new Error(`Unknown project kind: ${schema.kind}`);
43
+ }
44
+ await this.configure(project, options);
33
45
  await this.execute();
34
46
  } catch (e) {
35
47
  const cause = e as Error;
36
- process.send!({ cmd: 'error', data: cause.message });
48
+ if (process.send) {
49
+ process.send({ cmd: 'error', data: cause.message });
50
+ }
37
51
  }
38
52
  }
39
53
 
@@ -58,7 +72,9 @@ class ProjectExeWorker extends ProjectRunner {
58
72
  this.endTime = Date.now();
59
73
 
60
74
  const log = await this.createReport();
61
- process.send!({ cmd: 'result', data: log.iterations });
75
+ if (process.send) {
76
+ process.send({ cmd: 'result', data: log.iterations });
77
+ }
62
78
  this._state = State.Idle as State;
63
79
  return log;
64
80
  }