@pikku/cli 0.12.6 → 0.12.7

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.
Files changed (115) hide show
  1. package/cli.schema.json +1 -1
  2. package/console-app/assets/{index-AX4YS8AA.js → index-sUj3oFEL.js} +1 -1
  3. package/console-app/index.html +1 -1
  4. package/dist/.pikku/agent/pikku-agent-types.gen.d.ts +6 -6
  5. package/dist/.pikku/agent/pikku-agent-wirings-meta.gen.js +1 -1
  6. package/dist/.pikku/agent/pikku-agent-wirings.gen.d.ts +1 -1
  7. package/dist/.pikku/agent/pikku-agent-wirings.gen.js +1 -1
  8. package/dist/.pikku/channel/pikku-channel-types.gen.d.ts +1 -1
  9. package/dist/.pikku/channel/pikku-channel-types.gen.js +1 -1
  10. package/dist/.pikku/channel/pikku-channels-meta.gen.js +1 -1
  11. package/dist/.pikku/channel/pikku-channels.gen.d.ts +1 -1
  12. package/dist/.pikku/channel/pikku-channels.gen.js +1 -1
  13. package/dist/.pikku/cli/pikku-cli-channel.js +1 -1
  14. package/dist/.pikku/cli/pikku-cli-client.gen.d.ts +1 -1
  15. package/dist/.pikku/cli/pikku-cli-client.gen.js +1 -1
  16. package/dist/.pikku/cli/pikku-cli-types.gen.d.ts +1 -1
  17. package/dist/.pikku/cli/pikku-cli-types.gen.js +1 -1
  18. package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.js +1 -1
  19. package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.json +1 -1
  20. package/dist/.pikku/cli/pikku-cli-wirings.gen.d.ts +1 -1
  21. package/dist/.pikku/cli/pikku-cli-wirings.gen.js +1 -1
  22. package/dist/.pikku/cli/pikku-cli.gen.d.ts +1 -1
  23. package/dist/.pikku/cli/pikku-cli.gen.js +1 -1
  24. package/dist/.pikku/console/pikku-node-types.gen.d.ts +1 -1
  25. package/dist/.pikku/function/pikku-function-types.gen.d.ts +1 -1
  26. package/dist/.pikku/function/pikku-function-types.gen.js +1 -1
  27. package/dist/.pikku/function/pikku-functions-meta.gen.js +1 -1
  28. package/dist/.pikku/function/pikku-functions-meta.gen.json +195 -195
  29. package/dist/.pikku/function/pikku-functions.gen.js +1 -1
  30. package/dist/.pikku/http/pikku-http-types.gen.d.ts +1 -1
  31. package/dist/.pikku/http/pikku-http-types.gen.js +1 -1
  32. package/dist/.pikku/http/pikku-http-wirings-meta.gen.js +1 -1
  33. package/dist/.pikku/http/pikku-http-wirings.gen.d.ts +1 -1
  34. package/dist/.pikku/http/pikku-http-wirings.gen.js +1 -1
  35. package/dist/.pikku/mcp/pikku-mcp-types.gen.d.ts +1 -1
  36. package/dist/.pikku/mcp/pikku-mcp-types.gen.js +1 -1
  37. package/dist/.pikku/mcp/pikku-mcp-wirings-meta.gen.js +1 -1
  38. package/dist/.pikku/mcp/pikku-mcp-wirings.gen.d.ts +1 -1
  39. package/dist/.pikku/mcp/pikku-mcp-wirings.gen.js +1 -1
  40. package/dist/.pikku/pikku-bootstrap.gen.js +1 -1
  41. package/dist/.pikku/pikku-services.gen.d.ts +1 -1
  42. package/dist/.pikku/pikku-types.gen.d.ts +1 -1
  43. package/dist/.pikku/pikku-types.gen.js +1 -1
  44. package/dist/.pikku/pikku-websocket.gen.d.ts +1 -1
  45. package/dist/.pikku/pikku-websocket.gen.js +1 -1
  46. package/dist/.pikku/queue/pikku-queue-types.gen.d.ts +1 -1
  47. package/dist/.pikku/queue/pikku-queue-types.gen.js +1 -1
  48. package/dist/.pikku/queue/pikku-queue-workers-wirings-meta.gen.js +1 -1
  49. package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.d.ts +1 -1
  50. package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.js +1 -1
  51. package/dist/.pikku/rpc/pikku-remote-rpc-workers.gen.js +1 -1
  52. package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.js +1 -1
  53. package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.json +2 -2
  54. package/dist/.pikku/scheduler/pikku-scheduler-types.gen.d.ts +1 -1
  55. package/dist/.pikku/scheduler/pikku-scheduler-types.gen.js +1 -1
  56. package/dist/.pikku/scheduler/pikku-schedulers-wirings-meta.gen.js +1 -1
  57. package/dist/.pikku/scheduler/pikku-schedulers-wirings.gen.d.ts +1 -1
  58. package/dist/.pikku/scheduler/pikku-schedulers-wirings.gen.js +1 -1
  59. package/dist/.pikku/schemas/register.gen.js +9 -9
  60. package/dist/.pikku/schemas/schemas/PikkuCLIConfig.schema.json +1 -1
  61. package/dist/.pikku/secrets/pikku-secret-types.gen.d.ts +1 -1
  62. package/dist/.pikku/secrets/pikku-secret-types.gen.js +1 -1
  63. package/dist/.pikku/secrets/pikku-secrets.gen.d.ts +1 -1
  64. package/dist/.pikku/secrets/pikku-secrets.gen.js +1 -1
  65. package/dist/.pikku/trigger/pikku-trigger-types.gen.d.ts +1 -1
  66. package/dist/.pikku/trigger/pikku-trigger-types.gen.js +1 -1
  67. package/dist/.pikku/variables/pikku-variable-types.gen.d.ts +1 -1
  68. package/dist/.pikku/variables/pikku-variable-types.gen.js +1 -1
  69. package/dist/.pikku/variables/pikku-variables.gen.d.ts +1 -1
  70. package/dist/.pikku/variables/pikku-variables.gen.js +1 -1
  71. package/dist/.pikku/workflow/pikku-workflow-types.gen.d.ts +34 -5
  72. package/dist/.pikku/workflow/pikku-workflow-types.gen.js +5 -5
  73. package/dist/.pikku/workflow/pikku-workflow-wirings-meta.gen.js +1 -1
  74. package/dist/.pikku/workflow/pikku-workflow-wirings.gen.d.ts +1 -1
  75. package/dist/.pikku/workflow/pikku-workflow-wirings.gen.js +1 -1
  76. package/dist/src/cli.wiring.js +1 -1
  77. package/dist/src/functions/commands/all.js +16 -1
  78. package/dist/src/functions/commands/new-addon.js +20 -6
  79. package/dist/src/functions/commands/versions-check.js +4 -4
  80. package/dist/src/functions/commands/versions-init.js +1 -1
  81. package/dist/src/functions/commands/versions-update.js +1 -1
  82. package/dist/src/functions/runtimes/nextjs/pikku-command-nextjs.js +2 -2
  83. package/dist/src/functions/runtimes/nextjs/serialize-nextjs-backend-wrapper.d.ts +1 -1
  84. package/dist/src/functions/runtimes/nextjs/serialize-nextjs-backend-wrapper.js +2 -2
  85. package/dist/src/functions/runtimes/nextjs/serialize-nextjs-http-wrapper.d.ts +1 -1
  86. package/dist/src/functions/runtimes/nextjs/serialize-nextjs-http-wrapper.js +2 -2
  87. package/dist/src/functions/wirings/ai-agent/pikku-command-public-agent.js +1 -1
  88. package/dist/src/functions/wirings/ai-agent/serialize-ai-agent-types.js +12 -6
  89. package/dist/src/functions/wirings/ai-agent/serialize-public-agent.d.ts +1 -1
  90. package/dist/src/functions/wirings/ai-agent/serialize-public-agent.js +5 -5
  91. package/dist/src/functions/wirings/cli/pikku-command-cli-entry.js +1 -1
  92. package/dist/src/functions/wirings/cli/serialize-channel-cli.d.ts +1 -1
  93. package/dist/src/functions/wirings/cli/serialize-channel-cli.js +2 -2
  94. package/dist/src/functions/wirings/console/pikku-command-console-functions.js +1 -1
  95. package/dist/src/functions/wirings/console/serialize-console-functions.d.ts +1 -1
  96. package/dist/src/functions/wirings/console/serialize-console-functions.js +2 -2
  97. package/dist/src/functions/wirings/rpc/pikku-command-public-rpc.js +1 -1
  98. package/dist/src/functions/wirings/rpc/pikku-command-rpc-client.js +1 -1
  99. package/dist/src/functions/wirings/rpc/serialize-public-rpc.d.ts +1 -1
  100. package/dist/src/functions/wirings/rpc/serialize-public-rpc.js +4 -4
  101. package/dist/src/functions/wirings/rpc/serialize-rpc-wrapper.d.ts +1 -1
  102. package/dist/src/functions/wirings/rpc/serialize-rpc-wrapper.js +6 -6
  103. package/dist/src/functions/wirings/workflow/serialize-workflow-types.js +63 -8
  104. package/dist/src/services.js +1 -1
  105. package/dist/src/utils/pikku-cli-config.js +1 -0
  106. package/dist/tsconfig.tsbuildinfo +1 -1
  107. package/package.json +5 -4
  108. package/dist/src/utils/openapi/codegen.d.ts +0 -20
  109. package/dist/src/utils/openapi/codegen.js +0 -442
  110. package/dist/src/utils/openapi/naming.d.ts +0 -30
  111. package/dist/src/utils/openapi/naming.js +0 -167
  112. package/dist/src/utils/openapi/parse-openapi.d.ts +0 -61
  113. package/dist/src/utils/openapi/parse-openapi.js +0 -306
  114. package/dist/src/utils/openapi/zod-codegen.d.ts +0 -1
  115. package/dist/src/utils/openapi/zod-codegen.js +0 -1
