@opra/core 0.25.5 → 0.26.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.
Files changed (63) hide show
  1. package/cjs/augmentation/container.augmentation.js +2 -0
  2. package/cjs/http/adapters/express-adapter.host.js +34 -0
  3. package/cjs/http/{express-adapter.js → adapters/express-adapter.js} +1 -3
  4. package/cjs/http/{http-adapter.host.js → adapters/node-http-adapter.host.js} +30 -22
  5. package/cjs/http/adapters/node-http-adapter.js +14 -0
  6. package/cjs/http/helpers/json-body-loader.js +29 -0
  7. package/cjs/http/http-adapter-host.js +678 -0
  8. package/cjs/index.js +4 -3
  9. package/cjs/platform-adapter.host.js +74 -45
  10. package/cjs/{endpoint-context.js → request-context.js} +5 -5
  11. package/cjs/request.host.js +3 -0
  12. package/esm/augmentation/container.augmentation.js +1 -0
  13. package/esm/http/adapters/express-adapter.host.js +30 -0
  14. package/esm/http/{express-adapter.js → adapters/express-adapter.js} +1 -3
  15. package/esm/http/{http-adapter.host.js → adapters/node-http-adapter.host.js} +28 -20
  16. package/esm/http/adapters/node-http-adapter.js +11 -0
  17. package/esm/http/helpers/json-body-loader.js +24 -0
  18. package/esm/http/http-adapter-host.js +673 -0
  19. package/esm/index.js +4 -3
  20. package/esm/platform-adapter.host.js +75 -46
  21. package/esm/{endpoint-context.js → request-context.js} +4 -4
  22. package/esm/request.host.js +3 -0
  23. package/i18n/en/error.json +1 -2
  24. package/package.json +3 -3
  25. package/types/augmentation/collection.augmentation.d.ts +19 -16
  26. package/types/augmentation/container.augmentation.d.ts +13 -0
  27. package/types/augmentation/resource.augmentation.d.ts +2 -2
  28. package/types/augmentation/singleton.augmentation.d.ts +13 -9
  29. package/types/augmentation/storage.augmentation.d.ts +11 -14
  30. package/types/http/{express-adapter.d.ts → adapters/express-adapter.d.ts} +3 -3
  31. package/types/http/adapters/express-adapter.host.d.ts +12 -0
  32. package/types/http/{http-adapter.d.ts → adapters/node-http-adapter.d.ts} +5 -5
  33. package/types/http/adapters/node-http-adapter.host.d.ts +19 -0
  34. package/types/http/helpers/json-body-loader.d.ts +5 -0
  35. package/types/http/http-adapter-host.d.ts +34 -0
  36. package/types/index.d.ts +4 -3
  37. package/types/interfaces/request-handler.interface.d.ts +1 -1
  38. package/types/platform-adapter.d.ts +2 -2
  39. package/types/platform-adapter.host.d.ts +18 -14
  40. package/types/{endpoint-context.d.ts → request-context.d.ts} +3 -3
  41. package/types/request.d.ts +7 -2
  42. package/types/request.host.d.ts +5 -2
  43. package/cjs/http/express-adapter.host.js +0 -24
  44. package/cjs/http/http-adapter-base.js +0 -138
  45. package/cjs/http/http-adapter.js +0 -16
  46. package/cjs/http/request-handlers/entity-request-handler.js +0 -429
  47. package/cjs/http/request-handlers/parse-batch-request.js +0 -169
  48. package/cjs/http/request-handlers/request-handler-base.js +0 -37
  49. package/cjs/http/request-handlers/storage-request-handler.js +0 -139
  50. package/esm/http/express-adapter.host.js +0 -20
  51. package/esm/http/http-adapter-base.js +0 -134
  52. package/esm/http/http-adapter.js +0 -13
  53. package/esm/http/request-handlers/entity-request-handler.js +0 -424
  54. package/esm/http/request-handlers/parse-batch-request.js +0 -169
  55. package/esm/http/request-handlers/request-handler-base.js +0 -33
  56. package/esm/http/request-handlers/storage-request-handler.js +0 -134
  57. package/types/http/express-adapter.host.d.ts +0 -11
  58. package/types/http/http-adapter-base.d.ts +0 -23
  59. package/types/http/http-adapter.host.d.ts +0 -18
  60. package/types/http/request-handlers/entity-request-handler.d.ts +0 -24
  61. package/types/http/request-handlers/parse-batch-request.d.ts +0 -0
  62. package/types/http/request-handlers/request-handler-base.d.ts +0 -16
  63. package/types/http/request-handlers/storage-request-handler.d.ts +0 -23
