@ossy/tokens 1.1.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.
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@ossy/tokens",
3
+ "description": "Token domain — aggregate and events for the Ossy token model",
4
+ "version": "1.1.0",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "./src/index.js",
8
+ "module": "./src/index.js",
9
+ "exports": {
10
+ ".": "./src/index.js"
11
+ },
12
+ "author": "Ossy <yourfriends@ossy.se> (https://ossy.se)",
13
+ "license": "MIT",
14
+ "ossy": {
15
+ "src": "./src"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public",
19
+ "registry": "https://registry.npmjs.org"
20
+ },
21
+ "dependencies": {
22
+ "nanoid": "^5.1.11"
23
+ },
24
+ "files": [
25
+ "/src",
26
+ "README.md"
27
+ ],
28
+ "gitHead": "5f1feadf3b630aa96ec61f1d48c11e0e1aa4b095"
29
+ }
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './token.aggregate.js'
2
+ export * from './token.events.js'
@@ -0,0 +1,41 @@
1
+
2
+ export class Token {
3
+
4
+ static AggregateType = 'Token'
5
+
6
+ static View(events, savedState = {}) {
7
+ return events.reduce((token, event) => {
8
+
9
+ switch (event.type) {
10
+
11
+ case 'Created': {
12
+ return {
13
+ ...event.payload,
14
+ status: 'Active',
15
+ id: event.aggregateId,
16
+ created: event.created,
17
+ }
18
+ }
19
+
20
+ case 'Revoked':
21
+ return {
22
+ status: 'Revoked',
23
+ revokedAt: event.created
24
+ }
25
+
26
+ default:
27
+ return token
28
+
29
+ }
30
+ }, savedState)
31
+ }
32
+
33
+ static Redacted(events, state) {
34
+ const token = Token.View(events, state)
35
+ return {
36
+ ...token,
37
+ token: undefined
38
+ }
39
+ }
40
+
41
+ }
@@ -0,0 +1,48 @@
1
+ import { nanoid } from 'nanoid'
2
+
3
+ export class TokenEvents {
4
+
5
+ /**
6
+ * Creates a token-created domain event.
7
+ *
8
+ * The caller is responsible for supplying the pre-computed `token` (JWT string)
9
+ * and `expiresAt` (Unix ms timestamp). Keeping JWT signing and config out of
10
+ * this package lets it stay infrastructure-free.
11
+ */
12
+ static Created({
13
+ createdBy,
14
+ type,
15
+ description,
16
+ name,
17
+ subject,
18
+ audience,
19
+ token,
20
+ expiresAt,
21
+ ...payload
22
+ }) {
23
+ const id = nanoid()
24
+
25
+ return ({
26
+ type: 'Created',
27
+ aggregateId: id,
28
+ createdBy: createdBy,
29
+ payload: {
30
+ ...payload,
31
+ token,
32
+ type,
33
+ name,
34
+ description,
35
+ subject,
36
+ expiresAt,
37
+ }
38
+ })
39
+ }
40
+
41
+ static Revoked({ createdBy }) {
42
+ return ({
43
+ type: 'Revoked',
44
+ createdBy: createdBy,
45
+ })
46
+ }
47
+
48
+ }