@ojiepermana/angular-sdk 22.0.44 → 22.0.45

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.
@@ -0,0 +1,359 @@
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 types, validators
11
+ * metadata.ts aggregate metadata barrel (root only)
12
+ * openapi-helpers.ts metadata helpers (root only)
13
+ * permissions/index.ts aggregate operation rules (root only)
14
+ * <domain>/ one folder per OpenAPI tag (kebab-cased)
15
+ * models/ models only referenced by this domain
16
+ * fn/ tree-shakeable operation functions for this domain
17
+ * services/ Injectable service class(es) for this domain
18
+ * permissions/ per-tag permission rules
19
+ * public-api.ts
20
+ * public-api.ts aggregator: re-exports shared + every domain
21
+ * ```
22
+ *
23
+ * The emitters themselves are not aware of the transform — they keep producing
24
+ * flat paths and relative imports. This module walks the emitted virtual files,
25
+ * rewrites their paths, and patches relative imports so everything still lines
26
+ * up.
27
+ */
28
+ const node_path_1 = require("node:path");
29
+ const template_1 = require("../render/template");
30
+ const SHARED = 'shared';
31
+ const VIRTUAL_ROOT = '/__sdk__';
32
+ function relayoutPerDomain(files, ir, target) {
33
+ const mapping = computeMapping(files, ir, target);
34
+ // 1. Rewrite paths + relative imports, drop the original public-api.ts so we
35
+ // can regenerate it with domain awareness.
36
+ const rewritten = [];
37
+ for (const file of files) {
38
+ if (file.path === 'public-api.ts')
39
+ continue;
40
+ const newPath = mapping.oldToNew.get(file.path);
41
+ if (!newPath)
42
+ continue;
43
+ rewritten.push({
44
+ path: newPath,
45
+ content: rewriteImports(file.content, file.path, newPath, mapping.oldToNew),
46
+ });
47
+ }
48
+ // 2. Emit per-domain + root public-api.ts.
49
+ rewritten.push(...emitPublicApis(ir, target, mapping));
50
+ return rewritten;
51
+ }
52
+ function computeMapping(files, ir, target) {
53
+ // Domain per tag. Two strategies are supported, selected by
54
+ // `target.splitDepth`:
55
+ //
56
+ // 'service' (default): walk each tag's `parent` chain up to the root and
57
+ // use the kebab-cased root name. Every tag collapses into its owning
58
+ // backend service (e.g. `Role`, `Permission`, `Role Permission` →
59
+ // `access`; `Approval Definition`, `Approval Stage`, ... → `approval`;
60
+ // `GCS`, `S3` → `storage`).
61
+ //
62
+ // 'tag': emit one folder per leaf tag, nested under the parent chain so
63
+ // related tags stay grouped (e.g. `storage/gcs`, `storage/s3`,
64
+ // `access/role`, `access/role-permission`). Tags without a parent
65
+ // become a single-segment path (e.g. `auth`).
66
+ //
67
+ // In both cases the resulting string may contain `/` and is treated as a
68
+ // relative path. Collect operation + service lookups up front so callers
69
+ // can iterate without re-scanning.
70
+ const tagByName = new Map(ir.tags.map((t) => [t.name, t]));
71
+ const tagChain = (name) => {
72
+ const chain = [];
73
+ const seen = new Set();
74
+ let current = name;
75
+ while (current && !seen.has(current)) {
76
+ seen.add(current);
77
+ chain.push(current);
78
+ current = tagByName.get(current)?.parent;
79
+ }
80
+ return chain.reverse();
81
+ };
82
+ const domainForTag = (tag) => {
83
+ const chain = tagChain(tag);
84
+ if (chain.length === 0)
85
+ return SHARED;
86
+ if (target.splitDepth === 'tag') {
87
+ return (chain
88
+ .map((segment) => (0, template_1.kebabCase)(segment))
89
+ .filter(Boolean)
90
+ .join('/') || SHARED);
91
+ }
92
+ return (0, template_1.kebabCase)(chain[0]) || SHARED;
93
+ };
94
+ const domainOperations = new Map();
95
+ const domainServices = new Map();
96
+ const tagToDomain = new Map();
97
+ for (const op of ir.operations) {
98
+ const domain = domainForTag(op.tag);
99
+ tagToDomain.set(op.tag, domain);
100
+ const ops = domainOperations.get(domain) ?? [];
101
+ ops.push(op.operationId);
102
+ domainOperations.set(domain, ops);
103
+ const svcs = domainServices.get(domain) ?? [];
104
+ if (!svcs.includes(op.tag))
105
+ svcs.push(op.tag);
106
+ domainServices.set(domain, svcs);
107
+ }
108
+ const domains = [...domainOperations.keys()].sort();
109
+ // Model ownership: a model belongs to a domain only if it is exclusively
110
+ // referenced by that domain's operations (directly or transitively).
111
+ const modelDomains = new Map();
112
+ const markModel = (name, domain) => {
113
+ let set = modelDomains.get(name);
114
+ if (!set) {
115
+ set = new Set();
116
+ modelDomains.set(name, set);
117
+ }
118
+ set.add(domain);
119
+ };
120
+ for (const op of ir.operations) {
121
+ const domain = tagToDomain.get(op.tag) ?? SHARED;
122
+ for (const p of op.params) {
123
+ if (p.rule.ref)
124
+ markModel(p.rule.ref, domain);
125
+ if (p.rule.itemsRef)
126
+ markModel(p.rule.itemsRef, domain);
127
+ }
128
+ if (op.bodySchemaRef)
129
+ markModel(op.bodySchemaRef, domain);
130
+ if (op.successRef)
131
+ markModel(op.successRef, domain);
132
+ }
133
+ // Transitive closure: a model's nested refs inherit its domain set.
134
+ const schemaByName = new Map(ir.schemas.map((s) => [s.name, s]));
135
+ let changed = true;
136
+ while (changed) {
137
+ changed = false;
138
+ for (const [name, domains] of [...modelDomains]) {
139
+ const schema = schemaByName.get(name);
140
+ if (!schema)
141
+ continue;
142
+ const refs = new Set();
143
+ if (schema.arrayItemRef)
144
+ refs.add(schema.arrayItemRef);
145
+ for (const rule of Object.values(schema.properties)) {
146
+ if (rule.ref)
147
+ refs.add(rule.ref);
148
+ if (rule.itemsRef)
149
+ refs.add(rule.itemsRef);
150
+ }
151
+ for (const ref of refs) {
152
+ let target = modelDomains.get(ref);
153
+ if (!target) {
154
+ target = new Set();
155
+ modelDomains.set(ref, target);
156
+ }
157
+ for (const d of domains) {
158
+ if (!target.has(d)) {
159
+ target.add(d);
160
+ changed = true;
161
+ }
162
+ }
163
+ }
164
+ }
165
+ }
166
+ const modelOwner = new Map();
167
+ for (const schema of ir.schemas) {
168
+ const set = modelDomains.get(schema.name);
169
+ if (!set || set.size !== 1)
170
+ modelOwner.set(schema.name, SHARED);
171
+ else
172
+ modelOwner.set(schema.name, [...set][0]);
173
+ }
174
+ // Build old → new path map.
175
+ const oldToNew = new Map();
176
+ const kebabToSchema = new Map(ir.schemas.map((s) => [(0, template_1.kebabCase)(s.name), s.name]));
177
+ for (const file of files) {
178
+ const np = computeNewPath(file.path, {
179
+ modelOwner,
180
+ kebabToSchema,
181
+ tagToDomain,
182
+ });
183
+ if (np)
184
+ oldToNew.set(file.path, np);
185
+ }
186
+ return { oldToNew, modelOwner, domains, domainOperations, domainServices };
187
+ }
188
+ function computeNewPath(oldPath, ctx) {
189
+ // Root-level client primitives → shared/.
190
+ const rootShared = new Set([
191
+ 'api-configuration.ts',
192
+ 'base-service.ts',
193
+ 'request-builder.ts',
194
+ 'strict-http-response.ts',
195
+ 'api.ts',
196
+ 'api.navigation.ts',
197
+ 'metadata-types.ts',
198
+ ]);
199
+ if (rootShared.has(oldPath))
200
+ return `${SHARED}/${oldPath}`;
201
+ if (oldPath === 'metadata.ts' || oldPath === 'openapi-helpers.ts') {
202
+ return oldPath;
203
+ }
204
+ if (oldPath.startsWith('models/')) {
205
+ const kebab = oldPath.slice('models/'.length).replace(/\.ts$/, '');
206
+ const name = ctx.kebabToSchema.get(kebab);
207
+ const owner = (name && ctx.modelOwner.get(name)) || SHARED;
208
+ return `${owner}/models/${kebab}.ts`;
209
+ }
210
+ if (oldPath.startsWith('fn/')) {
211
+ // fn/<tag-kebab>/<op-kebab>.ts → <domain>/fn/<op-kebab>.ts
212
+ const parts = oldPath.split('/');
213
+ if (parts.length < 3)
214
+ return undefined;
215
+ const tagKebab = parts[1];
216
+ const fileName = parts.slice(2).join('/');
217
+ const domain = findDomainFromKebab(tagKebab, ctx.tagToDomain);
218
+ return `${domain}/fn/${fileName}`;
219
+ }
220
+ if (oldPath.startsWith('services/')) {
221
+ const fileName = oldPath.slice('services/'.length);
222
+ const tagKebab = fileName.replace(/\.service\.ts$/, '');
223
+ const domain = findDomainFromKebab(tagKebab, ctx.tagToDomain);
224
+ return `${domain}/services/${fileName}`;
225
+ }
226
+ if (oldPath === 'permissions/index.ts')
227
+ return oldPath;
228
+ if (oldPath.startsWith('permissions/')) {
229
+ const fileName = oldPath.slice('permissions/'.length);
230
+ const tagKebab = fileName.replace(/\.ts$/, '');
231
+ const domain = findDomainFromKebab(tagKebab, ctx.tagToDomain);
232
+ return `${domain}/permissions/${fileName}`;
233
+ }
234
+ if (oldPath.startsWith('validators/'))
235
+ return `${SHARED}/${oldPath}`;
236
+ return undefined;
237
+ }
238
+ function findDomainFromKebab(tagKebab, tagToDomain) {
239
+ for (const [tag, domain] of tagToDomain) {
240
+ if ((0, template_1.kebabCase)(tag) === tagKebab)
241
+ return domain;
242
+ }
243
+ return tagKebab || SHARED;
244
+ }
245
+ function rewriteImports(content, oldPath, newPath, oldToNew) {
246
+ const oldAbs = node_path_1.posix.join(VIRTUAL_ROOT, oldPath);
247
+ const newAbs = node_path_1.posix.join(VIRTUAL_ROOT, newPath);
248
+ const oldDir = (0, node_path_1.dirname)(oldAbs);
249
+ const newDir = (0, node_path_1.dirname)(newAbs);
250
+ const replaceRelative = (spec) => {
251
+ if (!spec.startsWith('./') && !spec.startsWith('../'))
252
+ return spec;
253
+ // Resolve target old absolute (append `.ts` candidates).
254
+ const resolved = node_path_1.posix.normalize(node_path_1.posix.join(oldDir, spec));
255
+ const candidates = [`${resolved}.ts`, `${resolved}/index.ts`, resolved];
256
+ for (const cand of candidates) {
257
+ const rel = cand.startsWith(VIRTUAL_ROOT + '/') ? cand.slice(VIRTUAL_ROOT.length + 1) : null;
258
+ if (!rel)
259
+ continue;
260
+ const np = oldToNew.get(rel);
261
+ if (!np)
262
+ continue;
263
+ const targetAbs = node_path_1.posix.join(VIRTUAL_ROOT, np);
264
+ let nextSpec = node_path_1.posix.relative(newDir, targetAbs).replace(/\.ts$/, '');
265
+ if (!nextSpec.startsWith('.'))
266
+ nextSpec = `./${nextSpec}`;
267
+ return nextSpec;
268
+ }
269
+ return spec;
270
+ };
271
+ return content.replace(/(from\s+['"])([^'"]+)(['"])/g, (_m, a, spec, c) => `${a}${replaceRelative(spec)}${c}`);
272
+ }
273
+ function emitPublicApis(ir, target, mapping) {
274
+ const out = [];
275
+ const schemaNames = ir.schemas.map((s) => s.name);
276
+ // shared/public-api.ts — shared primitives + shared models only.
277
+ const sharedLines = [target.banner, ''];
278
+ if (target.features.client) {
279
+ sharedLines.push(`export { ApiConfiguration, provideApiConfiguration } from './api-configuration';`);
280
+ sharedLines.push(`export { BaseService } from './base-service';`);
281
+ sharedLines.push(`export { RequestBuilder } from './request-builder';`);
282
+ sharedLines.push(`export type { StrictHttpResponse } from './strict-http-response';`);
283
+ sharedLines.push(`export { ${target.clientName} } from './api';`);
284
+ sharedLines.push('');
285
+ }
286
+ if (target.features.models) {
287
+ const sharedModels = schemaNames.filter((n) => mapping.modelOwner.get(n) === SHARED);
288
+ for (const name of sharedModels) {
289
+ sharedLines.push(`export type { ${(0, template_1.pascalCase)(name)} } from './models/${(0, template_1.kebabCase)(name)}';`);
290
+ }
291
+ if (sharedModels.length)
292
+ sharedLines.push('');
293
+ }
294
+ if (target.features.metadata) {
295
+ sharedLines.push(`export * from './metadata-types';`);
296
+ sharedLines.push(`export * from './validators/index';`);
297
+ sharedLines.push('');
298
+ }
299
+ if (target.features.navigation) {
300
+ sharedLines.push(`export { ApiNavigation } from './api.navigation';`);
301
+ }
302
+ out.push({ path: `${SHARED}/public-api.ts`, content: (0, template_1.finalize)(sharedLines.join('\n')) });
303
+ // Per-domain public-api.ts.
304
+ for (const domain of mapping.domains) {
305
+ const lines = [target.banner, ''];
306
+ // Keep each domain entrypoint scoped to its own surface. Re-exporting the
307
+ // shared barrel here makes the root barrel publish the same names twice
308
+ // once it also exports `./shared/public-api`.
309
+ if (target.features.models) {
310
+ const ownedModels = schemaNames.filter((n) => mapping.modelOwner.get(n) === domain);
311
+ for (const name of ownedModels) {
312
+ lines.push(`export type { ${(0, template_1.pascalCase)(name)} } from './models/${(0, template_1.kebabCase)(name)}';`);
313
+ }
314
+ if (ownedModels.length)
315
+ lines.push('');
316
+ }
317
+ if (target.features.metadata) {
318
+ const tags = (mapping.domainServices.get(domain) ?? []).slice().sort();
319
+ for (const tag of tags) {
320
+ lines.push(`export { ${(0, template_1.camelCase)(tag)}OperationRules } from './permissions/${(0, template_1.kebabCase)(tag)}';`);
321
+ }
322
+ if (tags.length)
323
+ lines.push('');
324
+ }
325
+ if (target.features.services) {
326
+ const tags = mapping.domainServices.get(domain) ?? [];
327
+ for (const tag of tags.sort()) {
328
+ lines.push(`export { ${(0, template_1.pascalCase)(tag)}Service } from './services/${(0, template_1.kebabCase)(tag)}.service';`);
329
+ }
330
+ if (tags.length)
331
+ lines.push('');
332
+ }
333
+ if (target.features.operations) {
334
+ const opIds = (mapping.domainOperations.get(domain) ?? []).slice().sort();
335
+ for (const opId of opIds) {
336
+ const fnName = (0, template_1.camelCase)(opId);
337
+ const paramsName = `${(0, template_1.pascalCase)(opId)}$Params`;
338
+ lines.push(`export { ${fnName}, type ${paramsName} } from './fn/${(0, template_1.kebabCase)(opId)}';`);
339
+ }
340
+ }
341
+ out.push({ path: `${domain}/public-api.ts`, content: (0, template_1.finalize)(lines.join('\n')) });
342
+ }
343
+ // Root public-api.ts aggregates everything for non-library consumers. In
344
+ // library mode, domain folders are real secondary entrypoints, so the root
345
+ // stays focused on shared/global exports and consumers deep import domains.
346
+ const rootLines = [target.banner, ''];
347
+ rootLines.push(`export * from './${SHARED}/public-api';`);
348
+ if (target.features.metadata) {
349
+ rootLines.push(`export * from './metadata';`);
350
+ rootLines.push(`export * from './openapi-helpers';`);
351
+ }
352
+ if (target.mode !== 'library') {
353
+ for (const domain of mapping.domains) {
354
+ rootLines.push(`export * from './${domain}/public-api';`);
355
+ }
356
+ }
357
+ out.push({ path: 'public-api.ts', content: (0, template_1.finalize)(rootLines.join('\n')) });
358
+ return out;
359
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.loadSpec = loadSpec;
7
+ const node_fs_1 = require("node:fs");
8
+ const node_path_1 = require("node:path");
9
+ const yaml_1 = __importDefault(require("yaml"));
10
+ /**
11
+ * Load an OpenAPI spec from disk. We deliberately do NOT run Swagger Parser's
12
+ * strict validator here because it rejects OpenAPI 3.2.x, while the rest of
13
+ * the pipeline only needs structural access to `components` + `paths` (which
14
+ * is version-stable across 3.x). Specs that rely on external `$ref`s should
15
+ * be bundled first with a separate tool.
16
+ *
17
+ * Accepts `.yaml`/`.yml`/`.json`.
18
+ */
19
+ async function loadSpec(path) {
20
+ const text = (0, node_fs_1.readFileSync)(path, 'utf8');
21
+ const ext = (0, node_path_1.extname)(path).toLowerCase();
22
+ if (ext === '.json')
23
+ return JSON.parse(text);
24
+ return yaml_1.default.parse(text);
25
+ }