@danqiusheng/nest-nacos 1.0.0 → 1.2.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/CHANGELOG.md +22 -0
- package/LICENSE +21 -21
- package/README.en.md +197 -0
- package/README.md +353 -385
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/nacos-config.service.d.ts +25 -45
- package/dist/nacos-config.service.d.ts.map +1 -1
- package/dist/nacos-config.service.js +241 -102
- package/dist/nacos-config.service.js.map +1 -1
- package/dist/nacos-naming.service.d.ts +16 -46
- package/dist/nacos-naming.service.d.ts.map +1 -1
- package/dist/nacos-naming.service.js +119 -88
- package/dist/nacos-naming.service.js.map +1 -1
- package/dist/nacos.decorators.d.ts +0 -4
- package/dist/nacos.decorators.d.ts.map +1 -1
- package/dist/nacos.decorators.js +1 -12
- package/dist/nacos.decorators.js.map +1 -1
- package/dist/nacos.health.d.ts +2 -6
- package/dist/nacos.health.d.ts.map +1 -1
- package/dist/nacos.health.js +5 -25
- package/dist/nacos.health.js.map +1 -1
- package/dist/nacos.interfaces.d.ts +85 -57
- package/dist/nacos.interfaces.d.ts.map +1 -1
- package/dist/nacos.module.d.ts +2 -11
- package/dist/nacos.module.d.ts.map +1 -1
- package/dist/nacos.module.js +22 -19
- package/dist/nacos.module.js.map +1 -1
- package/dist/nacos.providers.d.ts +4 -11
- package/dist/nacos.providers.d.ts.map +1 -1
- package/dist/nacos.providers.js +37 -23
- package/dist/nacos.providers.js.map +1 -1
- package/dist/nacos.utils.d.ts +6 -0
- package/dist/nacos.utils.d.ts.map +1 -0
- package/dist/nacos.utils.js +79 -0
- package/dist/nacos.utils.js.map +1 -0
- package/package.json +19 -10
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.2.0 (unreleased)
|
|
4
|
+
|
|
5
|
+
- Add shared top-level `serverAddr`, `namespace`, `username`, `password`, and
|
|
6
|
+
`ssl` options so Naming and Config no longer require duplicate connection
|
|
7
|
+
settings.
|
|
8
|
+
- Add `NacosModule.forRoot('host:8848')` as the smallest setup form.
|
|
9
|
+
- Expand the Chinese and English usage documentation.
|
|
10
|
+
|
|
11
|
+
## 1.1.0
|
|
12
|
+
|
|
13
|
+
- Fix asynchronous server status and health checks.
|
|
14
|
+
- Fix duplicate Config callbacks and SDK listener identity during unsubscribe.
|
|
15
|
+
- Fix Naming unsubscribe-all behavior.
|
|
16
|
+
- Close both SDK clients during module shutdown.
|
|
17
|
+
- Fix `forRootAsync({ useClass })` and export raw client tokens.
|
|
18
|
+
- Normalize the public Config namespace to the empty namespace ID.
|
|
19
|
+
- Add JSON, YAML, Properties and TXT parsing, preload merge, hot-update state,
|
|
20
|
+
local fallback cache, startup failure modes and retries.
|
|
21
|
+
- Add random, round-robin and weighted discovery with metadata filtering.
|
|
22
|
+
- Remove false Nacos 3.x and gRPC compatibility claims.
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 丹丘生
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 丹丘生
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# @danqiusheng/nest-nacos
|
|
2
|
+
|
|
3
|
+
Nacos 2.x configuration and service-discovery integration for NestJS 10/11.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @danqiusheng/nest-nacos nacos
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Minimal setup
|
|
12
|
+
|
|
13
|
+
One address enables both Naming and Config clients:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
NacosModule.forRoot('127.0.0.1:8848');
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Authentication and a custom namespace are still configured once:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
NacosModule.forRoot({
|
|
23
|
+
serverAddr: '127.0.0.1:8848',
|
|
24
|
+
namespace: 'production',
|
|
25
|
+
username: process.env.NACOS_USERNAME,
|
|
26
|
+
password: process.env.NACOS_PASSWORD,
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The public namespace and `DEFAULT_GROUP` are used by default. `serverAddr`
|
|
31
|
+
also accepts an array of server addresses.
|
|
32
|
+
|
|
33
|
+
## Async setup
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
NacosModule.forRootAsync({
|
|
37
|
+
inject: [ConfigService],
|
|
38
|
+
useFactory: (config: ConfigService) => ({
|
|
39
|
+
serverAddr: config.getOrThrow<string>('NACOS_SERVER_ADDR'),
|
|
40
|
+
namespace: config.get<string>('NACOS_NAMESPACE') ?? 'public',
|
|
41
|
+
username: config.get<string>('NACOS_USERNAME'),
|
|
42
|
+
password: config.get<string>('NACOS_PASSWORD'),
|
|
43
|
+
}),
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`useFactory`, `useClass`, and `useExisting` are supported.
|
|
48
|
+
|
|
49
|
+
## Automatic registration
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
NacosModule.forRoot({
|
|
53
|
+
serverAddr: '127.0.0.1:8848',
|
|
54
|
+
instances: [
|
|
55
|
+
{
|
|
56
|
+
serviceName: 'orders',
|
|
57
|
+
ip: '10.0.0.5',
|
|
58
|
+
port: 3000,
|
|
59
|
+
metadata: { environment: 'production' },
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Plugin-managed instances are deregistered when the Nest module is destroyed.
|
|
66
|
+
|
|
67
|
+
## Service discovery
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
constructor(
|
|
71
|
+
@InjectNacosNaming()
|
|
72
|
+
private readonly naming: NacosNamingService,
|
|
73
|
+
) {}
|
|
74
|
+
|
|
75
|
+
const instance = await this.naming.selectInstance('payments', {
|
|
76
|
+
strategy: 'roundRobin', // random | roundRobin | weighted
|
|
77
|
+
metadata: { environment: 'production' },
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Manual registration and subscriptions are also available:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
await this.naming.register(instanceOptions);
|
|
85
|
+
await this.naming.deregister(instanceOptions);
|
|
86
|
+
|
|
87
|
+
this.naming.subscribe('payments', listener);
|
|
88
|
+
this.naming.unSubscribe('payments', listener);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Configuration center
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
constructor(
|
|
95
|
+
@InjectNacosConfig()
|
|
96
|
+
private readonly nacos: NacosConfigService,
|
|
97
|
+
) {}
|
|
98
|
+
|
|
99
|
+
const raw = await this.nacos.getConfig('application.yaml');
|
|
100
|
+
const parsed = await this.nacos.getParsedConfig<AppConfig>(
|
|
101
|
+
'application.yaml',
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
this.nacos.subscribe(
|
|
105
|
+
'application.yaml',
|
|
106
|
+
'DEFAULT_GROUP',
|
|
107
|
+
(content, value) => console.log(value),
|
|
108
|
+
);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
JSON, YAML, Properties, and text are supported. Each `dataId + group` uses one
|
|
112
|
+
underlying SDK subscription, regardless of the number of business listeners.
|
|
113
|
+
|
|
114
|
+
Publish and remove configuration with:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
await this.nacos.publish('application.json', 'DEFAULT_GROUP', content);
|
|
118
|
+
await this.nacos.remove('application.json', 'DEFAULT_GROUP');
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Preload and fallback cache
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
NacosModule.forRoot({
|
|
125
|
+
serverAddr: '127.0.0.1:8848',
|
|
126
|
+
subscribeConfigs: [
|
|
127
|
+
{ dataId: 'application.yaml', format: 'yaml' },
|
|
128
|
+
{ dataId: 'database.properties', format: 'properties' },
|
|
129
|
+
],
|
|
130
|
+
configLoading: {
|
|
131
|
+
failureMode: 'fallback', // throw | fallback | continue
|
|
132
|
+
cacheDir: '.nacos-cache',
|
|
133
|
+
retry: { attempts: 3, delayMs: 300 },
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const merged = await this.nacos.getMergedConfig<AppConfig>();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Successful remote loads update cache files atomically. In `fallback` mode the
|
|
141
|
+
cache is used when Nacos is unavailable.
|
|
142
|
+
|
|
143
|
+
## Advanced client-specific options
|
|
144
|
+
|
|
145
|
+
Nested options are only needed when Naming and Config use different settings:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
NacosModule.forRoot({
|
|
149
|
+
naming: {
|
|
150
|
+
serverList: 'naming.example:8848',
|
|
151
|
+
namespace: 'services',
|
|
152
|
+
},
|
|
153
|
+
config: {
|
|
154
|
+
serverAddr: 'config.example:8848',
|
|
155
|
+
namespace: 'configuration',
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Nested fields override shared top-level fields. Supplying only `naming` or
|
|
161
|
+
only `config` enables only that client.
|
|
162
|
+
|
|
163
|
+
## Health and raw clients
|
|
164
|
+
|
|
165
|
+
Both health APIs are asynchronous:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
await this.naming.getServerStatus();
|
|
169
|
+
await this.healthIndicator.isHealthy();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Use `@InjectNacosNamingClient()` and `@InjectNacosConfigClient()` only when an
|
|
173
|
+
underlying SDK API is not exposed by the plugin. The module owns and closes
|
|
174
|
+
both clients.
|
|
175
|
+
|
|
176
|
+
## Compatibility
|
|
177
|
+
|
|
178
|
+
| Component | Supported |
|
|
179
|
+
|---|---|
|
|
180
|
+
| Node.js | >= 16 |
|
|
181
|
+
| NestJS | 10.x, 11.x |
|
|
182
|
+
| Nacos Node SDK | `nacos` 2.6.x |
|
|
183
|
+
| Nacos Server | 2.x; tested against 2.5.3 |
|
|
184
|
+
| Nacos Server 3.x / gRPC | Not supported |
|
|
185
|
+
|
|
186
|
+
The underlying SDK implements the HTTP OpenAPI, not the Nacos 3.x gRPC
|
|
187
|
+
protocol. The plugin rejects `transport: 'grpc'` instead of silently falling
|
|
188
|
+
back. Java is required only to run a local Nacos Server, not by the NestJS
|
|
189
|
+
plugin itself.
|
|
190
|
+
|
|
191
|
+
## Verify
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
npm ci
|
|
195
|
+
npm run build
|
|
196
|
+
npm test
|
|
197
|
+
```
|