@ojiepermana/angular 21.0.0 → 21.0.3
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/collection.json +25 -0
- package/fesm2022/ojiepermana-angular-generator-api.mjs +67 -0
- package/fesm2022/ojiepermana-angular-generator-api.mjs.map +1 -0
- package/fesm2022/ojiepermana-angular-layout.mjs +76 -56
- package/fesm2022/ojiepermana-angular-layout.mjs.map +1 -1
- package/fesm2022/ojiepermana-angular.mjs +1 -0
- package/fesm2022/ojiepermana-angular.mjs.map +1 -1
- package/generator/api/README.md +218 -0
- package/generator/api/bin/schematics/init/index.js +88 -0
- package/generator/api/bin/schematics/sdk/index.js +58 -0
- package/generator/api/bin/src/config/loader.js +41 -0
- package/generator/api/bin/src/config/schema.js +56 -0
- package/generator/api/bin/src/emit/client.js +246 -0
- package/generator/api/bin/src/emit/metadata.js +295 -0
- package/generator/api/bin/src/emit/models.js +106 -0
- package/generator/api/bin/src/emit/navigation.js +56 -0
- package/generator/api/bin/src/emit/operations.js +122 -0
- package/generator/api/bin/src/emit/public-api.js +54 -0
- package/generator/api/bin/src/emit/services.js +87 -0
- package/generator/api/bin/src/engine.js +65 -0
- package/generator/api/bin/src/layout/per-domain.js +346 -0
- package/generator/api/bin/src/parser/bundle.js +25 -0
- package/generator/api/bin/src/parser/ir.js +320 -0
- package/generator/api/bin/src/parser/types.js +7 -0
- package/generator/api/bin/src/render/template.js +58 -0
- package/generator/api/bin/src/writer/index.js +69 -0
- package/generator/api/schematics/init/schema.json +19 -0
- package/generator/api/schematics/sdk/schema.json +19 -0
- package/generator/api/sdk.config.example.json +22 -0
- package/generator/guide/README.md +84 -0
- package/generator/guide/bin/schematics/build/index.js +35 -0
- package/generator/guide/bin/schematics/init/index.js +70 -0
- package/generator/guide/bin/src/config/loader.js +50 -0
- package/generator/guide/bin/src/config/schema.js +12 -0
- package/generator/guide/bin/src/engine/component.js +73 -0
- package/generator/guide/bin/src/engine/frontmatter.js +42 -0
- package/generator/guide/bin/src/engine/index.js +42 -0
- package/generator/guide/bin/src/engine/naming.js +39 -0
- package/generator/guide/bin/src/engine/render.js +18 -0
- package/generator/guide/bin/src/engine/routes.js +106 -0
- package/generator/guide/bin/src/engine/walk.js +35 -0
- package/generator/guide/guide.config.example.json +9 -0
- package/generator/guide/schematics/build/schema.json +14 -0
- package/generator/guide/schematics/init/schema.json +19 -0
- package/package.json +10 -3
- package/types/ojiepermana-angular-generator-api.d.ts +85 -0
- package/types/ojiepermana-angular-layout.d.ts +2 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emitOperations = emitOperations;
|
|
4
|
+
const template_1 = require("../render/template");
|
|
5
|
+
const models_1 = require("./models");
|
|
6
|
+
/**
|
|
7
|
+
* Emit one tree-shakeable operation function per operationId at
|
|
8
|
+
* `fn/<tag-kebab>/<operation-kebab>.ts`.
|
|
9
|
+
*
|
|
10
|
+
* Each file exports:
|
|
11
|
+
* - `<operationId>$Params` interface
|
|
12
|
+
* - `<operationId>` function returning `Observable<StrictHttpResponse<R>>`
|
|
13
|
+
* - `<operationId>.PATH` constant
|
|
14
|
+
*/
|
|
15
|
+
function emitOperations(ir, target) {
|
|
16
|
+
return ir.operations.map((op) => ({
|
|
17
|
+
path: `fn/${(0, template_1.kebabCase)(op.tag)}/${(0, template_1.kebabCase)(op.operationId)}.ts`,
|
|
18
|
+
content: (0, template_1.finalize)(renderOperation(op, target)),
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
function renderOperation(op, target) {
|
|
22
|
+
const fnName = (0, template_1.camelCase)(op.operationId);
|
|
23
|
+
const paramsName = `${(0, template_1.pascalCase)(op.operationId)}$Params`;
|
|
24
|
+
const responseType = op.successRef ? (0, template_1.pascalCase)(op.successRef) : 'unknown';
|
|
25
|
+
const paramImports = collectParamImports(op);
|
|
26
|
+
const imports = [
|
|
27
|
+
`import { HttpClient, HttpContext, HttpResponse } from '@angular/common/http';`,
|
|
28
|
+
`import { Observable } from 'rxjs';`,
|
|
29
|
+
`import { filter, map } from 'rxjs/operators';`,
|
|
30
|
+
``,
|
|
31
|
+
`import { RequestBuilder } from '../../request-builder';`,
|
|
32
|
+
`import { StrictHttpResponse } from '../../strict-http-response';`,
|
|
33
|
+
];
|
|
34
|
+
for (const name of paramImports) {
|
|
35
|
+
imports.push(`import type { ${(0, template_1.pascalCase)(name)} } from '../../models/${(0, template_1.kebabCase)(name)}';`);
|
|
36
|
+
}
|
|
37
|
+
const paramsInterface = renderParamsInterface(op, paramsName);
|
|
38
|
+
const hasAnyParams = paramsInterface.hasAnyField;
|
|
39
|
+
const paramsOptional = !paramsInterface.anyRequired;
|
|
40
|
+
const bodyCT = op.bodySchemaRef ? "'application/json'" : '';
|
|
41
|
+
const pathCalls = op.params
|
|
42
|
+
.filter((p) => p.in === 'path')
|
|
43
|
+
.map((p) => ` rb.path('${p.name}', params.${safeProp(p.name)});`);
|
|
44
|
+
const queryCalls = op.params
|
|
45
|
+
.filter((p) => p.in === 'query')
|
|
46
|
+
.map((p) => ` rb.query('${p.name}', params.${safeProp(p.name)});`);
|
|
47
|
+
const headerCalls = op.params
|
|
48
|
+
.filter((p) => p.in === 'header')
|
|
49
|
+
.map((p) => ` rb.header('${p.name}', params.${safeProp(p.name)});`);
|
|
50
|
+
const bodyCall = op.bodySchemaRef ? ` rb.body(params.body, ${bodyCT});` : '';
|
|
51
|
+
const populate = pathCalls.length || queryCalls.length || headerCalls.length || bodyCall
|
|
52
|
+
? ` if (params) {
|
|
53
|
+
${[...pathCalls, ...queryCalls, ...headerCalls, bodyCall].filter(Boolean).join('\n')}
|
|
54
|
+
}`
|
|
55
|
+
: '';
|
|
56
|
+
const paramsArg = hasAnyParams
|
|
57
|
+
? `params${paramsOptional ? '?' : ''}: ${paramsName}`
|
|
58
|
+
: `_params?: Record<string, never>`;
|
|
59
|
+
return `${target.banner}
|
|
60
|
+
|
|
61
|
+
${imports.join('\n')}
|
|
62
|
+
|
|
63
|
+
${paramsInterface.source}
|
|
64
|
+
export function ${fnName}(
|
|
65
|
+
http: HttpClient,
|
|
66
|
+
rootUrl: string,
|
|
67
|
+
${paramsArg},
|
|
68
|
+
context?: HttpContext,
|
|
69
|
+
): Observable<StrictHttpResponse<${responseType}>> {
|
|
70
|
+
const rb = new RequestBuilder(rootUrl, ${fnName}.PATH, '${op.method}');
|
|
71
|
+
${populate}
|
|
72
|
+
return http.request(rb.build({ responseType: 'json', accept: 'application/json', context })).pipe(
|
|
73
|
+
filter((r: unknown): r is HttpResponse<unknown> => r instanceof HttpResponse),
|
|
74
|
+
map((r) => r as StrictHttpResponse<${responseType}>),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
${fnName}.PATH = '${op.path}';
|
|
79
|
+
`;
|
|
80
|
+
}
|
|
81
|
+
function collectParamImports(op) {
|
|
82
|
+
const set = new Set();
|
|
83
|
+
for (const p of op.params) {
|
|
84
|
+
if (p.rule.ref)
|
|
85
|
+
set.add(p.rule.ref);
|
|
86
|
+
if (p.rule.itemsRef)
|
|
87
|
+
set.add(p.rule.itemsRef);
|
|
88
|
+
}
|
|
89
|
+
if (op.bodySchemaRef)
|
|
90
|
+
set.add(op.bodySchemaRef);
|
|
91
|
+
if (op.successRef)
|
|
92
|
+
set.add(op.successRef);
|
|
93
|
+
return [...set].sort();
|
|
94
|
+
}
|
|
95
|
+
function renderParamsInterface(op, paramsName) {
|
|
96
|
+
const lines = [];
|
|
97
|
+
let anyRequired = false;
|
|
98
|
+
const pushField = (p) => {
|
|
99
|
+
const optional = p.required ? '' : '?';
|
|
100
|
+
if (p.required)
|
|
101
|
+
anyRequired = true;
|
|
102
|
+
const type = (0, models_1.renderFieldType)(p.rule);
|
|
103
|
+
const name = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(p.name) ? p.name : `'${p.name}'`;
|
|
104
|
+
lines.push(` ${name}${optional}: ${type};`);
|
|
105
|
+
};
|
|
106
|
+
for (const p of op.params)
|
|
107
|
+
pushField(p);
|
|
108
|
+
if (op.bodySchemaRef) {
|
|
109
|
+
const optional = op.bodyRequired ? '' : '?';
|
|
110
|
+
if (op.bodyRequired)
|
|
111
|
+
anyRequired = true;
|
|
112
|
+
lines.push(` body${optional}: ${(0, template_1.pascalCase)(op.bodySchemaRef)};`);
|
|
113
|
+
}
|
|
114
|
+
const hasAnyField = lines.length > 0;
|
|
115
|
+
const source = hasAnyField
|
|
116
|
+
? `export interface ${paramsName} {\n${lines.join('\n')}\n}\n`
|
|
117
|
+
: `export type ${paramsName} = Record<string, never>;\n`;
|
|
118
|
+
return { source, hasAnyField, anyRequired };
|
|
119
|
+
}
|
|
120
|
+
function safeProp(name) {
|
|
121
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : `['${name}']`;
|
|
122
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emitPublicApi = emitPublicApi;
|
|
4
|
+
const template_1 = require("../render/template");
|
|
5
|
+
/**
|
|
6
|
+
* Emit `public-api.ts` — the barrel that re-exports every generated artifact
|
|
7
|
+
* based on the resolved feature flags. Mirrors the convention used by the
|
|
8
|
+
* reference output in `old/api/index.ts`.
|
|
9
|
+
*/
|
|
10
|
+
function emitPublicApi(ir, target) {
|
|
11
|
+
const lines = [target.banner, ''];
|
|
12
|
+
if (target.features.client) {
|
|
13
|
+
lines.push(`export { ApiConfiguration, provideApiConfiguration } from './api-configuration';`);
|
|
14
|
+
lines.push(`export { BaseService } from './base-service';`);
|
|
15
|
+
lines.push(`export { RequestBuilder } from './request-builder';`);
|
|
16
|
+
lines.push(`export type { StrictHttpResponse } from './strict-http-response';`);
|
|
17
|
+
lines.push(`export { ${target.clientName} } from './api';`);
|
|
18
|
+
lines.push('');
|
|
19
|
+
}
|
|
20
|
+
if (target.features.models) {
|
|
21
|
+
for (const schema of ir.schemas) {
|
|
22
|
+
lines.push(`export type { ${(0, template_1.pascalCase)(schema.name)} } from './models/${(0, template_1.kebabCase)(schema.name)}';`);
|
|
23
|
+
}
|
|
24
|
+
lines.push('');
|
|
25
|
+
}
|
|
26
|
+
if (target.features.services) {
|
|
27
|
+
const tags = Array.from(new Set(ir.operations.map((o) => o.tag))).sort();
|
|
28
|
+
for (const tag of tags) {
|
|
29
|
+
lines.push(`export { ${(0, template_1.pascalCase)(tag)}Service } from './services/${(0, template_1.kebabCase)(tag)}.service';`);
|
|
30
|
+
}
|
|
31
|
+
lines.push('');
|
|
32
|
+
}
|
|
33
|
+
if (target.features.operations) {
|
|
34
|
+
const sorted = [...ir.operations].sort((a, b) => a.operationId.localeCompare(b.operationId));
|
|
35
|
+
for (const op of sorted) {
|
|
36
|
+
lines.push(renderFnReexport(op));
|
|
37
|
+
}
|
|
38
|
+
lines.push('');
|
|
39
|
+
}
|
|
40
|
+
if (target.features.metadata) {
|
|
41
|
+
lines.push(`export * from './metadata';`);
|
|
42
|
+
lines.push(`export * from './openapi-helpers';`);
|
|
43
|
+
lines.push('');
|
|
44
|
+
}
|
|
45
|
+
if (target.features.navigation) {
|
|
46
|
+
lines.push(`export { ApiNavigation } from './api.navigation';`);
|
|
47
|
+
}
|
|
48
|
+
return [{ path: 'public-api.ts', content: (0, template_1.finalize)(lines.join('\n')) }];
|
|
49
|
+
}
|
|
50
|
+
function renderFnReexport(op) {
|
|
51
|
+
const fnName = (0, template_1.camelCase)(op.operationId);
|
|
52
|
+
const paramsName = `${(0, template_1.pascalCase)(op.operationId)}$Params`;
|
|
53
|
+
return `export { ${fnName}, type ${paramsName} } from './fn/${(0, template_1.kebabCase)(op.tag)}/${(0, template_1.kebabCase)(op.operationId)}';`;
|
|
54
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emitServices = emitServices;
|
|
4
|
+
const template_1 = require("../render/template");
|
|
5
|
+
/**
|
|
6
|
+
* Emit one Injectable service per tag at `services/<tag-kebab>.service.ts`.
|
|
7
|
+
*
|
|
8
|
+
* Each method returns `Observable<T>` (body-only) plus a `<method>$Response`
|
|
9
|
+
* variant that returns the full `StrictHttpResponse<T>`. Bodies are dispatched
|
|
10
|
+
* through the tree-shakeable fn modules emitted by {@link emitOperations}, so
|
|
11
|
+
* unused methods can be shaken out.
|
|
12
|
+
*/
|
|
13
|
+
function emitServices(ir, target) {
|
|
14
|
+
const byTag = groupByTag(ir.operations);
|
|
15
|
+
const files = [];
|
|
16
|
+
for (const [tag, ops] of byTag) {
|
|
17
|
+
files.push({
|
|
18
|
+
path: `services/${(0, template_1.kebabCase)(tag)}.service.ts`,
|
|
19
|
+
content: (0, template_1.finalize)(renderService(tag, ops, target)),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return files;
|
|
23
|
+
}
|
|
24
|
+
function groupByTag(operations) {
|
|
25
|
+
const map = new Map();
|
|
26
|
+
for (const op of operations) {
|
|
27
|
+
const bucket = map.get(op.tag) ?? [];
|
|
28
|
+
bucket.push(op);
|
|
29
|
+
map.set(op.tag, bucket);
|
|
30
|
+
}
|
|
31
|
+
return map;
|
|
32
|
+
}
|
|
33
|
+
function renderService(tag, ops, target) {
|
|
34
|
+
const className = `${(0, template_1.pascalCase)(tag)}Service`;
|
|
35
|
+
const sorted = [...ops].sort((a, b) => a.operationId.localeCompare(b.operationId));
|
|
36
|
+
const fnImports = [];
|
|
37
|
+
const modelImports = new Set();
|
|
38
|
+
for (const op of sorted) {
|
|
39
|
+
const fnName = (0, template_1.camelCase)(op.operationId);
|
|
40
|
+
const paramsName = `${(0, template_1.pascalCase)(op.operationId)}$Params`;
|
|
41
|
+
fnImports.push(`import { ${fnName}, type ${paramsName} } from '../fn/${(0, template_1.kebabCase)(tag)}/${(0, template_1.kebabCase)(op.operationId)}';`);
|
|
42
|
+
if (op.successRef)
|
|
43
|
+
modelImports.add(op.successRef);
|
|
44
|
+
}
|
|
45
|
+
const modelImportLines = [...modelImports]
|
|
46
|
+
.sort()
|
|
47
|
+
.map((ref) => `import type { ${(0, template_1.pascalCase)(ref)} } from '../models/${(0, template_1.kebabCase)(ref)}';`);
|
|
48
|
+
const methods = sorted.map((op) => renderMethod(op)).join('\n\n');
|
|
49
|
+
return `${target.banner}
|
|
50
|
+
|
|
51
|
+
import { HttpContext, HttpResponse } from '@angular/common/http';
|
|
52
|
+
import { Injectable } from '@angular/core';
|
|
53
|
+
import { Observable } from 'rxjs';
|
|
54
|
+
import { filter, map } from 'rxjs/operators';
|
|
55
|
+
|
|
56
|
+
import { BaseService } from '../base-service';
|
|
57
|
+
import { StrictHttpResponse } from '../strict-http-response';
|
|
58
|
+
|
|
59
|
+
${fnImports.join('\n')}
|
|
60
|
+
${modelImportLines.length ? '\n' + modelImportLines.join('\n') : ''}
|
|
61
|
+
|
|
62
|
+
@Injectable({ providedIn: 'root' })
|
|
63
|
+
export class ${className} extends BaseService {
|
|
64
|
+
${methods}
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
function renderMethod(op) {
|
|
69
|
+
const fnName = (0, template_1.camelCase)(op.operationId);
|
|
70
|
+
const paramsName = `${(0, template_1.pascalCase)(op.operationId)}$Params`;
|
|
71
|
+
const responseType = op.successRef ? (0, template_1.pascalCase)(op.successRef) : 'unknown';
|
|
72
|
+
const hasParams = op.params.length > 0 || Boolean(op.bodySchemaRef);
|
|
73
|
+
const anyRequired = op.params.some((p) => p.required) || (op.bodySchemaRef != null && op.bodyRequired);
|
|
74
|
+
const paramsSig = hasParams ? `params${anyRequired ? '' : '?'}: ${paramsName}` : `params?: ${paramsName}`;
|
|
75
|
+
return ` /** ${op.summary ?? op.operationId} (${op.method.toUpperCase()} ${op.path}) */
|
|
76
|
+
${fnName}$Response(${paramsSig}, context?: HttpContext): Observable<StrictHttpResponse<${responseType}>> {
|
|
77
|
+
return ${fnName}(this.http, this.rootUrl, params as ${paramsName}, context).pipe(
|
|
78
|
+
filter((r: unknown): r is HttpResponse<unknown> => r instanceof HttpResponse),
|
|
79
|
+
map((r) => r as StrictHttpResponse<${responseType}>),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** ${op.summary ?? op.operationId} (${op.method.toUpperCase()} ${op.path}) */
|
|
84
|
+
${fnName}(${paramsSig}, context?: HttpContext): Observable<${responseType}> {
|
|
85
|
+
return this.${fnName}$Response(params as ${paramsName}, context).pipe(map((r) => r.body));
|
|
86
|
+
}`;
|
|
87
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generate = generate;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const client_1 = require("./emit/client");
|
|
6
|
+
const metadata_1 = require("./emit/metadata");
|
|
7
|
+
const models_1 = require("./emit/models");
|
|
8
|
+
const navigation_1 = require("./emit/navigation");
|
|
9
|
+
const operations_1 = require("./emit/operations");
|
|
10
|
+
const public_api_1 = require("./emit/public-api");
|
|
11
|
+
const services_1 = require("./emit/services");
|
|
12
|
+
const per_domain_1 = require("./layout/per-domain");
|
|
13
|
+
const ir_1 = require("./parser/ir");
|
|
14
|
+
const bundle_1 = require("./parser/bundle");
|
|
15
|
+
const index_1 = require("./writer/index");
|
|
16
|
+
/**
|
|
17
|
+
* Run the generator for a single resolved target. Pure in the sense that it
|
|
18
|
+
* does **not** touch the filesystem — the caller is expected to materialize
|
|
19
|
+
* `result.files` either into an Angular schematic `Tree` or onto disk.
|
|
20
|
+
*/
|
|
21
|
+
async function generate(target, workspaceRoot) {
|
|
22
|
+
const specPath = (0, node_path_1.isAbsolute)(target.input) ? target.input : (0, node_path_1.resolve)(workspaceRoot, target.input);
|
|
23
|
+
const doc = await (0, bundle_1.loadSpec)(specPath);
|
|
24
|
+
const ir = (0, ir_1.buildIR)(doc);
|
|
25
|
+
const files = [];
|
|
26
|
+
if (target.features.client)
|
|
27
|
+
files.push(...(0, client_1.emitClient)(target));
|
|
28
|
+
if (target.features.models)
|
|
29
|
+
files.push(...(0, models_1.emitModels)(ir, target));
|
|
30
|
+
if (target.features.operations)
|
|
31
|
+
files.push(...(0, operations_1.emitOperations)(ir, target));
|
|
32
|
+
if (target.features.services)
|
|
33
|
+
files.push(...(0, services_1.emitServices)(ir, target));
|
|
34
|
+
if (target.features.metadata)
|
|
35
|
+
files.push(...(0, metadata_1.emitMetadata)(ir, target));
|
|
36
|
+
if (target.features.navigation)
|
|
37
|
+
files.push(...(0, navigation_1.emitNavigation)(ir, target));
|
|
38
|
+
// public-api barrel is emitted last so it sees the final feature set.
|
|
39
|
+
files.push(...(0, public_api_1.emitPublicApi)(ir, target));
|
|
40
|
+
const laidOut = target.splitByDomain ? (0, per_domain_1.relayoutPerDomain)(files, ir, target) : files;
|
|
41
|
+
const wrapped = applyMode(laidOut, ir, target);
|
|
42
|
+
const outputDir = (0, node_path_1.isAbsolute)(target.output) ? target.output : (0, node_path_1.resolve)(workspaceRoot, target.output);
|
|
43
|
+
return {
|
|
44
|
+
target,
|
|
45
|
+
outputDir,
|
|
46
|
+
files: wrapped,
|
|
47
|
+
stats: {
|
|
48
|
+
schemas: ir.schemas.length,
|
|
49
|
+
operations: ir.operations.length,
|
|
50
|
+
tags: new Set(ir.operations.map((o) => o.tag)).size,
|
|
51
|
+
files: wrapped.length,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function applyMode(files, ir, target) {
|
|
56
|
+
switch (target.mode) {
|
|
57
|
+
case 'library':
|
|
58
|
+
return (0, index_1.writeLibrary)(files, ir, target);
|
|
59
|
+
case 'secondary-entrypoint':
|
|
60
|
+
return (0, index_1.writeSecondaryEntrypoint)(files, ir, target);
|
|
61
|
+
case 'standalone':
|
|
62
|
+
default:
|
|
63
|
+
return (0, index_1.writeStandalone)(files);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.relayoutPerDomain = relayoutPerDomain;
|
|
4
|
+
/**
|
|
5
|
+
* Post-emit transform that reorganises the flat generator output into a
|
|
6
|
+
* per-domain layout:
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* <outputDir>/
|
|
10
|
+
* shared/ client primitives, shared models, metadata, validators
|
|
11
|
+
* <domain>/ one folder per OpenAPI tag (kebab-cased)
|
|
12
|
+
* models/ models only referenced by this domain
|
|
13
|
+
* fn/ tree-shakeable operation functions for this domain
|
|
14
|
+
* services/ Injectable service class(es) for this domain
|
|
15
|
+
* permissions/ per-tag permission rules
|
|
16
|
+
* public-api.ts
|
|
17
|
+
* public-api.ts aggregator: re-exports shared + every domain
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The emitters themselves are not aware of the transform — they keep producing
|
|
21
|
+
* flat paths and relative imports. This module walks the emitted virtual files,
|
|
22
|
+
* rewrites their paths, and patches relative imports so everything still lines
|
|
23
|
+
* up.
|
|
24
|
+
*/
|
|
25
|
+
const node_path_1 = require("node:path");
|
|
26
|
+
const template_1 = require("../render/template");
|
|
27
|
+
const SHARED = 'shared';
|
|
28
|
+
const VIRTUAL_ROOT = '/__sdk__';
|
|
29
|
+
function relayoutPerDomain(files, ir, target) {
|
|
30
|
+
const mapping = computeMapping(files, ir, target);
|
|
31
|
+
// 1. Rewrite paths + relative imports, drop the original public-api.ts so we
|
|
32
|
+
// can regenerate it with domain awareness.
|
|
33
|
+
const rewritten = [];
|
|
34
|
+
for (const file of files) {
|
|
35
|
+
if (file.path === 'public-api.ts')
|
|
36
|
+
continue;
|
|
37
|
+
const newPath = mapping.oldToNew.get(file.path);
|
|
38
|
+
if (!newPath)
|
|
39
|
+
continue;
|
|
40
|
+
rewritten.push({
|
|
41
|
+
path: newPath,
|
|
42
|
+
content: rewriteImports(file.content, file.path, newPath, mapping.oldToNew),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
// 2. Emit per-domain + root public-api.ts.
|
|
46
|
+
rewritten.push(...emitPublicApis(ir, target, mapping));
|
|
47
|
+
return rewritten;
|
|
48
|
+
}
|
|
49
|
+
function computeMapping(files, ir, target) {
|
|
50
|
+
// Domain per tag. Two strategies are supported, selected by
|
|
51
|
+
// `target.splitDepth`:
|
|
52
|
+
//
|
|
53
|
+
// 'service' (default): walk each tag's `parent` chain up to the root and
|
|
54
|
+
// use the kebab-cased root name. Every tag collapses into its owning
|
|
55
|
+
// backend service (e.g. `Role`, `Permission`, `Role Permission` →
|
|
56
|
+
// `access`; `Approval Definition`, `Approval Stage`, ... → `approval`;
|
|
57
|
+
// `GCS`, `S3` → `storage`).
|
|
58
|
+
//
|
|
59
|
+
// 'tag': emit one folder per leaf tag, nested under the parent chain so
|
|
60
|
+
// related tags stay grouped (e.g. `storage/gcs`, `storage/s3`,
|
|
61
|
+
// `access/role`, `access/role-permission`). Tags without a parent
|
|
62
|
+
// become a single-segment path (e.g. `auth`).
|
|
63
|
+
//
|
|
64
|
+
// In both cases the resulting string may contain `/` and is treated as a
|
|
65
|
+
// relative path. Collect operation + service lookups up front so callers
|
|
66
|
+
// can iterate without re-scanning.
|
|
67
|
+
const tagByName = new Map(ir.tags.map((t) => [t.name, t]));
|
|
68
|
+
const tagChain = (name) => {
|
|
69
|
+
const chain = [];
|
|
70
|
+
const seen = new Set();
|
|
71
|
+
let current = name;
|
|
72
|
+
while (current && !seen.has(current)) {
|
|
73
|
+
seen.add(current);
|
|
74
|
+
chain.push(current);
|
|
75
|
+
current = tagByName.get(current)?.parent;
|
|
76
|
+
}
|
|
77
|
+
return chain.reverse();
|
|
78
|
+
};
|
|
79
|
+
const domainForTag = (tag) => {
|
|
80
|
+
const chain = tagChain(tag);
|
|
81
|
+
if (chain.length === 0)
|
|
82
|
+
return SHARED;
|
|
83
|
+
if (target.splitDepth === 'tag') {
|
|
84
|
+
return (chain
|
|
85
|
+
.map((segment) => (0, template_1.kebabCase)(segment))
|
|
86
|
+
.filter(Boolean)
|
|
87
|
+
.join('/') || SHARED);
|
|
88
|
+
}
|
|
89
|
+
return (0, template_1.kebabCase)(chain[0]) || SHARED;
|
|
90
|
+
};
|
|
91
|
+
const domainOperations = new Map();
|
|
92
|
+
const domainServices = new Map();
|
|
93
|
+
const tagToDomain = new Map();
|
|
94
|
+
for (const op of ir.operations) {
|
|
95
|
+
const domain = domainForTag(op.tag);
|
|
96
|
+
tagToDomain.set(op.tag, domain);
|
|
97
|
+
const ops = domainOperations.get(domain) ?? [];
|
|
98
|
+
ops.push(op.operationId);
|
|
99
|
+
domainOperations.set(domain, ops);
|
|
100
|
+
const svcs = domainServices.get(domain) ?? [];
|
|
101
|
+
if (!svcs.includes(op.tag))
|
|
102
|
+
svcs.push(op.tag);
|
|
103
|
+
domainServices.set(domain, svcs);
|
|
104
|
+
}
|
|
105
|
+
const domains = [...domainOperations.keys()].sort();
|
|
106
|
+
// Model ownership: a model belongs to a domain only if it is exclusively
|
|
107
|
+
// referenced by that domain's operations (directly or transitively).
|
|
108
|
+
const modelDomains = new Map();
|
|
109
|
+
const markModel = (name, domain) => {
|
|
110
|
+
let set = modelDomains.get(name);
|
|
111
|
+
if (!set) {
|
|
112
|
+
set = new Set();
|
|
113
|
+
modelDomains.set(name, set);
|
|
114
|
+
}
|
|
115
|
+
set.add(domain);
|
|
116
|
+
};
|
|
117
|
+
for (const op of ir.operations) {
|
|
118
|
+
const domain = tagToDomain.get(op.tag) ?? SHARED;
|
|
119
|
+
for (const p of op.params) {
|
|
120
|
+
if (p.rule.ref)
|
|
121
|
+
markModel(p.rule.ref, domain);
|
|
122
|
+
if (p.rule.itemsRef)
|
|
123
|
+
markModel(p.rule.itemsRef, domain);
|
|
124
|
+
}
|
|
125
|
+
if (op.bodySchemaRef)
|
|
126
|
+
markModel(op.bodySchemaRef, domain);
|
|
127
|
+
if (op.successRef)
|
|
128
|
+
markModel(op.successRef, domain);
|
|
129
|
+
}
|
|
130
|
+
// Transitive closure: a model's nested refs inherit its domain set.
|
|
131
|
+
const schemaByName = new Map(ir.schemas.map((s) => [s.name, s]));
|
|
132
|
+
let changed = true;
|
|
133
|
+
while (changed) {
|
|
134
|
+
changed = false;
|
|
135
|
+
for (const [name, domains] of [...modelDomains]) {
|
|
136
|
+
const schema = schemaByName.get(name);
|
|
137
|
+
if (!schema)
|
|
138
|
+
continue;
|
|
139
|
+
const refs = new Set();
|
|
140
|
+
if (schema.arrayItemRef)
|
|
141
|
+
refs.add(schema.arrayItemRef);
|
|
142
|
+
for (const rule of Object.values(schema.properties)) {
|
|
143
|
+
if (rule.ref)
|
|
144
|
+
refs.add(rule.ref);
|
|
145
|
+
if (rule.itemsRef)
|
|
146
|
+
refs.add(rule.itemsRef);
|
|
147
|
+
}
|
|
148
|
+
for (const ref of refs) {
|
|
149
|
+
let target = modelDomains.get(ref);
|
|
150
|
+
if (!target) {
|
|
151
|
+
target = new Set();
|
|
152
|
+
modelDomains.set(ref, target);
|
|
153
|
+
}
|
|
154
|
+
for (const d of domains) {
|
|
155
|
+
if (!target.has(d)) {
|
|
156
|
+
target.add(d);
|
|
157
|
+
changed = true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const modelOwner = new Map();
|
|
164
|
+
for (const schema of ir.schemas) {
|
|
165
|
+
const set = modelDomains.get(schema.name);
|
|
166
|
+
if (!set || set.size !== 1)
|
|
167
|
+
modelOwner.set(schema.name, SHARED);
|
|
168
|
+
else
|
|
169
|
+
modelOwner.set(schema.name, [...set][0]);
|
|
170
|
+
}
|
|
171
|
+
// Build old → new path map.
|
|
172
|
+
const oldToNew = new Map();
|
|
173
|
+
const kebabToSchema = new Map(ir.schemas.map((s) => [(0, template_1.kebabCase)(s.name), s.name]));
|
|
174
|
+
for (const file of files) {
|
|
175
|
+
const np = computeNewPath(file.path, {
|
|
176
|
+
modelOwner,
|
|
177
|
+
kebabToSchema,
|
|
178
|
+
tagToDomain,
|
|
179
|
+
});
|
|
180
|
+
if (np)
|
|
181
|
+
oldToNew.set(file.path, np);
|
|
182
|
+
}
|
|
183
|
+
return { oldToNew, modelOwner, domains, domainOperations, domainServices };
|
|
184
|
+
}
|
|
185
|
+
function computeNewPath(oldPath, ctx) {
|
|
186
|
+
// Root-level client primitives → shared/.
|
|
187
|
+
const rootShared = new Set([
|
|
188
|
+
'api-configuration.ts',
|
|
189
|
+
'base-service.ts',
|
|
190
|
+
'request-builder.ts',
|
|
191
|
+
'strict-http-response.ts',
|
|
192
|
+
'api.ts',
|
|
193
|
+
'api.navigation.ts',
|
|
194
|
+
'metadata.ts',
|
|
195
|
+
'metadata-types.ts',
|
|
196
|
+
'openapi-helpers.ts',
|
|
197
|
+
]);
|
|
198
|
+
if (rootShared.has(oldPath))
|
|
199
|
+
return `${SHARED}/${oldPath}`;
|
|
200
|
+
if (oldPath.startsWith('models/')) {
|
|
201
|
+
const kebab = oldPath.slice('models/'.length).replace(/\.ts$/, '');
|
|
202
|
+
const name = ctx.kebabToSchema.get(kebab);
|
|
203
|
+
const owner = (name && ctx.modelOwner.get(name)) || SHARED;
|
|
204
|
+
return `${owner}/models/${kebab}.ts`;
|
|
205
|
+
}
|
|
206
|
+
if (oldPath.startsWith('fn/')) {
|
|
207
|
+
// fn/<tag-kebab>/<op-kebab>.ts → <domain>/fn/<op-kebab>.ts
|
|
208
|
+
const parts = oldPath.split('/');
|
|
209
|
+
if (parts.length < 3)
|
|
210
|
+
return undefined;
|
|
211
|
+
const tagKebab = parts[1];
|
|
212
|
+
const fileName = parts.slice(2).join('/');
|
|
213
|
+
const domain = findDomainFromKebab(tagKebab, ctx.tagToDomain);
|
|
214
|
+
return `${domain}/fn/${fileName}`;
|
|
215
|
+
}
|
|
216
|
+
if (oldPath.startsWith('services/')) {
|
|
217
|
+
const fileName = oldPath.slice('services/'.length);
|
|
218
|
+
const tagKebab = fileName.replace(/\.service\.ts$/, '');
|
|
219
|
+
const domain = findDomainFromKebab(tagKebab, ctx.tagToDomain);
|
|
220
|
+
return `${domain}/services/${fileName}`;
|
|
221
|
+
}
|
|
222
|
+
if (oldPath === 'permissions/index.ts')
|
|
223
|
+
return `${SHARED}/permissions/index.ts`;
|
|
224
|
+
if (oldPath.startsWith('permissions/')) {
|
|
225
|
+
const fileName = oldPath.slice('permissions/'.length);
|
|
226
|
+
const tagKebab = fileName.replace(/\.ts$/, '');
|
|
227
|
+
const domain = findDomainFromKebab(tagKebab, ctx.tagToDomain);
|
|
228
|
+
return `${domain}/permissions/${fileName}`;
|
|
229
|
+
}
|
|
230
|
+
if (oldPath.startsWith('validators/'))
|
|
231
|
+
return `${SHARED}/${oldPath}`;
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
function findDomainFromKebab(tagKebab, tagToDomain) {
|
|
235
|
+
for (const [tag, domain] of tagToDomain) {
|
|
236
|
+
if ((0, template_1.kebabCase)(tag) === tagKebab)
|
|
237
|
+
return domain;
|
|
238
|
+
}
|
|
239
|
+
return tagKebab || SHARED;
|
|
240
|
+
}
|
|
241
|
+
function rewriteImports(content, oldPath, newPath, oldToNew) {
|
|
242
|
+
const oldAbs = node_path_1.posix.join(VIRTUAL_ROOT, oldPath);
|
|
243
|
+
const newAbs = node_path_1.posix.join(VIRTUAL_ROOT, newPath);
|
|
244
|
+
const oldDir = (0, node_path_1.dirname)(oldAbs);
|
|
245
|
+
const newDir = (0, node_path_1.dirname)(newAbs);
|
|
246
|
+
const replaceRelative = (spec) => {
|
|
247
|
+
if (!spec.startsWith('./') && !spec.startsWith('../'))
|
|
248
|
+
return spec;
|
|
249
|
+
// Resolve target old absolute (append `.ts` candidates).
|
|
250
|
+
const resolved = node_path_1.posix.normalize(node_path_1.posix.join(oldDir, spec));
|
|
251
|
+
const candidates = [`${resolved}.ts`, `${resolved}/index.ts`, resolved];
|
|
252
|
+
for (const cand of candidates) {
|
|
253
|
+
const rel = cand.startsWith(VIRTUAL_ROOT + '/') ? cand.slice(VIRTUAL_ROOT.length + 1) : null;
|
|
254
|
+
if (!rel)
|
|
255
|
+
continue;
|
|
256
|
+
const np = oldToNew.get(rel);
|
|
257
|
+
if (!np)
|
|
258
|
+
continue;
|
|
259
|
+
const targetAbs = node_path_1.posix.join(VIRTUAL_ROOT, np);
|
|
260
|
+
let nextSpec = node_path_1.posix.relative(newDir, targetAbs).replace(/\.ts$/, '');
|
|
261
|
+
if (!nextSpec.startsWith('.'))
|
|
262
|
+
nextSpec = `./${nextSpec}`;
|
|
263
|
+
return nextSpec;
|
|
264
|
+
}
|
|
265
|
+
return spec;
|
|
266
|
+
};
|
|
267
|
+
return content.replace(/(from\s+['"])([^'"]+)(['"])/g, (_m, a, spec, c) => `${a}${replaceRelative(spec)}${c}`);
|
|
268
|
+
}
|
|
269
|
+
function emitPublicApis(ir, target, mapping) {
|
|
270
|
+
const out = [];
|
|
271
|
+
const schemaNames = ir.schemas.map((s) => s.name);
|
|
272
|
+
// shared/public-api.ts — client primitives + shared models + metadata.
|
|
273
|
+
const sharedLines = [target.banner, ''];
|
|
274
|
+
if (target.features.client) {
|
|
275
|
+
sharedLines.push(`export { ApiConfiguration, provideApiConfiguration } from './api-configuration';`);
|
|
276
|
+
sharedLines.push(`export { BaseService } from './base-service';`);
|
|
277
|
+
sharedLines.push(`export { RequestBuilder } from './request-builder';`);
|
|
278
|
+
sharedLines.push(`export type { StrictHttpResponse } from './strict-http-response';`);
|
|
279
|
+
sharedLines.push(`export { ${target.clientName} } from './api';`);
|
|
280
|
+
sharedLines.push('');
|
|
281
|
+
}
|
|
282
|
+
if (target.features.models) {
|
|
283
|
+
const sharedModels = schemaNames.filter((n) => mapping.modelOwner.get(n) === SHARED);
|
|
284
|
+
for (const name of sharedModels) {
|
|
285
|
+
sharedLines.push(`export type { ${(0, template_1.pascalCase)(name)} } from './models/${(0, template_1.kebabCase)(name)}';`);
|
|
286
|
+
}
|
|
287
|
+
if (sharedModels.length)
|
|
288
|
+
sharedLines.push('');
|
|
289
|
+
}
|
|
290
|
+
if (target.features.metadata) {
|
|
291
|
+
sharedLines.push(`export * from './metadata';`);
|
|
292
|
+
sharedLines.push(`export * from './openapi-helpers';`);
|
|
293
|
+
sharedLines.push('');
|
|
294
|
+
}
|
|
295
|
+
if (target.features.navigation) {
|
|
296
|
+
sharedLines.push(`export { ApiNavigation } from './api.navigation';`);
|
|
297
|
+
}
|
|
298
|
+
out.push({ path: `${SHARED}/public-api.ts`, content: (0, template_1.finalize)(sharedLines.join('\n')) });
|
|
299
|
+
// Per-domain public-api.ts.
|
|
300
|
+
for (const domain of mapping.domains) {
|
|
301
|
+
const lines = [target.banner, ''];
|
|
302
|
+
// Re-export shared namespace for convenience. `domain` may be nested
|
|
303
|
+
// (e.g. `storage/gcs`), so compute the relative path instead of
|
|
304
|
+
// hard-coding `../shared/public-api`.
|
|
305
|
+
const domainDir = node_path_1.posix.join(VIRTUAL_ROOT, domain);
|
|
306
|
+
const sharedTarget = node_path_1.posix.join(VIRTUAL_ROOT, SHARED, 'public-api');
|
|
307
|
+
let sharedSpec = node_path_1.posix.relative(domainDir, sharedTarget);
|
|
308
|
+
if (!sharedSpec.startsWith('.'))
|
|
309
|
+
sharedSpec = `./${sharedSpec}`;
|
|
310
|
+
lines.push(`export * from '${sharedSpec}';`);
|
|
311
|
+
lines.push('');
|
|
312
|
+
if (target.features.models) {
|
|
313
|
+
const ownedModels = schemaNames.filter((n) => mapping.modelOwner.get(n) === domain);
|
|
314
|
+
for (const name of ownedModels) {
|
|
315
|
+
lines.push(`export type { ${(0, template_1.pascalCase)(name)} } from './models/${(0, template_1.kebabCase)(name)}';`);
|
|
316
|
+
}
|
|
317
|
+
if (ownedModels.length)
|
|
318
|
+
lines.push('');
|
|
319
|
+
}
|
|
320
|
+
if (target.features.services) {
|
|
321
|
+
const tags = mapping.domainServices.get(domain) ?? [];
|
|
322
|
+
for (const tag of tags.sort()) {
|
|
323
|
+
lines.push(`export { ${(0, template_1.pascalCase)(tag)}Service } from './services/${(0, template_1.kebabCase)(tag)}.service';`);
|
|
324
|
+
}
|
|
325
|
+
if (tags.length)
|
|
326
|
+
lines.push('');
|
|
327
|
+
}
|
|
328
|
+
if (target.features.operations) {
|
|
329
|
+
const opIds = (mapping.domainOperations.get(domain) ?? []).slice().sort();
|
|
330
|
+
for (const opId of opIds) {
|
|
331
|
+
const fnName = (0, template_1.camelCase)(opId);
|
|
332
|
+
const paramsName = `${(0, template_1.pascalCase)(opId)}$Params`;
|
|
333
|
+
lines.push(`export { ${fnName}, type ${paramsName} } from './fn/${(0, template_1.kebabCase)(opId)}';`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
out.push({ path: `${domain}/public-api.ts`, content: (0, template_1.finalize)(lines.join('\n')) });
|
|
337
|
+
}
|
|
338
|
+
// Root public-api.ts aggregates everything.
|
|
339
|
+
const rootLines = [target.banner, ''];
|
|
340
|
+
rootLines.push(`export * from './${SHARED}/public-api';`);
|
|
341
|
+
for (const domain of mapping.domains) {
|
|
342
|
+
rootLines.push(`export * from './${domain}/public-api';`);
|
|
343
|
+
}
|
|
344
|
+
out.push({ path: 'public-api.ts', content: (0, template_1.finalize)(rootLines.join('\n')) });
|
|
345
|
+
return out;
|
|
346
|
+
}
|