@opra/core 0.32.2 → 0.32.4

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.
@@ -1,2 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExecutionContext = void 0;
4
+ const execution_context_host_js_1 = require("./execution-context.host.js");
5
+ var ExecutionContext;
6
+ (function (ExecutionContext) {
7
+ function is(v) {
8
+ return v instanceof execution_context_host_js_1.ExecutionContextHost ||
9
+ (typeof v.protocol === 'string' &&
10
+ typeof v.platform === 'string' &&
11
+ typeof v.switchToHttp === 'function' &&
12
+ typeof v.switchToWs === 'function' &&
13
+ typeof v.switchToRpc === 'function');
14
+ }
15
+ ExecutionContext.is = is;
16
+ })(ExecutionContext || (exports.ExecutionContext = ExecutionContext = {}));
package/cjs/index.js CHANGED
@@ -17,7 +17,6 @@ tslib_1.__exportStar(require("./request.host.js"), exports);
17
17
  tslib_1.__exportStar(require("./request-context.js"), exports);
18
18
  tslib_1.__exportStar(require("./response.js"), exports);
19
19
  tslib_1.__exportStar(require("./response.host.js"), exports);
20
- tslib_1.__exportStar(require("./types.js"), exports);
21
20
  tslib_1.__exportStar(require("./http/adapters/express-adapter.js"), exports);
22
21
  tslib_1.__exportStar(require("./http/adapters/express-adapter.host.js"), exports);
23
22
  tslib_1.__exportStar(require("./http/adapters/node-http-adapter.js"), exports);
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RequestContext = void 0;
4
+ const common_1 = require("@opra/common");
5
+ const execution_context_js_1 = require("./execution-context.js");
4
6
  var RequestContext;
5
7
  (function (RequestContext) {
6
8
  function from(executionContext, api, request, response) {
@@ -13,4 +15,11 @@ var RequestContext;
13
15
  return out;
14
16
  }
15
17
  RequestContext.from = from;
18
+ function is(v) {
19
+ return execution_context_js_1.ExecutionContext.is(v) &&
20
+ v.api instanceof common_1.ApiDocument &&
21
+ v.request &&
22
+ v.response;
23
+ }
24
+ RequestContext.is = is;
16
25
  })(RequestContext || (exports.RequestContext = RequestContext = {}));
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiService = void 0;
4
+ const request_context_js_1 = require("../request-context.js");
4
5
  const cachedServices = Symbol('cachedServices');
