@orthograph/preset 0.0.0-stage → 0.1.0-next.1
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/LICENSE +202 -0
- package/README.md +2 -2
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.js +4 -0
- package/dist/modules/app/internal/folder.d.ts +2 -0
- package/dist/modules/app/internal/folder.js +9 -0
- package/dist/modules/app/internal/names.d.ts +6 -0
- package/dist/modules/app/internal/names.js +10 -0
- package/dist/modules/app/service.d.ts +2 -0
- package/dist/modules/app/service.js +38 -0
- package/dist/modules/app/types.d.ts +26 -0
- package/dist/modules/app/types.js +5 -0
- package/dist/modules/starter/internal/project.d.ts +57 -0
- package/dist/modules/starter/internal/project.js +96 -0
- package/dist/modules/starter/service.d.ts +2 -0
- package/dist/modules/starter/service.js +38 -0
- package/dist/modules/starter/types.d.ts +25 -0
- package/dist/modules/starter/types.js +1 -0
- package/files/blueprint/environments/local.ts +8 -0
- package/files/blueprint/environments/production.ts +10 -0
- package/files/blueprint/environments/staging.ts +9 -0
- package/files/blueprint/environments/test.ts +8 -0
- package/files/blueprint/refusals.lock +85 -0
- package/files/blueprint/starting-worlds/founded.ts +11 -0
- package/files/blueprint/starting-worlds/invited.ts +12 -0
- package/files/blueprint/things/api-key/thing.ts +8 -0
- package/files/blueprint/things/api-key/verbs/create.ts +15 -0
- package/files/blueprint/things/invitation/events.ts +7 -0
- package/files/blueprint/things/invitation/machine.ts +13 -0
- package/files/blueprint/things/invitation/roles.ts +5 -0
- package/files/blueprint/things/invitation/thing.ts +8 -0
- package/files/blueprint/things/invitation/verbs/accept.ts +16 -0
- package/files/blueprint/things/invitation/verbs/revoke.ts +14 -0
- package/files/blueprint/things/invitation/verbs/send.ts +14 -0
- package/files/blueprint/things/membership/roles.ts +5 -0
- package/files/blueprint/things/membership/thing.ts +9 -0
- package/files/blueprint/things/membership/verbs/get.ts +11 -0
- package/files/blueprint/things/organization/events.ts +4 -0
- package/files/blueprint/things/organization/machine.ts +9 -0
- package/files/blueprint/things/organization/roles.ts +5 -0
- package/files/blueprint/things/organization/thing.ts +7 -0
- package/files/blueprint/things/organization/verbs/create.ts +14 -0
- package/files/blueprint/things/organization/verbs/get.ts +10 -0
- package/files/blueprint/things/person/errors.ts +3 -0
- package/files/blueprint/things/person/thing.ts +6 -0
- package/files/blueprint/things/person/verbs/sign-up.ts +14 -0
- package/files/blueprint/things/session/errors.ts +3 -0
- package/files/blueprint/things/session/thing.ts +7 -0
- package/files/blueprint/things/session/verbs/show.ts +13 -0
- package/files/blueprint/things/session/verbs/sign-in.ts +15 -0
- package/files/blueprint/things/system/thing.ts +6 -0
- package/files/blueprint/things/system/verbs/health.ts +11 -0
- package/files/blueprint/values/identity.ts +15 -0
- package/files/generated/model/model.json +3658 -0
- package/files/journeys/README.md +112 -0
- package/files/journeys/an-invitation-is-used-once.ts +38 -0
- package/files/journeys/an-owner-makes-an-api-key.ts +50 -0
- package/files/journeys/health-is-up.ts +17 -0
- package/files/journeys/invite-and-accept.ts +103 -0
- package/files/journeys/sign-up-and-sign-in.ts +48 -0
- package/files/src/api-key/create.ts +20 -0
- package/files/src/invitation/accept.ts +24 -0
- package/files/src/invitation/revoke.ts +8 -0
- package/files/src/invitation/send.ts +21 -0
- package/files/src/organization/create.ts +25 -0
- package/files/src/person/sign-up.ts +22 -0
- package/files/src/session/show.ts +14 -0
- package/files/src/session/sign-in.ts +22 -0
- package/files/src/system/health.ts +8 -0
- package/generators.json +9 -0
- package/package.json +34 -3
- package/schema.json +8 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Journeys and planted fixtures (starter)
|
|
2
|
+
|
|
3
|
+
Written by the journey author, not the builder. The builder never edits these files, the evals or the runner's generic checks.
|
|
4
|
+
|
|
5
|
+
## Files
|
|
6
|
+
|
|
7
|
+
- `blueprint/starting-worlds/founded.ts`: ada (`owner`) runs `organization.create({ name: "Acme Demo" })`; it gives `organization`.
|
|
8
|
+
- `blueprint/starting-worlds/invited.ts`: from `founded`, ada runs `invitation.send` to olga's email; gives `organization` and `invitation`.
|
|
9
|
+
- `journeys/*.ts`: five journeys, listed below. Journey name is the file name with spaces ("health is up" in `health-is-up.ts`), as in acme.
|
|
10
|
+
- `test/fixtures/journeys/<name>/`: one planted break each, with its own README (what it replaces, what it breaks, which step must fail).
|
|
11
|
+
|
|
12
|
+
## Cast and runtime rules these journeys are written to
|
|
13
|
+
|
|
14
|
+
- A cast member whose role is not `Blueprint.anyone` is signed up through Better Auth before the journey and calls with their own bearer token (`person`, `email`, `password`).
|
|
15
|
+
- A `Blueprint.anyone` member gets a fresh `email` and `password`, no account and no token; their `person` ref is never used here.
|
|
16
|
+
- `s.last` in a payload is the answer of the latest call that answered 200; a refused call leaves it alone. In a check, `s.last` is the step's own answer. Journeys that chain ids rely on this.
|
|
17
|
+
- Sessions from `session.sign-in` are not used by later steps.
|
|
18
|
+
|
|
19
|
+
## health is up
|
|
20
|
+
|
|
21
|
+
| # | Call | Expected |
|
|
22
|
+
|---|---|---|
|
|
23
|
+
| 1 | visitor (anyone) `system.health` | `status` is "ok"; no events; span `system.health` |
|
|
24
|
+
|
|
25
|
+
## sign up and sign in
|
|
26
|
+
|
|
27
|
+
| # | Call | Expected |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| 1 | newcomer (anyone) `session.show` | refused `NotAllowed`; no events; span `session.show` absent |
|
|
30
|
+
| 2 | newcomer `person.sign-up` (own email, password, "Newcomer") | `person` is `per_...`; spans `person.sign-up`, `.create` |
|
|
31
|
+
| 3 | newcomer `person.sign-up` again, same email | refused `PersonEmailTaken`; no events |
|
|
32
|
+
| 4 | newcomer `session.sign-in` with a wrong password | refused `SessionCredentialsWrong`; no events; span `session.sign-in` |
|
|
33
|
+
| 5 | newcomer `session.sign-in` with the right password | `session` is `ses_...`; `token` non-empty; spans `session.sign-in`, `.verify` |
|
|
34
|
+
|
|
35
|
+
## invite and accept (from `founded`; ada owner, olga and bob signed in)
|
|
36
|
+
|
|
37
|
+
| # | Call | Expected |
|
|
38
|
+
|---|---|---|
|
|
39
|
+
| 1 | ada `session.show` | `person` is ada's person, `email` is ada's email; no events |
|
|
40
|
+
| 2 | ada `organization.get` | name "Acme Demo", owner ada, state "active"; organization is in `active` |
|
|
41
|
+
| 3 | bob `invitation.send` to olga's email | refused `NotAllowed`; no events; spans `invitation.send`, `.insert` absent |
|
|
42
|
+
| 4 | ada `invitation.send` to olga's email | `invitation` is `inv_...`; `InvitationSent { invitation, email: olga's }`; invitation `pending`; spans `invitation.send`, `.insert` |
|
|
43
|
+
| 5 | bob `invitation.accept` (olga's) | refused `NotAllowed`; no events; still `pending`; spans `invitation.accept`, `.join` absent |
|
|
44
|
+
| 6 | olga `invitation.accept` | `organization` is ada's; `membership` is `mem_...`; `InvitationAccepted { membership }`; spans `invitation.accept`, `.join` |
|
|
45
|
+
| 7 | bob `membership.get` (olga's) | refused `NotAllowed` |
|
|
46
|
+
| 8 | olga `membership.get` | organization ada's, person olga, role "member"; no events |
|
|
47
|
+
| 9 | ada `invitation.send` to bob's email | `inv_...`; `InvitationSent`; `pending` |
|
|
48
|
+
| 10 | olga `invitation.revoke` (the second) | refused `NotAllowed`; no events; spans `invitation.revoke`, `.update` absent |
|
|
49
|
+
| 11 | ada `invitation.revoke` | `InvitationRevoked`; spans `invitation.revoke`, `.update` |
|
|
50
|
+
|
|
51
|
+
## an invitation is used once (from `invited`; ada owner, olga signed in)
|
|
52
|
+
|
|
53
|
+
| # | Call | Expected |
|
|
54
|
+
|---|---|---|
|
|
55
|
+
| 1 | olga `invitation.accept` the world's invitation | organization ada's, `mem_...`; `InvitationAccepted { invitation, membership }`; invitation `accepted`; spans `accept`, `.join` |
|
|
56
|
+
| 2 | olga `invitation.accept` again | refused `InvitationNotPending` (`machine.notFrom`, `{ state: "accepted" }`); no events; still `accepted`; `.join` absent |
|
|
57
|
+
| 3 | ada `invitation.revoke` the accepted one | refused `InvitationNotPending` `{ state: "accepted" }`; no events; `.update` absent |
|
|
58
|
+
|
|
59
|
+
This journey exists because `invite and accept` cannot accept twice: after a successful accept `s.last` no longer holds the invitation id, and there is no other way to name an earlier step's answer. The `invited` world gives the id.
|
|
60
|
+
|
|
61
|
+
## an owner makes an api key (from `founded`; ada owner, bob signed in)
|
|
62
|
+
|
|
63
|
+
| # | Call | Expected |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| 1 | bob `api-key.create` in ada's organisation | refused `NotAllowed`; no events; spans `api-key.create`, `.issue`, `.insert` absent |
|
|
66
|
+
| 2 | ada `api-key.create` "ci" | `apiKey` is `key_...`; `key` non-empty; spans `api-key.create`, `.issue`, `.insert` |
|
|
67
|
+
| 3 | bob `organization.create` "Bob Co" | `org_...`; `OrganizationCreated { organization, owner: bob }`; organization `active`; spans `organization.create`, `.insert`, `.owner` |
|
|
68
|
+
| 4 | ada `organization.get` bob's | refused `NotAllowed` |
|
|
69
|
+
| 5 | bob `api-key.create` in his own organisation | `key_...`, non-empty `key` |
|
|
70
|
+
|
|
71
|
+
## Coverage this gives
|
|
72
|
+
|
|
73
|
+
| Operation | Called by | Move / outcome | Declared errors refused |
|
|
74
|
+
|---|---|---|---|
|
|
75
|
+
| `system.health` | health is up | none | none declared |
|
|
76
|
+
| `person.sign-up` | sign up and sign in | none | `PersonEmailTaken` (step 3) |
|
|
77
|
+
| `session.sign-in` | sign up and sign in | none | `SessionCredentialsWrong` (step 4) |
|
|
78
|
+
| `session.show` | sign up and sign in (refused), invite and accept (answered) | none | `NotAllowed` |
|
|
79
|
+
| `organization.create` | `founded` world, an owner makes an api key | `create`, `active` | `NotAllowed` is the runtime's, not required |
|
|
80
|
+
| `organization.get` | invite and accept, an owner makes an api key | read | `NotAllowed` |
|
|
81
|
+
| `invitation.send` | `invited` world, invite and accept | `send`, `pending` | `NotAllowed` |
|
|
82
|
+
| `invitation.accept` | invite and accept, an invitation is used once | `accept`, `accepted` | `InvitationNotPending`, `NotAllowed` |
|
|
83
|
+
| `invitation.revoke` | invite and accept | `revoke`, `revoked` | `InvitationNotPending` (once-used journey), `NotAllowed` |
|
|
84
|
+
| `membership.get` | invite and accept | read | `NotAllowed` |
|
|
85
|
+
| `api-key.create` | an owner makes an api key | none | `NotAllowed` |
|
|
86
|
+
|
|
87
|
+
All 11 operations, all four moves (`organization.create`, `send`, `accept`, `revoke`), all four outcome states (`active`, `pending`, `accepted`, `revoked`), and both declared errors are covered.
|
|
88
|
+
|
|
89
|
+
## Fixtures
|
|
90
|
+
|
|
91
|
+
| Fixture | Replaces | Break | Must fail |
|
|
92
|
+
|---|---|---|---|
|
|
93
|
+
| `wrong-password-signs-in` | `session/sign-in.ts` | any password signs in | sign up and sign in, step 4 |
|
|
94
|
+
| `accept-without-membership` | `invitation/accept.ts` | accepts, never writes the membership | invite and accept, step 8 |
|
|
95
|
+
| `owner-step-skipped` | `organization/create.ts` | declared `owner` step never run | steps eval for `organization.create`; an owner makes an api key, step 3 |
|
|
96
|
+
|
|
97
|
+
## Formats I chose where the docs were silent
|
|
98
|
+
|
|
99
|
+
- **Calls.** `s.<actor>.run(declaration, payload)`, never an operation name as a string, as in acme. Reads (`organization.get`, `membership.get`) are run with `{ id }`.
|
|
100
|
+
- **`s.<actor>.email` / `.password`** are refs, resolved at run time; used as payload values and in `emits(InvitationSent, { email: s.olga.email })`.
|
|
101
|
+
- **Refusing the machine.** `InvitationNotPending` is written `refuses(machine.notFrom, { state: "accepted" })`, as acme does, not by the generated class.
|
|
102
|
+
- **Hidden handlers.** A call refused before the handler has no operation span, so I assert `spans({ absent: [...] })` for the operation and its declared steps.
|
|
103
|
+
- **Refused calls and `s.last`.** The runner sets `last` only on a 200; steps 5 and 7 of `invite and accept` and step 4 of `an owner makes an api key` depend on that. If a read does not update `last` the same way as an operation, nothing here depends on it: no step uses `s.last` after a successful read.
|
|
104
|
+
- **Revocation.** The revoke step checks `emits(InvitationRevoked, {})` and spans only: the answer is `{}`, so `s.last` cannot name the invitation, and `machine.isIn` is not asserted. The `revoked` outcome is still reached (the runner reads the stored state of the move's subject).
|
|
105
|
+
- **No request keys.** `organization.create` is `idempotent: "request"`; neither the world nor the journey passes one, as acme's `founded` does. If the runtime demands a key, the builder should default one per call rather than edit these.
|
|
106
|
+
- **`invited` world.** Not in the brief; added for the accepted state and the second accept (see above). Its cast gives olga `Blueprint.signedIn`.
|
|
107
|
+
- **Member vs invitee.** olga is `Blueprint.signedIn`; she is the invitee because her email matches, and a member after accepting. `member` has no role of its own in the blueprint.
|
|
108
|
+
- **Names.** Second invitation goes to bob's email, which is only a convenient second address; bob never accepts it.
|
|
109
|
+
|
|
110
|
+
## Not verified
|
|
111
|
+
|
|
112
|
+
`tsc -p examples/starter/tsconfig.json --noEmit` currently fails for reasons outside these files: `generated/sdk/src/session/sign-in.ts` emits `client.session.sign-in(...)` (a hyphen is not a property name), and `blueprint/things/system/thing.ts` has `fields: {}` where `Blueprint.thing` wants an `id`. With those two excluded, the journeys and starting worlds type-check clean.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Journey-author file. The builder of invitation.accept and invitation.revoke never edits it (AGENTS.md, "Journeys and checks come first").
|
|
2
|
+
import { Blueprint, emits, expect, noEvents, refuses, spans } from "orthograph";
|
|
3
|
+
import { owner } from "../blueprint/things/organization/roles.ts";
|
|
4
|
+
import { machine } from "../blueprint/things/invitation/machine.ts";
|
|
5
|
+
import { InvitationAccepted } from "../blueprint/things/invitation/events.ts";
|
|
6
|
+
import accept from "../blueprint/things/invitation/verbs/accept.ts";
|
|
7
|
+
import revoke from "../blueprint/things/invitation/verbs/revoke.ts";
|
|
8
|
+
|
|
9
|
+
export default Blueprint.journey("an invitation is used once", {
|
|
10
|
+
cast: { ada: owner, olga: Blueprint.signedIn },
|
|
11
|
+
starts: "invited",
|
|
12
|
+
steps: (s) => [
|
|
13
|
+
s.step(
|
|
14
|
+
"olga accepts the invitation ada sent her",
|
|
15
|
+
s.olga.run(accept, { invitation: s.world.invitation }),
|
|
16
|
+
expect((a) => a.organization).toBe(s.world.organization),
|
|
17
|
+
expect((a) => a.membership).toMatch(/^mem_[0-9a-z]+$/),
|
|
18
|
+
emits(InvitationAccepted, { invitation: s.world.invitation, membership: s.last.membership }),
|
|
19
|
+
machine.isIn(s.world.invitation, "accepted"),
|
|
20
|
+
spans({ present: ["invitation.accept", "invitation.accept.join"] }),
|
|
21
|
+
),
|
|
22
|
+
s.step(
|
|
23
|
+
"accepting it again is refused",
|
|
24
|
+
s.olga.run(accept, { invitation: s.world.invitation }),
|
|
25
|
+
refuses(machine.notFrom, { state: "accepted" }),
|
|
26
|
+
noEvents(),
|
|
27
|
+
machine.isIn(s.world.invitation, "accepted"),
|
|
28
|
+
spans({ absent: ["invitation.accept.join"] }),
|
|
29
|
+
),
|
|
30
|
+
s.step(
|
|
31
|
+
"ada can't revoke an invitation that has been accepted",
|
|
32
|
+
s.ada.run(revoke, { invitation: s.world.invitation }),
|
|
33
|
+
refuses(machine.notFrom, { state: "accepted" }),
|
|
34
|
+
noEvents(),
|
|
35
|
+
spans({ absent: ["invitation.revoke.update"] }),
|
|
36
|
+
),
|
|
37
|
+
],
|
|
38
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Journey-author file. The builder of api-key.create and organization.create never edits it (AGENTS.md, "Journeys and checks come first").
|
|
2
|
+
import { Blueprint, emits, expect, noEvents, refuses, spans } from "orthograph";
|
|
3
|
+
import { owner } from "../blueprint/things/organization/roles.ts";
|
|
4
|
+
import { machine } from "../blueprint/things/organization/machine.ts";
|
|
5
|
+
import { OrganizationCreated } from "../blueprint/things/organization/events.ts";
|
|
6
|
+
import create from "../blueprint/things/organization/verbs/create.ts";
|
|
7
|
+
import getOrganization from "../blueprint/things/organization/verbs/get.ts";
|
|
8
|
+
import createKey from "../blueprint/things/api-key/verbs/create.ts";
|
|
9
|
+
|
|
10
|
+
// `s.last` in a payload is the answer of the latest call that succeeded; a refused call leaves it alone.
|
|
11
|
+
export default Blueprint.journey("an owner makes an api key", {
|
|
12
|
+
// ada owns the founded organisation; bob is signed in and owns nothing there.
|
|
13
|
+
cast: { ada: owner, bob: Blueprint.signedIn },
|
|
14
|
+
starts: "founded",
|
|
15
|
+
steps: (s) => [
|
|
16
|
+
s.step(
|
|
17
|
+
"bob can't make a key in ada's organisation",
|
|
18
|
+
s.bob.run(createKey, { organization: s.world.organization, name: "bob's key" }),
|
|
19
|
+
refuses(Blueprint.NotAllowed),
|
|
20
|
+
noEvents(),
|
|
21
|
+
spans({ absent: ["api-key.create", "api-key.create.issue", "api-key.create.insert"] }),
|
|
22
|
+
),
|
|
23
|
+
s.step(
|
|
24
|
+
"ada makes a key; the secret is shown once",
|
|
25
|
+
s.ada.run(createKey, { organization: s.world.organization, name: "ci" }),
|
|
26
|
+
expect((a) => a.apiKey).toMatch(/^key_[0-9a-z]+$/),
|
|
27
|
+
expect((a) => a.key).toMatch(/^.+$/),
|
|
28
|
+
spans({ present: ["api-key.create", "api-key.create.issue", "api-key.create.insert"] }),
|
|
29
|
+
),
|
|
30
|
+
s.step(
|
|
31
|
+
"bob founds an organisation of his own",
|
|
32
|
+
s.bob.run(create, { name: "Bob Co" }),
|
|
33
|
+
expect((a) => a.organization).toMatch(/^org_[0-9a-z]+$/),
|
|
34
|
+
emits(OrganizationCreated, { organization: s.last.organization, owner: s.bob.person }),
|
|
35
|
+
machine.isIn(s.last.organization, "active"),
|
|
36
|
+
spans({ present: ["organization.create", "organization.create.insert", "organization.create.owner"] }),
|
|
37
|
+
),
|
|
38
|
+
s.step(
|
|
39
|
+
"ada can't read bob's organisation",
|
|
40
|
+
s.ada.run(getOrganization, { id: s.last.organization }),
|
|
41
|
+
refuses(Blueprint.NotAllowed),
|
|
42
|
+
),
|
|
43
|
+
s.step(
|
|
44
|
+
"bob makes a key in his own organisation",
|
|
45
|
+
s.bob.run(createKey, { organization: s.last.organization, name: "bob's own key" }),
|
|
46
|
+
expect((a) => a.apiKey).toMatch(/^key_[0-9a-z]+$/),
|
|
47
|
+
expect((a) => a.key).toMatch(/^.+$/),
|
|
48
|
+
),
|
|
49
|
+
],
|
|
50
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Journey-author file. The builder of system.health never edits it (AGENTS.md, "Journeys and checks come first").
|
|
2
|
+
import { Blueprint, expect, noEvents, spans } from "orthograph";
|
|
3
|
+
import health from "../blueprint/things/system/verbs/health.ts";
|
|
4
|
+
|
|
5
|
+
export default Blueprint.journey("health is up", {
|
|
6
|
+
// A visitor with no account and no token.
|
|
7
|
+
cast: { visitor: Blueprint.anyone },
|
|
8
|
+
steps: (s) => [
|
|
9
|
+
s.step(
|
|
10
|
+
"anyone can ask if the application is up",
|
|
11
|
+
s.visitor.run(health, {}),
|
|
12
|
+
expect((a) => a.status).toBe("ok"),
|
|
13
|
+
noEvents(),
|
|
14
|
+
spans({ present: ["system.health"] }),
|
|
15
|
+
),
|
|
16
|
+
],
|
|
17
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Journey-author file. The builder of invitation.send, invitation.accept, invitation.revoke, membership.get,
|
|
2
|
+
// organization.get and session.show never edits it (AGENTS.md, "Journeys and checks come first").
|
|
3
|
+
import { Blueprint, emits, expect, noEvents, refuses, spans } from "orthograph";
|
|
4
|
+
import { owner } from "../blueprint/things/organization/roles.ts";
|
|
5
|
+
import { machine as organization } from "../blueprint/things/organization/machine.ts";
|
|
6
|
+
import { machine as invitation } from "../blueprint/things/invitation/machine.ts";
|
|
7
|
+
import { InvitationAccepted, InvitationRevoked, InvitationSent } from "../blueprint/things/invitation/events.ts";
|
|
8
|
+
import show from "../blueprint/things/session/verbs/show.ts";
|
|
9
|
+
import getOrganization from "../blueprint/things/organization/verbs/get.ts";
|
|
10
|
+
import getMembership from "../blueprint/things/membership/verbs/get.ts";
|
|
11
|
+
import send from "../blueprint/things/invitation/verbs/send.ts";
|
|
12
|
+
import accept from "../blueprint/things/invitation/verbs/accept.ts";
|
|
13
|
+
import revoke from "../blueprint/things/invitation/verbs/revoke.ts";
|
|
14
|
+
|
|
15
|
+
// `s.last` in a payload is the answer of the latest call that succeeded; a refused call leaves it alone.
|
|
16
|
+
export default Blueprint.journey("invite and accept", {
|
|
17
|
+
// ada owns the organisation; olga is the invitee; bob is a signed-in outsider.
|
|
18
|
+
cast: { ada: owner, olga: Blueprint.signedIn, bob: Blueprint.signedIn },
|
|
19
|
+
starts: "founded",
|
|
20
|
+
steps: (s) => [
|
|
21
|
+
s.step(
|
|
22
|
+
"ada sees who she is signed in as",
|
|
23
|
+
s.ada.run(show, {}),
|
|
24
|
+
expect((a) => a.person).toBe(s.ada.person),
|
|
25
|
+
expect((a) => a.email).toBe(s.ada.email),
|
|
26
|
+
noEvents(),
|
|
27
|
+
spans({ present: ["session.show"] }),
|
|
28
|
+
),
|
|
29
|
+
s.step(
|
|
30
|
+
"ada reads her organisation",
|
|
31
|
+
s.ada.run(getOrganization, { id: s.world.organization }),
|
|
32
|
+
expect((a) => a.name).toBe("Acme Demo"),
|
|
33
|
+
expect((a) => a.owner).toBe(s.ada.person),
|
|
34
|
+
expect((a) => a.state).toBe("active"),
|
|
35
|
+
organization.isIn(s.world.organization, "active"),
|
|
36
|
+
noEvents(),
|
|
37
|
+
),
|
|
38
|
+
s.step(
|
|
39
|
+
"bob can't invite anyone to it",
|
|
40
|
+
s.bob.run(send, { organization: s.world.organization, email: s.olga.email }),
|
|
41
|
+
refuses(Blueprint.NotAllowed),
|
|
42
|
+
noEvents(),
|
|
43
|
+
spans({ absent: ["invitation.send", "invitation.send.insert"] }),
|
|
44
|
+
),
|
|
45
|
+
s.step(
|
|
46
|
+
"ada invites olga by her email",
|
|
47
|
+
s.ada.run(send, { organization: s.world.organization, email: s.olga.email }),
|
|
48
|
+
expect((a) => a.invitation).toMatch(/^inv_[0-9a-z]+$/),
|
|
49
|
+
emits(InvitationSent, { invitation: s.last.invitation, email: s.olga.email }),
|
|
50
|
+
invitation.isIn(s.last.invitation, "pending"),
|
|
51
|
+
spans({ present: ["invitation.send", "invitation.send.insert"] }),
|
|
52
|
+
),
|
|
53
|
+
s.step(
|
|
54
|
+
"bob can't accept olga's invitation",
|
|
55
|
+
s.bob.run(accept, { invitation: s.last.invitation }),
|
|
56
|
+
refuses(Blueprint.NotAllowed),
|
|
57
|
+
noEvents(),
|
|
58
|
+
invitation.isIn(s.last.invitation, "pending"),
|
|
59
|
+
spans({ absent: ["invitation.accept", "invitation.accept.join"] }),
|
|
60
|
+
),
|
|
61
|
+
s.step(
|
|
62
|
+
"olga accepts and joins ada's organisation",
|
|
63
|
+
s.olga.run(accept, { invitation: s.last.invitation }),
|
|
64
|
+
expect((a) => a.organization).toBe(s.world.organization),
|
|
65
|
+
expect((a) => a.membership).toMatch(/^mem_[0-9a-z]+$/),
|
|
66
|
+
emits(InvitationAccepted, { membership: s.last.membership }),
|
|
67
|
+
spans({ present: ["invitation.accept", "invitation.accept.join"] }),
|
|
68
|
+
),
|
|
69
|
+
s.step(
|
|
70
|
+
"bob can't read olga's membership",
|
|
71
|
+
s.bob.run(getMembership, { id: s.last.membership }),
|
|
72
|
+
refuses(Blueprint.NotAllowed),
|
|
73
|
+
),
|
|
74
|
+
s.step(
|
|
75
|
+
"olga reads her membership: she is a member, not an owner",
|
|
76
|
+
s.olga.run(getMembership, { id: s.last.membership }),
|
|
77
|
+
expect((a) => a.organization).toBe(s.world.organization),
|
|
78
|
+
expect((a) => a.person).toBe(s.olga.person),
|
|
79
|
+
expect((a) => a.role).toBe("member"),
|
|
80
|
+
noEvents(),
|
|
81
|
+
),
|
|
82
|
+
s.step(
|
|
83
|
+
"ada sends a second invitation",
|
|
84
|
+
s.ada.run(send, { organization: s.world.organization, email: s.bob.email }),
|
|
85
|
+
expect((a) => a.invitation).toMatch(/^inv_[0-9a-z]+$/),
|
|
86
|
+
emits(InvitationSent, { invitation: s.last.invitation, email: s.bob.email }),
|
|
87
|
+
invitation.isIn(s.last.invitation, "pending"),
|
|
88
|
+
),
|
|
89
|
+
s.step(
|
|
90
|
+
"olga, a member but not the owner, can't revoke it",
|
|
91
|
+
s.olga.run(revoke, { invitation: s.last.invitation }),
|
|
92
|
+
refuses(Blueprint.NotAllowed),
|
|
93
|
+
noEvents(),
|
|
94
|
+
spans({ absent: ["invitation.revoke", "invitation.revoke.update"] }),
|
|
95
|
+
),
|
|
96
|
+
s.step(
|
|
97
|
+
"ada revokes it",
|
|
98
|
+
s.ada.run(revoke, { invitation: s.last.invitation }),
|
|
99
|
+
emits(InvitationRevoked, {}),
|
|
100
|
+
spans({ present: ["invitation.revoke", "invitation.revoke.update"] }),
|
|
101
|
+
),
|
|
102
|
+
],
|
|
103
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Journey-author file. The builder of person.sign-up and session.sign-in never edits it (AGENTS.md, "Journeys and checks come first").
|
|
2
|
+
import { Blueprint, expect, noEvents, refuses, spans } from "orthograph";
|
|
3
|
+
import { PersonEmailTaken } from "../blueprint/things/person/errors.ts";
|
|
4
|
+
import { SessionCredentialsWrong } from "../blueprint/things/session/errors.ts";
|
|
5
|
+
import signUp from "../blueprint/things/person/verbs/sign-up.ts";
|
|
6
|
+
import signIn from "../blueprint/things/session/verbs/sign-in.ts";
|
|
7
|
+
import show from "../blueprint/things/session/verbs/show.ts";
|
|
8
|
+
|
|
9
|
+
export default Blueprint.journey("sign up and sign in", {
|
|
10
|
+
// The newcomer is not signed up by the world: they get a fresh email and password and no token.
|
|
11
|
+
cast: { newcomer: Blueprint.anyone },
|
|
12
|
+
steps: (s) => [
|
|
13
|
+
s.step(
|
|
14
|
+
"before signing up the newcomer is nobody",
|
|
15
|
+
s.newcomer.run(show, {}),
|
|
16
|
+
refuses(Blueprint.NotAllowed),
|
|
17
|
+
noEvents(),
|
|
18
|
+
spans({ absent: ["session.show"] }),
|
|
19
|
+
),
|
|
20
|
+
s.step(
|
|
21
|
+
"the newcomer signs up",
|
|
22
|
+
s.newcomer.run(signUp, { email: s.newcomer.email, password: s.newcomer.password, name: "Newcomer" }),
|
|
23
|
+
expect((a) => a.person).toMatch(/^per_[0-9a-z]+$/),
|
|
24
|
+
spans({ present: ["person.sign-up", "person.sign-up.create"] }),
|
|
25
|
+
),
|
|
26
|
+
s.step(
|
|
27
|
+
"signing up again with the same email is refused",
|
|
28
|
+
s.newcomer.run(signUp, { email: s.newcomer.email, password: s.newcomer.password, name: "Newcomer again" }),
|
|
29
|
+
refuses(PersonEmailTaken),
|
|
30
|
+
noEvents(),
|
|
31
|
+
spans({ present: ["person.sign-up"] }),
|
|
32
|
+
),
|
|
33
|
+
s.step(
|
|
34
|
+
"a wrong password is refused",
|
|
35
|
+
s.newcomer.run(signIn, { email: s.newcomer.email, password: "not-the-password" }),
|
|
36
|
+
refuses(SessionCredentialsWrong),
|
|
37
|
+
noEvents(),
|
|
38
|
+
spans({ present: ["session.sign-in"] }),
|
|
39
|
+
),
|
|
40
|
+
s.step(
|
|
41
|
+
"the right password answers a session and a token",
|
|
42
|
+
s.newcomer.run(signIn, { email: s.newcomer.email, password: s.newcomer.password }),
|
|
43
|
+
expect((a) => a.session).toMatch(/^ses_[0-9a-z]+$/),
|
|
44
|
+
expect((a) => a.token).toMatch(/^.+$/),
|
|
45
|
+
spans({ present: ["session.sign-in", "session.sign-in.verify"] }),
|
|
46
|
+
),
|
|
47
|
+
],
|
|
48
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/api-key/verbs/create.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Auth, Records } from "@orthograph/language-effect";
|
|
4
|
+
import type { Handler } from "../../generated/api/src/api-key/create.handler.ts";
|
|
5
|
+
import { ApiKey } from "../../blueprint/things/api-key/thing.ts";
|
|
6
|
+
|
|
7
|
+
/** Create an API key in an organisation, on its owner's behalf. The key itself is answered once and never kept. */
|
|
8
|
+
const create: Handler = ({ organization, name }, { principal, steps }) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
// A key is made in person: the declaration says `.via(Via.session, Via.device)`, so neither an agent nor a key reaches here.
|
|
11
|
+
if (principal.kind !== "person") return yield* Effect.die(new Error("reached without a signed-in person"));
|
|
12
|
+
const auth = yield* Auth;
|
|
13
|
+
const records = yield* Records;
|
|
14
|
+
const issued = yield* steps.issue(auth.createApiKey({ person: principal.person, name }));
|
|
15
|
+
const apiKey = issued.apiKey as never;
|
|
16
|
+
yield* steps.insert(records.insert(ApiKey, { id: apiKey, organization, name, createdBy: principal.person as never }));
|
|
17
|
+
return { apiKey, key: issued.key };
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export default create;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/invitation/verbs/accept.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Ids, Records } from "@orthograph/language-effect";
|
|
4
|
+
import type { Handler } from "../../generated/api/src/invitation/accept.handler.ts";
|
|
5
|
+
import { Invitation } from "../../blueprint/things/invitation/thing.ts";
|
|
6
|
+
import { Membership } from "../../blueprint/things/membership/thing.ts";
|
|
7
|
+
|
|
8
|
+
/** Accept an invitation: the invitee becomes a member of its organisation. */
|
|
9
|
+
const accept: Handler = ({ invitation }, { principal, steps, move }) =>
|
|
10
|
+
Effect.gen(function* () {
|
|
11
|
+
// Consent is a person's own act: the declaration says `.notVia(Via.agent)`, so an agent never reaches here, and a guard that means "a person" says so.
|
|
12
|
+
if (principal.kind !== "person") return yield* Effect.die(new Error("reached without a signed-in person"));
|
|
13
|
+
const records = yield* Records;
|
|
14
|
+
// Only the invitee may accept, so the runtime has found the invitation before this runs.
|
|
15
|
+
const found = yield* records.find(Invitation, invitation);
|
|
16
|
+
if (!found) return yield* Effect.die(new Error(`invitation ${invitation} was checked and then not found`));
|
|
17
|
+
const organization = found["organization"] as never;
|
|
18
|
+
const membership = yield* Ids.of(Membership.fields.id);
|
|
19
|
+
yield* steps.join(records.insert(Membership, { id: membership, organization, person: principal.person as never, role: "member" }));
|
|
20
|
+
yield* move("accepted");
|
|
21
|
+
return { organization, membership };
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export default accept;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/invitation/verbs/revoke.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import type { Handler } from "../../generated/api/src/invitation/revoke.handler.ts";
|
|
4
|
+
|
|
5
|
+
/** Withdraw an invitation that hasn't been accepted: the move is the whole change. */
|
|
6
|
+
const revoke: Handler = (_payload, { steps, move }) => steps.update(move("revoked")).pipe(Effect.as({}));
|
|
7
|
+
|
|
8
|
+
export default revoke;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/invitation/verbs/send.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { acting } from "orthograph";
|
|
4
|
+
import { Ids, Records } from "@orthograph/language-effect";
|
|
5
|
+
import type { Handler } from "../../generated/api/src/invitation/send.handler.ts";
|
|
6
|
+
import { Invitation } from "../../blueprint/things/invitation/thing.ts";
|
|
7
|
+
|
|
8
|
+
/** Invite someone to an organisation by email. Only its owner gets this far. */
|
|
9
|
+
const send: Handler = ({ organization, email }, { principal, steps, move }) =>
|
|
10
|
+
Effect.gen(function* () {
|
|
11
|
+
// The owner's agent holds the owner's role (M2-auth, fork 2): the invitation is the person's, sent by hands that may be an agent's.
|
|
12
|
+
const who = acting(principal);
|
|
13
|
+
if (!who) return yield* Effect.die(new Error("reached without a signed-in person"));
|
|
14
|
+
const records = yield* Records;
|
|
15
|
+
const invitation = yield* Ids.of(Invitation.fields.id);
|
|
16
|
+
yield* steps.insert(records.insert(Invitation, { id: invitation, organization, email, invitedBy: who.person as never }));
|
|
17
|
+
yield* move("pending");
|
|
18
|
+
return { invitation };
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export default send;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/organization/verbs/create.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { acting } from "orthograph";
|
|
4
|
+
import { Ids, Records } from "@orthograph/language-effect";
|
|
5
|
+
import type { Handler } from "../../generated/api/src/organization/create.handler.ts";
|
|
6
|
+
import { Organization } from "../../blueprint/things/organization/thing.ts";
|
|
7
|
+
import { Membership } from "../../blueprint/things/membership/thing.ts";
|
|
8
|
+
|
|
9
|
+
/** Found an organisation: whoever founds it owns it, and is its first member, as its owner. */
|
|
10
|
+
const create: Handler = ({ name }, { principal, steps, move }) =>
|
|
11
|
+
Effect.gen(function* () {
|
|
12
|
+
// `signedIn` admits a person and an agent acting for one: the organisation is the person's either way.
|
|
13
|
+
const who = acting(principal);
|
|
14
|
+
if (!who) return yield* Effect.die(new Error("reached without a signed-in person"));
|
|
15
|
+
const records = yield* Records;
|
|
16
|
+
const organization = yield* Ids.of(Organization.fields.id);
|
|
17
|
+
const owner = who.person as never;
|
|
18
|
+
yield* steps.insert(records.insert(Organization, { id: organization, name, owner }));
|
|
19
|
+
const membership = yield* Ids.of(Membership.fields.id);
|
|
20
|
+
yield* steps.owner(records.insert(Membership, { id: membership, organization, person: owner, role: "owner" }));
|
|
21
|
+
yield* move("active");
|
|
22
|
+
return { organization };
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export default create;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/person/verbs/sign-up.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Auth } from "@orthograph/language-effect";
|
|
4
|
+
import type { Handler } from "../../generated/api/src/person/sign-up.handler.ts";
|
|
5
|
+
import { PersonEmailTaken } from "../../blueprint/things/person/errors.ts";
|
|
6
|
+
|
|
7
|
+
/** Sign up with an email address and a password. Better Auth keeps the person and the password's hash. */
|
|
8
|
+
const signUp: Handler = ({ email, password, name }, { steps }) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
const auth = yield* Auth;
|
|
11
|
+
const made = yield* steps.create(auth.signUp({ email, password, name })).pipe(
|
|
12
|
+
Effect.catchTag("AuthRefused", () =>
|
|
13
|
+
Effect.gen(function* () {
|
|
14
|
+
yield* Effect.logInfo("someone has already signed up with this email");
|
|
15
|
+
return yield* new PersonEmailTaken({});
|
|
16
|
+
}),
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
return { person: made.person as never };
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export default signUp;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/session/verbs/show.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { acting } from "orthograph";
|
|
4
|
+
import type { Handler } from "../../generated/api/src/session/show.handler.ts";
|
|
5
|
+
|
|
6
|
+
/** Who is signed in, and how: the principal the runtime resolved from the bearer token. An agent is signed in for its person, and says `agent`. */
|
|
7
|
+
const show: Handler = (_payload, { principal }) => {
|
|
8
|
+
const who = acting(principal);
|
|
9
|
+
return who?.email !== undefined
|
|
10
|
+
? Effect.succeed({ person: who.person as never, email: who.email, via: who.via })
|
|
11
|
+
: Effect.die(new Error("reached without a signed-in person"));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default show;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/session/verbs/sign-in.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Auth } from "@orthograph/language-effect";
|
|
4
|
+
import type { Handler } from "../../generated/api/src/session/sign-in.handler.ts";
|
|
5
|
+
import { SessionCredentialsWrong } from "../../blueprint/things/session/errors.ts";
|
|
6
|
+
|
|
7
|
+
/** Sign in with email and password; the token is the bearer for later calls. */
|
|
8
|
+
const signIn: Handler = ({ email, password }, { steps }) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
const auth = yield* Auth;
|
|
11
|
+
const signed = yield* steps.verify(auth.signIn({ email, password })).pipe(
|
|
12
|
+
Effect.catchTag("AuthRefused", () =>
|
|
13
|
+
Effect.gen(function* () {
|
|
14
|
+
yield* Effect.logInfo("the email and password don't match anyone");
|
|
15
|
+
return yield* new SessionCredentialsWrong({});
|
|
16
|
+
}),
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
return { session: signed.session as never, token: signed.token };
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export default signIn;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Scaffolded from blueprint/things/system/verbs/health.ts. Yours from now on.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import type { Handler } from "../../generated/api/src/system/health.handler.ts";
|
|
4
|
+
|
|
5
|
+
/** Is the application up? Answering at all says it is. */
|
|
6
|
+
const health: Handler = () => Effect.succeed({ status: "ok" as const });
|
|
7
|
+
|
|
8
|
+
export default health;
|
package/generators.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orthograph/preset",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-next.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"generators": "./generators.json",
|
|
7
|
+
"imports": {
|
|
8
|
+
"#modules/*.js": "./dist/modules/*.js"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@nx/devkit": "24.0.3",
|
|
19
|
+
"effect": "4.1.4",
|
|
20
|
+
"yaml": "2.9.0",
|
|
21
|
+
"nx": "24.0.3",
|
|
22
|
+
"@orthograph/compiler": "0.1.0-next.1",
|
|
23
|
+
"@orthograph/generators": "0.1.0-next.1",
|
|
24
|
+
"orthograph": "0.1.0-next.1"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"generators.json",
|
|
29
|
+
"schema.json",
|
|
30
|
+
"files"
|
|
31
|
+
],
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public",
|
|
34
|
+
"tag": "next"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {}
|
|
6
37
|
}
|
package/schema.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"$id": "OrthographPreset",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"properties": { "name": { "type": "string", "description": "The project's name.", "$default": { "$source": "argv", "index": 0 } } },
|
|
6
|
+
"required": ["name"],
|
|
7
|
+
"additionalProperties": true
|
|
8
|
+
}
|