@domain-first/project-structure 4.2.0 → 4.2.1

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
@@ -164,30 +164,15 @@ Every endpoint uses the one endpoint generator of the project, `RestEndpointGene
164
164
  const adapter = undefined as unknown as Adapter<any[], any>
165
165
 
166
166
  export class RestEndpointGeneratorPresentationService {
167
- /**
168
- * Default errors of a client request, to spread into a generator's
169
- * own error mappings.
170
- */
171
- private get defaultClientRequestErrors(): ErrorMapping[] {
172
- return [
173
- httpStatus(400, {
174
- code: 'INVALID_REQUEST',
175
- description: 'Invalid request',
176
- message: (error) => error.message,
177
- match: [RequestParsingError, InputMappingError]
178
- })
179
- ]
180
- }
181
-
182
167
  get withoutGuards(): EndpointGenerator {
183
168
  return createEndpoint({
184
- errors: this.defaultClientRequestErrors
169
+ errors: [invalidRequest()]
185
170
  })(adapter) as EndpointGenerator
186
171
  }
187
172
  }
188
173
  ```
189
174
 
190
- Set the module-level `adapter` constant above the class to the adapter of your HTTP framework; the class uses it through closure. If you connected the Next adapter during initialization, the file imports `nextAdapter` from `@domain-first/handlers-next` instead, with nothing to set. Each generating method is a getter returning `EndpointGenerator`: add one per kind of endpoint (for example `withAuth`, with its `context` and error mappings). Reuse the default mappings there and add your own: `errors: [httpError(MyError, 404), ...this.defaultClientRequestErrors]`. 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(...)`.
175
+ Set the module-level `adapter` constant above the class to the adapter of your HTTP framework; the class uses it through closure. If you connected the Next adapter during initialization, the file imports `nextAdapter` from `@domain-first/handlers-next` instead, with nothing to set. Each generating method is a getter returning `EndpointGenerator`: add one per kind of endpoint (for example `withAuth`, with its `context` and error mappings). Keep `invalidRequest()` of `@domain-first/handlers-rest` there for invalid requests (`400`) and add your own mappings: `errors: [httpError(MyError, 404), invalidRequest()]`. 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(...)`.
191
176
 
192
177
  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.
193
178
 