@@ -1,442 +0,0 @@
1
- import { schemaToZod, schemaVarName, sanitizeTypeName, createContext, } from './zod-codegen.js';
2
- import { generateOperationNames, detectCommonPrefix, } from './naming.js';
3
- function safeKey(key) {
4
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
5
- }
6
- const GENERIC_SUMMARIES = new Set([
7
- 'index',
8
- 'show',
9
- 'create',
10
- 'update',
11
- 'destroy',
12
- 'delete',
13
- 'list',
14
- ]);
15
- /** Map from HTTP status code to pikku error class name */
16
- const STATUS_TO_ERROR = {
17
- 400: 'BadRequestError',
18
- 401: 'UnauthorizedError',
19
- 403: 'ForbiddenError',
20
- 404: 'NotFoundError',
21
- 405: 'MethodNotAllowedError',
22
- 409: 'ConflictError',
23
- 422: 'UnprocessableContentError',
24
- 429: 'TooManyRequestsError',
25
- 500: 'InternalServerError',
26
- };
27
- function capitalize(str) {
28
- return str.charAt(0).toUpperCase() + str.slice(1);
29
- }
30
- function humanDescription(parsed) {
31
- const description = parsed.description?.trim();
32
- if (description && !GENERIC_SUMMARIES.has(description.toLowerCase())) {
33
- return capitalize(description);
34
- }
35
- return undefined;
36
- }
37
- function getErrorClassesForResponses(errorResponses) {
38
- const classes = [];
39
- for (const err of errorResponses) {
40
- const cls = STATUS_TO_ERROR[err.statusCode];
41
- if (cls && !classes.includes(cls)) {
42
- classes.push(cls);
43
- }
44
- }
45
- return classes;
46
- }
47
- export function generateAddonFromOpenAPI(spec, vars, flags) {
48
- const files = {};
49
- const { name } = vars;
50
- // Build context for Zod codegen with component schema refs
51
- const schemaRefs = new Map();
52
- for (const schemaName of Object.keys(spec.componentSchemas)) {
53
- schemaRefs.set(schemaName, schemaVarName(schemaName));
54
- }
55
- const ctx = createContext(schemaRefs);
56
- // Generate operation names
57
- const paths = spec.operations.map((op) => op.path);
58
- const commonPrefix = detectCommonPrefix(paths);
59
- const namedOps = generateOperationNames(spec.operations.map((op) => ({
60
- method: op.method,
61
- path: op.path,
62
- operationId: op.operationId,
63
- })), commonPrefix);
64
- // Pair named operations with their parsed data
65
- const opPairs = namedOps.map((named, i) => ({ named, parsed: spec.operations[i] }));
66
- // Generate types file with shared schemas (only if there are any)
67
- if (Object.keys(spec.componentSchemas).length > 0) {
68
- files[`src/${name}.types.ts`] = generateTypesFile(spec, ctx);
69
- }
70
- // Generate function files
71
- const functionExports = [];
72
- for (const { named, parsed } of opPairs) {
73
- const funcCtx = createContext(schemaRefs);
74
- const funcCode = generateFunctionFile(named, parsed, vars, funcCtx, spec, flags);
75
- files[`src/functions/${named.functionName}.function.ts`] = funcCode;
76
- functionExports.push(named.functionName);
77
- }
78
- // Generate index.ts with all exports
79
- files['src/index.ts'] = generateIndexFile(functionExports);
80
- // Generate typed API service class with route map
81
- files[`src/${name}-api.service.ts`] = generateServiceFile(spec, opPairs, vars, flags);
82
- // Generate variable file for BASE_URL
83
- files[`src/${name}.variable.ts`] = generateVariableFile(spec, vars);
84
- return files;
85
- }
86
- function generateTypesFile(spec, ctx) {
87
- const lines = [];
88
- lines.push("import { z } from 'zod'");
89
- lines.push('');
90
- lines.push(`// Shared schemas from ${spec.info.title} v${spec.info.version}`);
91
- lines.push('');
92
- for (const [name, schema] of Object.entries(spec.componentSchemas)) {
93
- const varName = schemaVarName(name);
94
- const zodCode = schemaToZod(schema, ctx);
95
- lines.push(`export const ${varName} = ${zodCode}`);
96
- lines.push(`export type ${sanitizeTypeName(name)} = z.infer<typeof ${varName}>`);
97
- lines.push('');
98
- }
99
- return lines.join('\n');
100
- }
101
- function generateFunctionFile(named, parsed, vars, ctx, spec, flags) {
102
- const lines = [];
103
- const { camelName } = vars;
104
- // Tag description as file header
105
- if (parsed.tags.length > 0) {
106
- const tag = parsed.tags[0];
107
- const tagDesc = spec.tagDescriptions[tag];
108
- if (tagDesc) {
109
- lines.push(`// ${tag} — ${tagDesc}`);
110
- lines.push('');
111
- }
112
- }
113
- const hasInput = parsed.pathParams.length > 0 ||
114
- parsed.queryParams.length > 0 ||
115
- parsed.headerParams.length > 0 ||
116
- parsed.requestBody;
117
- const pascalName = named.functionName.charAt(0).toUpperCase() + named.functionName.slice(1);
118
- const inputName = `${pascalName}Input`;
119
- const outputName = `${pascalName}Output`;
120
- // Determine error imports needed
121
- const errorClasses = getErrorClassesForResponses(parsed.errorResponses);
122
- lines.push("import { z } from 'zod'");
123
- lines.push("import { pikkuSessionlessFunc } from '#pikku'");
124
- if (errorClasses.length > 0) {
125
- lines.push(`import { ${errorClasses.join(', ')} } from '@pikku/core/errors'`);
126
- }
127
- lines.push('');
128
- // Build Input schema (exported for pikku schema discovery)
129
- if (hasInput) {
130
- const inputCode = buildInputSchema(parsed, ctx);
131
- lines.push(`export const ${inputName} = ${inputCode}`);
132
- lines.push('');
133
- }
134
- // Build Output schema (exported for pikku schema discovery)
135
- if (parsed.responseSchema) {
136
- const outputCode = buildOutputSchema(parsed.responseSchema, ctx);
137
- lines.push(`export const ${outputName} = ${outputCode}`);
138
- lines.push('');
139
- }
140
- const description = humanDescription(parsed);
141
- const method = parsed.method.toUpperCase();
142
- const funcConfig = [];
143
- if (description) {
144
- funcConfig.push(` description: ${JSON.stringify(description)},`);
145
- }
146
- if (hasInput)
147
- funcConfig.push(` input: ${inputName},`);
148
- if (parsed.responseSchema) {
149
- funcConfig.push(` output: ${outputName},`);
150
- }
151
- else {
152
- funcConfig.push(' output: z.void(),');
153
- }
154
- if (errorClasses.length > 0) {
155
- funcConfig.push(` errors: [${errorClasses.join(', ')}],`);
156
- }
157
- if (flags.mcp) {
158
- funcConfig.push(' mcp: true,');
159
- }
160
- const funcParams = hasInput ? `{ ${camelName} }, data` : `{ ${camelName} }`;
161
- const returnCast = parsed.responseSchema ? ' as any' : '';
162
- funcConfig.push(` func: async (${funcParams}) => {`, ` return ${camelName}.call('${method}', '${parsed.path}'${hasInput ? ', data' : ''})${returnCast}`, ' },');
163
- lines.push(`export const ${named.functionName} = pikkuSessionlessFunc({`);
164
- lines.push(funcConfig.join('\n'));
165
- lines.push('})');
166
- lines.push('');
167
- return lines.join('\n');
168
- }
169
- function buildInputSchema(parsed, ctx) {
170
- const props = [];
171
- for (const param of parsed.pathParams) {
172
- props.push(formatParamProp(param, ctx));
173
- }
174
- for (const param of parsed.queryParams) {
175
- props.push(formatParamProp(param, ctx));
176
- }
177
- for (const param of parsed.headerParams) {
178
- props.push(formatParamProp(param, ctx));
179
- }
180
- if (parsed.requestBody) {
181
- if (parsed.requestBody.properties) {
182
- const requiredSet = new Set(parsed.requestBody.required ?? []);
183
- for (const [key, propSchema] of Object.entries(parsed.requestBody.properties)) {
184
- // Skip readOnly properties from input
185
- if (propSchema.readOnly)
186
- continue;
187
- const isOptional = !requiredSet.has(key);
188
- const zodCode = schemaToZod(propSchema, ctx, { optional: isOptional });
189
- const withDesc = propSchema.description
190
- ? `${zodCode}.describe(${JSON.stringify(propSchema.description)})`
191
- : zodCode;
192
- props.push(` ${safeKey(key)}: ${withDesc},`);
193
- }
194
- }
195
- else {
196
- const bodyZod = schemaToZod(parsed.requestBody, ctx);
197
- props.push(` body: ${bodyZod},`);
198
- }
199
- }
200
- return `z.object({\n${props.join('\n')}\n})`;
201
- }
202
- function formatParamProp(param, ctx) {
203
- const zodCode = schemaToZod(param.schema, ctx, { optional: !param.required });
204
- let descParts = [];
205
- if (param.description)
206
- descParts.push(param.description);
207
- if (param.example !== undefined)
208
- descParts.push(`Example: ${JSON.stringify(param.example)}`);
209
- const desc = descParts.length > 0
210
- ? `${zodCode}.describe(${JSON.stringify(descParts.join('. '))})`
211
- : zodCode;
212
- return ` ${safeKey(param.name)}: ${desc},`;
213
- }
214
- function buildOutputSchema(schema, ctx) {
215
- // For output schemas, filter out writeOnly properties
216
- if (schema.properties) {
217
- const filteredProps = {};
218
- for (const [key, propSchema] of Object.entries(schema.properties)) {
219
- if (!propSchema.writeOnly) {
220
- filteredProps[key] = propSchema;
221
- }
222
- }
223
- const filteredSchema = { ...schema, properties: filteredProps };
224
- return schemaToZod(filteredSchema, ctx);
225
- }
226
- return schemaToZod(schema, ctx);
227
- }
228
- function generateIndexFile(functionExports) {
229
- const lines = [];
230
- for (const name of functionExports) {
231
- lines.push(`export { ${name} } from './functions/${name}.function.js'`);
232
- }
233
- lines.push('');
234
- return lines.join('\n');
235
- }
236
- function generateServiceFile(spec, opPairs, vars, flags) {
237
- const { name, pascalName, screamingName, displayName } = vars;
238
- const lines = [];
239
- // Always import all error classes used in the switch statement
240
- const allErrorClasses = new Set(Object.values(STATUS_TO_ERROR));
241
- if (flags.oauth) {
242
- lines.push("import { OAuth2Client } from '@pikku/core/oauth2'");
243
- lines.push("import type { TypedSecretService } from '#pikku/secrets/pikku-secrets.gen.js'");
244
- }
245
- else if (flags.secret) {
246
- lines.push(`import type { ${pascalName}Secrets } from './${name}.secret.js'`);
247
- }
248
- if (allErrorClasses.size > 0) {
249
- lines.push(`import { ${[...allErrorClasses].sort().join(', ')} } from '@pikku/core/errors'`);
250
- }
251
- lines.push(`import type { TypedVariablesService } from '#pikku/variables/pikku-variables.gen.js'`);
252
- lines.push('');
253
- if (flags.oauth) {
254
- // Use OAuth2 details from spec if available
255
- const oauthScheme = Object.values(spec.securitySchemes).find((s) => s.type === 'oauth2');
256
- const authUrl = oauthScheme?.flows?.authorizationUrl ??
257
- 'https://example.com/oauth2/authorize';
258
- const tokenUrl = oauthScheme?.flows?.tokenUrl ?? 'https://example.com/oauth2/token';
259
- const scopes = oauthScheme?.flows?.scopes
260
- ? Object.keys(oauthScheme.flows.scopes)
261
- : ['read', 'write'];
262
- lines.push(`export const ${screamingName}_OAUTH2_CONFIG = {`);
263
- lines.push(` tokenSecretId: '${screamingName}_TOKENS',`);
264
- lines.push(` authorizationUrl: ${JSON.stringify(authUrl)},`);
265
- lines.push(` tokenUrl: ${JSON.stringify(tokenUrl)},`);
266
- lines.push(` scopes: ${JSON.stringify(scopes)},`);
267
- lines.push('}');
268
- lines.push('');
269
- }
270
- // Generate route map from parsed operations
271
- const routes = {};
272
- for (const { parsed } of opPairs) {
273
- const key = `${parsed.method.toUpperCase()} ${parsed.path}`;
274
- const route = {
275
- path: parsed.pathParams.map((p) => p.name),
276
- query: parsed.queryParams.map((p) => p.name),
277
- headers: parsed.headerParams.map((p) => p.name),
278
- };
279
- if (parsed.errorResponses.length > 0) {
280
- route.errors = {};
281
- for (const err of parsed.errorResponses) {
282
- route.errors[err.statusCode] = err.description;
283
- }
284
- }
285
- routes[key] = route;
286
- }
287
- lines.push(`const ROUTES: Record<string, { path: string[], query: string[], headers: string[], errors?: Record<number, string> }> = ${JSON.stringify(routes, null, 2)}`);
288
- lines.push('');
289
- // Class declaration
290
- lines.push(`export class ${pascalName}Service {`);
291
- lines.push(' private baseUrl: string');
292
- if (flags.oauth) {
293
- lines.push(' private oauth: OAuth2Client');
294
- lines.push('');
295
- lines.push(` constructor(secrets: TypedSecretService, variables: TypedVariablesService) {`);
296
- lines.push(` this.baseUrl = variables.get('${screamingName}_BASE_URL') as string`);
297
- lines.push(' this.oauth = new OAuth2Client(');
298
- lines.push(` ${screamingName}_OAUTH2_CONFIG,`);
299
- lines.push(` '${screamingName}_APP_CREDENTIALS',`);
300
- lines.push(' secrets');
301
- lines.push(' )');
302
- lines.push(' }');
303
- }
304
- else if (flags.secret) {
305
- lines.push('');
306
- lines.push(` constructor(private creds: ${pascalName}Secrets, variables: TypedVariablesService) {`);
307
- lines.push(` this.baseUrl = variables.get('${screamingName}_BASE_URL') as string`);
308
- lines.push(' }');
309
- }
310
- else {
311
- lines.push('');
312
- lines.push(` constructor(variables: TypedVariablesService) {`);
313
- lines.push(` this.baseUrl = variables.get('${screamingName}_BASE_URL') as string`);
314
- lines.push(' }');
315
- }
316
- lines.push('');
317
- // call() method — splits data into path/query/headers/body using route map
318
- lines.push(' async call<T>(');
319
- lines.push(" method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH',");
320
- lines.push(' path: string,');
321
- lines.push(' data?: Record<string, unknown>');
322
- lines.push(' ): Promise<T> {');
323
- lines.push(' const route = ROUTES[`${method} ${path}`]');
324
- lines.push(' let endpoint = path');
325
- lines.push(' let body: Record<string, unknown> | undefined');
326
- lines.push(' const query: Record<string, string> = {}');
327
- lines.push(' const headers: Record<string, string> = {');
328
- lines.push(" 'Content-Type': 'application/json',");
329
- lines.push(' }');
330
- lines.push('');
331
- lines.push(' if (data && route) {');
332
- lines.push(' // Interpolate path params');
333
- lines.push(' for (const param of route.path) {');
334
- lines.push(' if (data[param] !== undefined) {');
335
- lines.push(' endpoint = endpoint.replace(`{${param}}`, String(data[param]))');
336
- lines.push(' }');
337
- lines.push(' }');
338
- lines.push(' // Extract query params');
339
- lines.push(' for (const param of route.query) {');
340
- lines.push(' if (data[param] !== undefined) {');
341
- lines.push(' query[param] = String(data[param])');
342
- lines.push(' }');
343
- lines.push(' }');
344
- lines.push(' // Extract header params');
345
- lines.push(' for (const param of route.headers) {');
346
- lines.push(' if (data[param] !== undefined) {');
347
- lines.push(' headers[param] = String(data[param])');
348
- lines.push(' }');
349
- lines.push(' }');
350
- lines.push(' // Everything else goes into body');
351
- lines.push(' const pathQueryHeaders = new Set([...route.path, ...route.query, ...route.headers])');
352
- lines.push(' const remaining = Object.fromEntries(');
353
- lines.push(' Object.entries(data).filter(([k]) => !pathQueryHeaders.has(k))');
354
- lines.push(' )');
355
- lines.push(' if (Object.keys(remaining).length > 0) {');
356
- lines.push(' body = remaining');
357
- lines.push(' }');
358
- lines.push(' }');
359
- lines.push('');
360
- lines.push(' const url = new URL(`${this.baseUrl}${endpoint}`)');
361
- lines.push(' for (const [key, value] of Object.entries(query)) {');
362
- lines.push(' url.searchParams.set(key, value)');
363
- lines.push(' }');
364
- lines.push('');
365
- if (flags.oauth) {
366
- lines.push(' const response = await this.oauth.request(url.toString(), {');
367
- lines.push(' method,');
368
- lines.push(' headers,');
369
- lines.push(' body: body ? JSON.stringify(body) : undefined,');
370
- lines.push(' })');
371
- }
372
- else if (flags.secret) {
373
- // Use apiKey details from spec if available
374
- const apiKeyScheme = Object.values(spec.securitySchemes).find((s) => s.type === 'apiKey');
375
- if (apiKeyScheme?.name && apiKeyScheme?.in === 'header') {
376
- lines.push(` headers[${JSON.stringify(apiKeyScheme.name)}] = this.creds.apiKey`);
377
- }
378
- else {
379
- lines.push(' headers.Authorization = `Bearer ${this.creds.apiKey}`');
380
- }
381
- lines.push('');
382
- lines.push(' const response = await fetch(url.toString(), {');
383
- lines.push(' method,');
384
- lines.push(' headers,');
385
- lines.push(' body: body ? JSON.stringify(body) : undefined,');
386
- lines.push(' })');
387
- }
388
- else {
389
- lines.push(' const response = await fetch(url.toString(), {');
390
- lines.push(' method,');
391
- lines.push(' headers,');
392
- lines.push(' body: body ? JSON.stringify(body) : undefined,');
393
- lines.push(' })');
394
- }
395
- lines.push('');
396
- lines.push(' if (!response.ok) {');
397
- lines.push(' const errorText = await response.text()');
398
- lines.push(' const errorMessage = route?.errors?.[response.status] ?? errorText');
399
- lines.push(' switch (response.status) {');
400
- lines.push(' case 400: throw new BadRequestError(errorMessage)');
401
- lines.push(' case 401: throw new UnauthorizedError(errorMessage)');
402
- lines.push(' case 403: throw new ForbiddenError(errorMessage)');
403
- lines.push(' case 404: throw new NotFoundError(errorMessage)');
404
- lines.push(' case 405: throw new MethodNotAllowedError(errorMessage)');
405
- lines.push(' case 409: throw new ConflictError(errorMessage)');
406
- lines.push(' case 422: throw new UnprocessableContentError(errorMessage)');
407
- lines.push(' case 429: throw new TooManyRequestsError(errorMessage)');
408
- lines.push(' case 500: throw new InternalServerError(errorMessage)');
409
- lines.push(` default: throw new Error(\`${displayName} API error (\${response.status}): \${errorText}\`)`);
410
- lines.push(' }');
411
- lines.push(' }');
412
- lines.push('');
413
- lines.push(' const text = await response.text()');
414
- lines.push(' if (!text) return {} as T');
415
- lines.push(' return JSON.parse(text) as T');
416
- lines.push(' }');
417
- lines.push('}');
418
- lines.push('');
419
- return lines.join('\n');
420
- }
421
- function generateVariableFile(spec, vars) {
422
- const { camelName, screamingName, displayName } = vars;
423
- const serverUrls = spec.serverUrls.length > 0 ? spec.serverUrls : [];
424
- const defaultUrl = serverUrls[0];
425
- const lines = [];
426
- lines.push("import { z } from 'zod'");
427
- lines.push("import { wireVariable } from '@pikku/core/variable'");
428
- lines.push('');
429
- const schemaVarName = `${camelName}BaseUrlSchema`;
430
- const urlsLiteral = serverUrls.map((u) => JSON.stringify(u)).join(', ');
431
- lines.push(`export const ${schemaVarName} = z.enum([${urlsLiteral}]).default(${JSON.stringify(defaultUrl)})`);
432
- lines.push('');
433
- lines.push(`wireVariable({`);
434
- lines.push(` name: '${screamingName}_BASE_URL',`);
435
- lines.push(` displayName: '${displayName} Base URL',`);
436
- lines.push(` description: 'The base URL for the ${displayName} API.',`);
437
- lines.push(` variableId: '${screamingName}_BASE_URL',`);
438
- lines.push(` schema: ${schemaVarName},`);
439
- lines.push(`})`);
440
- lines.push('');
441
- return lines.join('\n');
442
- }
@@ -1,30 +0,0 @@
1
- /**
2
- * Derives function/method names from HTTP method + path.
3
- * Uses operationId when available, otherwise generates from path segments.
4
- */
5
- export declare function singularize(word: string): string;
6
- export declare function toCamelCase(str: string): string;
7
- export declare function toPascalCase(str: string): string;
8
- /**
9
- * Strip common API prefix from paths (e.g. /api/v2/).
10
- * Uses majority voting — a prefix shared by >75% of paths is stripped,
11
- * so a few outlier paths (e.g. /oauth/...) don't break detection.
12
- */
13
- export declare function detectCommonPrefix(paths: string[]): string;
14
- interface OperationForNaming {
15
- method: string;
16
- path: string;
17
- operationId?: string;
18
- }
19
- export interface NamedOperation {
20
- method: string;
21
- path: string;
22
- functionName: string;
23
- methodName: string;
24
- }
25
- /**
26
- * Generate function names for a list of operations.
27
- * Handles collision detection and auto-deduplication.
28
- */
29
- export declare function generateOperationNames(operations: OperationForNaming[], commonPrefix: string): NamedOperation[];
30
- export {};
@@ -1,167 +0,0 @@
1
- /**
2
- * Derives function/method names from HTTP method + path.
3
- * Uses operationId when available, otherwise generates from path segments.
4
- */
5
- const IRREGULAR_PLURALS = {
6
- addresses: 'address',
7
- statuses: 'status',
8
- indices: 'index',
9
- analyses: 'analysis',
10
- quizzes: 'quiz',
11
- matrices: 'matrix',
12
- vertices: 'vertex',
13
- aliases: 'alias',
14
- buses: 'bus',
15
- };
16
- export function singularize(word) {
17
- const lower = word.toLowerCase();
18
- if (IRREGULAR_PLURALS[lower]) {
19
- const singular = IRREGULAR_PLURALS[lower];
20
- return word[0] === word[0].toUpperCase()
21
- ? singular.charAt(0).toUpperCase() + singular.slice(1)
22
- : singular;
23
- }
24
- if (lower.endsWith('ies') && lower.length > 4) {
25
- return word.slice(0, -3) + 'y';
26
- }
27
- // -shes, -ches, -xes, -zes, -sses → drop "es"
28
- if (lower.endsWith('shes') || lower.endsWith('ches') || lower.endsWith('xes') || lower.endsWith('zes') || lower.endsWith('sses')) {
29
- return word.slice(0, -2);
30
- }
31
- // General: drop trailing "s" (covers courses→course, products→product, etc.)
32
- if (lower.endsWith('s') && !lower.endsWith('ss') && !lower.endsWith('us') && lower.length > 2) {
33
- return word.slice(0, -1);
34
- }
35
- return word;
36
- }
37
- export function toCamelCase(str) {
38
- return str
39
- .replace(/[^a-zA-Z0-9]+(.)/g, (_, c) => c.toUpperCase())
40
- .replace(/^[A-Z]/, (c) => c.toLowerCase());
41
- }
42
- export function toPascalCase(str) {
43
- const camel = toCamelCase(str);
44
- return camel.charAt(0).toUpperCase() + camel.slice(1);
45
- }
46
- /**
47
- * Strip common API prefix from paths (e.g. /api/v2/).
48
- * Uses majority voting — a prefix shared by >75% of paths is stripped,
49
- * so a few outlier paths (e.g. /oauth/...) don't break detection.
50
- */
51
- export function detectCommonPrefix(paths) {
52
- if (paths.length === 0)
53
- return '';
54
- const segments = paths.map((p) => p.replace(/^\//, '').split('/'));
55
- const threshold = Math.ceil(paths.length * 0.75);
56
- // Find the longest prefix shared by at least `threshold` paths
57
- let prefixLen = 0;
58
- for (let i = 0; i < 10; i++) {
59
- // Count how many paths have the same segment at position i as the first path
60
- const candidateSeg = segments[0][i];
61
- if (!candidateSeg || candidateSeg.startsWith('{'))
62
- break;
63
- const count = segments.filter((s) => s.length > i + 1 && s[i] === candidateSeg).length;
64
- if (count >= threshold) {
65
- prefixLen = i + 1;
66
- }
67
- else {
68
- break;
69
- }
70
- }
71
- if (prefixLen === 0)
72
- return '';
73
- return '/' + segments[0].slice(0, prefixLen).join('/') + '/';
74
- }
75
- /** Convert operationId to camelCase function name */
76
- function fromOperationId(operationId) {
77
- // operationId might be snake_case, camelCase, PascalCase, or kebab-case
78
- return toCamelCase(operationId
79
- .replace(/[^a-zA-Z0-9_-]/g, '_')
80
- .replace(/_+/g, '_')
81
- .replace(/^_|_$/g, ''));
82
- }
83
- const METHOD_PREFIXES = {
84
- get: 'get',
85
- post: 'create',
86
- put: 'update',
87
- patch: 'update',
88
- delete: 'delete',
89
- };
90
- /**
91
- * Generate function names for a list of operations.
92
- * Handles collision detection and auto-deduplication.
93
- */
94
- export function generateOperationNames(operations, commonPrefix) {
95
- const results = [];
96
- const usedNames = new Map();
97
- for (const op of operations) {
98
- let name;
99
- if (op.operationId) {
100
- name = fromOperationId(op.operationId);
101
- }
102
- else {
103
- name = deriveNameFromPath(op.method, op.path, commonPrefix);
104
- }
105
- // Handle collisions
106
- const existing = usedNames.get(name) ?? 0;
107
- if (existing > 0) {
108
- usedNames.set(name, existing + 1);
109
- name = `${name}${existing + 1}`;
110
- }
111
- usedNames.set(name, (usedNames.get(name) ?? 0) || 1);
112
- results.push({
113
- method: op.method,
114
- path: op.path,
115
- functionName: name,
116
- methodName: name,
117
- });
118
- }
119
- return results;
120
- }
121
- function deriveNameFromPath(method, path, commonPrefix) {
122
- // Strip common prefix
123
- let cleanPath = path;
124
- if (commonPrefix && cleanPath.startsWith(commonPrefix)) {
125
- cleanPath = '/' + cleanPath.slice(commonPrefix.length);
126
- }
127
- const segments = cleanPath
128
- .replace(/^\//, '')
129
- .split('/')
130
- .filter(Boolean);
131
- const methodLower = method.toLowerCase();
132
- const prefix = METHOD_PREFIXES[methodLower] || methodLower;
133
- // Separate param and non-param segments
134
- const nonParams = [];
135
- let hasTrailingParam = false;
136
- for (let i = 0; i < segments.length; i++) {
137
- if (segments[i].startsWith('{')) {
138
- if (i === segments.length - 1) {
139
- hasTrailingParam = true;
140
- }
141
- }
142
- else {
143
- nonParams.push(segments[i]);
144
- }
145
- }
146
- if (nonParams.length === 0) {
147
- return prefix;
148
- }
149
- // For GET with trailing param: singularize the last non-param segment
150
- // For GET without trailing param: keep plural (list)
151
- // For POST without trailing param: use prefix (create)
152
- const parts = nonParams.map((seg, i) => {
153
- const isLast = i === nonParams.length - 1;
154
- let word = seg;
155
- if (isLast && hasTrailingParam) {
156
- word = singularize(word);
157
- }
158
- // For GET on collection (no trailing param), use "list" prefix instead of "get"
159
- return toPascalCase(word);
160
- });
161
- // For GET on collection, use "list" instead of "get"
162
- let finalPrefix = prefix;
163
- if (methodLower === 'get' && !hasTrailingParam) {
164
- finalPrefix = 'list';
165
- }
166
- return finalPrefix + parts.join('');
167
- }