@lorion-org/registry-hub 1.0.0-beta.0 → 1.0.0-beta.2
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/README.md +39 -31
- package/examples/node-basic.js +8 -4
- package/examples/node-basic.ts +14 -10
- package/examples/renderer-registry.js +5 -5
- package/examples/renderer-registry.ts +10 -9
- package/package.json +8 -2
- package/src/index.ts +83 -0
package/README.md
CHANGED
|
@@ -39,17 +39,21 @@ import { createRegistry, createRegistryHub } from '@lorion-org/registry-hub';
|
|
|
39
39
|
```ts
|
|
40
40
|
import { createRegistry, type RegistryItem } from '@lorion-org/registry-hub';
|
|
41
41
|
|
|
42
|
-
type
|
|
43
|
-
|
|
42
|
+
type PaymentProvider = RegistryItem & {
|
|
43
|
+
createCheckoutPath: (input: { shopId: string }) => string;
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
const
|
|
46
|
+
const paymentProviders = createRegistry<PaymentProvider>('payment-checkout-providers');
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
paymentProviders.register({
|
|
49
|
+
id: 'payment-provider-stripe',
|
|
50
|
+
createCheckoutPath: (input) =>
|
|
51
|
+
`/providers/payment-provider-stripe/checkout?shop=${encodeURIComponent(input.shopId)}`,
|
|
52
|
+
});
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
paymentProviders.get('payment-provider-stripe');
|
|
55
|
+
paymentProviders.list();
|
|
56
|
+
paymentProviders.entries();
|
|
53
57
|
```
|
|
54
58
|
|
|
55
59
|
### Use a registry hub
|
|
@@ -57,37 +61,38 @@ commands.entries();
|
|
|
57
61
|
```ts
|
|
58
62
|
import { createRegistryHub, type RegistryItem } from '@lorion-org/registry-hub';
|
|
59
63
|
|
|
60
|
-
type
|
|
61
|
-
|
|
64
|
+
type Shop = RegistryItem & {
|
|
65
|
+
path: string;
|
|
62
66
|
};
|
|
63
67
|
|
|
64
68
|
const hub = createRegistryHub();
|
|
65
69
|
|
|
66
|
-
hub.createRegistry<
|
|
67
|
-
hub.register<
|
|
70
|
+
hub.createRegistry<Shop>('shops');
|
|
71
|
+
hub.register<Shop>('shops', { id: 'shop-coffee', path: '/shops/coffee' });
|
|
68
72
|
|
|
69
|
-
const
|
|
70
|
-
const
|
|
73
|
+
const shop = hub.get<Shop>('shops', 'shop-coffee');
|
|
74
|
+
const shops = hub.list<Shop>('shops');
|
|
71
75
|
const registries = hub.entries();
|
|
72
76
|
```
|
|
73
77
|
|
|
74
|
-
###
|
|
78
|
+
### Shop registry example
|
|
75
79
|
|
|
76
80
|
```ts
|
|
77
81
|
import { createRegistry, type RegistryItem } from '@lorion-org/registry-hub';
|
|
78
82
|
|
|
79
|
-
type
|
|
80
|
-
|
|
83
|
+
type Shop = RegistryItem & {
|
|
84
|
+
name: string;
|
|
85
|
+
path: string;
|
|
81
86
|
};
|
|
82
87
|
|
|
83
|
-
const
|
|
88
|
+
const shops = createRegistry<Shop>('shops');
|
|
84
89
|
|
|
85
|
-
|
|
86
|
-
{ id: '
|
|
87
|
-
{ id: '
|
|
90
|
+
shops.register([
|
|
91
|
+
{ id: 'shop-coffee', name: 'Bean Supply', path: '/shops/coffee' },
|
|
92
|
+
{ id: 'shop-stationery', name: 'Paper Desk', path: '/shops/stationery' },
|
|
88
93
|
]);
|
|
89
94
|
|
|
90
|
-
const
|
|
95
|
+
const knownShops = shops.entries();
|
|
91
96
|
```
|
|
92
97
|
|
|
93
98
|
### Framework recipe
|
|
@@ -113,14 +118,16 @@ Another plugin can then consume that injected hub and register its own entries.
|
|
|
113
118
|
```ts
|
|
114
119
|
import type { RegistryItem } from '@lorion-org/registry-hub';
|
|
115
120
|
|
|
116
|
-
type
|
|
117
|
-
|
|
121
|
+
type Shop = RegistryItem & {
|
|
122
|
+
name: string;
|
|
123
|
+
path: string;
|
|
118
124
|
};
|
|
119
125
|
|
|
120
126
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
121
|
-
nuxtApp.$registryHub.register<
|
|
122
|
-
id: '
|
|
123
|
-
|
|
127
|
+
nuxtApp.$registryHub.register<Shop>('shops', {
|
|
128
|
+
id: 'shop-coffee',
|
|
129
|
+
name: 'Bean Supply',
|
|
130
|
+
path: '/shops/coffee',
|
|
124
131
|
});
|
|
125
132
|
});
|
|
126
133
|
```
|
|
@@ -130,18 +137,19 @@ A composable can later load all entries of one typed registry from that same hub
|
|
|
130
137
|
```ts
|
|
131
138
|
import type { RegistryItem } from '@lorion-org/registry-hub';
|
|
132
139
|
|
|
133
|
-
type
|
|
134
|
-
|
|
140
|
+
type Shop = RegistryItem & {
|
|
141
|
+
name: string;
|
|
142
|
+
path: string;
|
|
135
143
|
};
|
|
136
144
|
|
|
137
|
-
export function
|
|
138
|
-
return useNuxtApp().$registryHub.list<
|
|
145
|
+
export function useShops(): Shop[] {
|
|
146
|
+
return useNuxtApp().$registryHub.list<Shop>('shops');
|
|
139
147
|
}
|
|
140
148
|
```
|
|
141
149
|
|
|
142
150
|
Runnable example files live in [`examples/`](./examples).
|
|
143
151
|
The Node example imports the published package name instead of local source files so it mirrors real consumer usage.
|
|
144
|
-
The examples use
|
|
152
|
+
The examples use the same shop and checkout-provider domain as the LORION playgrounds.
|
|
145
153
|
|
|
146
154
|
## Local commands
|
|
147
155
|
|
package/examples/node-basic.js
CHANGED
|
@@ -3,8 +3,12 @@ import process from 'node:process';
|
|
|
3
3
|
|
|
4
4
|
const hub = createRegistryHub();
|
|
5
5
|
|
|
6
|
-
hub.register('
|
|
7
|
-
|
|
6
|
+
hub.register('payment-checkout-providers', {
|
|
7
|
+
id: 'payment-provider-stripe',
|
|
8
|
+
createCheckoutPath: (input) =>
|
|
9
|
+
`/providers/payment-provider-stripe/checkout?shop=${encodeURIComponent(input.shopId)}`,
|
|
10
|
+
});
|
|
11
|
+
hub.register('shops', { id: 'shop-coffee', path: '/shops/coffee' });
|
|
8
12
|
|
|
9
|
-
process.stdout.write(`${JSON.stringify(hub.list('
|
|
10
|
-
process.stdout.write(`${JSON.stringify(hub.get('
|
|
13
|
+
process.stdout.write(`${JSON.stringify(hub.list('payment-checkout-providers'))}\n`);
|
|
14
|
+
process.stdout.write(`${JSON.stringify(hub.get('shops', 'shop-coffee'))}\n`);
|
package/examples/node-basic.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
import { createRegistryHub, type RegistryItem } from '@lorion-org/registry-hub';
|
|
2
2
|
|
|
3
|
-
type
|
|
4
|
-
|
|
3
|
+
type PaymentProvider = RegistryItem & {
|
|
4
|
+
createCheckoutPath: (input: { shopId: string }) => string;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
-
type
|
|
8
|
-
|
|
7
|
+
type Shop = RegistryItem & {
|
|
8
|
+
path: string;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
const hub = createRegistryHub();
|
|
12
12
|
|
|
13
|
-
hub.register<
|
|
14
|
-
|
|
13
|
+
hub.register<PaymentProvider>('payment-checkout-providers', {
|
|
14
|
+
id: 'payment-provider-stripe',
|
|
15
|
+
createCheckoutPath: (input) =>
|
|
16
|
+
`/providers/payment-provider-stripe/checkout?shop=${encodeURIComponent(input.shopId)}`,
|
|
17
|
+
});
|
|
18
|
+
hub.register<Shop>('shops', { id: 'shop-coffee', path: '/shops/coffee' });
|
|
15
19
|
|
|
16
|
-
console.log(hub.list<
|
|
17
|
-
// [{ id: '
|
|
20
|
+
console.log(hub.list<PaymentProvider>('payment-checkout-providers'));
|
|
21
|
+
// [{ id: 'payment-provider-stripe', createCheckoutPath: [Function] }]
|
|
18
22
|
|
|
19
|
-
console.log(hub.get<
|
|
20
|
-
// { id: '
|
|
23
|
+
console.log(hub.get<Shop>('shops', 'shop-coffee'));
|
|
24
|
+
// { id: 'shop-coffee', path: '/shops/coffee' }
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { createRegistry } from '@lorion-org/registry-hub';
|
|
2
2
|
import process from 'node:process';
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const shops = createRegistry('shops');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
{ id: '
|
|
8
|
-
{ id: '
|
|
6
|
+
shops.register([
|
|
7
|
+
{ id: 'shop-coffee', name: 'Bean Supply', path: '/shops/coffee' },
|
|
8
|
+
{ id: 'shop-stationery', name: 'Paper Desk', path: '/shops/stationery' },
|
|
9
9
|
]);
|
|
10
10
|
|
|
11
11
|
process.stdout.write(
|
|
12
|
-
`${
|
|
12
|
+
`${shops.id}:${shops
|
|
13
13
|
.entries()
|
|
14
14
|
.map(([id]) => id)
|
|
15
15
|
.join(',')}\n`,
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { createRegistry, type RegistryItem } from '@lorion-org/registry-hub';
|
|
2
2
|
|
|
3
|
-
type
|
|
4
|
-
|
|
3
|
+
type Shop = RegistryItem & {
|
|
4
|
+
name: string;
|
|
5
|
+
path: string;
|
|
5
6
|
};
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
+
const shops = createRegistry<Shop>('shops');
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
{ id: '
|
|
11
|
-
{ id: '
|
|
10
|
+
shops.register([
|
|
11
|
+
{ id: 'shop-coffee', name: 'Bean Supply', path: '/shops/coffee' },
|
|
12
|
+
{ id: 'shop-stationery', name: 'Paper Desk', path: '/shops/stationery' },
|
|
12
13
|
]);
|
|
13
14
|
|
|
14
|
-
console.log(
|
|
15
|
+
console.log(shops.entries());
|
|
15
16
|
// [
|
|
16
|
-
// ['
|
|
17
|
-
// ['
|
|
17
|
+
// ['shop-coffee', { id: 'shop-coffee', name: 'Bean Supply', path: '/shops/coffee' }],
|
|
18
|
+
// ['shop-stationery', { id: 'shop-stationery', name: 'Paper Desk', path: '/shops/stationery' }]
|
|
18
19
|
// ]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lorion-org/registry-hub",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Framework-free typed registry and registry-hub primitives.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -22,6 +22,10 @@
|
|
|
22
22
|
"types": "./dist/index.d.ts",
|
|
23
23
|
"exports": {
|
|
24
24
|
".": {
|
|
25
|
+
"lorion-source": {
|
|
26
|
+
"types": "./src/index.ts",
|
|
27
|
+
"default": "./src/index.ts"
|
|
28
|
+
},
|
|
25
29
|
"import": {
|
|
26
30
|
"types": "./dist/index.d.ts",
|
|
27
31
|
"default": "./dist/index.js"
|
|
@@ -35,7 +39,9 @@
|
|
|
35
39
|
"files": [
|
|
36
40
|
"dist",
|
|
37
41
|
"examples",
|
|
38
|
-
"LICENSE"
|
|
42
|
+
"LICENSE",
|
|
43
|
+
"src/**/*.ts",
|
|
44
|
+
"!src/**/*.spec.ts"
|
|
39
45
|
],
|
|
40
46
|
"keywords": [
|
|
41
47
|
"registry",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export type RegistryItem = {
|
|
2
|
+
id: string;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export type Registry<T extends RegistryItem = RegistryItem> = {
|
|
6
|
+
id: string;
|
|
7
|
+
get: (id: string) => T | undefined;
|
|
8
|
+
list: () => T[];
|
|
9
|
+
register: (items: T | T[]) => T[];
|
|
10
|
+
entries: () => Array<[string, T]>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type RegistryHub = {
|
|
14
|
+
createRegistry: <T extends RegistryItem>(id: string) => Registry<T>;
|
|
15
|
+
getRegistry: <T extends RegistryItem>(id: string) => Registry<T> | undefined;
|
|
16
|
+
register: <T extends RegistryItem>(registryId: string, items: T | T[]) => T[];
|
|
17
|
+
get: <T extends RegistryItem>(registryId: string, itemId: string) => T | undefined;
|
|
18
|
+
list: <T extends RegistryItem>(registryId: string) => T[];
|
|
19
|
+
entries: () => Array<[string, Registry<RegistryItem>]>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type AnyRegistry = Registry<RegistryItem>;
|
|
23
|
+
|
|
24
|
+
export function createRegistry<T extends RegistryItem>(id: string): Registry<T> {
|
|
25
|
+
const map: Map<string, T> = new Map();
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
id,
|
|
29
|
+
get: (itemId: string) => map.get(itemId),
|
|
30
|
+
list: () => Array.from(map.values()),
|
|
31
|
+
entries: () => Array.from(map.entries()),
|
|
32
|
+
register: (items: T | T[]) => {
|
|
33
|
+
const normalized: T[] = Array.isArray(items) ? items : [items];
|
|
34
|
+
|
|
35
|
+
for (const item of normalized) map.set(item.id, item);
|
|
36
|
+
|
|
37
|
+
return normalized;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createRegistryHub(): RegistryHub {
|
|
43
|
+
const registries: Map<string, AnyRegistry> = new Map();
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
createRegistry: <T extends RegistryItem>(id: string) => {
|
|
47
|
+
const existing: AnyRegistry | undefined = registries.get(id);
|
|
48
|
+
|
|
49
|
+
if (existing) return existing as unknown as Registry<T>;
|
|
50
|
+
|
|
51
|
+
const created: Registry<T> = createRegistry<T>(id);
|
|
52
|
+
registries.set(id, created as unknown as AnyRegistry);
|
|
53
|
+
|
|
54
|
+
return created;
|
|
55
|
+
},
|
|
56
|
+
getRegistry: <T extends RegistryItem>(id: string) =>
|
|
57
|
+
registries.get(id) as Registry<T> | undefined,
|
|
58
|
+
register: <T extends RegistryItem>(registryId: string, items: T | T[]) => {
|
|
59
|
+
const registry: Registry<T> =
|
|
60
|
+
(registries.get(registryId) as Registry<T> | undefined) ?? createRegistry<T>(registryId);
|
|
61
|
+
|
|
62
|
+
if (!registries.has(registryId))
|
|
63
|
+
registries.set(registryId, registry as unknown as AnyRegistry);
|
|
64
|
+
|
|
65
|
+
return registry.register(items);
|
|
66
|
+
},
|
|
67
|
+
get: <T extends RegistryItem>(registryId: string, itemId: string) => {
|
|
68
|
+
const registry: Registry<T> | undefined = registries.get(registryId) as
|
|
69
|
+
| Registry<T>
|
|
70
|
+
| undefined;
|
|
71
|
+
|
|
72
|
+
return registry?.get(itemId);
|
|
73
|
+
},
|
|
74
|
+
list: <T extends RegistryItem>(registryId: string) => {
|
|
75
|
+
const registry: Registry<T> | undefined = registries.get(registryId) as
|
|
76
|
+
| Registry<T>
|
|
77
|
+
| undefined;
|
|
78
|
+
|
|
79
|
+
return registry?.list() ?? [];
|
|
80
|
+
},
|
|
81
|
+
entries: () => Array.from(registries.entries()),
|
|
82
|
+
};
|
|
83
|
+
}
|