package/dist/201.js CHANGED
@@ -40,7 +40,8 @@ const defaultDependencies = {
40
40
  'application-ports': infrastructure,
41
41
  'application-services': [
42
42
  ...domain,
43
- 'application-ports'
43
+ 'application-ports',
44
+ ...commandsAndQueries
44
45
  ],
45
46
  'use-cases': [
46
47
  ...domain,
package/dist/index.cjs CHANGED
@@ -69,7 +69,8 @@ const defaultDependencies = {
69
69
  'application-ports': infrastructure,
70
70
  'application-services': [
71
71
  ...domain,
72
- 'application-ports'
72
+ 'application-ports',
73
+ ...commandsAndQueries
73
74
  ],
74
75
  'use-cases': [
75
76
  ...domain,
@@ -2129,11 +2130,8 @@ const scaffoldRestEndpointGenerator = (sharedFolder, { withNextAdapter = false }
2129
2130
  return `
2130
2131
  import {
2131
2132
  ${withNextAdapter ? '' : ' type Adapter,\n'} type EndpointGenerator,
2132
- type ErrorMapping,
2133
2133
  createEndpoint,
2134
- httpStatus,
2135
- InputMappingError,
2136
- RequestParsingError
2134
+ invalidRequest
2137
2135
  } from '@domain-first/handlers-rest'
2138
2136
  ${withNextAdapter ? "import { nextAdapter } from '@domain-first/handlers-next'" : `
2139
2137
  /**
@@ -2142,24 +2140,9 @@ ${withNextAdapter ? "import { nextAdapter } from '@domain-first/handlers-next'"
2142
2140
  const adapter = undefined as unknown as Adapter<any[], any>`}
2143
2141
 
2144
2142
  export class ${className} {
2145
- /**
2146
- * Default errors of a client request, to spread into a generator's
2147
- * own error mappings.
2148
- */
2149
- private get defaultClientRequestErrors(): ErrorMapping[] {
2150
- return [
2151
- httpStatus(400, {
2152
- code: 'INVALID_REQUEST',
2153
- description: 'Invalid request',
2154
- message: (error) => error.message,
2155
- match: [RequestParsingError, InputMappingError]
2156
- })
2157
- ]
2158
- }
2159
-
2160
2143
  get withoutGuards(): EndpointGenerator {
2161
2144
  return createEndpoint({
2162
- errors: this.defaultClientRequestErrors
2145
+ errors: [invalidRequest()]
2163
2146
  })(${adapter}) as EndpointGenerator
2164
2147
  }
2165
2148
  }
package/dist/index.js CHANGED
@@ -1429,11 +1429,8 @@ const scaffoldRestEndpointGenerator = (sharedFolder, { withNextAdapter = false }
1429
1429
  return `
1430
1430
  import {
1431
1431
  ${withNextAdapter ? '' : ' type Adapter,\n'} type EndpointGenerator,
1432
- type ErrorMapping,
1433
1432
  createEndpoint,
1434
- httpStatus,
1435
- InputMappingError,
1436
- RequestParsingError
1433
+ invalidRequest
1437
1434
  } from '@domain-first/handlers-rest'
1438
1435
  ${withNextAdapter ? "import { nextAdapter } from '@domain-first/handlers-next'" : `
1439
1436
  /**
@@ -1442,24 +1439,9 @@ ${withNextAdapter ? "import { nextAdapter } from '@domain-first/handlers-next'"
1442
1439
  const adapter = undefined as unknown as Adapter<any[], any>`}
1443
1440
 
1444
1441
  export class ${className} {
1445
- /**
1446
- * Default errors of a client request, to spread into a generator's
1447
- * own error mappings.
1448
- */
1449
- private get defaultClientRequestErrors(): ErrorMapping[] {
1450
- return [
1451
- httpStatus(400, {
1452
- code: 'INVALID_REQUEST',
1453
- description: 'Invalid request',
1454
- message: (error) => error.message,
1455
- match: [RequestParsingError, InputMappingError]
1456
- })
1457
- ]
1458
- }
1459
-
1460
1442
  get withoutGuards(): EndpointGenerator {
1461
1443
  return createEndpoint({
1462
- errors: this.defaultClientRequestErrors
1444
+ errors: [invalidRequest()]
1463
1445
  })(${adapter}) as EndpointGenerator
1464
1446
  }
1465
1447
  }
package/dist/lint.cjs CHANGED
@@ -69,7 +69,8 @@ const defaultDependencies = {
69
69
  'application-ports': infrastructure,
70
70
  'application-services': [
71
71
  ...domain,
72
- 'application-ports'
72
+ 'application-ports',
73
+ ...commandsAndQueries
73
74
  ],
74
75
  'use-cases': [
75
76
  ...domain,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@domain-first/project-structure",
3
3
  "description": "Build and manage your Domain-First project structure with CLI",
4
- "version": "4.2.0",
4
+ "version": "4.2.1",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -86,7 +86,7 @@ export const wirePlaceOrderUseCase = wireClass(PlaceOrderUseCase, [wireOrderRepo
86
86
 
87
87
  ## REST endpoints ({handlers-rest})
88
88
 
89
- 1. Generator: initialization creates `RestEndpointGeneratorPresentationService` in `<rootFolder>/shared/presentation/services/`; every endpoint uses it. Set the module-level `const adapter` above the class (`undefined` until set) to the HTTP framework's adapter; the class reads it through closure. If initialization connected the Next adapter, the file already imports `nextAdapter` from `@domain-first/handlers-next` and passes it instead; just make sure that package is installed. The default `400` mapping lives in the private `defaultClientRequestErrors` getter (`ErrorMapping[]`); extend it with `errors: [httpError(MyError, 404), ...this.defaultClientRequestErrors]`. Map domain errors to statuses in the generator's `errors` with `httpError(ErrorClass, status, { description, message, details: { schema, map } })` (one class) or `httpStatus(status, { code, match })` (many errors, one code); the first match wins, so put specific mappings first, importing them from the contexts' `domain/errors` (the only thing `shared/` may import from a context, besides domain models in the in-memory DB). A new kind of endpoint (auth, other context) is a new getter returning `createEndpoint({ context, errors })(adapter) as EndpointGenerator` (`nextAdapter` with the Next adapter), next to `withoutGuards`. Generate the exposed handler before the endpoint: with wire, the `Handler:` pick fills its type and the wire (`[wireRestEndpointGeneratorPresentationService, wireGetRecentOrdersUseCase]`).
89
+ 1. Generator: initialization creates `RestEndpointGeneratorPresentationService` in `<rootFolder>/shared/presentation/services/`; every endpoint uses it. Set the module-level `const adapter` above the class (`undefined` until set) to the HTTP framework's adapter; the class reads it through closure. If initialization connected the Next adapter, the file already imports `nextAdapter` from `@domain-first/handlers-next` and passes it instead; just make sure that package is installed. Invalid requests (`400`) are answered by `invalidRequest()` of `@domain-first/handlers-rest` in `errors`; don't map `RequestParsingError` / `InputMappingError` by hand, add your own mappings next to it: `errors: [httpError(MyError, 404), invalidRequest()]`. Map domain errors to statuses in the generator's `errors` with `httpError(ErrorClass, status, { description, message, details: { schema, map } })` (one class) or `httpStatus(status, { code, match })` (many errors, one code); the first match wins, so put specific mappings first, importing them from the contexts' `domain/errors` (the only thing `shared/` may import from a context, besides domain models in the in-memory DB). A new kind of endpoint (auth, other context) is a new getter returning `createEndpoint({ context, errors: [..., invalidRequest()] })(adapter) as EndpointGenerator` (`nextAdapter` with the Next adapter), next to `withoutGuards`. Generate the exposed handler before the endpoint: with wire, the `Handler:` pick fills its type and the wire (`[wireRestEndpointGeneratorPresentationService, wireGetRecentOrdersUseCase]`).
90
90
  2. If `handler: never` is left (no handler existed at generation), re-run the endpoint generator as an approved overwrite to pick it (rule 3); don't replace it by hand.
91
91
  3. Complete `get handle()`: the generated `this.endpointGenerator(...)` doesn't compile on purpose. Pick the generating getter that fits the endpoint (`this.endpointGenerator.withoutGuards(...)`, or one with the needed guards), then `.input(...)`, `.mapInput(...)`, optional `.output(...)`, `.mapOutput(...)`.
92
92
  4. Register `restHandlers` from `<rootFolder>/presentation/rest/index.ts` in the server bootstrap (rule 2, exception 5).
@@ -21,7 +21,7 @@ Split the request into pieces of logic (one responsibility each). For each piece
21
21
  | 11 | One read/write on this context's own storage that isn't "load/save one aggregate" (read model, report, bulk update)? | **Execution command** (write) / **query** (read) |
22
22
  | 12 | A reusable read/write composed of other commands/queries? | **Orchestrator command** (execution commands and queries) / **query** (execution queries and repositories only, calling only their reading methods) |
23
23
  | 13 | A rule at the junction of several entities / value objects not all belonging to one aggregate (a discount from purchase history and the current discount), no I/O? | **Domain service**, always |
24
- | 14 | Application logic reused by 2+ use cases, with any external interaction delegated to injected ports? | **Application service** |
24
+ | 14 | Application logic reused by 2+ use cases, with any external interaction or storage access delegated to injected ports, commands or queries? | **Application service** |
25
25
  | 15 | A technical helper used only by infrastructure code? | **Infrastructure service** |
26
26
  | 16 | A helper used only by presentation code? | **Presentation service** |
27
27
  | — | None of the above | A method or function in an existing file |
@@ -36,7 +36,7 @@ Split the request into pieces of logic (one responsibility each). For each piece
36
36
  ## Port or service?
37
37
 
38
38
  - A **port** describes a capability at the boundary: an external system or time. Its adapter performs the actual interaction. Own storage still follows the repository / execution command / query rules above. Another context is not a port: it's an execution query/command (step 9).
39
- - An **application service** contains reusable application logic and may call injected ports. It does not call external SDKs, HTTP clients, or another context directly. A domain service remains pure: no I/O, ports, or repositories.
39
+ - An **application service** contains reusable application logic and may call injected ports, commands and queries. It does not call external SDKs, HTTP clients, or another context directly. A domain service remains pure: no I/O, ports, or repositories.
40
40
  - Wanting to mock a class or imagining multiple implementations is not enough to make it a port. Identify the boundary capability first. Ports get swappable adapters and a test implementation under `NODE_ENV=test`; test a service's logic with fake dependencies.
41
41
 
42
42
  Example: `order-confirmation` prepares a message and calls the injected `mailer` port. It stays an application service; the mailer adapter calls the email provider. Tests use a fake mailer and exercise the real service.
@@ -52,6 +52,7 @@ Example: `order-confirmation` prepares a message and calls the injected `mailer`
52
52
  | Domain / application service | Stated without requests, permissions, responses, scenarios → domain. Otherwise application. |
53
53
  | Use case / application service | Use case = one action with `handle`. Application service = helper used by use cases. |
54
54
  | Use case / orchestrator | Use case = action exposed outside. Orchestrator = reusable operation called by use cases. |
55
+ | Orchestrator / application service | Only composes commands/queries into one reusable storage operation → orchestrator. Also needs ports or application logic beyond that composition → application service (it may call commands and queries, orchestrators included). |
55
56
  | Application / infrastructure service | Used by use cases → application. Only by adapters/repositories → infrastructure. |
56
57
  | Logic infrastructure needs from an application service | Infrastructure never sees application services. Pure logic without ports → domain service. Logic over ports → infrastructure calls the ports directly. Technical (mapping, retries, external API formats) → infrastructure service. |
57
58
  | Clock | Always a port (`clock`, usually in Shared Layer). |
@@ -6,7 +6,7 @@ Every step list starts with `"pick:Bounded Context: <ctx>"` or `"pick:Shared Lay
6
6
 
7
7
  Any generator: if the run would replace existing files, the last prompt is the confirm `Overwrite them?` (the files are listed above it). Without it, nothing extra is asked. Added lines (barrels, errors, wiring indexes) never trigger it.
8
8
 
9
- {wire} Generators that create a wire (all below except endpoints, which ask their own two selects) then ask the checkbox `Dependencies:`, before `Overwrite them?`. It lists wired modules of `<C>` and of the Shared Layer (`(shared)` suffix), grouped under headers in this order: Repositories, Domain services, Ports, Application services, Queries, Commands, Use cases, Infrastructure services, Presentation services. Inside a group: by type name, `<C>` first. Headers are not counted: `toggle:<n,..>` numbers only modules, top to bottom. Answer `toggle:<n,..>` or `none`. It is skipped when nothing fits, so check `<C>/wiring/` and `<rootFolder>/shared/wiring/` before counting steps. What fits depends on the layer of the class the wire builds: domain → domain; application → domain, application; infrastructure (repository, execution command/query, and port implementations, infrastructure services) → domain, application ports (of `<C>` and the Shared Layer; no other application modules), infrastructure; presentation → application, presentation. On top of that, application services see only ports from application, use cases never see use cases, repository implementations never see repositories, execution command/query implementations never see commands or queries, orchestration commands see only execution commands and queries from application, and orchestration queries only execution queries (repositories from domain stay available; they call only reading methods). These are the defaults: `rules.dependencies` in the config changes them per kind (`use-cases`, `orchestration-queries`, …), so count items from what the project's rules allow. Endpoints are never listed. Each chosen module goes into the wire (`wireClass(X, [wireUserRepository])`) and the constructor (`private readonly userRepository: UserRepository`, with `import type`), in the same order. An env-branched wire (repository, execution command/query, port) wires each implementation in isolation: instead of `Dependencies:` it asks `Dependencies for <Implementation>:` once per implementation, in the order of the wire (main, then test). Each list leaves out only what that implementation already has, and a list with nothing to choose is skipped. It may start with a `Suggested` group: infrastructure modules whose file name has the implementation label (its folder, e.g. `prisma`, `in-memory`) as whole words, such as `in-memory-db-infrastructure-service` for `in-memory`. They are listed there only, so numbering starts with them, and they are **checked**: `none` keeps them, `toggle:<n>` on one of them unchecks it.
9
+ {wire} Generators that create a wire (all below except endpoints, which ask their own two selects) then ask the checkbox `Dependencies:`, before `Overwrite them?`. It lists wired modules of `<C>` and of the Shared Layer (`(shared)` suffix), grouped under headers in this order: Repositories, Domain services, Ports, Application services, Queries, Commands, Use cases, Infrastructure services, Presentation services. Inside a group: by type name, `<C>` first. Headers are not counted: `toggle:<n,..>` numbers only modules, top to bottom. Answer `toggle:<n,..>` or `none`. It is skipped when nothing fits, so check `<C>/wiring/` and `<rootFolder>/shared/wiring/` before counting steps. What fits depends on the layer of the class the wire builds: domain → domain; application → domain, application; infrastructure (repository, execution command/query, and port implementations, infrastructure services) → domain, application ports (of `<C>` and the Shared Layer; no other application modules), infrastructure; presentation → application, presentation. On top of that, application services see only ports, commands and queries from application, use cases never see use cases, repository implementations never see repositories, execution command/query implementations never see commands or queries, orchestration commands see only execution commands and queries from application, and orchestration queries only execution queries (repositories from domain stay available; they call only reading methods). These are the defaults: `rules.dependencies` in the config changes them per kind (`use-cases`, `orchestration-queries`, …), so count items from what the project's rules allow. Endpoints are never listed. Each chosen module goes into the wire (`wireClass(X, [wireUserRepository])`) and the constructor (`private readonly userRepository: UserRepository`, with `import type`), in the same order. An env-branched wire (repository, execution command/query, port) wires each implementation in isolation: instead of `Dependencies:` it asks `Dependencies for <Implementation>:` once per implementation, in the order of the wire (main, then test). Each list leaves out only what that implementation already has, and a list with nothing to choose is skipped. It may start with a `Suggested` group: infrastructure modules whose file name has the implementation label (its folder, e.g. `prisma`, `in-memory`) as whole words, such as `in-memory-db-infrastructure-service` for `in-memory`. They are listed there only, so numbering starts with them, and they are **checked**: `none` keeps them, `toggle:<n>` on one of them unchecks it.
10
10
 
11
11
  ## Bounded context
12
12