package/esm/index.js CHANGED
@@ -1,16 +1,17 @@
1
1
  import "reflect-metadata";
2
2
  import './augmentation/resource.augmentation.js';
3
3
  import './augmentation/collection.augmentation.js';
4
+ import './augmentation/container.augmentation.js';
4
5
  import './augmentation/singleton.augmentation.js';
5
6
  import './augmentation/storage.augmentation.js';
6
7
  export * from './types.js';
7
8
  export * from './execution-context.js';
8
- export * from './endpoint-context.js';
9
+ export * from './request-context.js';
9
10
  export * from './platform-adapter.js';
10
11
  export * from './request.js';
11
12
  export * from './response.js';
12
- export * from './http/express-adapter.js';
13
- export * from './http/http-adapter.js';
13
+ export * from './http/adapters/express-adapter.js';
14
+ export * from './http/adapters/node-http-adapter.js';
14
15
  export * from './http/impl/http-incoming-message.host.js';
15
16
  export * from './http/impl/http-outgoing-message.host.js';
16
17
  export * from './http/http-server-request.js';
@@ -1,34 +1,19 @@
1
1
  import path from 'path';
2
2
  import { pascalCase } from 'putil-varhelpers';
3
3
  import { AsyncEventEmitter } from 'strict-typed-events';
4
- import { getStackFileName, I18n, Resource } from '@opra/common';
4
+ import { Container, CrudResource, ForbiddenError, getStackFileName, I18n, Resource, translate } from '@opra/common';
5
5
  import { Logger } from './services/logger.js';
6
+ const resourceInitialized = Symbol.for('opra.resource.initialized');
6
7
  /**
7
8
  * @class PlatformAdapterHost
8
9
  */