5
6
  class ApiService {
6
7
  get context() {
@@ -8,32 +9,36 @@ class ApiService {
8
9
  throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
9
10
  return this._context;
10
11
  }
11
- forContext(arg0, attributes) {
12
- let context;
13
- if (arg0 instanceof ApiService) {
12
+ for(arg0, attributes) {
13
+ let context = this._context;
14
+ if (request_context_js_1.RequestContext.is(arg0))
15
+ context = arg0;
16
+ else if (arg0 instanceof ApiService) {
14
17
  context = arg0.context;
15
- attributes = arg0;
16
18
  }
17
19
  else
18
- context = arg0;
20
+ attributes = attributes || arg0;
21
+ // Create cache map and instance array
19
22
  const cacheMap = context[cachedServices] = context[cachedServices] || new WeakMap();
20
23
  const ctor = Object.getPrototypeOf(this).constructor;
21
24
  const cachedInstances = cacheMap.get(ctor) || [];
22
25
  cacheMap.set(ctor, cachedInstances);
23
- const newInstance = !this._context || this._context !== context;
24
- if (!newInstance) {
25
- const instance = cachedInstances
26
- .find(service => this._cacheMatch(service, context, attributes));
27
- if (instance)
28
- return instance;
29
- }
30
- const instance = { context };
26
+ // Find cached instance that matches given arguments
27
+ let instance = cachedInstances.find((service) => this._instanceCompare(service, context, attributes));
28
+ if (instance)
29
+ return instance;
30
+ // Create new instance
31
+ instance = { context };
32
+ if (attributes)
33
+ Object.assign(instance, attributes);
31
34
  Object.setPrototypeOf(instance, this);
32
35
  cachedInstances.push(instance);
33
36
  return instance;
34
37
  }
35
38
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
36
- _cacheMatch(service, context, attributes) {
39
+ _instanceCompare(service, context, attributes) {
40
+ if (attributes && Object.keys(attributes).find(k => attributes[k] !== service[k]))
41
+ return false;
37
42
  return service.context === context;
38
43
  }
39
44
  }
@@ -1 +1,13 @@
1
- export {};
1
+ import { ExecutionContextHost } from './execution-context.host.js';
2
+ export var ExecutionContext;
3
+ (function (ExecutionContext) {
4
+ function is(v) {
5
+ return v instanceof ExecutionContextHost ||
6
+ (typeof v.protocol === 'string' &&
7
+ typeof v.platform === 'string' &&
8
+ typeof v.switchToHttp === 'function' &&
9
+ typeof v.switchToWs === 'function' &&
10
+ typeof v.switchToRpc === 'function');
11
+ }
12
+ ExecutionContext.is = is;
13
+ })(ExecutionContext || (ExecutionContext = {}));
package/esm/index.js CHANGED
@@ -14,7 +14,6 @@ export * from './request.host.js';
14
14
  export * from './request-context.js';
15
15
  export * from './response.js';
16
16
  export * from './response.host.js';
17
- export * from './types.js';
18
17
  export * from './http/adapters/express-adapter.js';
19
18
  export * from './http/adapters/express-adapter.host.js';
20
19
  export * from './http/adapters/node-http-adapter.js';
@@ -1,3 +1,5 @@
1
+ import { ApiDocument } from '@opra/common';
2
+ import { ExecutionContext } from './execution-context.js';
1
3
  export var RequestContext;
2
4
  (function (RequestContext) {
3
5
  function from(executionContext, api, request, response) {
@@ -10,4 +12,11 @@ export var RequestContext;
10
12
  return out;
11
13
  }
12
14
  RequestContext.from = from;
15
+ function is(v) {
16
+ return ExecutionContext.is(v) &&
17
+ v.api instanceof ApiDocument &&
18
+ v.request &&
19
+ v.response;
20
+ }
21
+ RequestContext.is = is;
13
22
  })(RequestContext || (RequestContext = {}));
@@ -1,3 +1,4 @@
1
+ import { RequestContext } from '../request-context.js';
1
2
  const cachedServices = Symbol('cachedServices');
2
3
  export class ApiService {
3
4
  get context() {
@@ -5,32 +6,36 @@ export class ApiService {
5
6
  throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
6
7
  return this._context;
7
8
  }
8
- forContext(arg0, attributes) {
9
- let context;
10
- if (arg0 instanceof ApiService) {
9
+ for(arg0, attributes) {
10
+ let context = this._context;
11
+ if (RequestContext.is(arg0))
12
+ context = arg0;
13
+ else if (arg0 instanceof ApiService) {
11
14
  context = arg0.context;
12
- attributes = arg0;
13
15
  }
14
16
  else
15
- context = arg0;
17
+ attributes = attributes || arg0;
18
+ // Create cache map and instance array
16
19
  const cacheMap = context[cachedServices] = context[cachedServices] || new WeakMap();
17
20
  const ctor = Object.getPrototypeOf(this).constructor;
18
21
  const cachedInstances = cacheMap.get(ctor) || [];
19
22
  cacheMap.set(ctor, cachedInstances);
20
- const newInstance = !this._context || this._context !== context;
21
- if (!newInstance) {
22
- const instance = cachedInstances
23
- .find(service => this._cacheMatch(service, context, attributes));
24
- if (instance)
25
- return instance;
26
- }
27
- const instance = { context };
23
+ // Find cached instance that matches given arguments
24
+ let instance = cachedInstances.find((service) => this._instanceCompare(service, context, attributes));
25
+ if (instance)
26
+ return instance;
27
+ // Create new instance
28
+ instance = { context };
29
+ if (attributes)
30
+ Object.assign(instance, attributes);
28
31
  Object.setPrototypeOf(instance, this);
29
32
  cachedInstances.push(instance);
30
33
  return instance;
31
34
  }
32
35
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
33
- _cacheMatch(service, context, attributes) {
36
+ _instanceCompare(service, context, attributes) {
37
+ if (attributes && Object.keys(attributes).find(k => attributes[k] !== service[k]))
38
+ return false;
34
39
  return service.context === context;
35
40
  }
36
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/core",
3
- "version": "0.32.2",
3
+ "version": "0.32.4",
4
4
  "description": "Opra schema package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@browsery/type-is": "^0.6.3",
32
- "@opra/common": "^0.32.2",
32
+ "@opra/common": "^0.32.4",
33
33
  "accepts": "^1.3.8",
34
34
  "content-disposition": "^0.5.4",
35
35
  "content-type": "^1.0.5",
@@ -66,7 +66,7 @@
66
66
  "@types/vary": "^1.1.3",
67
67
  "crypto-browserify": "^3.12.0",
68
68
  "path-browserify": "^1.0.1",
69
- "ts-gems": "^2.7.1"
69
+ "ts-gems": "^2.8.0"
70
70
  },
71
71
  "type": "module",
72
72
  "module": "./esm/index.js",
@@ -1,5 +1,5 @@
1
1
  import { StrictOmit } from 'ts-gems';
2
- import type { Action as _Action, CrudOperation as _Operation, PartialOutput } from '@opra/common';
2
+ import type { Action as _Action, CrudOperation as _Operation, PartialDTO } from '@opra/common';
3
3
  import type { Request as _Request } from '../request.js';
4
4
  import type { RequestContext } from '../request-context';
5
5
  declare module "@opra/common" {
@@ -133,12 +133,12 @@ declare module "@opra/common" {
133
133
  }
134
134
  }
135
135
  interface ICollection<T> {
136
- create?(context: Collection.Create.Context): Promise<PartialOutput<T>>;
136
+ create?(context: Collection.Create.Context): Promise<PartialDTO<T>>;
137
137
  delete?(context: Collection.Delete.Context): Promise<number> | undefined;
138
138
  deleteMany?(context: Collection.DeleteMany.Context): Promise<number> | undefined;
139
- findMany?(context: Collection.FindMany.Context): Promise<PartialOutput<T>[] | undefined>;
140
- get?(context: Collection.Get.Context): Promise<PartialOutput<T> | undefined>;
141
- update?(context: Collection.Update.Context): Promise<PartialOutput<T> | undefined>;
139
+ findMany?(context: Collection.FindMany.Context): Promise<PartialDTO<T>[] | undefined>;
140
+ get?(context: Collection.Get.Context): Promise<PartialDTO<T> | undefined>;
141
+ update?(context: Collection.Update.Context): Promise<PartialDTO<T> | undefined>;
142
142
  updateMany?(context: Collection.UpdateMany.Context): Promise<number> | undefined;
143
143
  onInit?(): Promise<void>;
144
144
  onShutdown?(): Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import { StrictOmit } from 'ts-gems';
2
- import type { Action as _Action, CrudOperation as _Operation, PartialOutput } from '@opra/common';
2
+ import type { Action as _Action, CrudOperation as _Operation, PartialDTO } from '@opra/common';
3
3
  import type { Request as _Request } from '../request.js';
4
4
  import type { RequestContext } from '../request-context.js';
5
5
  declare module "@opra/common" {
@@ -73,10 +73,10 @@ declare module "@opra/common" {
73
73
  }
74
74
  }
75
75
  interface ISingleton<T> {
76
- create?(context: Singleton.Create.Context): Promise<PartialOutput<T>>;
76
+ create?(context: Singleton.Create.Context): Promise<PartialDTO<T>>;
77
77
  delete?(context: Singleton.Delete.Context): Promise<number> | undefined;
78
- get?(context: Singleton.Get.Context): Promise<PartialOutput<T> | undefined>;
79
- update?(context: Singleton.Update.Context): Promise<PartialOutput<T> | undefined>;
78
+ get?(context: Singleton.Get.Context): Promise<PartialDTO<T> | undefined>;
79
+ update?(context: Singleton.Update.Context): Promise<PartialDTO<T> | undefined>;
80
80
  onInit?(): Promise<void>;
81
81
  onShutdown?(): Promise<void>;
82
82
  }
@@ -14,6 +14,7 @@ export declare namespace ExecutionContext {
14
14
  context: ExecutionContext;
15
15
  failed: boolean;
16
16
  };
17
+ function is(v: any): boolean;
17
18
  }
18
19
  export interface HttpMessageContext {
19
20
  readonly platform: string;
package/types/index.d.ts CHANGED
@@ -14,7 +14,6 @@ export * from './request.host.js';
14
14
  export * from './request-context.js';
15
15
  export * from './response.js';
16
16
  export * from './response.host.js';
17
- export * from './types.js';
18
17
  export * from './http/adapters/express-adapter.js';
19
18
  export * from './http/adapters/express-adapter.host.js';
20
19
  export * from './http/adapters/node-http-adapter.js';
@@ -1,5 +1,5 @@
1
1
  import { ApiDocument } from '@opra/common';
2
- import type { ExecutionContext } from './execution-context.js';
2
+ import { ExecutionContext } from './execution-context.js';
3
3
  import type { Request } from './request.js';
4
4
  import type { Response } from './response.js';
5
5
  export interface RequestContext extends ExecutionContext {
@@ -9,4 +9,5 @@ export interface RequestContext extends ExecutionContext {
9
9
  }
10
10
  export declare namespace RequestContext {
11
11
  function from(executionContext: ExecutionContext, api: ApiDocument, request: Request, response: Response): RequestContext;
12
+ function is(v: any): v is RequestContext;
12
13
  }
@@ -1,8 +1,14 @@
1
1
  import { RequestContext } from '../request-context.js';
2
+ export declare namespace ApiService {
3
+ interface ExtendOptions {
4
+ [key: string | symbol]: any;
5
+ }
6
+ }
2
7
  export declare abstract class ApiService {
3
8
  protected _context: RequestContext;
4
9
  get context(): RequestContext;
5
- forContext(source: ApiService): this;
6
- forContext(context: RequestContext, attributes?: any): this;
7
- protected _cacheMatch(service: ApiService, context: RequestContext, attributes?: any): boolean;
10
+ for(context: RequestContext | ApiService): this;
11
+ for<O extends (ApiService.ExtendOptions & Partial<this>)>(context: RequestContext | ApiService, attributes: O): this & O;
12
+ for<O extends ApiService.ExtendOptions & Partial<this>>(attributes: O): this & O;
13
+ protected _instanceCompare(service: ApiService, context: RequestContext, attributes?: Object): boolean;
8
14
  }
package/cjs/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/esm/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/types/types.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { PartialInput, PartialOutput } from '@opra/common';
2
- export { PartialInput, PartialOutput };