@domain-first/project-structure 2.18.1 → 3.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 CHANGED
@@ -28,7 +28,7 @@ The full technical specification lives in [API.md](./API.md).
28
28
 
29
29
  ### How to initialize a project
30
30
 
31
- Run `df-ps` in a project with a `package.json`. The first run asks for the source folder, the `@domain-first` packages you use, the default persistence implementation, React, Storybook and the testing library, then writes `domain-first.project-structure.config.json` and exits.
31
+ Run `df-ps` in a project with a `package.json`. The first run asks for the source folder, the `@domain-first` packages you use, the default persistence implementation, React, Storybook and the testing library, then writes `domain-first.project-structure.config.json` and exits. With `@domain-first/handlers-rest`, it also scaffolds the [endpoint generator](#how-to-add-a-rest-endpoint).
32
32
 
33
33
  ```
34
34
  npx df-ps
@@ -153,7 +153,41 @@ With `@domain-first/handlers-rest`, choose **Presentation → New Handlers-REST
153
153
  src/bounded-contexts/sales/presentation/rest/orders/recent/GET.ts // GET /sales/orders/recent
154
154
  ```
155
155
 
156
- With wiring, the endpoint takes an endpoint generator and a handler you pick from the context, and all endpoints are collected as `restHandlers` in `src/presentation/rest/index.ts`. Connect them to your HTTP server.
156
+ Every endpoint uses the one endpoint generator of the project, `RestEndpointGeneratorPresentationService` in `src/shared/presentation/services/`. Initialization scaffolds it as a regular Shared Layer presentation service (class, spec and wire):
157
+
158
+ ```ts
159
+ export class RestEndpointGeneratorPresentationService {
160
+ get withoutGuards(): EndpointGenerator {
161
+ return createEndpoint({
162
+ errorStatuses: {
163
+ 400: {
164
+ checks: this.invalidRequestChecks,
165
+ description: 'Invalid request'
166
+ }
167
+ }
168
+ })(this.adapter) as EndpointGenerator
169
+ }
170
+
171
+ /**
172
+ * Default checks of an invalid request, to spread into a generator's
173
+ * own checks.
174
+ */
175
+ private get invalidRequestChecks(): ErrorStatuses[number]['checks'] {
176
+ return [RequestParsingError.is, InputMappingError.is]
177
+ }
178
+
179
+ /**
180
+ * The adapter of your HTTP framework.
181
+ */
182
+ private get adapter(): Adapter<any[], any> {
183
+ throw new Error('REST adapter is not set')
184
+ }
185
+ }
186
+ ```
187
+
188
+ Set `adapter` to the adapter of your HTTP framework. Each generating method is a getter returning `EndpointGenerator`: add one per kind of endpoint (for example `withAuth`, with its `context` and error statuses). Reuse the default checks there and add your own: `checks: [...this.invalidRequestChecks, MyError.is]`. A new endpoint calls `this.endpointGenerator(...)` and doesn't compile on purpose: choose the getter yourself in its `get handle()`, e.g. `this.endpointGenerator.withoutGuards(...)`.
189
+
190
+ With wiring, the endpoint takes the generator and a handler you pick from the context's queries, commands and use cases, and all endpoints are collected as `restHandlers` in `src/presentation/rest/index.ts`. Connect them to your HTTP server.
157
191
 
158
192
  ### How to scaffold React modules
159
193