@lssm/bundle.contractspec-workspace 0.0.0-canary-20251217060834 → 0.0.0-canary-20251217072406
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/dist/_virtual/rolldown_runtime.js +22 -1
- package/dist/adapters/ai.js +82 -1
- package/dist/adapters/factory.js +36 -1
- package/dist/adapters/fs.js +118 -1
- package/dist/adapters/git.js +54 -1
- package/dist/adapters/index.js +7 -1
- package/dist/adapters/logger.js +80 -1
- package/dist/adapters/watcher.js +69 -1
- package/dist/adapters/workspace.js +190 -2
- package/dist/ai/agents/claude-code-agent.js +146 -9
- package/dist/ai/agents/cursor-agent.js +286 -17
- package/dist/ai/agents/index.js +5 -1
- package/dist/ai/agents/openai-codex-agent.js +140 -8
- package/dist/ai/agents/orchestrator.js +142 -1
- package/dist/ai/agents/simple-agent.js +80 -4
- package/dist/ai/client.js +162 -1
- package/dist/ai/index.js +27 -1
- package/dist/ai/prompts/code-generation.js +55 -13
- package/dist/ai/prompts/index.js +12 -1
- package/dist/ai/prompts/spec-creation.js +61 -20
- package/dist/ai/providers.js +40 -1
- package/dist/formatters/index.js +18 -1
- package/dist/formatters/json.js +71 -1
- package/dist/formatters/sarif.js +163 -1
- package/dist/formatters/text.js +208 -2
- package/dist/index.d.ts +0 -3
- package/dist/index.js +81 -1
- package/dist/libs/ai-providers/dist/factory.js +154 -0
- package/dist/libs/ai-providers/dist/index.js +4 -0
- package/dist/libs/ai-providers/dist/legacy.js +72 -0
- package/dist/libs/ai-providers/dist/models.js +287 -0
- package/dist/libs/ai-providers/dist/validation.js +1 -0
- package/dist/libs/contracts/dist/capabilities/openbanking.js +88 -0
- package/dist/libs/contracts/dist/client/index.js +5 -0
- package/dist/libs/contracts/dist/client/react/feature-render.js +2 -0
- package/dist/libs/contracts/dist/client/react/form-render.js +4 -0
- package/dist/libs/contracts/dist/client/react/index.js +4 -0
- package/dist/libs/contracts/dist/contract-registry/index.js +1 -0
- package/dist/libs/contracts/dist/contract-registry/schemas.js +60 -0
- package/dist/libs/contracts/dist/docs/PUBLISHING.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/accessibility_wcag_compliance_specs.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/index.js +29 -0
- package/dist/libs/contracts/dist/docs/presentations.js +71 -0
- package/dist/libs/contracts/dist/docs/registry.js +44 -0
- package/dist/libs/contracts/dist/docs/tech/PHASE_1_QUICKSTART.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/tech/PHASE_2_AI_NATIVE_OPERATIONS.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/tech/PHASE_3_AUTO_EVOLUTION.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/tech/PHASE_4_PERSONALIZATION_ENGINE.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/tech/PHASE_5_ZERO_TOUCH_OPERATIONS.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/tech/auth/better-auth-nextjs.docblock.js +80 -0
- package/dist/libs/contracts/dist/docs/tech/contracts/openapi-export.docblock.js +57 -0
- package/dist/libs/contracts/dist/docs/tech/lifecycle-stage-system.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/tech/llm/llm-integration.docblock.js +357 -0
- package/dist/libs/contracts/dist/docs/tech/mcp-endpoints.docblock.js +37 -0
- package/dist/libs/contracts/dist/docs/tech/presentation-runtime.docblock.js +16 -0
- package/dist/libs/contracts/dist/docs/tech/schema/README.docblock.js +20 -0
- package/dist/libs/contracts/dist/docs/tech/studio/learning-events.docblock.js +48 -0
- package/dist/libs/contracts/dist/docs/tech/studio/learning-journeys.docblock.js +79 -0
- package/dist/libs/contracts/dist/docs/tech/studio/platform-admin-panel.docblock.js +84 -0
- package/dist/libs/contracts/dist/docs/tech/studio/project-access-teams.docblock.js +45 -0
- package/dist/libs/contracts/dist/docs/tech/studio/project-routing.docblock.js +67 -0
- package/dist/libs/contracts/dist/docs/tech/studio/sandbox-unlogged.docblock.js +40 -0
- package/dist/libs/contracts/dist/docs/tech/studio/team-invitations.docblock.js +69 -0
- package/dist/libs/contracts/dist/docs/tech/studio/workspace-ops.docblock.js +47 -0
- package/dist/libs/contracts/dist/docs/tech/studio/workspaces.docblock.js +62 -0
- package/dist/libs/contracts/dist/docs/tech/telemetry-ingest.docblock.js +155 -0
- package/dist/libs/contracts/dist/docs/tech/templates/runtime.docblock.js +20 -0
- package/dist/libs/contracts/dist/docs/tech/vscode-extension.docblock.js +101 -0
- package/dist/libs/contracts/dist/docs/tech/workflows/overview.docblock.js +20 -0
- package/dist/libs/contracts/dist/events.js +8 -0
- package/dist/libs/contracts/dist/experiments/evaluator.js +1 -0
- package/dist/libs/contracts/dist/index.js +72 -0
- package/dist/libs/contracts/dist/install.js +2 -0
- package/dist/libs/contracts/dist/integrations/contracts.js +377 -0
- package/dist/libs/contracts/dist/integrations/index.js +18 -0
- package/dist/libs/contracts/dist/integrations/openbanking/contracts/accounts.js +228 -0
- package/dist/libs/contracts/dist/integrations/openbanking/contracts/balances.js +159 -0
- package/dist/libs/contracts/dist/integrations/openbanking/contracts/index.js +3 -0
- package/dist/libs/contracts/dist/integrations/openbanking/contracts/transactions.js +210 -0
- package/dist/libs/contracts/dist/integrations/openbanking/models.js +242 -0
- package/dist/libs/contracts/dist/integrations/openbanking/telemetry.js +13 -0
- package/dist/libs/contracts/dist/integrations/providers/elevenlabs.js +52 -0
- package/dist/libs/contracts/dist/integrations/providers/gcs-storage.js +75 -0
- package/dist/libs/contracts/dist/integrations/providers/gmail.js +87 -0
- package/dist/libs/contracts/dist/integrations/providers/google-calendar.js +66 -0
- package/dist/libs/contracts/dist/integrations/providers/index.js +11 -0
- package/dist/libs/contracts/dist/integrations/providers/mistral.js +68 -0
- package/dist/libs/contracts/dist/integrations/providers/postmark.js +68 -0
- package/dist/libs/contracts/dist/integrations/providers/powens.js +116 -0
- package/dist/libs/contracts/dist/integrations/providers/qdrant.js +73 -0
- package/dist/libs/contracts/dist/integrations/providers/registry.js +10 -0
- package/dist/libs/contracts/dist/integrations/providers/stripe.js +83 -0
- package/dist/libs/contracts/dist/integrations/providers/twilio-sms.js +61 -0
- package/dist/libs/contracts/dist/jsonschema.js +24 -0
- package/dist/libs/contracts/dist/knowledge/contracts.js +306 -0
- package/dist/libs/contracts/dist/knowledge/index.js +7 -0
- package/dist/libs/contracts/dist/knowledge/spaces/email-threads.js +34 -0
- package/dist/libs/contracts/dist/knowledge/spaces/financial-docs.js +34 -0
- package/dist/libs/contracts/dist/knowledge/spaces/financial-overview.js +38 -0
- package/dist/libs/contracts/dist/knowledge/spaces/index.js +6 -0
- package/dist/libs/contracts/dist/knowledge/spaces/product-canon.js +34 -0
- package/dist/libs/contracts/dist/knowledge/spaces/support-faq.js +37 -0
- package/dist/libs/contracts/dist/knowledge/spaces/uploaded-docs.js +34 -0
- package/dist/libs/contracts/dist/llm/exporters.js +352 -0
- package/dist/libs/contracts/dist/llm/index.js +2 -0
- package/dist/libs/contracts/dist/llm/prompts.js +211 -0
- package/dist/libs/contracts/dist/onboarding-base.js +196 -0
- package/dist/libs/contracts/dist/openapi.js +75 -0
- package/dist/libs/contracts/dist/ownership.js +21 -0
- package/dist/libs/contracts/dist/presentations.js +1 -0
- package/dist/libs/contracts/dist/presentations.v2.js +11 -0
- package/dist/libs/contracts/dist/prompt.js +1 -0
- package/dist/libs/contracts/dist/promptRegistry.js +1 -0
- package/dist/libs/contracts/dist/regenerator/index.js +2 -0
- package/dist/libs/contracts/dist/regenerator/service.js +92 -0
- package/dist/libs/contracts/dist/regenerator/utils.js +51 -0
- package/dist/libs/contracts/dist/registry.js +208 -0
- package/dist/libs/contracts/dist/resources.js +1 -0
- package/dist/libs/contracts/dist/schema/dist/EnumType.js +2 -0
- package/dist/libs/contracts/dist/schema/dist/FieldType.js +49 -0
- package/dist/libs/contracts/dist/schema/dist/ScalarTypeEnum.js +236 -0
- package/dist/libs/contracts/dist/schema/dist/SchemaModel.js +34 -0
- package/dist/libs/contracts/dist/schema/dist/entity/defineEntity.js +1 -0
- package/dist/libs/contracts/dist/schema/dist/entity/index.js +2 -0
- package/dist/libs/contracts/dist/schema/dist/entity/types.js +1 -0
- package/dist/libs/contracts/dist/schema/dist/index.js +6 -0
- package/dist/libs/contracts/dist/server/graphql-pothos.js +6 -0
- package/dist/libs/contracts/dist/server/index.js +8 -0
- package/dist/libs/contracts/dist/server/mcp/createMcpServer.js +4 -0
- package/dist/libs/contracts/dist/server/mcp/registerPresentations.js +2 -0
- package/dist/libs/contracts/dist/server/mcp/registerPrompts.js +1 -0
- package/dist/libs/contracts/dist/server/mcp/registerResources.js +2 -0
- package/dist/libs/contracts/dist/server/mcp/registerTools.js +1 -0
- package/dist/libs/contracts/dist/server/provider-mcp.js +1 -0
- package/dist/libs/contracts/dist/server/rest-elysia.js +1 -0
- package/dist/libs/contracts/dist/server/rest-express.js +1 -0
- package/dist/libs/contracts/dist/server/rest-generic.js +1 -0
- package/dist/libs/contracts/dist/server/rest-next-app.js +1 -0
- package/dist/libs/contracts/dist/server/rest-next-pages.js +1 -0
- package/dist/libs/contracts/dist/spec.js +35 -0
- package/dist/libs/contracts/dist/telemetry/index.js +1 -0
- package/dist/libs/contracts/dist/telemetry/tracker.js +1 -0
- package/dist/libs/contracts/dist/tests/index.js +1 -0
- package/dist/libs/contracts/dist/tests/runner.js +150 -0
- package/dist/libs/contracts/dist/workflow/index.js +1 -0
- package/dist/libs/contracts/dist/workflow/runner.js +1 -0
- package/dist/libs/contracts-transformers/dist/common/utils.js +47 -0
- package/dist/libs/contracts-transformers/dist/openapi/exporter.js +1 -0
- package/dist/libs/contracts-transformers/dist/openapi/importer.js +255 -0
- package/dist/libs/contracts-transformers/dist/openapi/index.js +4 -0
- package/dist/libs/contracts-transformers/dist/openapi/parser.js +231 -0
- package/dist/libs/contracts-transformers/dist/openapi/schema-converter.js +201 -0
- package/dist/modules/contractspec-workspace/dist/ai/code-generation.js +137 -0
- package/dist/modules/contractspec-workspace/dist/ai/spec-creation.js +101 -0
- package/dist/modules/contractspec-workspace/dist/analysis/deps/graph.js +84 -0
- package/dist/modules/contractspec-workspace/dist/analysis/deps/parse-imports.js +30 -0
- package/dist/modules/contractspec-workspace/dist/analysis/diff/semantic.js +96 -0
- package/dist/modules/contractspec-workspace/dist/analysis/feature-scan.js +151 -0
- package/dist/modules/contractspec-workspace/dist/analysis/spec-scan.js +344 -0
- package/dist/modules/contractspec-workspace/dist/analysis/validate/spec-structure.js +122 -0
- package/dist/modules/contractspec-workspace/dist/templates/app-config.js +105 -0
- package/dist/modules/contractspec-workspace/dist/templates/data-view.js +68 -0
- package/dist/modules/contractspec-workspace/dist/templates/event.js +38 -0
- package/dist/modules/contractspec-workspace/dist/templates/experiment.js +87 -0
- package/dist/modules/contractspec-workspace/dist/templates/handler.js +95 -0
- package/dist/modules/contractspec-workspace/dist/templates/integration-utils.js +104 -0
- package/dist/modules/contractspec-workspace/dist/templates/integration.js +62 -0
- package/dist/modules/contractspec-workspace/dist/templates/knowledge.js +68 -0
- package/dist/modules/contractspec-workspace/dist/templates/migration.js +60 -0
- package/dist/modules/contractspec-workspace/dist/templates/operation.js +100 -0
- package/dist/modules/contractspec-workspace/dist/templates/presentation.js +78 -0
- package/dist/modules/contractspec-workspace/dist/templates/telemetry.js +89 -0
- package/dist/modules/contractspec-workspace/dist/templates/utils.js +38 -0
- package/dist/modules/contractspec-workspace/dist/templates/workflow-runner.js +48 -0
- package/dist/modules/contractspec-workspace/dist/templates/workflow.js +67 -0
- package/dist/modules/contractspec-workspace/dist/types/generation-types.js +20 -0
- package/dist/services/agent-guide/adapters/claude-code.js +144 -3
- package/dist/services/agent-guide/adapters/cursor-cli.js +135 -3
- package/dist/services/agent-guide/adapters/generic-mcp.js +159 -3
- package/dist/services/agent-guide/adapters/index.js +30 -1
- package/dist/services/agent-guide/agent-guide-service.js +148 -1
- package/dist/services/agent-guide/index.js +5 -1
- package/dist/services/build.js +140 -1
- package/dist/services/ci-check/ci-check-service.js +393 -1
- package/dist/services/ci-check/index.js +2 -1
- package/dist/services/ci-check/types.js +28 -1
- package/dist/services/clean.js +71 -1
- package/dist/services/config.js +76 -1
- package/dist/services/deps.js +62 -1
- package/dist/services/diff.js +33 -1
- package/dist/services/doctor/checks/ai.js +118 -2
- package/dist/services/doctor/checks/cli.js +146 -1
- package/dist/services/doctor/checks/config.js +170 -1
- package/dist/services/doctor/checks/deps.js +180 -1
- package/dist/services/doctor/checks/index.js +6 -1
- package/dist/services/doctor/checks/mcp.js +144 -1
- package/dist/services/doctor/checks/workspace.js +243 -1
- package/dist/services/doctor/doctor-service.js +115 -2
- package/dist/services/doctor/index.js +2 -1
- package/dist/services/doctor/types.js +26 -1
- package/dist/services/implementation/discovery.js +143 -2
- package/dist/services/implementation/index.js +2 -1
- package/dist/services/implementation/resolver.js +223 -1
- package/dist/services/index.js +53 -1
- package/dist/services/integrity-diagram.js +274 -6
- package/dist/services/integrity.js +272 -1
- package/dist/services/list.js +35 -1
- package/dist/services/openapi/export-service.js +51 -2
- package/dist/services/openapi/import-service.js +75 -1
- package/dist/services/openapi/index.js +4 -1
- package/dist/services/openapi/sync-service.js +121 -1
- package/dist/services/openapi/validate-service.js +130 -1
- package/dist/services/regenerator.js +23 -1
- package/dist/services/registry.js +73 -1
- package/dist/services/setup/config-generators.js +113 -26
- package/dist/services/setup/file-merger.js +60 -2
- package/dist/services/setup/index.js +4 -1
- package/dist/services/setup/setup-service.js +95 -1
- package/dist/services/setup/targets/agents-md.js +46 -1
- package/dist/services/setup/targets/cli-config.js +59 -1
- package/dist/services/setup/targets/cursor-rules.js +47 -1
- package/dist/services/setup/targets/mcp-claude.js +59 -1
- package/dist/services/setup/targets/mcp-cursor.js +58 -1
- package/dist/services/setup/targets/vscode-settings.js +62 -1
- package/dist/services/setup/types.js +26 -1
- package/dist/services/sync.js +62 -1
- package/dist/services/test.js +30 -1
- package/dist/services/validate-implementation.js +69 -1
- package/dist/services/validate.js +47 -1
- package/dist/services/verification-cache/adapters/filesystem.js +121 -1
- package/dist/services/verification-cache/adapters/in-memory.js +45 -1
- package/dist/services/verification-cache/adapters/index.js +3 -1
- package/dist/services/verification-cache/adapters/workspace-state.js +90 -1
- package/dist/services/verification-cache/cache-service.js +255 -1
- package/dist/services/verification-cache/index.js +6 -1
- package/dist/services/verification-cache/types.js +15 -1
- package/dist/services/verify/ai-verifier.js +336 -9
- package/dist/services/verify/behavior-verifier.js +185 -1
- package/dist/services/verify/index.js +4 -1
- package/dist/services/verify/structure-verifier.js +195 -2
- package/dist/services/verify/verify-service.js +203 -3
- package/dist/services/watch.js +31 -1
- package/dist/services/workspace-info.js +102 -2
- package/dist/templates/app-config.template.js +101 -28
- package/dist/templates/data-view.template.js +42 -27
- package/dist/templates/event.template.js +29 -14
- package/dist/templates/experiment.template.js +77 -51
- package/dist/templates/handler.template.js +53 -17
- package/dist/templates/index.js +36 -1
- package/dist/templates/integration.template.js +134 -50
- package/dist/templates/knowledge.template.js +62 -21
- package/dist/templates/migration.template.js +50 -26
- package/dist/templates/operation.template.js +44 -28
- package/dist/templates/presentation.template.js +46 -20
- package/dist/templates/telemetry.template.js +74 -53
- package/dist/templates/workflow-runner.template.js +12 -6
- package/dist/templates/workflow.template.js +51 -24
- package/package.json +13 -9
- package/dist/adapters/index.d.ts +0 -7
- package/dist/ports/index.d.ts +0 -5
- package/dist/services/agent-guide/index.d.ts +0 -6
- package/dist/services/ci-check/index.d.ts +0 -2
- package/dist/services/doctor/index.d.ts +0 -2
- package/dist/services/implementation/index.d.ts +0 -3
- package/dist/services/index.d.ts +0 -56
- package/dist/services/openapi/index.d.ts +0 -5
- package/dist/services/verification-cache/adapters/index.d.ts +0 -3
- package/dist/services/verification-cache/index.d.ts +0 -6
- package/dist/services/verify/index.d.ts +0 -5
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { toFileName, toPascalCase, toSpecName, toValidIdentifier } from "../common/utils.js";
|
|
2
|
+
import { generateImports, generateSchemaModelCode } from "./schema-converter.js";
|
|
3
|
+
|
|
4
|
+
//#region ../../libs/contracts-transformers/dist/openapi/importer.js
|
|
5
|
+
/**
|
|
6
|
+
* HTTP methods that typically indicate a command (state-changing).
|
|
7
|
+
*/
|
|
8
|
+
const COMMAND_METHODS = [
|
|
9
|
+
"post",
|
|
10
|
+
"put",
|
|
11
|
+
"delete",
|
|
12
|
+
"patch"
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Determine if an operation is a command or query based on HTTP method.
|
|
16
|
+
*/
|
|
17
|
+
function inferOpKind(method) {
|
|
18
|
+
return COMMAND_METHODS.includes(method.toLowerCase()) ? "command" : "query";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Determine auth level based on security requirements.
|
|
22
|
+
*/
|
|
23
|
+
function inferAuthLevel(operation, defaultAuth) {
|
|
24
|
+
if (!operation.security || operation.security.length === 0) return defaultAuth;
|
|
25
|
+
for (const sec of operation.security) if (Object.keys(sec).length === 0) return "anonymous";
|
|
26
|
+
return "user";
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Build a merged input schema from all parameter sources.
|
|
30
|
+
*/
|
|
31
|
+
function buildInputSchema(operation) {
|
|
32
|
+
const fields = [];
|
|
33
|
+
for (const param of operation.pathParams) fields.push({
|
|
34
|
+
name: param.name,
|
|
35
|
+
schema: param.schema,
|
|
36
|
+
required: true
|
|
37
|
+
});
|
|
38
|
+
for (const param of operation.queryParams) fields.push({
|
|
39
|
+
name: param.name,
|
|
40
|
+
schema: param.schema,
|
|
41
|
+
required: param.required
|
|
42
|
+
});
|
|
43
|
+
const excludedHeaders = [
|
|
44
|
+
"authorization",
|
|
45
|
+
"content-type",
|
|
46
|
+
"accept",
|
|
47
|
+
"user-agent"
|
|
48
|
+
];
|
|
49
|
+
for (const param of operation.headerParams) if (!excludedHeaders.includes(param.name.toLowerCase())) fields.push({
|
|
50
|
+
name: param.name,
|
|
51
|
+
schema: param.schema,
|
|
52
|
+
required: param.required
|
|
53
|
+
});
|
|
54
|
+
if (operation.requestBody?.schema) {
|
|
55
|
+
const bodySchema = operation.requestBody.schema;
|
|
56
|
+
if (!("$ref" in bodySchema)) {
|
|
57
|
+
const schemaObj = bodySchema;
|
|
58
|
+
const properties = schemaObj["properties"];
|
|
59
|
+
const required = schemaObj["required"] ?? [];
|
|
60
|
+
if (properties) for (const [propName, propSchema] of Object.entries(properties)) fields.push({
|
|
61
|
+
name: propName,
|
|
62
|
+
schema: propSchema,
|
|
63
|
+
required: required.includes(propName)
|
|
64
|
+
});
|
|
65
|
+
} else fields.push({
|
|
66
|
+
name: "body",
|
|
67
|
+
schema: bodySchema,
|
|
68
|
+
required: operation.requestBody.required
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (fields.length === 0) return {
|
|
72
|
+
schema: null,
|
|
73
|
+
fields: []
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
schema: {
|
|
77
|
+
type: "object",
|
|
78
|
+
properties: fields.reduce((acc, f) => {
|
|
79
|
+
acc[f.name] = f.schema;
|
|
80
|
+
return acc;
|
|
81
|
+
}, {}),
|
|
82
|
+
required: fields.filter((f) => f.required).map((f) => f.name)
|
|
83
|
+
},
|
|
84
|
+
fields
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get the output schema from the operation responses.
|
|
89
|
+
*/
|
|
90
|
+
function getOutputSchema(operation) {
|
|
91
|
+
for (const code of [
|
|
92
|
+
"200",
|
|
93
|
+
"201",
|
|
94
|
+
"202",
|
|
95
|
+
"204"
|
|
96
|
+
]) {
|
|
97
|
+
const response = operation.responses[code];
|
|
98
|
+
if (response?.schema) return response.schema;
|
|
99
|
+
}
|
|
100
|
+
for (const [code, response] of Object.entries(operation.responses)) if (code.startsWith("2") && response.schema) return response.schema;
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Generate ContractSpec TypeScript code for an operation.
|
|
105
|
+
*/
|
|
106
|
+
function generateSpecCode(operation, options, inputModel, outputModel) {
|
|
107
|
+
const specName = toSpecName(operation.operationId, options.prefix);
|
|
108
|
+
const kind = inferOpKind(operation.method);
|
|
109
|
+
const auth = inferAuthLevel(operation, options.defaultAuth ?? "user");
|
|
110
|
+
const lines = [];
|
|
111
|
+
lines.push("import { defineCommand, defineQuery } from '@lssm/lib.contracts';");
|
|
112
|
+
if (inputModel || outputModel) lines.push(generateImports([...inputModel?.fields ?? [], ...outputModel?.fields ?? []]));
|
|
113
|
+
lines.push("");
|
|
114
|
+
if (inputModel && inputModel.code) {
|
|
115
|
+
lines.push("// Input schema");
|
|
116
|
+
lines.push(inputModel.code);
|
|
117
|
+
lines.push("");
|
|
118
|
+
}
|
|
119
|
+
if (outputModel && outputModel.code) {
|
|
120
|
+
lines.push("// Output schema");
|
|
121
|
+
lines.push(outputModel.code);
|
|
122
|
+
lines.push("");
|
|
123
|
+
}
|
|
124
|
+
const defineFunc = kind === "command" ? "defineCommand" : "defineQuery";
|
|
125
|
+
const safeName = toValidIdentifier(toPascalCase(operation.operationId));
|
|
126
|
+
lines.push(`/**`);
|
|
127
|
+
lines.push(` * ${operation.summary ?? operation.operationId}`);
|
|
128
|
+
if (operation.description) {
|
|
129
|
+
lines.push(` *`);
|
|
130
|
+
lines.push(` * ${operation.description}`);
|
|
131
|
+
}
|
|
132
|
+
lines.push(` *`);
|
|
133
|
+
lines.push(` * @source OpenAPI: ${operation.method.toUpperCase()} ${operation.path}`);
|
|
134
|
+
lines.push(` */`);
|
|
135
|
+
lines.push(`export const ${safeName}Spec = ${defineFunc}({`);
|
|
136
|
+
lines.push(" meta: {");
|
|
137
|
+
lines.push(` name: '${specName}',`);
|
|
138
|
+
lines.push(" version: 1,");
|
|
139
|
+
lines.push(` stability: '${options.defaultStability ?? "stable"}',`);
|
|
140
|
+
lines.push(` owners: [${(options.defaultOwners ?? []).map((o) => `'${o}'`).join(", ")}],`);
|
|
141
|
+
lines.push(` tags: [${operation.tags.map((t) => `'${t}'`).join(", ")}],`);
|
|
142
|
+
lines.push(` description: ${JSON.stringify(operation.summary ?? operation.operationId)},`);
|
|
143
|
+
lines.push(` goal: ${JSON.stringify(operation.description ?? "Imported from OpenAPI")},`);
|
|
144
|
+
lines.push(` context: 'Imported from OpenAPI: ${operation.method.toUpperCase()} ${operation.path}',`);
|
|
145
|
+
lines.push(" },");
|
|
146
|
+
lines.push(" io: {");
|
|
147
|
+
if (inputModel) lines.push(` input: ${inputModel.name},`);
|
|
148
|
+
else lines.push(" input: null,");
|
|
149
|
+
if (outputModel) lines.push(` output: ${outputModel.name},`);
|
|
150
|
+
else lines.push(" output: null, // TODO: Define output schema");
|
|
151
|
+
lines.push(" },");
|
|
152
|
+
lines.push(" policy: {");
|
|
153
|
+
lines.push(` auth: '${auth}',`);
|
|
154
|
+
lines.push(" },");
|
|
155
|
+
lines.push(" transport: {");
|
|
156
|
+
lines.push(" rest: {");
|
|
157
|
+
lines.push(` method: '${operation.method.toUpperCase()}',`);
|
|
158
|
+
lines.push(` path: '${operation.path}',`);
|
|
159
|
+
lines.push(" },");
|
|
160
|
+
lines.push(" },");
|
|
161
|
+
lines.push("});");
|
|
162
|
+
return lines.join("\n");
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Import operations from a parsed OpenAPI document.
|
|
166
|
+
*/
|
|
167
|
+
function importFromOpenApi(parseResult, options = {}) {
|
|
168
|
+
const { tags, exclude = [], include } = options;
|
|
169
|
+
const specs = [];
|
|
170
|
+
const skipped = [];
|
|
171
|
+
const errors = [];
|
|
172
|
+
for (const operation of parseResult.operations) {
|
|
173
|
+
if (tags && tags.length > 0) {
|
|
174
|
+
if (!operation.tags.some((t) => tags.includes(t))) {
|
|
175
|
+
skipped.push({
|
|
176
|
+
sourceId: operation.operationId,
|
|
177
|
+
reason: `No matching tags (has: ${operation.tags.join(", ")})`
|
|
178
|
+
});
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (include && include.length > 0) {
|
|
183
|
+
if (!include.includes(operation.operationId)) {
|
|
184
|
+
skipped.push({
|
|
185
|
+
sourceId: operation.operationId,
|
|
186
|
+
reason: "Not in include list"
|
|
187
|
+
});
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
} else if (exclude.includes(operation.operationId)) {
|
|
191
|
+
skipped.push({
|
|
192
|
+
sourceId: operation.operationId,
|
|
193
|
+
reason: "In exclude list"
|
|
194
|
+
});
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (operation.deprecated && options.defaultStability !== "deprecated") {
|
|
198
|
+
skipped.push({
|
|
199
|
+
sourceId: operation.operationId,
|
|
200
|
+
reason: "Deprecated operation"
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
const { schema: inputSchema } = buildInputSchema(operation);
|
|
206
|
+
const inputModel = inputSchema ? generateSchemaModelCode(inputSchema, `${operation.operationId}Input`) : null;
|
|
207
|
+
const outputSchema = getOutputSchema(operation);
|
|
208
|
+
const code = generateSpecCode(operation, options, inputModel, outputSchema ? generateSchemaModelCode(outputSchema, `${operation.operationId}Output`) : null);
|
|
209
|
+
const fileName = toFileName(toSpecName(operation.operationId, options.prefix));
|
|
210
|
+
const transportHints = { rest: {
|
|
211
|
+
method: operation.method.toUpperCase(),
|
|
212
|
+
path: operation.path,
|
|
213
|
+
params: {
|
|
214
|
+
path: operation.pathParams.map((p) => p.name),
|
|
215
|
+
query: operation.queryParams.map((p) => p.name),
|
|
216
|
+
header: operation.headerParams.map((p) => p.name),
|
|
217
|
+
cookie: operation.cookieParams.map((p) => p.name)
|
|
218
|
+
}
|
|
219
|
+
} };
|
|
220
|
+
const source = {
|
|
221
|
+
type: "openapi",
|
|
222
|
+
sourceId: operation.operationId,
|
|
223
|
+
operationId: operation.operationId,
|
|
224
|
+
openApiVersion: parseResult.version,
|
|
225
|
+
importedAt: /* @__PURE__ */ new Date()
|
|
226
|
+
};
|
|
227
|
+
specs.push({
|
|
228
|
+
spec: {},
|
|
229
|
+
code,
|
|
230
|
+
fileName,
|
|
231
|
+
source,
|
|
232
|
+
transportHints
|
|
233
|
+
});
|
|
234
|
+
} catch (error) {
|
|
235
|
+
errors.push({
|
|
236
|
+
sourceId: operation.operationId,
|
|
237
|
+
error: error instanceof Error ? error.message : String(error)
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
specs,
|
|
243
|
+
skipped,
|
|
244
|
+
errors,
|
|
245
|
+
summary: {
|
|
246
|
+
total: parseResult.operations.length,
|
|
247
|
+
imported: specs.length,
|
|
248
|
+
skipped: skipped.length,
|
|
249
|
+
errors: errors.length
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
//#endregion
|
|
255
|
+
export { importFromOpenApi };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString } from "./parser.js";
|
|
2
|
+
import "./exporter.js";
|
|
3
|
+
import { generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType } from "./schema-converter.js";
|
|
4
|
+
import { importFromOpenApi } from "./importer.js";
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { parse } from "yaml";
|
|
2
|
+
|
|
3
|
+
//#region ../../libs/contracts-transformers/dist/openapi/parser.js
|
|
4
|
+
/**
|
|
5
|
+
* OpenAPI document parser.
|
|
6
|
+
* Parses OpenAPI 3.x documents from JSON/YAML files or URLs.
|
|
7
|
+
*/
|
|
8
|
+
const HTTP_METHODS = [
|
|
9
|
+
"get",
|
|
10
|
+
"post",
|
|
11
|
+
"put",
|
|
12
|
+
"delete",
|
|
13
|
+
"patch",
|
|
14
|
+
"head",
|
|
15
|
+
"options",
|
|
16
|
+
"trace"
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Parse an OpenAPI document from a string (JSON or YAML).
|
|
20
|
+
*/
|
|
21
|
+
function parseOpenApiString(content, format = "json") {
|
|
22
|
+
if (format === "yaml") return parse(content);
|
|
23
|
+
return JSON.parse(content);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Detect the format of content (JSON or YAML).
|
|
27
|
+
*/
|
|
28
|
+
function detectFormat(content) {
|
|
29
|
+
const trimmed = content.trim();
|
|
30
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) return "json";
|
|
31
|
+
return "yaml";
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Detect OpenAPI version from document.
|
|
35
|
+
*/
|
|
36
|
+
function detectVersion(doc) {
|
|
37
|
+
if (doc.openapi.startsWith("3.1")) return "3.1";
|
|
38
|
+
return "3.0";
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check if a value is a reference object.
|
|
42
|
+
*/
|
|
43
|
+
function isReference(obj) {
|
|
44
|
+
return typeof obj === "object" && obj !== null && "$ref" in obj;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolve a $ref reference in the document.
|
|
48
|
+
*/
|
|
49
|
+
function resolveRef(doc, ref) {
|
|
50
|
+
if (!ref.startsWith("#/")) return;
|
|
51
|
+
const path = ref.slice(2).split("/");
|
|
52
|
+
let current = doc;
|
|
53
|
+
for (const part of path) {
|
|
54
|
+
if (current === null || current === void 0) return void 0;
|
|
55
|
+
if (typeof current !== "object") return void 0;
|
|
56
|
+
current = current[part];
|
|
57
|
+
}
|
|
58
|
+
return current;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolve a schema, following $ref if needed.
|
|
62
|
+
*/
|
|
63
|
+
function resolveSchema(doc, schema) {
|
|
64
|
+
if (!schema) return void 0;
|
|
65
|
+
if (isReference(schema)) return resolveRef(doc, schema.$ref) ?? schema;
|
|
66
|
+
return schema;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Parse parameters from an operation.
|
|
70
|
+
*/
|
|
71
|
+
function parseParameters(doc, params) {
|
|
72
|
+
const result = {
|
|
73
|
+
path: [],
|
|
74
|
+
query: [],
|
|
75
|
+
header: [],
|
|
76
|
+
cookie: []
|
|
77
|
+
};
|
|
78
|
+
if (!params) return result;
|
|
79
|
+
for (const param of params) {
|
|
80
|
+
let resolved;
|
|
81
|
+
if (isReference(param)) {
|
|
82
|
+
const ref = resolveRef(doc, param.$ref);
|
|
83
|
+
if (!ref) continue;
|
|
84
|
+
resolved = ref;
|
|
85
|
+
} else resolved = param;
|
|
86
|
+
const parsed = {
|
|
87
|
+
name: resolved.name,
|
|
88
|
+
in: resolved.in,
|
|
89
|
+
required: resolved.required ?? resolved.in === "path",
|
|
90
|
+
description: resolved.description,
|
|
91
|
+
schema: resolved.schema,
|
|
92
|
+
deprecated: resolved.deprecated ?? false
|
|
93
|
+
};
|
|
94
|
+
result[resolved.in]?.push(parsed);
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Generate an operationId if not present.
|
|
100
|
+
*/
|
|
101
|
+
function generateOperationId(method, path) {
|
|
102
|
+
return method + path.split("/").filter(Boolean).map((part) => {
|
|
103
|
+
if (part.startsWith("{") && part.endsWith("}")) return "By" + part.slice(1, -1).charAt(0).toUpperCase() + part.slice(2, -1);
|
|
104
|
+
return part.charAt(0).toUpperCase() + part.slice(1);
|
|
105
|
+
}).join("");
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Parse a single operation.
|
|
109
|
+
*/
|
|
110
|
+
function parseOperation(doc, method, path, operation, pathParams) {
|
|
111
|
+
const params = parseParameters(doc, [...pathParams ?? [], ...operation.parameters ?? []]);
|
|
112
|
+
let requestBody;
|
|
113
|
+
if (operation.requestBody) {
|
|
114
|
+
const body = isReference(operation.requestBody) ? resolveRef(doc, operation.requestBody.$ref) : operation.requestBody;
|
|
115
|
+
if (body) {
|
|
116
|
+
const contentType = Object.keys(body.content ?? {})[0] ?? "application/json";
|
|
117
|
+
const content = body.content?.[contentType];
|
|
118
|
+
if (content?.schema) requestBody = {
|
|
119
|
+
required: body.required ?? false,
|
|
120
|
+
schema: resolveSchema(doc, content.schema),
|
|
121
|
+
contentType
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const responses = {};
|
|
126
|
+
for (const [status, response] of Object.entries(operation.responses ?? {})) {
|
|
127
|
+
const resolved = isReference(response) ? resolveRef(doc, response.$ref) : response;
|
|
128
|
+
if (resolved) {
|
|
129
|
+
const contentType = Object.keys(resolved.content ?? {})[0];
|
|
130
|
+
const content = contentType ? resolved.content?.[contentType] : void 0;
|
|
131
|
+
responses[status] = {
|
|
132
|
+
description: resolved.description,
|
|
133
|
+
schema: content?.schema ? resolveSchema(doc, content.schema) : void 0,
|
|
134
|
+
contentType
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const contractSpecMeta = operation?.["x-contractspec"];
|
|
139
|
+
return {
|
|
140
|
+
operationId: operation.operationId ?? generateOperationId(method, path),
|
|
141
|
+
method,
|
|
142
|
+
path,
|
|
143
|
+
summary: operation.summary,
|
|
144
|
+
description: operation.description,
|
|
145
|
+
tags: operation.tags ?? [],
|
|
146
|
+
pathParams: params.path,
|
|
147
|
+
queryParams: params.query,
|
|
148
|
+
headerParams: params.header,
|
|
149
|
+
cookieParams: params.cookie,
|
|
150
|
+
requestBody,
|
|
151
|
+
responses,
|
|
152
|
+
deprecated: operation.deprecated ?? false,
|
|
153
|
+
security: operation.security,
|
|
154
|
+
contractSpecMeta
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Parse an OpenAPI document into a structured result.
|
|
159
|
+
*/
|
|
160
|
+
function parseOpenApiDocument(doc, _options = {}) {
|
|
161
|
+
const version = detectVersion(doc);
|
|
162
|
+
const warnings = [];
|
|
163
|
+
const operations = [];
|
|
164
|
+
for (const [path, pathItem] of Object.entries(doc.paths ?? {})) {
|
|
165
|
+
if (!pathItem) continue;
|
|
166
|
+
const pathParams = pathItem.parameters;
|
|
167
|
+
for (const method of HTTP_METHODS) {
|
|
168
|
+
const operation = pathItem[method];
|
|
169
|
+
if (operation) try {
|
|
170
|
+
operations.push(parseOperation(doc, method, path, operation, pathParams));
|
|
171
|
+
} catch (error) {
|
|
172
|
+
warnings.push(`Failed to parse ${method.toUpperCase()} ${path}: ${error}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const schemas = {};
|
|
177
|
+
const components = doc.components;
|
|
178
|
+
if (components?.schemas) for (const [name, schema] of Object.entries(components.schemas)) schemas[name] = schema;
|
|
179
|
+
const servers = (doc.servers ?? []).map((s) => ({
|
|
180
|
+
url: s.url,
|
|
181
|
+
description: s.description,
|
|
182
|
+
variables: s.variables
|
|
183
|
+
}));
|
|
184
|
+
return {
|
|
185
|
+
document: doc,
|
|
186
|
+
version,
|
|
187
|
+
info: {
|
|
188
|
+
title: doc.info.title,
|
|
189
|
+
version: doc.info.version,
|
|
190
|
+
description: doc.info.description
|
|
191
|
+
},
|
|
192
|
+
operations,
|
|
193
|
+
schemas,
|
|
194
|
+
servers,
|
|
195
|
+
warnings
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Parse OpenAPI from a file path or URL.
|
|
200
|
+
* Note: This is an async function that requires I/O adapters.
|
|
201
|
+
* For pure parsing, use parseOpenApiString or parseOpenApiDocument.
|
|
202
|
+
*/
|
|
203
|
+
async function parseOpenApi(source, options = {}) {
|
|
204
|
+
const { fetch: fetchFn = globalThis.fetch, readFile, timeout = 3e4 } = options;
|
|
205
|
+
let content;
|
|
206
|
+
let format;
|
|
207
|
+
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
208
|
+
const controller = new AbortController();
|
|
209
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
210
|
+
try {
|
|
211
|
+
const response = await fetchFn(source, { signal: controller.signal });
|
|
212
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
213
|
+
content = await response.text();
|
|
214
|
+
} finally {
|
|
215
|
+
clearTimeout(timeoutId);
|
|
216
|
+
}
|
|
217
|
+
if (source.endsWith(".yaml") || source.endsWith(".yml")) format = "yaml";
|
|
218
|
+
else if (source.endsWith(".json")) format = "json";
|
|
219
|
+
else format = detectFormat(content);
|
|
220
|
+
} else {
|
|
221
|
+
if (!readFile) throw new Error("readFile adapter required for file paths");
|
|
222
|
+
content = await readFile(source);
|
|
223
|
+
if (source.endsWith(".yaml") || source.endsWith(".yml")) format = "yaml";
|
|
224
|
+
else if (source.endsWith(".json")) format = "json";
|
|
225
|
+
else format = detectFormat(content);
|
|
226
|
+
}
|
|
227
|
+
return parseOpenApiDocument(parseOpenApiString(content, format), options);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
//#endregion
|
|
231
|
+
export { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString };
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { toCamelCase, toPascalCase, toValidIdentifier } from "../common/utils.js";
|
|
2
|
+
|
|
3
|
+
//#region ../../libs/contracts-transformers/dist/openapi/schema-converter.js
|
|
4
|
+
/**
|
|
5
|
+
* Map JSON Schema types to ContractSpec ScalarTypeEnum values.
|
|
6
|
+
*/
|
|
7
|
+
const JSON_SCHEMA_TO_SCALAR = {
|
|
8
|
+
string: "ScalarTypeEnum.STRING",
|
|
9
|
+
integer: "ScalarTypeEnum.INT",
|
|
10
|
+
number: "ScalarTypeEnum.FLOAT",
|
|
11
|
+
boolean: "ScalarTypeEnum.BOOLEAN",
|
|
12
|
+
"string:date": "ScalarTypeEnum.DATE",
|
|
13
|
+
"string:date-time": "ScalarTypeEnum.DATE_TIME",
|
|
14
|
+
"string:email": "ScalarTypeEnum.EMAIL",
|
|
15
|
+
"string:uri": "ScalarTypeEnum.URL",
|
|
16
|
+
"string:uuid": "ScalarTypeEnum.ID"
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Check if a schema is a reference object.
|
|
20
|
+
*/
|
|
21
|
+
function isReference(schema) {
|
|
22
|
+
return "$ref" in schema;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Extract type name from a $ref.
|
|
26
|
+
*/
|
|
27
|
+
function typeNameFromRef(ref) {
|
|
28
|
+
const parts = ref.split("/");
|
|
29
|
+
return parts[parts.length - 1] ?? "Unknown";
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Convert a JSON Schema to a TypeScript type representation.
|
|
33
|
+
*/
|
|
34
|
+
function jsonSchemaToType(schema, name) {
|
|
35
|
+
if (isReference(schema)) return {
|
|
36
|
+
type: toPascalCase(typeNameFromRef(schema.$ref)),
|
|
37
|
+
optional: false,
|
|
38
|
+
array: false,
|
|
39
|
+
primitive: false
|
|
40
|
+
};
|
|
41
|
+
const schemaObj = schema;
|
|
42
|
+
const type = schemaObj["type"];
|
|
43
|
+
const format = schemaObj["format"];
|
|
44
|
+
const nullable = schemaObj["nullable"];
|
|
45
|
+
if (type === "array") {
|
|
46
|
+
const items = schemaObj["items"];
|
|
47
|
+
if (items) return {
|
|
48
|
+
...jsonSchemaToType(items, name),
|
|
49
|
+
array: true,
|
|
50
|
+
optional: nullable ?? false
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
type: "unknown",
|
|
54
|
+
optional: nullable ?? false,
|
|
55
|
+
array: true,
|
|
56
|
+
primitive: false
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (type === "object" || schemaObj["properties"]) return {
|
|
60
|
+
type: name ? toPascalCase(name) : "Record<string, unknown>",
|
|
61
|
+
optional: nullable ?? false,
|
|
62
|
+
array: false,
|
|
63
|
+
primitive: false
|
|
64
|
+
};
|
|
65
|
+
if (schemaObj["enum"]) return {
|
|
66
|
+
type: name ? toPascalCase(name) : "string",
|
|
67
|
+
optional: nullable ?? false,
|
|
68
|
+
array: false,
|
|
69
|
+
primitive: false
|
|
70
|
+
};
|
|
71
|
+
format && `${type}${format}`;
|
|
72
|
+
if (type === "string") return {
|
|
73
|
+
type: "string",
|
|
74
|
+
optional: nullable ?? false,
|
|
75
|
+
array: false,
|
|
76
|
+
primitive: true
|
|
77
|
+
};
|
|
78
|
+
if (type === "integer" || type === "number") return {
|
|
79
|
+
type: "number",
|
|
80
|
+
optional: nullable ?? false,
|
|
81
|
+
array: false,
|
|
82
|
+
primitive: true
|
|
83
|
+
};
|
|
84
|
+
if (type === "boolean") return {
|
|
85
|
+
type: "boolean",
|
|
86
|
+
optional: nullable ?? false,
|
|
87
|
+
array: false,
|
|
88
|
+
primitive: true
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
type: "unknown",
|
|
92
|
+
optional: nullable ?? false,
|
|
93
|
+
array: false,
|
|
94
|
+
primitive: false
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get the ScalarTypeEnum value for a JSON Schema type.
|
|
99
|
+
*/
|
|
100
|
+
function getScalarType(schema) {
|
|
101
|
+
if (isReference(schema)) return;
|
|
102
|
+
const schemaObj = schema;
|
|
103
|
+
const type = schemaObj["type"];
|
|
104
|
+
const format = schemaObj["format"];
|
|
105
|
+
if (!type) return void 0;
|
|
106
|
+
return JSON_SCHEMA_TO_SCALAR[format ? `${type}:${format}` : type] ?? JSON_SCHEMA_TO_SCALAR[type];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Convert a JSON Schema to a SchemaModel field definition.
|
|
110
|
+
*/
|
|
111
|
+
function jsonSchemaToField(schema, fieldName, required) {
|
|
112
|
+
const type = jsonSchemaToType(schema, fieldName);
|
|
113
|
+
const scalarType = getScalarType(schema);
|
|
114
|
+
let enumValues;
|
|
115
|
+
if (!isReference(schema)) {
|
|
116
|
+
const enumArr = schema["enum"];
|
|
117
|
+
if (enumArr) enumValues = enumArr.map(String);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
name: toValidIdentifier(toCamelCase(fieldName)),
|
|
121
|
+
type: {
|
|
122
|
+
...type,
|
|
123
|
+
optional: !required || type.optional,
|
|
124
|
+
description: !isReference(schema) ? schema["description"] : void 0
|
|
125
|
+
},
|
|
126
|
+
scalarType,
|
|
127
|
+
enumValues
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Generate SchemaModel TypeScript code for a JSON Schema object.
|
|
132
|
+
*/
|
|
133
|
+
function generateSchemaModelCode(schema, modelName, indent = 0) {
|
|
134
|
+
const spaces = " ".repeat(indent);
|
|
135
|
+
const fields = [];
|
|
136
|
+
let description;
|
|
137
|
+
if (isReference(schema)) return {
|
|
138
|
+
name: toPascalCase(typeNameFromRef(schema.$ref)),
|
|
139
|
+
fields: [],
|
|
140
|
+
code: `// Reference to ${schema.$ref}`
|
|
141
|
+
};
|
|
142
|
+
const schemaObj = schema;
|
|
143
|
+
description = schemaObj["description"];
|
|
144
|
+
const properties = schemaObj["properties"];
|
|
145
|
+
const required = schemaObj["required"] ?? [];
|
|
146
|
+
if (!properties) return {
|
|
147
|
+
name: toPascalCase(modelName),
|
|
148
|
+
description,
|
|
149
|
+
fields: [],
|
|
150
|
+
code: `${spaces}// Empty schema for ${modelName}`
|
|
151
|
+
};
|
|
152
|
+
for (const [propName, propSchema] of Object.entries(properties)) {
|
|
153
|
+
const isRequired = required.includes(propName);
|
|
154
|
+
fields.push(jsonSchemaToField(propSchema, propName, isRequired));
|
|
155
|
+
}
|
|
156
|
+
const lines = [];
|
|
157
|
+
const safeModelName = toPascalCase(toValidIdentifier(modelName));
|
|
158
|
+
lines.push(`${spaces}export const ${safeModelName} = defineSchemaModel({`);
|
|
159
|
+
lines.push(`${spaces} name: '${safeModelName}',`);
|
|
160
|
+
if (description) lines.push(`${spaces} description: ${JSON.stringify(description)},`);
|
|
161
|
+
lines.push(`${spaces} fields: {`);
|
|
162
|
+
for (const field of fields) {
|
|
163
|
+
const fieldLines = generateFieldCode(field, indent + 2);
|
|
164
|
+
lines.push(fieldLines);
|
|
165
|
+
}
|
|
166
|
+
lines.push(`${spaces} },`);
|
|
167
|
+
lines.push(`${spaces}});`);
|
|
168
|
+
return {
|
|
169
|
+
name: safeModelName,
|
|
170
|
+
description,
|
|
171
|
+
fields,
|
|
172
|
+
code: lines.join("\n")
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Generate TypeScript code for a single field.
|
|
177
|
+
*/
|
|
178
|
+
function generateFieldCode(field, indent) {
|
|
179
|
+
const spaces = " ".repeat(indent);
|
|
180
|
+
const lines = [];
|
|
181
|
+
lines.push(`${spaces}${field.name}: {`);
|
|
182
|
+
if (field.enumValues) lines.push(`${spaces} type: new EnumType([${field.enumValues.map((v) => `'${v}'`).join(", ")}]),`);
|
|
183
|
+
else if (field.scalarType) lines.push(`${spaces} type: ${field.scalarType},`);
|
|
184
|
+
else lines.push(`${spaces} type: ${field.type.type}, // TODO: Define or import this type`);
|
|
185
|
+
if (field.type.optional) lines.push(`${spaces} isOptional: true,`);
|
|
186
|
+
if (field.type.array) lines.push(`${spaces} isArray: true,`);
|
|
187
|
+
lines.push(`${spaces}},`);
|
|
188
|
+
return lines.join("\n");
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Generate import statements for a SchemaModel.
|
|
192
|
+
*/
|
|
193
|
+
function generateImports(fields) {
|
|
194
|
+
const imports = /* @__PURE__ */ new Set();
|
|
195
|
+
imports.add("import { defineSchemaModel, ScalarTypeEnum, EnumType } from '@lssm/lib.schema';");
|
|
196
|
+
for (const field of fields) if (!field.type.primitive && !field.enumValues && !field.scalarType) {}
|
|
197
|
+
return Array.from(imports).join("\n");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
export { generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType };
|