9
10
  export class PlatformAdapterHost extends AsyncEventEmitter {
10
- constructor(api, options) {
11
- super();
12
- this.api = api;
13
- this._controllers = new WeakMap();
14
- this._initialized = false;
15
- this._options = options || {};
16
- this._interceptors = [...(options?.interceptors || [])];
17
- this._logger = options?.logger && options.logger instanceof Logger
18
- ? options.logger
19
- : new Logger({ instance: options?.logger });
20
- // Assign events
21
- if (options?.on) {
22
- for (const [event, fn] of Object.entries(options.on)) {
23
- /* istanbul ignore next */
24
- if (typeof fn === 'function')
25
- this.on(event, fn);
26
- }
27
- }
28
- // Make a safe service name
29
- this._serviceName = pascalCase((api.info.title || '').replace(/[^a-z0-9_ ]/ig, '')) || 'OpraService';
30
- if (!/^[a-z]/i.test(this._serviceName))
31
- this._serviceName = 'X' + this._serviceName;
11
+ constructor() {
12
+ super(...arguments);
13
+ this._controllers = new Map();
14
+ }
15
+ get api() {
16
+ return this._api;
32
17
  }
33
18
  get platform() {
34
19
  return this._platform;
@@ -37,56 +22,100 @@ export class PlatformAdapterHost extends AsyncEventEmitter {
37
22
  return this._protocol;
38
23
  }
39
24
  get serviceName() {
40
- return this.api.info.title;
25
+ return this._serviceName;
26
+ }
27
+ get i18n() {
28
+ return this._i18n;
41
29
  }
42
30
  async close() {
43
31
  const promises = [];
44
- for (const r of this.api.resources.values()) {
45
- const onShutdown = r.onShutdown;
32
+ for (const r of this._controllers.values()) {
33
+ const onShutdown = r?.onShutdown;
46
34
  if (onShutdown)
47
35
  promises.push((async () => onShutdown.call(r.controller, r))());
48
36
  }
49
37
  await Promise.allSettled(promises);
38
+ this._controllers.clear();
50
39
  }
51
40
  /**
52
41
  * Initializes the adapter
53
42
  */
54
- async init() {
55
- if (this._initialized)
56
- return;
43
+ async init(api, options) {
44
+ if (this._api)
45
+ throw new Error(`Already initialized`);
46
+ this._api = api;
47
+ this._interceptors = [...(options?.interceptors || [])];
48
+ this._logger = options?.logger && options.logger instanceof Logger
49
+ ? options.logger
50
+ : new Logger({ instance: options?.logger });
51
+ // Assign events
52
+ if (options?.on) {
53
+ for (const [event, fn] of Object.entries(options.on)) {
54
+ /* istanbul ignore next */
55
+ if (typeof fn === 'function')
56
+ this.on(event, fn);
57
+ }
58
+ }
59
+ // Make a safe service name
60
+ this._serviceName = pascalCase((api.info.title || '').replace(/[^a-z0-9_ ]/ig, '')) || 'OpraService';
61
+ if (!/^[a-z]/i.test(this._serviceName))
62
+ this._serviceName = 'X' + this._serviceName;
57
63
  // Init I18n
58
- if (this._options?.i18n instanceof I18n)
59
- this._i18n = this._options.i18n;
60
- else if (typeof this._options?.i18n === 'function')
61
- this._i18n = await this._options.i18n();
64
+ if (options?.i18n instanceof I18n)
65
+ this._i18n = options.i18n;
66
+ else if (typeof options?.i18n === 'function')
67
+ this._i18n = await options.i18n();
62
68
  else
63
- this._i18n = await this._createI18n(this._options?.i18n);
69
+ this._i18n = await this._createI18n(options?.i18n);
64
70
  this._i18n = this._i18n || I18n.defaultInstance;
65
71
  if (!this._i18n.isInitialized)
66
72
  await this._i18n.init();
67
73
  // Initialize all resources
68
- for (const resource of this.api.resources.values()) {
69
- await this.getController(resource);
70
- }
71
- this._initialized = true;
74
+ await this.getController(this.api.root);
72
75
  }
73
76
  async getController(resource) {
74
77
  resource = typeof resource === 'object' && resource instanceof Resource
75
78
  ? resource : this.api.getResource(resource);
76
79
  let controller = this._controllers.get(resource);
77
80
  if (!controller) {
78
- if (resource.controller) {
79
- controller = typeof resource.controller === 'function' ?
80
- new resource.controller()
81
- : resource.controller;
82
- // Initialize controller
83
- if (typeof controller.onInit === 'function')
84
- await controller.onInit.call(controller);
85
- this._controllers.set(resource, controller);
81
+ controller = resource.controller;
82
+ if (!controller && resource.ctor) {
83
+ controller = new resource.ctor();
84
+ }
85
+ this._controllers.set(resource, controller);
86
+ }
87
+ if (controller && !controller[resourceInitialized]) {
88
+ controller[resourceInitialized] = true;
89
+ // Initialize controller
90
+ if (typeof controller.onInit === 'function')
91
+ await controller.onInit.call(controller);
92
+ // Initialize sub resources of Container
93
+ if (resource instanceof Container) {
94
+ for (const r of resource.resources.values()) {
95
+ await this.getController(r);
96
+ }
86
97
  }
87
98
  }
88
99
  return controller;
89
100
  }
101
+ async getOperationHandler(resource, operationOrAction) {
102
+ resource = typeof resource === 'object' && resource instanceof Resource
103
+ ? resource
104
+ : this.api.getResource(resource);
105
+ const controller = await this.getController(resource);
106
+ const endpoint = (resource instanceof CrudResource && resource.operations.get(operationOrAction)) ||
107
+ resource.actions.get(operationOrAction);
108
+ if (endpoint) {
109
+ const handler = typeof controller[operationOrAction] === 'function' ? controller[operationOrAction] : undefined;
110
+ if (handler)
111
+ return { controller, endpoint, handler };
112
+ }
113
+ throw new ForbiddenError({
114
+ message: translate('OPERATION_FORBIDDEN', { resource: resource.name, endpoint: operationOrAction }, `'{{resource}}' does not accept '{{endpoint}}' operations`),
115
+ severity: 'error',
116
+ code: 'OPERATION_FORBIDDEN'
117
+ });
118
+ }
90
119
  async _createI18n(options) {
91
120
  const opts = {
92
121
  ...options,
@@ -1,5 +1,5 @@
1
- export var EndpointContext;
2
- (function (EndpointContext) {
1
+ export var RequestContext;
2
+ (function (RequestContext) {
3
3
  function from(executionContext, request, response) {
4
4
  const out = {
5
5
  request,
@@ -9,5 +9,5 @@ export var EndpointContext;
9
9
  Object.setPrototypeOf(out, executionContext);
10
10
  return out;
11
11
  }
12
- EndpointContext.from = from;
13
- })(EndpointContext || (EndpointContext = {}));
12
+ RequestContext.from = from;
13
+ })(RequestContext || (RequestContext = {}));
@@ -3,6 +3,9 @@ export class RequestHost {
3
3
  Object.assign(this, init);
4
4
  this.params = this.params || {};
5
5
  }
6
+ get resource() {
7
+ return this.endpoint.resource;
8
+ }
6
9
  switchToHttp() {
7
10
  if (this.http)
8
11
  return this.http;
@@ -8,12 +8,11 @@
8
8
  "NOT_FOUND": "Not found",
9
9
  "UNAUTHORIZED": "You have not been authenticated to perform this action",
10
10
  "UNPROCESSABLE_ENTITY": "Unprocessable entity",
11
- "SOURCE_NOT_FOUND": "Source not found",
12
11
  "REQUEST_VALIDATION": "Request validation failed",
13
12
  "RESPONSE_VALIDATION": "Response validation failed",
14
13
  "RESOURCE_NOT_FOUND": "Resource not found",
15
14
  "RESOURCE_CONFLICT": "There is already an other {{resource}} resource with same field values ({{fields}})",
16
- "RESOLVER_FORBIDDEN": "The {{resource}} endpoint does not accept '{{endpoint}}' operations",
15
+ "OPERATION_FORBIDDEN": "The {{resource}} endpoint does not accept '{{endpoint}}' operations",
17
16
  "UNKNOWN_FIELD": "Unknown field '{{field}}'",
18
17
  "UNACCEPTED_SORT_FIELD": "Field '{{field}}' is not available for sort operation",
19
18
  "UNACCEPTED_FILTER_FIELD": "Field '{{field}}' is not available for filter operation",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/core",
3
- "version": "0.25.5",
3
+ "version": "0.26.0",
4
4
  "description": "Opra schema package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -27,7 +27,7 @@
27
27
  "clean:cover": "rimraf ../../coverage/core"
28
28
  },
29
29
  "dependencies": {
30
- "@opra/common": "^0.25.5",
30
+ "@opra/common": "^0.26.0",
31
31
  "accepts": "^1.3.8",
32
32
  "content-disposition": "^0.5.4",
33
33
  "content-type": "^1.0.5",
@@ -65,7 +65,7 @@
65
65
  "@types/vary": "^1.1.0",
66
66
  "crypto-browserify": "^3.12.0",
67
67
  "path-browserify": "^1.0.1",
68
- "ts-gems": "^2.4.1"
68
+ "ts-gems": "^2.5.0"
69
69
  },
70
70
  "type": "module",
71
71
  "module": "./esm/index.js",
@@ -1,16 +1,19 @@
1
1
  import type { PartialOutput } from '@opra/common';
2
- import type { EndpointContext } from '../endpoint-context.js';
3
2
  import type { Request as _Request } from '../request.js';
3
+ import type { RequestContext } from '../request-context';
4
4
  declare module "@opra/common" {
5
5
  namespace Collection {
6
6
  namespace Action {
7
+ interface Request extends _Request {
8
+ operation: 'action';
9
+ action: string;
10
+ }
7
11
  interface Context<TSession extends {} = {}> extends Resource.Context<TSession> {
8
- key?: any;
9
12
  }
10
13
  }
11
14
  namespace Create {
12
15
  interface Request extends _Request {
13
- endpoint: 'create';
16
+ operation: 'create';
14
17
  data: any;
15
18
  params: {
16
19
  pick?: string[];
@@ -19,34 +22,34 @@ declare module "@opra/common" {
19
22
  [key: string]: any;
20
23
  };
21
24
  }
22
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
25
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
23
26
  request: Request;
24
27
  }
25
28
  }
26
29
  namespace Delete {
27
30
  interface Request extends _Request {
28
- endpoint: 'delete';
31
+ operation: 'delete';
29
32
  key: any;
30
33
  }
31
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
34
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
32
35
  request: Request;
33
36
  }
34
37
  }
35
38
  namespace DeleteMany {
36
39
  interface Request extends _Request {
37
- endpoint: 'deleteMany';
40
+ operation: 'deleteMany';
38
41
  params: {
39
42
  filter?: any;
40
43
  [key: string]: any;
41
44
  };
42
45
  }
43
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
46
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
44
47
  request: Request;
45
48
  }
46
49
  }
47
50
  namespace FindMany {
48
51
  interface Request extends _Request {
49
- endpoint: 'findMany';
52
+ operation: 'findMany';
50
53
  params: {
51
54
  filter?: any;
52
55
  pick?: string[];
@@ -60,13 +63,13 @@ declare module "@opra/common" {
60
63
  [key: string]: any;
61
64
  };
62
65
  }
63
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
66
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
64
67
  request: Request;
65
68
  }
66
69
  }
67
70
  namespace Get {
68
71
  interface Request extends _Request {
69
- endpoint: 'get';
72
+ operation: 'get';
70
73
  key: any;
71
74
  params: {
72
75
  pick?: string[];
@@ -75,13 +78,13 @@ declare module "@opra/common" {
75
78
  [key: string]: any;
76
79
  };
77
80
  }
78
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
81
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
79
82
  request: Request;
80
83
  }
81
84
  }
82
85
  namespace Update {
83
86
  interface Request extends _Request {
84
- endpoint: 'update';
87
+ operation: 'update';
85
88
  key: any;
86
89
  data: any;
87
90
  params: {
@@ -91,20 +94,20 @@ declare module "@opra/common" {
91
94
  [key: string]: any;
92
95
  };
93
96
  }
94
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
97
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
95
98
  request: Request;
96
99
  }
97
100
  }
98
101
  namespace UpdateMany {
99
102
  interface Request extends _Request {
100
- endpoint: 'updateMany';
103
+ operation: 'updateMany';
101
104
  data: any;
102
105
  params: {
103
106
  filter?: any;
104
107
  [key: string]: any;
105
108
  };
106
109
  }
107
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
110
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
108
111
  request: Request;
109
112
  }
110
113
  }
@@ -0,0 +1,13 @@
1
+ import type { Request as _Request } from '../request.js';
2
+ declare module "@opra/common" {
3
+ namespace Container {
4
+ namespace Action {
5
+ interface Request extends _Request {
6
+ operation: 'action';
7
+ action: string;
8
+ }
9
+ interface Context<TSession extends {} = {}> extends Resource.Context<TSession> {
10
+ }
11
+ }
12
+ }
13
+ }
@@ -1,4 +1,4 @@
1
- import { EndpointContext } from '../endpoint-context.js';
1
+ import { RequestContext } from '../request-context.js';
2
2
  declare module "@opra/common" {
3
3
  interface CollectionConstructor {
4
4
  OnInit(): any;
@@ -9,7 +9,7 @@ declare module "@opra/common" {
9
9
  OnShutdown(): any;
10
10
  }
11
11
  namespace Resource {
12
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
12
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
13
13
  params: Record<string, any>;
14
14
  }
15
15
  }
@@ -1,15 +1,19 @@
1
1
  import type { PartialOutput } from '@opra/common';
2
- import type { EndpointContext } from '../endpoint-context.js';
3
2
  import type { Request as _Request } from '../request.js';
3
+ import type { RequestContext } from '../request-context.js';
4
4
  declare module "@opra/common" {
5
5
  namespace Singleton {
6
6
  namespace Action {
7
+ interface Request extends _Request {
8
+ operation: 'action';
9
+ action: string;
10
+ }
7
11
  interface Context<TSession extends {} = {}> extends Resource.Context<TSession> {
8
12
  }
9
13
  }
10
14
  namespace Create {
11
15
  interface Request extends _Request {
12
- endpoint: 'create';
16
+ operation: 'create';
13
17
  data: any;
14
18
  params: {
15
19
  pick?: string[];
@@ -17,34 +21,34 @@ declare module "@opra/common" {
17
21
  include?: string[];
18
22
  };
19
23
  }
20
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
24
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
21
25
  request: Request;
22
26
  }
23
27
  }
24
28
  namespace Delete {
25
29
  interface Request extends _Request {
26
- endpoint: 'delete';
30
+ operation: 'delete';
27
31
  }
28
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
32
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
29
33
  request: Request;
30
34
  }
31
35
  }
32
36
  namespace Get {
33
37
  interface Request extends _Request {
34
- endpoint: 'get';
38
+ operation: 'get';
35
39
  params: {
36
40
  pick?: string[];
37
41
  omit?: string[];
38
42
  include?: string[];
39
43
  };
40
44
  }
41
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
45
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
42
46
  request: Request;
43
47
  }
44
48
  }
45
49
  namespace Update {
46
50
  interface Request extends _Request {
47
- endpoint: 'update';
51
+ operation: 'update';
48
52
  data: any;
49
53
  params: {
50
54
  pick?: string[];
@@ -52,7 +56,7 @@ declare module "@opra/common" {
52
56
  include?: string[];
53
57
  };
54
58
  }
55
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
59
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
56
60
  request: Request;
57
61
  }
58
62
  }
@@ -1,46 +1,43 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import type { Readable } from 'stream';
4
- import type { EndpointContext } from '../endpoint-context.js';
5
1
  import type { MultipartIterator } from '../http/helpers/multipart-helper';
6
2
  import type { Request as _Request } from '../request.js';
3
+ import type { RequestContext } from '../request-context.js';
7
4
  declare module "@opra/common" {
8
5
  namespace Storage {
9
6
  namespace Action {
7
+ interface Request extends _Request {
8
+ operation: 'action';
9
+ action: string;
10
+ }
10
11
  interface Context<TSession extends {} = {}> extends Resource.Context<TSession> {
11
12
  }
12
13
  }
13
14
  namespace Delete {
14
15
  interface Request extends _Request {
16
+ operation: 'delete';
15
17
  path?: string;
16
18
  }
17
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
19
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
18
20
  request: Request;
19
21
  }
20
22
  }
21
23
  namespace Get {
22
24
  interface Request extends _Request {
25
+ operation: 'get';
23
26
  path?: string;
24
27
  }
25
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
28
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
26
29
  request: Request;
27
30
  }
28
31
  }
29
32
  namespace Post {
30
33
  interface Request extends _Request {
34
+ operation: 'post';
31
35
  path?: string;
32
36
  parts: MultipartIterator;
33
37
  }
34
- interface Context<TSession extends {} = {}> extends EndpointContext<TSession> {
38
+ interface Context<TSession extends {} = {}> extends RequestContext<TSession> {
35
39
  request: Request;
36
40
  }
37
41
  }
38
42
  }
39
- interface IStorage {
40
- delete?(context: Storage.Delete.Context): Promise<number | undefined>;
41
- get?(context: Storage.Get.Context): Promise<Buffer | Readable | undefined>;
42
- post?(context: Storage.Post.Context): Promise<void>;
43
- onInit?(): Promise<void>;
44
- onShutdown?(): Promise<void>;
45
- }
46
43
  }
@@ -1,7 +1,7 @@
1
1
  import type { Application } from 'express';
2
2
  import type { ApiDocument } from '@opra/common';
3
- import type { PlatformAdapter } from '../platform-adapter.js';
4
- import type { HttpAdapter } from './http-adapter.js';
3
+ import type { PlatformAdapter } from '../../platform-adapter';
4
+ import type { NodeHttpAdapter } from './node-http-adapter';
5
5
  export interface ExpressAdapter extends PlatformAdapter {
6
6
  readonly app: Application;
7
7
  }
@@ -9,7 +9,7 @@ export interface ExpressAdapter extends PlatformAdapter {
9
9
  * @namespace
10
10
  */
11
11
  export declare namespace ExpressAdapter {
12
- interface Options extends HttpAdapter.Options {
12
+ interface Options extends NodeHttpAdapter.Options {
13
13
  }
14
14
  function create(app: Application, api: ApiDocument, options?: ExpressAdapter.Options): Promise<ExpressAdapter>;
15
15
  }
@@ -0,0 +1,12 @@
1
+ import type { Application } from 'express';
2
+ import { ApiDocument } from '@opra/common';
3
+ import { HttpAdapterHost } from '../http-adapter-host.js';
4
+ import type { ExpressAdapter } from './express-adapter.js';
5
+ export declare class ExpressAdapterHost extends HttpAdapterHost implements ExpressAdapter {
6
+ protected _platform: string;
7
+ protected _app: Application;
8
+ constructor(app: Application);
9
+ get app(): Application;
10
+ protected init(api: ApiDocument, options?: ExpressAdapter.Options): Promise<void>;
11
+ static create(app: Application, api: ApiDocument, options?: ExpressAdapter.Options): Promise<ExpressAdapter>;
12
+ }
@@ -1,17 +1,17 @@
1
1
  /// <reference types="node" />
2
2
  import http from 'http';
3
3
  import type { ApiDocument, OpraURLPath } from '@opra/common';
4
- import type { PlatformAdapter } from '../platform-adapter.js';
5
- export interface HttpAdapter extends PlatformAdapter {
4
+ import type { PlatformAdapter } from '../../platform-adapter';
5
+ export interface NodeHttpAdapter extends PlatformAdapter {
6
6
  readonly basePath: OpraURLPath;
7
7
  readonly server: http.Server;
8
8
  }
9
9
  /**
10
- * @namespace HttpAdapter
10
+ * @namespace NodeHttpAdapter
11
11
  */
12
- export declare namespace HttpAdapter {
12
+ export declare namespace NodeHttpAdapter {
13
13
  type Options = PlatformAdapter.Options & {
14
14
  basePath?: string;
15
15
  };
16
- function create(api: ApiDocument, options?: Options): Promise<HttpAdapter>;
16
+ function create(api: ApiDocument, options?: Options): Promise<NodeHttpAdapter>;
17
17
  }
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" />
2
+ import http from 'http';
3
+ import { ApiDocument, OpraURLPath } from '@opra/common';
4
+ import { HttpAdapterHost } from '../http-adapter-host.js';
5
+ import type { NodeHttpAdapter } from './node-http-adapter';
6
+ /**
7
+ * @class NodeHttpAdapterHost
8
+ */
9
+ export declare class NodeHttpAdapterHost extends HttpAdapterHost implements NodeHttpAdapter {
10
+ protected _platform: string;
11
+ protected _basePath: OpraURLPath;
12
+ protected _server: http.Server;
13
+ get basePath(): OpraURLPath;
14
+ get server(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
15
+ close(): Promise<void>;
16
+ protected init(api: ApiDocument, options?: NodeHttpAdapter.Options): Promise<void>;
17
+ protected _serverListener(incomingMessage: http.IncomingMessage, serverResponse: http.ServerResponse): void;
18
+ static create(api: ApiDocument, options?: NodeHttpAdapter.Options): Promise<NodeHttpAdapter>;
19
+ }
@@ -0,0 +1,5 @@
1
+ import bodyParser from 'body-parser';
2
+ import type { HttpServerRequest } from '../http-server-request.js';
3
+ type BodyLoaderFunction = (incoming: HttpServerRequest) => Promise<any>;
4
+ export declare function jsonBodyLoader(options?: bodyParser.OptionsJson, cachePoint?: Object): BodyLoaderFunction;
5
+ export {};
@@ -0,0 +1,34 @@
1
+ import { Collection, OpraURLPath, OpraURLPathComponent, Parameter, Resource, Singleton, Storage } from '@opra/common';
2
+ import { ExecutionContext } from '../execution-context.js';
3
+ import { PlatformAdapterHost } from '../platform-adapter.host.js';
4
+ import type { Protocol } from '../platform-adapter.js';
5
+ import { RequestHost } from '../request.host.js';
6
+ import { Request } from '../request.js';
7
+ import { RequestContext } from '../request-context.js';
8
+ import { HttpServerRequest } from './http-server-request.js';
9
+ import { HttpServerResponse } from './http-server-response.js';
10
+ /**
11
+ *
12
+ * @class HttpAdapterHost
13
+ */
14
+ export declare abstract class HttpAdapterHost extends PlatformAdapterHost {
15
+ protected _protocol: Protocol;
16
+ protected _tempDir: string;
17
+ /**
18
+ * Main http request handler
19
+ * @param incoming
20
+ * @param outgoing
21
+ * @protected
22
+ */
23
+ protected handleHttp(incoming: HttpServerRequest, outgoing: HttpServerResponse): Promise<void>;
24
+ handleExecution(executionContext: ExecutionContext): Promise<void>;
25
+ parseRequest(executionContext: ExecutionContext): Promise<Request>;
26
+ protected _parseRequestAction(executionContext: ExecutionContext, resource: Resource, urlPath: OpraURLPath, searchParams: URLSearchParams): Promise<RequestHost>;
27
+ protected _parseRequestCollection(executionContext: ExecutionContext, resource: Collection, urlPath: OpraURLPath, searchParams: URLSearchParams): Promise<RequestHost>;
28
+ protected _parseRequestSingleton(executionContext: ExecutionContext, resource: Singleton, urlPath: OpraURLPath, searchParams?: URLSearchParams): Promise<RequestHost>;
29
+ protected _parseRequestStorage(executionContext: ExecutionContext, resource: Storage, urlPath: OpraURLPath, searchParams: URLSearchParams): Promise<RequestHost>;
30
+ protected parseParameters(paramDefs: Map<string, Parameter>, pathComponent: OpraURLPathComponent, searchParams?: URLSearchParams): Record<string, any>;
31
+ protected executeRequest(context: RequestContext): Promise<void>;
32
+ sendResponse(context: RequestContext): Promise<void>;
33
+ protected handleError(context: ExecutionContext): Promise<void>;
34
+ }