@toa.io/core 1.0.0-alpha.0 → 1.0.0-alpha.107
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 +7 -8
- package/src/assignment.js +11 -2
- package/src/call.js +4 -0
- package/src/cascade.js +2 -3
- package/src/component.js +15 -18
- package/src/composition.js +1 -1
- package/src/connector.js +3 -8
- package/src/contract/contract.js +23 -0
- package/src/contract/reply.js +26 -9
- package/src/contract/request.js +33 -14
- package/src/contract/schemas/index.js +2 -3
- package/src/contract/schemas/query.yaml +2 -4
- package/src/discovery.js +2 -5
- package/src/effect.js +19 -0
- package/src/entities/changeset.js +8 -15
- package/src/entities/entity.js +31 -12
- package/src/entities/factory.js +6 -0
- package/src/exceptions.js +21 -19
- package/src/exposition.js +3 -2
- package/src/index.js +2 -0
- package/src/locator.js +7 -2
- package/src/observation.js +1 -13
- package/src/operation.js +29 -7
- package/src/query/criteria.js +3 -3
- package/src/query/options.js +3 -0
- package/src/receiver.js +13 -10
- package/src/remote.js +5 -7
- package/src/state.js +63 -45
- package/src/transition.js +9 -7
- package/src/transmission.js +12 -3
- package/test/component.test.js +2 -1
- package/test/contract/conditions.test.js +5 -5
- package/test/contract/request.test.js +30 -28
- package/test/entities/entity.fixtures.js +5 -2
- package/test/entities/entity.test.js +7 -46
- package/types/bindings.d.ts +3 -1
- package/types/bridges.ts +30 -0
- package/types/component.d.ts +4 -1
- package/types/context.d.ts +12 -18
- package/types/extensions.d.ts +4 -3
- package/types/index.ts +15 -0
- package/types/locator.d.ts +2 -1
- package/types/operations.d.ts +6 -0
- package/types/remote.d.ts +18 -0
- package/types/request.d.ts +1 -0
- package/src/contract/conditions.js +0 -21
- package/types/bridges.d.ts +0 -41
- package/types/index.d.ts +0 -14
|
@@ -16,10 +16,13 @@ const state = () => ({
|
|
|
16
16
|
_created: generate(),
|
|
17
17
|
_updated: generate(),
|
|
18
18
|
_deleted: generate(),
|
|
19
|
-
_version:
|
|
19
|
+
_version: 0
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
const failed = () => ({
|
|
22
|
+
const failed = () => ({
|
|
23
|
+
...state(),
|
|
24
|
+
fail: true
|
|
25
|
+
})
|
|
23
26
|
|
|
24
27
|
exports.schema = schema
|
|
25
28
|
exports.state = state
|
|
@@ -7,37 +7,12 @@ beforeEach(() => {
|
|
|
7
7
|
jest.clearAllMocks()
|
|
8
8
|
})
|
|
9
9
|
|
|
10
|
-
describe('new', () => {
|
|
11
|
-
it('should throw on schema error', () => {
|
|
12
|
-
const entity = new Entity(fixtures.schema)
|
|
13
|
-
|
|
14
|
-
expect(() => entity.set(fixtures.failed())).toThrow()
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('should provide state', () => {
|
|
18
|
-
const entity = new Entity(fixtures.schema)
|
|
19
|
-
const state = fixtures.state()
|
|
20
|
-
|
|
21
|
-
entity.set(state)
|
|
22
|
-
|
|
23
|
-
expect(entity.get()).toEqual(state)
|
|
24
|
-
})
|
|
25
|
-
})
|
|
26
|
-
|
|
27
10
|
describe('argument', () => {
|
|
28
|
-
it('should provide initial state if no argument passed', () => {
|
|
29
|
-
const entity = new Entity(fixtures.schema)
|
|
30
|
-
const defaults = fixtures.schema.defaults.mock.results[0].value
|
|
31
|
-
const expected = { ...defaults, _version: 0 }
|
|
32
|
-
|
|
33
|
-
expect(entity.get()).toStrictEqual(expected)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
11
|
it('should set state', () => {
|
|
37
12
|
const state = fixtures.state()
|
|
38
13
|
const entity = new Entity(fixtures.schema, state)
|
|
39
14
|
|
|
40
|
-
expect(entity.get()).
|
|
15
|
+
expect(entity.get()).toEqual(state)
|
|
41
16
|
})
|
|
42
17
|
})
|
|
43
18
|
|
|
@@ -51,26 +26,12 @@ it('should provide event', () => {
|
|
|
51
26
|
|
|
52
27
|
const event = entity.event()
|
|
53
28
|
|
|
54
|
-
expect(event).
|
|
29
|
+
expect(event).toEqual(expect.objectContaining({
|
|
55
30
|
state,
|
|
56
31
|
origin,
|
|
57
|
-
changeset: {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const origin = fixtures.state()
|
|
63
|
-
const entity = new Entity(fixtures.schema, origin)
|
|
64
|
-
const state = entity.get()
|
|
65
|
-
|
|
66
|
-
expect(() => (state.id = 1)).toThrow('assign to read only property')
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
it('should seal id', async () => {
|
|
70
|
-
const origin = fixtures.state()
|
|
71
|
-
const entity = new Entity(fixtures.schema, origin)
|
|
72
|
-
const state = entity.get()
|
|
73
|
-
const redefine = () => Object.defineProperty(state, 'id', { writable: true })
|
|
74
|
-
|
|
75
|
-
expect(redefine).toThrow('redefine property')
|
|
32
|
+
changeset: expect.objectContaining({
|
|
33
|
+
foo: 'new value',
|
|
34
|
+
_version: 1
|
|
35
|
+
})
|
|
36
|
+
}))
|
|
76
37
|
})
|
package/types/bindings.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ declare namespace toa.core.bindings {
|
|
|
8
8
|
|
|
9
9
|
interface Consumer extends _core.Connector {
|
|
10
10
|
request (request: Request): Promise<_core.Reply>
|
|
11
|
+
|
|
12
|
+
task (request: Request): Promise<void>
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
interface Emitter extends _core.Connector {
|
|
@@ -37,4 +39,4 @@ declare namespace toa.core.bindings {
|
|
|
37
39
|
export type Emitter = toa.core.bindings.Emitter
|
|
38
40
|
export type Factory = toa.core.bindings.Factory
|
|
39
41
|
export type Properties = toa.core.bindings.Properties
|
|
40
|
-
export type Broadcast<L> = toa.core.bindings.Broadcast<L>
|
|
42
|
+
export type Broadcast<L = any> = toa.core.bindings.Broadcast<L>
|
package/types/bridges.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Request, Reply } from './request'
|
|
2
|
+
import type { Context } from './context'
|
|
3
|
+
|
|
4
|
+
export interface Algorithm {
|
|
5
|
+
mount: (context?: Context) => Promise<void>
|
|
6
|
+
|
|
7
|
+
execute: ((input: any, scope: object | object[]) => Promise<Reply>) |
|
|
8
|
+
((input: any) => Promise<Reply>) |
|
|
9
|
+
(() => Promise<Reply>)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface Event {
|
|
13
|
+
condition: (object: object) => Promise<boolean>
|
|
14
|
+
|
|
15
|
+
payload: (object: object) => Promise<object>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Receiver {
|
|
19
|
+
condition: (object: object) => Promise<boolean>
|
|
20
|
+
|
|
21
|
+
request: (object: object) => Promise<Request>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Factory {
|
|
25
|
+
algorithm?: (path: string, name: string, context: Context) => Algorithm
|
|
26
|
+
|
|
27
|
+
event?: (path: string, label: string) => Event
|
|
28
|
+
|
|
29
|
+
receiver?: (path: string, label: string) => Receiver
|
|
30
|
+
}
|
package/types/component.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Connector } from './connector'
|
|
2
2
|
import { Locator } from './locator'
|
|
3
3
|
import { Request } from './request'
|
|
4
|
+
import { Operation } from './operations'
|
|
4
5
|
|
|
5
|
-
export
|
|
6
|
+
export class Component extends Connector {
|
|
6
7
|
locator: Locator
|
|
7
8
|
|
|
9
|
+
constructor (locator: Locator, operations: Record<string, Operation>)
|
|
10
|
+
|
|
8
11
|
invoke<T = any> (endpoint: string, request: Request): Promise<T>
|
|
9
12
|
}
|
package/types/context.d.ts
CHANGED
|
@@ -3,25 +3,19 @@ import * as _reply from './reply'
|
|
|
3
3
|
import * as _extensions from './extensions'
|
|
4
4
|
import * as _connector from './connector'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
export interface Context extends _connector.Connector{
|
|
7
|
+
aspects: _extensions.Aspect[]
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Calls local endpoint
|
|
11
|
+
*/
|
|
12
|
+
apply (endpoint: string, request: _request.Request): Promise<_reply.Reply>
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Calls remote endpoint
|
|
18
|
-
*/
|
|
19
|
-
call(namespace: string, name: string, endpoint: string, request: _request.Request): Promise<_reply.Reply>
|
|
20
|
-
|
|
21
|
-
// shortcuts
|
|
22
|
-
[key: string]: any
|
|
23
|
-
}
|
|
14
|
+
/**
|
|
15
|
+
* Calls remote endpoint
|
|
16
|
+
*/
|
|
17
|
+
call (namespace: string, name: string, endpoint: string, request: _request.Request): Promise<_reply.Reply>
|
|
24
18
|
|
|
19
|
+
// shortcuts
|
|
20
|
+
[key: string]: any
|
|
25
21
|
}
|
|
26
|
-
|
|
27
|
-
export type Context = toa.core.Context
|
package/types/extensions.d.ts
CHANGED
|
@@ -3,9 +3,10 @@ import * as _component from './component'
|
|
|
3
3
|
import * as _context from './context'
|
|
4
4
|
import * as _storages from './storages'
|
|
5
5
|
import * as _bindings from './bindings'
|
|
6
|
+
import { Manifest } from '@toa.io/norm'
|
|
6
7
|
|
|
7
|
-
export interface Factory{
|
|
8
|
-
tenant? (locator: _core.Locator, manifest: object): _core.Connector
|
|
8
|
+
export interface Factory {
|
|
9
|
+
tenant? (locator: _core.Locator, manifest: object, component: Manifest): _core.Connector
|
|
9
10
|
|
|
10
11
|
aspect? (locator: _core.Locator, manifest: object | null): Aspect | Aspect[]
|
|
11
12
|
|
|
@@ -22,7 +23,7 @@ export interface Factory{
|
|
|
22
23
|
receiver? (receiver: _core.Receiver, locator: _core.Locator): _core.Receiver
|
|
23
24
|
}
|
|
24
25
|
|
|
25
|
-
export interface Aspect extends _core.Connector{
|
|
26
|
+
export interface Aspect extends _core.Connector {
|
|
26
27
|
name: string
|
|
27
28
|
|
|
28
29
|
invoke (...args: any[]): any
|
package/types/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * as bindings from './bindings'
|
|
2
|
+
export * as extensions from './extensions'
|
|
3
|
+
export * as storages from './storages'
|
|
4
|
+
export * as bridges from './bridges'
|
|
5
|
+
export * as operations from './operations'
|
|
6
|
+
|
|
7
|
+
export type { Component } from './component'
|
|
8
|
+
export type { Remote } from './remote'
|
|
9
|
+
export { Connector } from './connector'
|
|
10
|
+
export type { Context } from './context'
|
|
11
|
+
export type { Exception } from './exception'
|
|
12
|
+
export { Locator } from './locator'
|
|
13
|
+
export type { Receiver } from './receiver'
|
|
14
|
+
export type { Message } from './message'
|
|
15
|
+
export type { Request, Query, Reply } from './request'
|
package/types/locator.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export class Locator
|
|
1
|
+
export class Locator{
|
|
2
2
|
public readonly name: string
|
|
3
3
|
public readonly namespace: string
|
|
4
4
|
|
|
5
5
|
public readonly id: string
|
|
6
6
|
public readonly label: string
|
|
7
7
|
public readonly uppercase: string
|
|
8
|
+
public readonly lowercase: string
|
|
8
9
|
|
|
9
10
|
constructor (name: string, namespace?: string)
|
|
10
11
|
|
package/types/operations.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
import { Request } from './request'
|
|
2
|
+
|
|
1
3
|
export type type = 'transition' | 'observation' | 'assignment' | 'computation' | 'effect'
|
|
2
4
|
export type scope = 'object' | 'objects' | 'changeset'
|
|
5
|
+
|
|
6
|
+
export class Operation {
|
|
7
|
+
invoke<T = any> (request: Request): Promise<T>
|
|
8
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Component } from './component'
|
|
2
|
+
|
|
3
|
+
export class Remote extends Component {
|
|
4
|
+
explain (endpoint: string): Promise<Explanation>
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface Explanation {
|
|
8
|
+
input: Schema | null
|
|
9
|
+
output: Schema | null
|
|
10
|
+
errors?: string[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface Schema {
|
|
14
|
+
type: string
|
|
15
|
+
properties: {
|
|
16
|
+
[key: string]: Schema
|
|
17
|
+
}
|
|
18
|
+
}
|
package/types/request.d.ts
CHANGED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { SystemException } = require('../exceptions')
|
|
4
|
-
|
|
5
|
-
class Conditions {
|
|
6
|
-
#schema
|
|
7
|
-
|
|
8
|
-
constructor (schema) {
|
|
9
|
-
this.#schema = schema
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
fit (value) {
|
|
13
|
-
const error = this.#schema.fit(value)
|
|
14
|
-
|
|
15
|
-
if (error !== null) throw new this.constructor.Exception(error)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
static Exception = SystemException
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
exports.Conditions = Conditions
|
package/types/bridges.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import * as _core from '@toa.io/core/types'
|
|
2
|
-
import type * as reply from './reply'
|
|
3
|
-
import type * as context from './context'
|
|
4
|
-
import type * as connector from './connector'
|
|
5
|
-
|
|
6
|
-
declare namespace toa.core.bridges {
|
|
7
|
-
|
|
8
|
-
interface Algorithm extends connector.Connector {
|
|
9
|
-
mount(context?: context.Context): Promise<void>
|
|
10
|
-
|
|
11
|
-
execute(input: any, scope: object | object[]): Promise<reply.Reply>
|
|
12
|
-
|
|
13
|
-
execute(input: any): Promise<reply.Reply>
|
|
14
|
-
|
|
15
|
-
execute(): Promise<reply.Reply>
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface Event {
|
|
19
|
-
condition(object): Promise<boolean>
|
|
20
|
-
|
|
21
|
-
payload(object): Promise<object>
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface Receiver {
|
|
25
|
-
condition(object): Promise<boolean>
|
|
26
|
-
|
|
27
|
-
request(object): Promise<_core.Request>
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface Factory {
|
|
31
|
-
algorithm(path: string, name: string, context: context.Context): Algorithm
|
|
32
|
-
|
|
33
|
-
event(path: string, label: string): Event
|
|
34
|
-
|
|
35
|
-
receiver(path: string, label: string): Receiver
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export type Algorithm = toa.core.bridges.Algorithm
|
|
41
|
-
export type Factory = toa.core.bridges.Factory
|
package/types/index.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export * as bindings from './bindings'
|
|
2
|
-
export * as extensions from './extensions'
|
|
3
|
-
export * as storages from './storages'
|
|
4
|
-
export * as bridges from './bridges'
|
|
5
|
-
export * as operations from './operations'
|
|
6
|
-
|
|
7
|
-
export { Component } from './component'
|
|
8
|
-
export { Connector } from './connector'
|
|
9
|
-
export { Context } from './context'
|
|
10
|
-
export { Exception } from './exception'
|
|
11
|
-
export { Locator } from './locator'
|
|
12
|
-
export { Receiver } from './receiver'
|
|
13
|
-
export { Message } from './message'
|
|
14
|
-
export { Request, Query, Reply } from './request'
|