@abgov/adsp-sdk-mcp-server 0.0.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/README.md +51 -0
- package/assets/docs/architecture.md +79 -0
- package/assets/docs/getting-started.md +118 -0
- package/assets/docs/important-notes.md +47 -0
- package/assets/docs/index.md +9 -0
- package/assets/docs/platform/deployment.md +80 -0
- package/assets/docs/platform/getting-started.md +66 -0
- package/assets/docs/platform/operations.md +8 -0
- package/assets/docs/platform/platform-django-sdk.md +330 -0
- package/assets/docs/platform/platform-dotnet-sdk.md +457 -0
- package/assets/docs/platform/platform-flask-sdk.md +355 -0
- package/assets/docs/platform/platform-node-sdk.md +433 -0
- package/assets/docs/platform/platform-spring-sdk.md +344 -0
- package/assets/docs/platform/platform.md +8 -0
- package/assets/docs/services/access-service.md +75 -0
- package/assets/docs/services/agent-service.md +316 -0
- package/assets/docs/services/cache-service.md +61 -0
- package/assets/docs/services/calendar-service.md +86 -0
- package/assets/docs/services/comment-service.md +87 -0
- package/assets/docs/services/configuration-service.md +92 -0
- package/assets/docs/services/directory-service.md +57 -0
- package/assets/docs/services/event-service.md +122 -0
- package/assets/docs/services/feedback-service.md +152 -0
- package/assets/docs/services/file-service.md +164 -0
- package/assets/docs/services/form-service.md +149 -0
- package/assets/docs/services/notification-service.md +151 -0
- package/assets/docs/services/pdf-service.md +116 -0
- package/assets/docs/services/pii-service.md +45 -0
- package/assets/docs/services/push-service.md +106 -0
- package/assets/docs/services/services.md +6 -0
- package/assets/docs/services/status-service.md +29 -0
- package/assets/docs/services/task-service.md +139 -0
- package/assets/docs/services/tenant-service.md +18 -0
- package/assets/docs/services/token-handler.md +135 -0
- package/assets/docs/services/value-service.md +80 -0
- package/assets/docs/services/verify-service.md +61 -0
- package/assets/docs/tutorials/access-service/access-service.md +9 -0
- package/assets/docs/tutorials/access-service/adsp-clients.md +29 -0
- package/assets/docs/tutorials/access-service/application-clients.md +61 -0
- package/assets/docs/tutorials/access-service/front-end-access.md +53 -0
- package/assets/docs/tutorials/access-service/introduction.md +33 -0
- package/assets/docs/tutorials/access-service/tenants.md +68 -0
- package/assets/docs/tutorials/access-service/themes.md +43 -0
- package/assets/docs/tutorials/feedback-service/accessing-data.md +41 -0
- package/assets/docs/tutorials/feedback-service/feedback-service.md +9 -0
- package/assets/docs/tutorials/feedback-service/installation.md +142 -0
- package/assets/docs/tutorials/feedback-service/introduction.md +28 -0
- package/assets/docs/tutorials/feedback-service/security.md +40 -0
- package/assets/docs/tutorials/feedback-service/triggering.md +21 -0
- package/assets/docs/tutorials/file-service/file-service.md +9 -0
- package/assets/docs/tutorials/file-service/file-types.md +35 -0
- package/assets/docs/tutorials/file-service/introduction.md +34 -0
- package/assets/docs/tutorials/file-service/security.md +31 -0
- package/assets/docs/tutorials/file-service/working-with.md +65 -0
- package/assets/docs/tutorials/form-service/Integration.md +31 -0
- package/assets/docs/tutorials/form-service/ai-agent.md +450 -0
- package/assets/docs/tutorials/form-service/building-forms.md +264 -0
- package/assets/docs/tutorials/form-service/cheat-sheet.md +1461 -0
- package/assets/docs/tutorials/form-service/custom-error-messages.md +87 -0
- package/assets/docs/tutorials/form-service/data-registers.md +204 -0
- package/assets/docs/tutorials/form-service/file-uploader.md +81 -0
- package/assets/docs/tutorials/form-service/form-app.md +72 -0
- package/assets/docs/tutorials/form-service/form-editor.md +62 -0
- package/assets/docs/tutorials/form-service/form-service.md +9 -0
- package/assets/docs/tutorials/form-service/form-submissions.md +77 -0
- package/assets/docs/tutorials/form-service/instructions.md +173 -0
- package/assets/docs/tutorials/form-service/introduction.md +66 -0
- package/assets/docs/tutorials/form-service/repeated-items.md +87 -0
- package/assets/docs/tutorials/form-service/security.md +36 -0
- package/assets/docs/tutorials/form-service/steppers.md +237 -0
- package/assets/docs/tutorials/notification-service/emailAttachment.md +75 -0
- package/assets/docs/tutorials/notification-service/events.md +20 -0
- package/assets/docs/tutorials/notification-service/introduction.md +37 -0
- package/assets/docs/tutorials/notification-service/notification-service.md +9 -0
- package/assets/docs/tutorials/notification-service/notification-types.md +86 -0
- package/assets/docs/tutorials/notification-service/subscribers.md +33 -0
- package/assets/docs/tutorials/pdf-service/building-a-template.md +339 -0
- package/assets/docs/tutorials/pdf-service/generate-a-pdf.md +150 -0
- package/assets/docs/tutorials/pdf-service/handlebars.md +51 -0
- package/assets/docs/tutorials/pdf-service/introduction.md +26 -0
- package/assets/docs/tutorials/pdf-service/pdf-service.md +9 -0
- package/assets/docs/tutorials/pdf-service/puppeteer.md +25 -0
- package/assets/docs/tutorials/pdf-service/video.md +18 -0
- package/assets/docs/tutorials/status-service/Introduction.md +46 -0
- package/assets/docs/tutorials/status-service/health.md +51 -0
- package/assets/docs/tutorials/status-service/notices.md +34 -0
- package/assets/docs/tutorials/status-service/notifications.md +107 -0
- package/assets/docs/tutorials/status-service/status-service.md +9 -0
- package/assets/docs/tutorials/status-service/status.md +35 -0
- package/assets/docs/tutorials/status-service/webhooks.md +24 -0
- package/assets/docs/tutorials/task-service/introduction.md +23 -0
- package/assets/docs/tutorials/task-service/task-app.md +43 -0
- package/assets/docs/tutorials/task-service/task-definitions.md +49 -0
- package/assets/docs/tutorials/task-service/task-queues.md +67 -0
- package/assets/docs/tutorials/task-service/task-service.md +9 -0
- package/assets/docs/tutorials/tutorials.md +8 -0
- package/package.json +18 -0
- package/src/docs/docsRepository.d.ts +22 -0
- package/src/docs/docsRepository.js +110 -0
- package/src/docs/docsRepository.js.map +1 -0
- package/src/main.d.ts +2 -0
- package/src/main.js +16 -0
- package/src/main.js.map +1 -0
- package/src/sdk-reference/reference.d.ts +17 -0
- package/src/sdk-reference/reference.js +547 -0
- package/src/sdk-reference/reference.js.map +1 -0
- package/src/sdk-reference/search.d.ts +6 -0
- package/src/sdk-reference/search.js +41 -0
- package/src/sdk-reference/search.js.map +1 -0
- package/src/server.d.ts +6 -0
- package/src/server.js +30 -0
- package/src/server.js.map +1 -0
- package/src/tools/registerDocsTools.d.ts +3 -0
- package/src/tools/registerDocsTools.js +66 -0
- package/src/tools/registerDocsTools.js.map +1 -0
- package/src/tools/registerQuickstartTool.d.ts +2 -0
- package/src/tools/registerQuickstartTool.js +82 -0
- package/src/tools/registerQuickstartTool.js.map +1 -0
- package/src/tools/registerSdkTools.d.ts +2 -0
- package/src/tools/registerSdkTools.js +43 -0
- package/src/tools/registerSdkTools.js.map +1 -0
- package/src/tools/types.d.ts +12 -0
- package/src/tools/types.js +3 -0
- package/src/tools/types.js.map +1 -0
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
---
|
|
2
|
+
layout: page
|
|
3
|
+
title: Service SDK (Node.js)
|
|
4
|
+
nav_order: 3
|
|
5
|
+
parent: Platform development
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
<details open markdown="block">
|
|
9
|
+
<summary>
|
|
10
|
+
Table of contents
|
|
11
|
+
</summary>
|
|
12
|
+
{: .text-delta }
|
|
13
|
+
1. TOC
|
|
14
|
+
{:toc}
|
|
15
|
+
</details>
|
|
16
|
+
|
|
17
|
+
# ADSP service SDK (Node.js)
|
|
18
|
+
Platform services integrate into the foundational capabilities via a Software Development Kit (SDK). The SDK includes interfaces and utilities for handling tenancy, configurations, and registration. The same SDK can be used for development of tenant services.
|
|
19
|
+
|
|
20
|
+
Note that the SDK provides friendly interfaces on top of APIs. It is intended to speed up service development but is not the only way to access platform capabilities.
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm i @abgov/adsp-service-sdk
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Generating service
|
|
27
|
+
The ADSP project includes a [workspace generator](https://nx.dev/l/r/generators/workspace-generators) for creating new express based backend services. The base template includes usages of SDK capabilities and is a good starting point for understanding the SDK.
|
|
28
|
+
|
|
29
|
+
Generate a new service by running:
|
|
30
|
+
```bash
|
|
31
|
+
npx nx workspace-generator adsp-service
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The generate output includes sub-project structure and initial files under `/apps` as well as deployment manifests under `.openshift/managed` and `.compose`.
|
|
35
|
+
|
|
36
|
+
## Setting up a service account
|
|
37
|
+
<!-- tags: [keycloak] -->
|
|
38
|
+
The SDK requires credentials for a service account and uses this account for accessing platform capabilities. Fine grained configuration is possible and principle of least privilege should be applied.
|
|
39
|
+
|
|
40
|
+
In order to create the service account.
|
|
41
|
+
1. Create a confidential Client with a client ID in the format: `urn:ads:{tenant}:{service}` . The SDK does not authenticate end users and so all authentication grant types can be disabled.
|
|
42
|
+
2. Enable service account for the client.
|
|
43
|
+
3. In Service Account Roles, add the appropriate Client Roles for the capabilities that will be accessed:
|
|
44
|
+
1. Client `urn:ads:platform:tenant-service ` role `platform-service` is required.
|
|
45
|
+
2. Client `urn:ads:platform:configuration-service` role `configured-service` is needed for registration and accessing service specific configuration
|
|
46
|
+
3. Client `urn:ads:platform:event-service` role `event-sender` is needed for sending domain events.
|
|
47
|
+
4. Additional audiences in the service account access token are required for some capabilities:
|
|
48
|
+
1. Client `urn:ads:platform:push-service` needs to be include via an audience mapper for socket based configuration cache invalidation.
|
|
49
|
+
|
|
50
|
+
## Initializing the SDK
|
|
51
|
+
SDK capabilities are access via either the `initializePlatform` or the `initializeService` function. It takes service metadata as inputs and returns an object with the initialized platform interfaces and utilities.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { AdspId, initializePlatform } from '@abgov/adsp-service-sdk';
|
|
55
|
+
|
|
56
|
+
const serviceId = AdspId.parse(environment.CLIENT_ID);
|
|
57
|
+
const {
|
|
58
|
+
coreStrategy,
|
|
59
|
+
tenantStrategy,
|
|
60
|
+
...sdkCapabilities,
|
|
61
|
+
} = await initializePlatform(
|
|
62
|
+
{
|
|
63
|
+
displayName: 'My platform service',
|
|
64
|
+
description: 'Example of a platform service.',
|
|
65
|
+
serviceId,
|
|
66
|
+
accessServiceUrl: new URL(environment.KEYCLOAK_ROOT_URL),
|
|
67
|
+
clientSecret: environment.CLIENT_SECRET,
|
|
68
|
+
directoryUrl: new URL(environment.DIRECTORY_URL),
|
|
69
|
+
configurationSchema,
|
|
70
|
+
events: [],
|
|
71
|
+
roles: [],
|
|
72
|
+
notifications: [],
|
|
73
|
+
},
|
|
74
|
+
{ logger }
|
|
75
|
+
);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Authorizing requests
|
|
79
|
+
The SDK provides [Passport](https://www.passportjs.org/) strategies for verifying JWT bearer tokens in tenant and core realm requests.
|
|
80
|
+
|
|
81
|
+
Verify tenant bearer token:
|
|
82
|
+
```typescript
|
|
83
|
+
const {
|
|
84
|
+
tenantStrategy,
|
|
85
|
+
...sdkCapabilities,
|
|
86
|
+
} = await initializePlatform(parameters);
|
|
87
|
+
|
|
88
|
+
passport.use('tenant', tenantStrategy);
|
|
89
|
+
const authenticateHandler = passport.authenticate(['tenant'], { session: false });
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Using the core strategy
|
|
93
|
+
Core requests are used by platform services making requests to other platform services under a service account. However, users may have core accounts as well. Services should only use the core strategy when requests from a core context is expected, and should enforce role-based access controls on operations.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
const {
|
|
97
|
+
coreStrategy,
|
|
98
|
+
...sdkCapabilities,
|
|
99
|
+
} = await initializePlatform(parameters);
|
|
100
|
+
|
|
101
|
+
passport.use('core', coreStrategy);
|
|
102
|
+
const authenticateHandler = passport.authenticate(['core'], { session: false });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Service specific user roles
|
|
106
|
+
<!-- tags: [keycloak] -->
|
|
107
|
+
Keycloak issued tokens contain client roles nested under `realm_access`. Both tenant and core strategies flatten service specific roles from the token and qualifies roles related to other service clients with the client ID..
|
|
108
|
+
|
|
109
|
+
For example:
|
|
110
|
+
```json
|
|
111
|
+
{
|
|
112
|
+
"realm_access": { "roles": ["user"] },
|
|
113
|
+
"resource_access": {
|
|
114
|
+
"my-service": { "roles": ["my-user"] },
|
|
115
|
+
"other-service": { "roles": ["other-user"] }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
For my-service, the roles are mapped to roles:
|
|
121
|
+
- user
|
|
122
|
+
- my-user
|
|
123
|
+
- other-service:other-user
|
|
124
|
+
|
|
125
|
+
## Determining tenancy
|
|
126
|
+
Requests to platform services are in the context of a specific tenant with few exceptions. The context is implicit when a request is made with a tenant bearer token. It can be explicit in cases where an endpoint allows anonymous access or when a platform service makes a request to another platform service under a core service account.
|
|
127
|
+
|
|
128
|
+
The SDK provides a request handler that resolves implicit from user tenancy and explicit from a `tenantId` query parameter. Resolved tenant is set on the request object; no value is set if tenancy cannot be resolved.
|
|
129
|
+
|
|
130
|
+
Getting tenancy using the tenant request handler:
|
|
131
|
+
```typescript
|
|
132
|
+
const {
|
|
133
|
+
tenantHandler,
|
|
134
|
+
...sdkCapabilities
|
|
135
|
+
} = await initializePlatform(parameters);
|
|
136
|
+
|
|
137
|
+
app.use(
|
|
138
|
+
'/my-resource',
|
|
139
|
+
authenticateHandler,
|
|
140
|
+
tenantHandler,
|
|
141
|
+
(req, res) => { res.send(req.tenant) }
|
|
142
|
+
);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The handler uses the tenant service client to retrieve tenant information. This is also available from the SDK for direct use.
|
|
146
|
+
|
|
147
|
+
Getting tenant information using the tenant service:
|
|
148
|
+
```typescript
|
|
149
|
+
const {
|
|
150
|
+
tenantService,
|
|
151
|
+
...sdkCapabilities
|
|
152
|
+
} = await initializePlatform(parameters);
|
|
153
|
+
|
|
154
|
+
app.use(
|
|
155
|
+
'/my-resource',
|
|
156
|
+
authenticateHandler,
|
|
157
|
+
async (req, res) => {
|
|
158
|
+
const tenant = await tenantService.getTenant(req.user.tenantId);
|
|
159
|
+
res.send(tenant);
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Finding services
|
|
165
|
+
Service discovery in ADSP is handled using client side service discovery with a directory of services providing a register of available services.
|
|
166
|
+
|
|
167
|
+
Getting a service URL from the directory:
|
|
168
|
+
```typescript
|
|
169
|
+
const {
|
|
170
|
+
directory,
|
|
171
|
+
...sdkCapabilities,
|
|
172
|
+
} = await initializePlatform(parameters);
|
|
173
|
+
|
|
174
|
+
const serviceUrl = await directory.getServiceUrl(adspId`urn:ads:platform:event-service`);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Handling configuration
|
|
178
|
+
Platform services can make use of a common configuration service for managing configuration. The SDK allows services to define their configuration schema and access configuration.
|
|
179
|
+
|
|
180
|
+
Defining the configuration json schema:
|
|
181
|
+
```typescript
|
|
182
|
+
const {
|
|
183
|
+
configurationHandler,
|
|
184
|
+
configurationService,
|
|
185
|
+
...sdkCapabilities,
|
|
186
|
+
} = await initializePlatform({
|
|
187
|
+
configurationSchema: {
|
|
188
|
+
type: 'object',
|
|
189
|
+
properties: {
|
|
190
|
+
types: {
|
|
191
|
+
type: 'object',
|
|
192
|
+
additionalProperties: {
|
|
193
|
+
type: 'object',
|
|
194
|
+
properties: {
|
|
195
|
+
name: { type: 'string' },
|
|
196
|
+
description: { type: 'string' }
|
|
197
|
+
},
|
|
198
|
+
required: ['name'],
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
...parameters,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const serviceUrl = await directory.getServiceUrl(adspId`urn:ads:platform:event-service`);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Each service can have core configuration that applies across tenants and configuration specific to each tenant. The SDK provides a configuration request handler that will retrieve configuration in the request tenant context.
|
|
210
|
+
|
|
211
|
+
Getting configuration via the request:
|
|
212
|
+
```typescript
|
|
213
|
+
const {
|
|
214
|
+
configurationHandler,
|
|
215
|
+
...sdkCapabilities,
|
|
216
|
+
} = await initializePlatform(parameters);
|
|
217
|
+
|
|
218
|
+
app.use(
|
|
219
|
+
'/my-resource',
|
|
220
|
+
authenticateHandler,
|
|
221
|
+
tenantHandler,
|
|
222
|
+
configurationHandler,
|
|
223
|
+
async (req, res) => {
|
|
224
|
+
const [tenantConfig, coreConfig] = await req.getConfiguration<MyServiceConfiguration>();
|
|
225
|
+
res.send(tenantConfig);
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
```
|
|
229
|
+
The tenant context is based on `req.tenant` set by the tenant request handler when available and falls back to `req.user.tenantId`.
|
|
230
|
+
|
|
231
|
+
The handler uses configuration service client to retrieve configuration. This is also available from the SDK for direct use.
|
|
232
|
+
|
|
233
|
+
Getting configuration using the configuration service:
|
|
234
|
+
```typescript
|
|
235
|
+
const {
|
|
236
|
+
configurationService,
|
|
237
|
+
...sdkCapabilities,
|
|
238
|
+
} = await initializePlatform(parameters);
|
|
239
|
+
|
|
240
|
+
app.use(
|
|
241
|
+
'/my-resource',
|
|
242
|
+
authenticateHandler,
|
|
243
|
+
tenantHandler,
|
|
244
|
+
async (req, res) => {
|
|
245
|
+
const [tenantConfig, coreConfig] = await configurationService.getConfiguration<MyServiceConfiguration>(
|
|
246
|
+
serviceId,
|
|
247
|
+
accessToken,
|
|
248
|
+
tenantId,
|
|
249
|
+
);
|
|
250
|
+
res.send(tenantConfig);
|
|
251
|
+
}
|
|
252
|
+
);
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Converting configuration
|
|
256
|
+
Services may want to apply transformations on the retrieved configuration. The SDK allows services to provide functions for converting and combining core and tenant configuration. For example, services can use these to generate effective configuration when tenant overrides parts of core configuration.
|
|
257
|
+
|
|
258
|
+
Provide conversion functions:
|
|
259
|
+
```typescript
|
|
260
|
+
const {
|
|
261
|
+
configurationHandler,
|
|
262
|
+
configurationService,
|
|
263
|
+
...sdkCapabilities,
|
|
264
|
+
} = await initializePlatform({
|
|
265
|
+
configurationSchema,
|
|
266
|
+
configurationConverter: (config, tenantId) => new MyConfigurationEntity(config, tenantId),
|
|
267
|
+
combineConfiguration: (
|
|
268
|
+
tenantConfig: MyConfigurationEntity,
|
|
269
|
+
coreConfig: MyConfigurationEntity,
|
|
270
|
+
tenantId
|
|
271
|
+
) => tenantConfig.merge(coreConfig),
|
|
272
|
+
...parameters,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
app.use(
|
|
276
|
+
'/my-resource',
|
|
277
|
+
authenticateHandler,
|
|
278
|
+
tenantHandler,
|
|
279
|
+
configurationHandler,
|
|
280
|
+
async (req, res) => {
|
|
281
|
+
const effectiveConfig = await req.getConfiguration<MyServiceConfiguration, MyConfigurationEntity>();
|
|
282
|
+
res.send(effectiveConfig);
|
|
283
|
+
}
|
|
284
|
+
);
|
|
285
|
+
```
|
|
286
|
+
`configurationConverter` is called for both core and tenant configuration then the results are input into `combineConfiguration`.
|
|
287
|
+
|
|
288
|
+
## Registering event definitions, notification types, etc.
|
|
289
|
+
The SDK allows services to register configuration for some platform services.
|
|
290
|
+
|
|
291
|
+
- `roles` defines the client roles of the service. New tenant realms are created with a client that includes the roles specified here.
|
|
292
|
+
- `events` defines the domain events of the service.
|
|
293
|
+
- `notifications` defines the notification types of the service.
|
|
294
|
+
|
|
295
|
+
Defining configuration for other platform services:
|
|
296
|
+
```typescript
|
|
297
|
+
const {
|
|
298
|
+
...sdkCapabilities,
|
|
299
|
+
} = await initializePlatform({
|
|
300
|
+
roles: [{
|
|
301
|
+
name: 'my-service-admin',
|
|
302
|
+
description: 'Administrator role for my-service.',
|
|
303
|
+
inTenantAdmin: true,
|
|
304
|
+
}],
|
|
305
|
+
events: [{
|
|
306
|
+
name: 'intake-submitted',
|
|
307
|
+
description: 'Signalled when an intake is submitted',
|
|
308
|
+
payloadSchema: {
|
|
309
|
+
type: 'object',
|
|
310
|
+
properties: {
|
|
311
|
+
submitter: {
|
|
312
|
+
type: 'string'
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}],
|
|
317
|
+
notifications: [{
|
|
318
|
+
name: 'intake-updates',
|
|
319
|
+
description: 'Provides updates on intake application.',
|
|
320
|
+
publicSubscribe: false,
|
|
321
|
+
subscriberRoles: [],
|
|
322
|
+
channels: [Channel.email],
|
|
323
|
+
events: [{
|
|
324
|
+
namespace: 'my-service',
|
|
325
|
+
name: 'intake-submitted',
|
|
326
|
+
templates: {
|
|
327
|
+
[Channel.email]: {
|
|
328
|
+
subject: 'Intake submitted',
|
|
329
|
+
body: 'Hi {{ event.payload.submitter }}, Your intake was submitted.',
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}],
|
|
333
|
+
}],
|
|
334
|
+
...parameters,
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Additional utilities
|
|
339
|
+
The SDK provides several other useful utilities.
|
|
340
|
+
|
|
341
|
+
### ADSP ID
|
|
342
|
+
Utilities for handling ADSP URNs.
|
|
343
|
+
|
|
344
|
+
Parse and convert urns:
|
|
345
|
+
```typescript
|
|
346
|
+
import { adspId, AdspId } from '@abgov/adsp-service-sdk';
|
|
347
|
+
|
|
348
|
+
const dynamicAdspId = AdspId.parse(dynamicId);
|
|
349
|
+
const staticAdspId = adspId`urn:ads:platform:task-service`;
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### Role-based authorization
|
|
353
|
+
The SDK provides an `authorize` middleware factory for enforcing role-based access control on Express routes. It uses the roles resolved from the JWT bearer token by the Passport strategies, so role names match what is configured in the Keycloak client.
|
|
354
|
+
|
|
355
|
+
Protecting a route with a required role:
|
|
356
|
+
```typescript
|
|
357
|
+
import { authorize } from '@abgov/adsp-service-sdk';
|
|
358
|
+
|
|
359
|
+
app.post(
|
|
360
|
+
'/my-resource',
|
|
361
|
+
authenticateHandler,
|
|
362
|
+
tenantHandler,
|
|
363
|
+
authorize('my-service-admin'),
|
|
364
|
+
(req, res) => { res.send('OK') }
|
|
365
|
+
);
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
When the authenticated user lacks the required role, `authorize` passes an `UnauthorizedUserError` to Express's `next()`, which `createErrorHandler` maps to a 403 response automatically.
|
|
369
|
+
|
|
370
|
+
Multiple roles can be specified; the user must have at least one:
|
|
371
|
+
```typescript
|
|
372
|
+
app.delete(
|
|
373
|
+
'/my-resource/:id',
|
|
374
|
+
authenticateHandler,
|
|
375
|
+
authorize('my-service-admin', 'my-service-editor'),
|
|
376
|
+
deleteHandler,
|
|
377
|
+
);
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### Error handling
|
|
381
|
+
The SDK provides a `createErrorHandler` factory that normalizes error responses across GoA services. It recognizes `GoAError` subclasses (including `UnauthorizedUserError`) and maps their `statusCode` to the HTTP response status. Internal server errors (5xx) have their message masked in production to prevent information disclosure.
|
|
382
|
+
|
|
383
|
+
Mount `createErrorHandler` last in the Express middleware chain:
|
|
384
|
+
```typescript
|
|
385
|
+
import { createErrorHandler } from '@abgov/adsp-service-sdk';
|
|
386
|
+
|
|
387
|
+
const {
|
|
388
|
+
logger,
|
|
389
|
+
...sdkCapabilities,
|
|
390
|
+
} = await initializePlatform(parameters);
|
|
391
|
+
|
|
392
|
+
// ... routes and other middleware ...
|
|
393
|
+
|
|
394
|
+
app.use(createErrorHandler(logger));
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
When a logger is provided, errors are logged with the HTTP status code, request path and method, and correlation ID from the `x-correlation-id` request header.
|
|
398
|
+
|
|
399
|
+
Throwing a typed error from a route handler:
|
|
400
|
+
```typescript
|
|
401
|
+
import { GoAError } from '@abgov/adsp-service-sdk';
|
|
402
|
+
|
|
403
|
+
app.get('/my-resource/:id', authenticateHandler, authorize('my-service-user'), async (req, res) => {
|
|
404
|
+
const resource = await findResource(req.params.id);
|
|
405
|
+
if (!resource) {
|
|
406
|
+
throw new GoAError('Resource not found', { statusCode: 404 });
|
|
407
|
+
}
|
|
408
|
+
res.json(resource);
|
|
409
|
+
});
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### Platform health check
|
|
413
|
+
Include platform service checks in the service health check endpoint:
|
|
414
|
+
```typescript
|
|
415
|
+
const {
|
|
416
|
+
healthCheck,
|
|
417
|
+
...sdkCapabilities,
|
|
418
|
+
} = await initializePlatform(parameters);
|
|
419
|
+
|
|
420
|
+
app.get('/health', async (_req, res) => {
|
|
421
|
+
const platform = await healthCheck();
|
|
422
|
+
res.json(platform);
|
|
423
|
+
});
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
### Logging
|
|
427
|
+
Import a standard logger for platform services:
|
|
428
|
+
```typescript
|
|
429
|
+
const {
|
|
430
|
+
logger,
|
|
431
|
+
...sdkCapabilities,
|
|
432
|
+
} = await initializePlatform(parameters);
|
|
433
|
+
```
|