@domain-first/project-structure 3.3.2 → 4.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
@@ -166,26 +166,28 @@ const adapter = undefined as unknown as Adapter<any[], any>
166
166
  export class RestEndpointGeneratorPresentationService {
167
167
  /**
168
168
  * Default errors of a client request, to spread into a generator's
169
- * own checks.
169
+ * own error mappings.
170
170
  */
171
- private get defaultClientRequestErrors(): ErrorStatuses[number]['checks'] {
172
- return [RequestParsingError.is, InputMappingError.is]
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
+ ]
173
180
  }
174
181
 
175
182
  get withoutGuards(): EndpointGenerator {
176
183
  return createEndpoint({
177
- errorStatuses: {
178
- 400: {
179
- checks: this.defaultClientRequestErrors,
180
- description: 'Invalid request'
181
- }
182
- }
184
+ errors: this.defaultClientRequestErrors
183
185
  })(adapter) as EndpointGenerator
184
186
  }
185
187
  }
186
188
  ```
187
189
 
188
- 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 statuses). Reuse the default checks there and add your own: `checks: [...this.defaultClientRequestErrors, 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(...)`.
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(...)`.
189
191
 
190
192
  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.
191
193
 
package/dist/index.cjs CHANGED
@@ -2024,8 +2024,9 @@ const scaffoldRestEndpointGenerator = (sharedFolder, { withNextAdapter = false }
2024
2024
  return `
2025
2025
  import {
2026
2026
  ${withNextAdapter ? '' : ' type Adapter,\n'} type EndpointGenerator,
2027
- type ErrorStatuses,
2027
+ type ErrorMapping,
2028
2028
  createEndpoint,
2029
+ httpStatus,
2029
2030
  InputMappingError,
2030
2031
  RequestParsingError
2031
2032
  } from '@domain-first/handlers-rest'
@@ -2038,20 +2039,22 @@ const adapter = undefined as unknown as Adapter<any[], any>`}
2038
2039
  export class ${className} {
2039
2040
  /**
2040
2041
  * Default errors of a client request, to spread into a generator's
2041
- * own checks.
2042
+ * own error mappings.
2042
2043
  */
2043
- private get defaultClientRequestErrors(): ErrorStatuses[number]['checks'] {
2044
- return [RequestParsingError.is, InputMappingError.is]
2044
+ private get defaultClientRequestErrors(): ErrorMapping[] {
2045
+ return [
2046
+ httpStatus(400, {
2047
+ code: 'INVALID_REQUEST',
2048
+ description: 'Invalid request',
2049
+ message: (error) => error.message,
2050
+ match: [RequestParsingError, InputMappingError]
2051
+ })
2052
+ ]
2045
2053
  }
2046
2054
 
2047
2055
  get withoutGuards(): EndpointGenerator {
2048
2056
  return createEndpoint({
2049
- errorStatuses: {
2050
- 400: {
2051
- checks: this.defaultClientRequestErrors,
2052
- description: 'Invalid request'
2053
- }
2054
- }
2057
+ errors: this.defaultClientRequestErrors
2055
2058
  })(${adapter}) as EndpointGenerator
2056
2059
  }
2057
2060
  }
package/dist/index.js CHANGED
@@ -1994,8 +1994,9 @@ const scaffoldRestEndpointGenerator = (sharedFolder, { withNextAdapter = false }
1994
1994
  return `
1995
1995
  import {
1996
1996
  ${withNextAdapter ? '' : ' type Adapter,\n'} type EndpointGenerator,
1997
- type ErrorStatuses,
1997
+ type ErrorMapping,
1998
1998
  createEndpoint,
1999
+ httpStatus,
1999
2000
  InputMappingError,
2000
2001
  RequestParsingError
2001
2002
  } from '@domain-first/handlers-rest'
@@ -2008,20 +2009,22 @@ const adapter = undefined as unknown as Adapter<any[], any>`}
2008
2009
  export class ${className} {
2009
2010
  /**
2010
2011
  * Default errors of a client request, to spread into a generator's
2011
- * own checks.
2012
+ * own error mappings.
2012
2013
  */
2013
- private get defaultClientRequestErrors(): ErrorStatuses[number]['checks'] {
2014
- return [RequestParsingError.is, InputMappingError.is]
2014
+ private get defaultClientRequestErrors(): ErrorMapping[] {
2015
+ return [
2016
+ httpStatus(400, {
2017
+ code: 'INVALID_REQUEST',
2018
+ description: 'Invalid request',
2019
+ message: (error) => error.message,
2020
+ match: [RequestParsingError, InputMappingError]
2021
+ })
2022
+ ]
2015
2023
  }
2016
2024
 
2017
2025
  get withoutGuards(): EndpointGenerator {
2018
2026
  return createEndpoint({
2019
- errorStatuses: {
2020
- 400: {
2021
- checks: this.defaultClientRequestErrors,
2022
- description: 'Invalid request'
2023
- }
2024
- }
2027
+ errors: this.defaultClientRequestErrors
2025
2028
  })(${adapter}) as EndpointGenerator
2026
2029
  }
2027
2030
  }
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": "3.3.2",
4
+ "version": "4.0.0",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -77,7 +77,7 @@ export const wirePlaceOrderUseCase = wireClass(PlaceOrderUseCase, [wireOrderRepo
77
77
 
78
78
  ## REST endpoints ({handlers-rest})
79
79
 
80
- 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` checks live in the private `defaultClientRequestErrors` getter; extend a status with `checks: [...this.defaultClientRequestErrors, MyError.is]`. Map domain errors to statuses in the generator's `errorStatuses`, 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, errorStatuses })(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]`).
80
+ 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]`).
81
81
  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.
82
82
  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(...)`.
83
83
  4. Register `restHandlers` from `<rootFolder>/presentation/rest/index.ts` in the server bootstrap (rule 2, exception 5).