@domain-first/project-structure 3.0.0 → 3.1.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 +17 -19
- package/dist/index.cjs +73 -48
- package/dist/index.js +72 -47
- package/dist/print-next-steps.d.ts +5 -0
- package/dist/use-cases/initialize.d.ts +4 -1
- package/dist/use-cases/rest-endpoint-generator.d.ts +5 -2
- package/dist/use-cases/rest-endpoint-generator.spec.d.ts +1 -0
- package/package.json +1 -1
- package/skills/df-ps-implement/SKILL.md +1 -1
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. With `@domain-first/handlers-rest`, it also scaffolds the [endpoint generator](#how-to-add-a-rest-endpoint).
|
|
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); if `next` is among your dependencies, it offers to connect `nextAdapter` from `@domain-first/handlers-next` right away and prints the install command for it when it's missing.
|
|
32
32
|
|
|
33
33
|
```
|
|
34
34
|
npx df-ps
|
|
@@ -156,36 +156,34 @@ src/bounded-contexts/sales/presentation/rest/orders/recent/GET.ts // GET /sale
|
|
|
156
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
157
|
|
|
158
158
|
```ts
|
|
159
|
+
/**
|
|
160
|
+
* The adapter of your HTTP framework.
|
|
161
|
+
*/
|
|
162
|
+
const adapter = undefined as unknown as Adapter<any[], any>
|
|
163
|
+
|
|
159
164
|
export class RestEndpointGeneratorPresentationService {
|
|
165
|
+
/**
|
|
166
|
+
* Default errors of a client request, to spread into a generator's
|
|
167
|
+
* own checks.
|
|
168
|
+
*/
|
|
169
|
+
private get defaultClientRequestErrors(): ErrorStatuses[number]['checks'] {
|
|
170
|
+
return [RequestParsingError.is, InputMappingError.is]
|
|
171
|
+
}
|
|
172
|
+
|
|
160
173
|
get withoutGuards(): EndpointGenerator {
|
|
161
174
|
return createEndpoint({
|
|
162
175
|
errorStatuses: {
|
|
163
176
|
400: {
|
|
164
|
-
checks: this.
|
|
177
|
+
checks: this.defaultClientRequestErrors,
|
|
165
178
|
description: 'Invalid request'
|
|
166
179
|
}
|
|
167
180
|
}
|
|
168
|
-
})(
|
|
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')
|
|
181
|
+
})(adapter) as EndpointGenerator
|
|
184
182
|
}
|
|
185
183
|
}
|
|
186
184
|
```
|
|
187
185
|
|
|
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.
|
|
186
|
+
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(...)`.
|
|
189
187
|
|
|
190
188
|
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
189
|
|
package/dist/index.cjs
CHANGED
|
@@ -219,6 +219,28 @@ class ConfigFile {
|
|
|
219
219
|
new File(ConfigFile.path).data = JSON.stringify(config, null, '\t');
|
|
220
220
|
};
|
|
221
221
|
}
|
|
222
|
+
const external_node_util_namespaceObject = require("node:util");
|
|
223
|
+
const printNextSteps = (nextSteps)=>{
|
|
224
|
+
const isCommand = (line)=>/^\s*(?:npm (?:i|install)|npx playwright install)\b/.test(line);
|
|
225
|
+
const commands = nextSteps.filter(isCommand);
|
|
226
|
+
const notes = nextSteps.filter((line)=>!isCommand(line));
|
|
227
|
+
const sections = [];
|
|
228
|
+
if (commands.length) sections.push([
|
|
229
|
+
'Run these commands to install the required dependencies:',
|
|
230
|
+
...commands.map((line)=>(0, external_node_util_namespaceObject.styleText)([
|
|
231
|
+
'bold',
|
|
232
|
+
'cyan'
|
|
233
|
+
], line))
|
|
234
|
+
].join('\n'));
|
|
235
|
+
if (notes.length) sections.push([
|
|
236
|
+
'Additional information:',
|
|
237
|
+
...notes.map((line)=>line.replace(/^(\s*Add )(!\S+)/, (_, prefix, path)=>prefix + (0, external_node_util_namespaceObject.styleText)([
|
|
238
|
+
'bold',
|
|
239
|
+
'cyan'
|
|
240
|
+
], path)))
|
|
241
|
+
].join('\n'));
|
|
242
|
+
if (sections.length) console.log(sections.join('\n\n'));
|
|
243
|
+
};
|
|
222
244
|
const savePendingChanges = async ()=>{
|
|
223
245
|
const overwrittenFiles = filesToOverwrite();
|
|
224
246
|
if (overwrittenFiles.length) {
|
|
@@ -238,7 +260,6 @@ const savePendingChanges = async ()=>{
|
|
|
238
260
|
writePendingChanges();
|
|
239
261
|
return true;
|
|
240
262
|
};
|
|
241
|
-
const external_node_util_namespaceObject = require("node:util");
|
|
242
263
|
const missingDomainFirstPackages = ()=>{
|
|
243
264
|
const packages = ConfigFile.Instance.data.domainFirstPackages;
|
|
244
265
|
const manifest = JSON.parse(new File(external_node_path_default().join(new ProjectRoot().path, 'package.json')).data);
|
|
@@ -1162,27 +1183,7 @@ const addIntegration = async ()=>{
|
|
|
1162
1183
|
}))
|
|
1163
1184
|
});
|
|
1164
1185
|
const nextSteps = await integration.run();
|
|
1165
|
-
if (await savePendingChanges())
|
|
1166
|
-
const isCommand = (line)=>/^\s*(?:npm (?:i|install)|npx playwright install)\b/.test(line);
|
|
1167
|
-
const commands = nextSteps.filter(isCommand);
|
|
1168
|
-
const notes = nextSteps.filter((line)=>!isCommand(line));
|
|
1169
|
-
const sections = [];
|
|
1170
|
-
if (commands.length) sections.push([
|
|
1171
|
-
'Run these commands to install the required dependencies:',
|
|
1172
|
-
...commands.map((line)=>(0, external_node_util_namespaceObject.styleText)([
|
|
1173
|
-
'bold',
|
|
1174
|
-
'cyan'
|
|
1175
|
-
], line))
|
|
1176
|
-
].join('\n'));
|
|
1177
|
-
if (notes.length) sections.push([
|
|
1178
|
-
'Additional information:',
|
|
1179
|
-
...notes.map((line)=>line.replace(/^(\s*Add )(!\S+)/, (_, prefix, path)=>prefix + (0, external_node_util_namespaceObject.styleText)([
|
|
1180
|
-
'bold',
|
|
1181
|
-
'cyan'
|
|
1182
|
-
], path)))
|
|
1183
|
-
].join('\n'));
|
|
1184
|
-
if (sections.length) console.log(sections.join('\n\n'));
|
|
1185
|
-
}
|
|
1186
|
+
if (await savePendingChanges()) printNextSteps(nextSteps);
|
|
1186
1187
|
};
|
|
1187
1188
|
class UnknownFormatNaming {
|
|
1188
1189
|
rawSource;
|
|
@@ -1685,44 +1686,44 @@ const scaffoldNewPresentationService = async (boundedContextFolder)=>{
|
|
|
1685
1686
|
if (wireFile) await wireNewModuleDependencies(boundedContextFolder, wireFile);
|
|
1686
1687
|
};
|
|
1687
1688
|
const rest_endpoint_generator_name = 'rest-endpoint-generator';
|
|
1688
|
-
const scaffoldRestEndpointGenerator = (sharedFolder)=>createPresentationService(sharedFolder, rest_endpoint_generator_name, (className)
|
|
1689
|
+
const scaffoldRestEndpointGenerator = (sharedFolder, { withNextAdapter = false } = {})=>createPresentationService(sharedFolder, rest_endpoint_generator_name, (className)=>{
|
|
1690
|
+
const adapter = withNextAdapter ? 'nextAdapter' : 'adapter';
|
|
1691
|
+
return `
|
|
1689
1692
|
import {
|
|
1690
|
-
type Adapter,
|
|
1691
|
-
type EndpointGenerator,
|
|
1693
|
+
${withNextAdapter ? '' : ' type Adapter,\n'} type EndpointGenerator,
|
|
1692
1694
|
type ErrorStatuses,
|
|
1693
1695
|
createEndpoint,
|
|
1694
1696
|
InputMappingError,
|
|
1695
1697
|
RequestParsingError
|
|
1696
1698
|
} from '@domain-first/handlers-rest'
|
|
1699
|
+
${withNextAdapter ? "import { nextAdapter } from '@domain-first/handlers-next'" : `
|
|
1700
|
+
/**
|
|
1701
|
+
* The adapter of your HTTP framework.
|
|
1702
|
+
*/
|
|
1703
|
+
const adapter = undefined as unknown as Adapter<any[], any>`}
|
|
1697
1704
|
|
|
1698
1705
|
export class ${className} {
|
|
1706
|
+
/**
|
|
1707
|
+
* Default errors of a client request, to spread into a generator's
|
|
1708
|
+
* own checks.
|
|
1709
|
+
*/
|
|
1710
|
+
private get defaultClientRequestErrors(): ErrorStatuses[number]['checks'] {
|
|
1711
|
+
return [RequestParsingError.is, InputMappingError.is]
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1699
1714
|
get withoutGuards(): EndpointGenerator {
|
|
1700
1715
|
return createEndpoint({
|
|
1701
1716
|
errorStatuses: {
|
|
1702
1717
|
400: {
|
|
1703
|
-
checks: this.
|
|
1718
|
+
checks: this.defaultClientRequestErrors,
|
|
1704
1719
|
description: 'Invalid request'
|
|
1705
1720
|
}
|
|
1706
1721
|
}
|
|
1707
|
-
})(
|
|
1708
|
-
}
|
|
1709
|
-
|
|
1710
|
-
/**
|
|
1711
|
-
* Default checks of an invalid request, to spread into a generator's
|
|
1712
|
-
* own checks.
|
|
1713
|
-
*/
|
|
1714
|
-
private get invalidRequestChecks(): ErrorStatuses[number]['checks'] {
|
|
1715
|
-
return [RequestParsingError.is, InputMappingError.is]
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
|
-
/**
|
|
1719
|
-
* The adapter of your HTTP framework.
|
|
1720
|
-
*/
|
|
1721
|
-
private get adapter(): Adapter<any[], any> {
|
|
1722
|
-
throw new Error('REST adapter is not set')
|
|
1722
|
+
})(${adapter}) as EndpointGenerator
|
|
1723
1723
|
}
|
|
1724
1724
|
}
|
|
1725
|
-
`.trim()
|
|
1725
|
+
`.trim();
|
|
1726
|
+
});
|
|
1726
1727
|
const findRestEndpointGenerator = (sharedFolder)=>{
|
|
1727
1728
|
const typeFile = external_node_path_default().join(sharedFolder.path, 'presentation', 'services', `${rest_endpoint_generator_name}-presentation-service.ts`);
|
|
1728
1729
|
if (!new File(typeFile).exists) return;
|
|
@@ -1733,7 +1734,18 @@ const findRestEndpointGenerator = (sharedFolder)=>{
|
|
|
1733
1734
|
wireFile: external_node_path_default().join(sharedFolder.path, 'wiring', 'presentation-services', `wire-${rest_endpoint_generator_name}-presentation-service.ts`)
|
|
1734
1735
|
};
|
|
1735
1736
|
};
|
|
1737
|
+
const hasDependency = (root, name)=>{
|
|
1738
|
+
const manifestFile = new File(external_node_path_default().join(root, 'package.json'));
|
|
1739
|
+
if (!manifestFile.exists) return false;
|
|
1740
|
+
const manifest = JSON.parse(manifestFile.data);
|
|
1741
|
+
return [
|
|
1742
|
+
manifest.dependencies,
|
|
1743
|
+
manifest.devDependencies,
|
|
1744
|
+
manifest.optionalDependencies
|
|
1745
|
+
].some((dependencies)=>dependencies && Object.hasOwn(dependencies, name));
|
|
1746
|
+
};
|
|
1736
1747
|
const initializeUseCase = async ()=>{
|
|
1748
|
+
const nextSteps = [];
|
|
1737
1749
|
const rootFolderName = await (0, prompts_namespaceObject.input)({
|
|
1738
1750
|
message: 'Root folder (use "/" for inner folders):',
|
|
1739
1751
|
default: 'src'
|
|
@@ -1748,6 +1760,9 @@ const initializeUseCase = async ()=>{
|
|
|
1748
1760
|
'@domain-first/handlers-rest'
|
|
1749
1761
|
]
|
|
1750
1762
|
});
|
|
1763
|
+
const withNextAdapter = domainFirstPackages.includes('@domain-first/handlers-rest') && hasDependency(new ProjectRoot().path, 'next') && await (0, prompts_namespaceObject.confirm)({
|
|
1764
|
+
message: 'Use Next adapter for REST endpoints'
|
|
1765
|
+
});
|
|
1751
1766
|
const defaultPersistenceLayerImplementation = await (0, prompts_namespaceObject.input)({
|
|
1752
1767
|
message: 'Default persistence layer implementation:'
|
|
1753
1768
|
});
|
|
@@ -1802,10 +1817,16 @@ import { errorNamespace } from '@domain-first/errors'
|
|
|
1802
1817
|
|
|
1803
1818
|
export const SharedErrors = errorNamespace('shared')
|
|
1804
1819
|
`.trim());
|
|
1805
|
-
if (domainFirstPackages.includes('@domain-first/handlers-rest'))
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1820
|
+
if (domainFirstPackages.includes('@domain-first/handlers-rest')) {
|
|
1821
|
+
scaffoldRestEndpointGenerator(rootFolder.subitem([
|
|
1822
|
+
rootFolderName,
|
|
1823
|
+
'shared'
|
|
1824
|
+
]), {
|
|
1825
|
+
withNextAdapter
|
|
1826
|
+
});
|
|
1827
|
+
if (withNextAdapter && !hasDependency(path, '@domain-first/handlers-next')) nextSteps.push('npm i @domain-first/handlers-next');
|
|
1828
|
+
}
|
|
1829
|
+
return nextSteps;
|
|
1809
1830
|
};
|
|
1810
1831
|
const splitSegments = (rawSource)=>rawSource.split(/[\\/]/).map((segment)=>segment.trim()).filter((segment)=>!!segment);
|
|
1811
1832
|
class NestedName {
|
|
@@ -3337,7 +3358,11 @@ export const use${ClassName}UIModel = (widgetProps: Omit<${ClassName}WidgetProps
|
|
|
3337
3358
|
}
|
|
3338
3359
|
};
|
|
3339
3360
|
const main = async ()=>{
|
|
3340
|
-
if (!ConfigFile.exists)
|
|
3361
|
+
if (!ConfigFile.exists) {
|
|
3362
|
+
const nextSteps = await initializeUseCase();
|
|
3363
|
+
if (await savePendingChanges()) printNextSteps(nextSteps);
|
|
3364
|
+
return;
|
|
3365
|
+
}
|
|
3341
3366
|
const rootPath = new ProjectRoot().path;
|
|
3342
3367
|
const currentBoundedContextOptions = new Folder(rootPath).subitem([
|
|
3343
3368
|
ConfigFile.Instance.data.rootFolder,
|
package/dist/index.js
CHANGED
|
@@ -190,6 +190,27 @@ class ConfigFile {
|
|
|
190
190
|
new File(ConfigFile.path).data = JSON.stringify(config, null, '\t');
|
|
191
191
|
};
|
|
192
192
|
}
|
|
193
|
+
const printNextSteps = (nextSteps)=>{
|
|
194
|
+
const isCommand = (line)=>/^\s*(?:npm (?:i|install)|npx playwright install)\b/.test(line);
|
|
195
|
+
const commands = nextSteps.filter(isCommand);
|
|
196
|
+
const notes = nextSteps.filter((line)=>!isCommand(line));
|
|
197
|
+
const sections = [];
|
|
198
|
+
if (commands.length) sections.push([
|
|
199
|
+
'Run these commands to install the required dependencies:',
|
|
200
|
+
...commands.map((line)=>styleText([
|
|
201
|
+
'bold',
|
|
202
|
+
'cyan'
|
|
203
|
+
], line))
|
|
204
|
+
].join('\n'));
|
|
205
|
+
if (notes.length) sections.push([
|
|
206
|
+
'Additional information:',
|
|
207
|
+
...notes.map((line)=>line.replace(/^(\s*Add )(!\S+)/, (_, prefix, path)=>prefix + styleText([
|
|
208
|
+
'bold',
|
|
209
|
+
'cyan'
|
|
210
|
+
], path)))
|
|
211
|
+
].join('\n'));
|
|
212
|
+
if (sections.length) console.log(sections.join('\n\n'));
|
|
213
|
+
};
|
|
193
214
|
const savePendingChanges = async ()=>{
|
|
194
215
|
const overwrittenFiles = filesToOverwrite();
|
|
195
216
|
if (overwrittenFiles.length) {
|
|
@@ -1132,27 +1153,7 @@ const addIntegration = async ()=>{
|
|
|
1132
1153
|
}))
|
|
1133
1154
|
});
|
|
1134
1155
|
const nextSteps = await integration.run();
|
|
1135
|
-
if (await savePendingChanges())
|
|
1136
|
-
const isCommand = (line)=>/^\s*(?:npm (?:i|install)|npx playwright install)\b/.test(line);
|
|
1137
|
-
const commands = nextSteps.filter(isCommand);
|
|
1138
|
-
const notes = nextSteps.filter((line)=>!isCommand(line));
|
|
1139
|
-
const sections = [];
|
|
1140
|
-
if (commands.length) sections.push([
|
|
1141
|
-
'Run these commands to install the required dependencies:',
|
|
1142
|
-
...commands.map((line)=>styleText([
|
|
1143
|
-
'bold',
|
|
1144
|
-
'cyan'
|
|
1145
|
-
], line))
|
|
1146
|
-
].join('\n'));
|
|
1147
|
-
if (notes.length) sections.push([
|
|
1148
|
-
'Additional information:',
|
|
1149
|
-
...notes.map((line)=>line.replace(/^(\s*Add )(!\S+)/, (_, prefix, path)=>prefix + styleText([
|
|
1150
|
-
'bold',
|
|
1151
|
-
'cyan'
|
|
1152
|
-
], path)))
|
|
1153
|
-
].join('\n'));
|
|
1154
|
-
if (sections.length) console.log(sections.join('\n\n'));
|
|
1155
|
-
}
|
|
1156
|
+
if (await savePendingChanges()) printNextSteps(nextSteps);
|
|
1156
1157
|
};
|
|
1157
1158
|
class UnknownFormatNaming {
|
|
1158
1159
|
rawSource;
|
|
@@ -1655,44 +1656,44 @@ const scaffoldNewPresentationService = async (boundedContextFolder)=>{
|
|
|
1655
1656
|
if (wireFile) await wireNewModuleDependencies(boundedContextFolder, wireFile);
|
|
1656
1657
|
};
|
|
1657
1658
|
const rest_endpoint_generator_name = 'rest-endpoint-generator';
|
|
1658
|
-
const scaffoldRestEndpointGenerator = (sharedFolder)=>createPresentationService(sharedFolder, rest_endpoint_generator_name, (className)
|
|
1659
|
+
const scaffoldRestEndpointGenerator = (sharedFolder, { withNextAdapter = false } = {})=>createPresentationService(sharedFolder, rest_endpoint_generator_name, (className)=>{
|
|
1660
|
+
const adapter = withNextAdapter ? 'nextAdapter' : 'adapter';
|
|
1661
|
+
return `
|
|
1659
1662
|
import {
|
|
1660
|
-
type Adapter,
|
|
1661
|
-
type EndpointGenerator,
|
|
1663
|
+
${withNextAdapter ? '' : ' type Adapter,\n'} type EndpointGenerator,
|
|
1662
1664
|
type ErrorStatuses,
|
|
1663
1665
|
createEndpoint,
|
|
1664
1666
|
InputMappingError,
|
|
1665
1667
|
RequestParsingError
|
|
1666
1668
|
} from '@domain-first/handlers-rest'
|
|
1669
|
+
${withNextAdapter ? "import { nextAdapter } from '@domain-first/handlers-next'" : `
|
|
1670
|
+
/**
|
|
1671
|
+
* The adapter of your HTTP framework.
|
|
1672
|
+
*/
|
|
1673
|
+
const adapter = undefined as unknown as Adapter<any[], any>`}
|
|
1667
1674
|
|
|
1668
1675
|
export class ${className} {
|
|
1676
|
+
/**
|
|
1677
|
+
* Default errors of a client request, to spread into a generator's
|
|
1678
|
+
* own checks.
|
|
1679
|
+
*/
|
|
1680
|
+
private get defaultClientRequestErrors(): ErrorStatuses[number]['checks'] {
|
|
1681
|
+
return [RequestParsingError.is, InputMappingError.is]
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1669
1684
|
get withoutGuards(): EndpointGenerator {
|
|
1670
1685
|
return createEndpoint({
|
|
1671
1686
|
errorStatuses: {
|
|
1672
1687
|
400: {
|
|
1673
|
-
checks: this.
|
|
1688
|
+
checks: this.defaultClientRequestErrors,
|
|
1674
1689
|
description: 'Invalid request'
|
|
1675
1690
|
}
|
|
1676
1691
|
}
|
|
1677
|
-
})(
|
|
1678
|
-
}
|
|
1679
|
-
|
|
1680
|
-
/**
|
|
1681
|
-
* Default checks of an invalid request, to spread into a generator's
|
|
1682
|
-
* own checks.
|
|
1683
|
-
*/
|
|
1684
|
-
private get invalidRequestChecks(): ErrorStatuses[number]['checks'] {
|
|
1685
|
-
return [RequestParsingError.is, InputMappingError.is]
|
|
1686
|
-
}
|
|
1687
|
-
|
|
1688
|
-
/**
|
|
1689
|
-
* The adapter of your HTTP framework.
|
|
1690
|
-
*/
|
|
1691
|
-
private get adapter(): Adapter<any[], any> {
|
|
1692
|
-
throw new Error('REST adapter is not set')
|
|
1692
|
+
})(${adapter}) as EndpointGenerator
|
|
1693
1693
|
}
|
|
1694
1694
|
}
|
|
1695
|
-
`.trim()
|
|
1695
|
+
`.trim();
|
|
1696
|
+
});
|
|
1696
1697
|
const findRestEndpointGenerator = (sharedFolder)=>{
|
|
1697
1698
|
const typeFile = node_path.join(sharedFolder.path, 'presentation', 'services', `${rest_endpoint_generator_name}-presentation-service.ts`);
|
|
1698
1699
|
if (!new File(typeFile).exists) return;
|
|
@@ -1703,7 +1704,18 @@ const findRestEndpointGenerator = (sharedFolder)=>{
|
|
|
1703
1704
|
wireFile: node_path.join(sharedFolder.path, 'wiring', 'presentation-services', `wire-${rest_endpoint_generator_name}-presentation-service.ts`)
|
|
1704
1705
|
};
|
|
1705
1706
|
};
|
|
1707
|
+
const hasDependency = (root, name)=>{
|
|
1708
|
+
const manifestFile = new File(node_path.join(root, 'package.json'));
|
|
1709
|
+
if (!manifestFile.exists) return false;
|
|
1710
|
+
const manifest = JSON.parse(manifestFile.data);
|
|
1711
|
+
return [
|
|
1712
|
+
manifest.dependencies,
|
|
1713
|
+
manifest.devDependencies,
|
|
1714
|
+
manifest.optionalDependencies
|
|
1715
|
+
].some((dependencies)=>dependencies && Object.hasOwn(dependencies, name));
|
|
1716
|
+
};
|
|
1706
1717
|
const initializeUseCase = async ()=>{
|
|
1718
|
+
const nextSteps = [];
|
|
1707
1719
|
const rootFolderName = await input({
|
|
1708
1720
|
message: 'Root folder (use "/" for inner folders):',
|
|
1709
1721
|
default: 'src'
|
|
@@ -1718,6 +1730,9 @@ const initializeUseCase = async ()=>{
|
|
|
1718
1730
|
'@domain-first/handlers-rest'
|
|
1719
1731
|
]
|
|
1720
1732
|
});
|
|
1733
|
+
const withNextAdapter = domainFirstPackages.includes('@domain-first/handlers-rest') && hasDependency(new ProjectRoot().path, 'next') && await prompts_confirm({
|
|
1734
|
+
message: 'Use Next adapter for REST endpoints'
|
|
1735
|
+
});
|
|
1721
1736
|
const defaultPersistenceLayerImplementation = await input({
|
|
1722
1737
|
message: 'Default persistence layer implementation:'
|
|
1723
1738
|
});
|
|
@@ -1772,10 +1787,16 @@ import { errorNamespace } from '@domain-first/errors'
|
|
|
1772
1787
|
|
|
1773
1788
|
export const SharedErrors = errorNamespace('shared')
|
|
1774
1789
|
`.trim());
|
|
1775
|
-
if (domainFirstPackages.includes('@domain-first/handlers-rest'))
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1790
|
+
if (domainFirstPackages.includes('@domain-first/handlers-rest')) {
|
|
1791
|
+
scaffoldRestEndpointGenerator(rootFolder.subitem([
|
|
1792
|
+
rootFolderName,
|
|
1793
|
+
'shared'
|
|
1794
|
+
]), {
|
|
1795
|
+
withNextAdapter
|
|
1796
|
+
});
|
|
1797
|
+
if (withNextAdapter && !hasDependency(path, '@domain-first/handlers-next')) nextSteps.push('npm i @domain-first/handlers-next');
|
|
1798
|
+
}
|
|
1799
|
+
return nextSteps;
|
|
1779
1800
|
};
|
|
1780
1801
|
const splitSegments = (rawSource)=>rawSource.split(/[\\/]/).map((segment)=>segment.trim()).filter((segment)=>!!segment);
|
|
1781
1802
|
class NestedName {
|
|
@@ -3307,7 +3328,11 @@ export const use${ClassName}UIModel = (widgetProps: Omit<${ClassName}WidgetProps
|
|
|
3307
3328
|
}
|
|
3308
3329
|
};
|
|
3309
3330
|
const main = async ()=>{
|
|
3310
|
-
if (!ConfigFile.exists)
|
|
3331
|
+
if (!ConfigFile.exists) {
|
|
3332
|
+
const nextSteps = await initializeUseCase();
|
|
3333
|
+
if (await savePendingChanges()) printNextSteps(nextSteps);
|
|
3334
|
+
return;
|
|
3335
|
+
}
|
|
3311
3336
|
const rootPath = new ProjectRoot().path;
|
|
3312
3337
|
const currentBoundedContextOptions = new Folder(rootPath).subitem([
|
|
3313
3338
|
ConfigFile.Instance.data.rootFolder,
|
|
@@ -2,9 +2,12 @@ import { File, type Folder } from '../file-system';
|
|
|
2
2
|
/**
|
|
3
3
|
* The one endpoint generator of the project: a Shared Layer presentation
|
|
4
4
|
* service, scaffolded like any other. Every generating method is a getter
|
|
5
|
-
* typed as `EndpointGenerator`.
|
|
5
|
+
* typed as `EndpointGenerator`. With `withNextAdapter`, the adapter is
|
|
6
|
+
* `nextAdapter` of `@domain-first/handlers-next` instead of a placeholder.
|
|
6
7
|
*/
|
|
7
|
-
export declare const scaffoldRestEndpointGenerator: (sharedFolder: Folder
|
|
8
|
+
export declare const scaffoldRestEndpointGenerator: (sharedFolder: Folder, { withNextAdapter }?: {
|
|
9
|
+
withNextAdapter?: boolean;
|
|
10
|
+
}) => File | undefined;
|
|
8
11
|
/**
|
|
9
12
|
* The generator's type and factory, or undefined in projects initialized
|
|
10
13
|
* without it.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -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.
|
|
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). 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]`).
|
|
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).
|