@pikku/core 0.9.11 → 0.9.12-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/function/function-runner.d.ts +13 -8
  3. package/dist/function/function-runner.js +49 -37
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.js +3 -1
  6. package/dist/middleware-runner.d.ts +61 -40
  7. package/dist/middleware-runner.js +118 -66
  8. package/dist/permissions.js +1 -0
  9. package/dist/pikku-state.d.ts +26 -1
  10. package/dist/pikku-state.js +10 -1
  11. package/dist/services/content-service.d.ts +2 -3
  12. package/dist/services/index.d.ts +0 -1
  13. package/dist/services/index.js +0 -1
  14. package/dist/services/local-content.d.ts +2 -4
  15. package/dist/types/core.types.d.ts +51 -2
  16. package/dist/types/core.types.js +21 -0
  17. package/dist/wirings/channel/channel-handler.d.ts +2 -2
  18. package/dist/wirings/channel/channel-handler.js +12 -9
  19. package/dist/wirings/channel/channel-runner.d.ts +0 -2
  20. package/dist/wirings/channel/channel-runner.js +2 -3
  21. package/dist/wirings/channel/channel.types.d.ts +5 -3
  22. package/dist/wirings/channel/local/local-channel-runner.js +12 -12
  23. package/dist/wirings/channel/serverless/serverless-channel-runner.js +7 -8
  24. package/dist/wirings/cli/channel/cli-channel-runner.d.ts +12 -0
  25. package/dist/wirings/cli/channel/cli-channel-runner.js +80 -0
  26. package/dist/wirings/cli/channel/index.d.ts +1 -0
  27. package/dist/wirings/cli/channel/index.js +1 -0
  28. package/dist/wirings/cli/cli-runner.d.ts +32 -0
  29. package/dist/wirings/cli/cli-runner.js +328 -0
  30. package/dist/wirings/cli/cli.types.d.ts +177 -0
  31. package/dist/wirings/cli/cli.types.js +1 -0
  32. package/dist/wirings/cli/command-parser.d.ts +19 -0
  33. package/dist/wirings/cli/command-parser.js +373 -0
  34. package/dist/wirings/cli/index.d.ts +5 -0
  35. package/dist/wirings/cli/index.js +5 -0
  36. package/dist/wirings/http/http-runner.d.ts +29 -10
  37. package/dist/wirings/http/http-runner.js +103 -131
  38. package/dist/wirings/http/http.types.d.ts +10 -10
  39. package/dist/wirings/http/index.d.ts +1 -1
  40. package/dist/wirings/http/index.js +1 -1
  41. package/dist/wirings/http/pikku-fetch-http-response.js +3 -1
  42. package/dist/wirings/http/routers/http-router.d.ts +0 -2
  43. package/dist/wirings/http/routers/path-to-regex.d.ts +1 -1
  44. package/dist/wirings/http/routers/path-to-regex.js +5 -34
  45. package/dist/wirings/mcp/mcp-runner.d.ts +4 -5
  46. package/dist/wirings/mcp/mcp-runner.js +30 -34
  47. package/dist/wirings/mcp/mcp.types.d.ts +11 -8
  48. package/dist/wirings/queue/queue-runner.d.ts +2 -2
  49. package/dist/wirings/queue/queue-runner.js +25 -27
  50. package/dist/wirings/queue/queue.types.d.ts +6 -5
  51. package/dist/wirings/rpc/index.d.ts +1 -1
  52. package/dist/wirings/rpc/index.js +1 -1
  53. package/dist/wirings/rpc/rpc-runner.d.ts +6 -18
  54. package/dist/wirings/rpc/rpc-runner.js +13 -8
  55. package/dist/wirings/scheduler/scheduler-runner.d.ts +2 -2
  56. package/dist/wirings/scheduler/scheduler-runner.js +37 -35
  57. package/dist/wirings/scheduler/scheduler.types.d.ts +4 -3
  58. package/package.json +4 -3
  59. package/src/function/function-runner.test.ts +73 -23
  60. package/src/function/function-runner.ts +74 -55
  61. package/src/index.ts +8 -1
  62. package/src/middleware-runner.test.ts +24 -16
  63. package/src/middleware-runner.ts +136 -83
  64. package/src/permissions.ts +1 -0
  65. package/src/pikku-state.ts +47 -2
  66. package/src/services/content-service.ts +5 -4
  67. package/src/services/index.ts +0 -1
  68. package/src/services/local-content.ts +9 -4
  69. package/src/types/core.types.ts +74 -3
  70. package/src/wirings/channel/channel-handler.ts +9 -13
  71. package/src/wirings/channel/channel-runner.ts +2 -6
  72. package/src/wirings/channel/channel.types.ts +5 -1
  73. package/src/wirings/channel/local/local-channel-runner.ts +25 -15
  74. package/src/wirings/channel/serverless/serverless-channel-runner.ts +15 -17
  75. package/src/wirings/cli/channel/cli-channel-runner.ts +118 -0
  76. package/src/wirings/cli/channel/index.ts +1 -0
  77. package/src/wirings/cli/cli-runner.test.ts +382 -0
  78. package/src/wirings/cli/cli-runner.ts +503 -0
  79. package/src/wirings/cli/cli.types.ts +320 -0
  80. package/src/wirings/cli/command-parser.test.ts +440 -0
  81. package/src/wirings/cli/command-parser.ts +470 -0
  82. package/src/wirings/cli/index.ts +12 -0
  83. package/src/wirings/http/http-runner.test.ts +8 -7
  84. package/src/wirings/http/http-runner.ts +126 -159
  85. package/src/wirings/http/http.types.ts +56 -11
  86. package/src/wirings/http/index.ts +1 -1
  87. package/src/wirings/http/pikku-fetch-http-response.ts +3 -1
  88. package/src/wirings/http/routers/http-router.ts +0 -2
  89. package/src/wirings/http/routers/path-to-regex.test.ts +4 -5
  90. package/src/wirings/http/routers/path-to-regex.ts +6 -43
  91. package/src/wirings/mcp/mcp-runner.ts +56 -55
  92. package/src/wirings/mcp/mcp.types.ts +23 -8
  93. package/src/wirings/queue/queue-runner.ts +44 -47
  94. package/src/wirings/queue/queue.types.ts +10 -6
  95. package/src/wirings/rpc/index.ts +1 -1
  96. package/src/wirings/rpc/rpc-runner.ts +27 -9
  97. package/src/wirings/scheduler/scheduler-runner.ts +57 -56
  98. package/src/wirings/scheduler/scheduler.types.ts +9 -2
  99. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,320 @@
1
+ import {
2
+ CorePikkuMiddleware,
3
+ CoreSingletonServices,
4
+ CoreUserSession,
5
+ PikkuDocs,
6
+ CoreServices,
7
+ MiddlewareMetadata,
8
+ } from '../../types/core.types.js'
9
+ import {
10
+ CorePikkuFunctionConfig,
11
+ CorePikkuPermission,
12
+ CorePikkuFunction,
13
+ CorePikkuFunctionSessionless,
14
+ } from '../../function/functions.types.js'
15
+ import { PikkuChannel } from '../channel/channel.types.js'
16
+
17
+ /**
18
+ * CLI option definition
19
+ */
20
+ export interface CLIOption<T = any> {
21
+ description: string
22
+ short?: string
23
+ default?: T
24
+ choices?: T[]
25
+ array?: boolean
26
+ required?: boolean
27
+ }
28
+
29
+ /**
30
+ * CLI options collection
31
+ */
32
+ export type CLIOptions<T> = {
33
+ [K in keyof T]: CLIOption<T[K]>
34
+ }
35
+
36
+ /**
37
+ * Positional argument definition
38
+ */
39
+ export interface CLIPositional {
40
+ name: string
41
+ required: boolean
42
+ variadic?: boolean
43
+ }
44
+
45
+ /**
46
+ * CLI interaction context
47
+ */
48
+ export type PikkuCLI = {
49
+ program: string
50
+ command: string[]
51
+ data: Record<string, any> // All positionals and options merged
52
+ channel: PikkuChannel<unknown, unknown>
53
+ }
54
+
55
+ /**
56
+ * Runtime CLI program state
57
+ */
58
+ export interface CLIProgramState {
59
+ defaultRenderer?: CorePikkuCLIRender<any>
60
+ middleware: CorePikkuMiddleware[]
61
+ renderers: Record<string, CorePikkuCLIRender<any>>
62
+ commandOptions?: Record<string, Record<string, CLIOption>>
63
+ commandMiddleware?: Record<string, CorePikkuMiddleware[]>
64
+ tags?: string[]
65
+ }
66
+
67
+ /**
68
+ * CLI command metadata for runtime
69
+ */
70
+ export interface CLICommandMeta {
71
+ parameters?: string
72
+ pikkuFuncName: string
73
+ positionals: CLIPositional[]
74
+ options: Record<string, CLIOption>
75
+ renderName?: string
76
+ description?: string
77
+ docs?: PikkuDocs
78
+ tags?: string[]
79
+ subcommands?: Record<string, CLICommandMeta>
80
+ middleware?: MiddlewareMetadata[] // Pre-resolved middleware chain (tag + explicit)
81
+ }
82
+
83
+ /**
84
+ * CLI metadata collection (per program)
85
+ */
86
+ export type CLIProgramMeta = {
87
+ program: string
88
+ commands: Record<string, CLICommandMeta>
89
+ options: Record<string, CLIOption>
90
+ defaultRenderName?: string
91
+ }
92
+
93
+ /**
94
+ * All CLI programs metadata
95
+ */
96
+ export type CLIMeta = Record<string, CLIProgramMeta>
97
+
98
+ /**
99
+ * CLI-specific renderer that outputs to console
100
+ */
101
+ export type CorePikkuCLIRender<
102
+ Data,
103
+ Services extends CoreSingletonServices = CoreServices,
104
+ Session extends CoreUserSession = CoreUserSession,
105
+ > = CorePikkuRender<Data, void, Services, Session>
106
+
107
+ /**
108
+ * Extract input parameters from a Pikku function config type
109
+ */
110
+ export type ExtractFunctionInput<Func> =
111
+ Func extends CorePikkuFunctionConfig<infer FuncType>
112
+ ? FuncType extends
113
+ | CorePikkuFunction<infer Input, any, any, any, any>
114
+ | CorePikkuFunctionSessionless<infer Input, any, any, any, any>
115
+ ? Input
116
+ : never
117
+ : never
118
+
119
+ /**
120
+ * Strip < > [ ] characters from a string
121
+ */
122
+ type StripBrackets<S extends string> = S extends `<${infer Inner}>`
123
+ ? Inner
124
+ : S extends `[${infer Inner}]`
125
+ ? Inner
126
+ : S
127
+
128
+ /**
129
+ * Split string by spaces
130
+ */
131
+ type SplitBySpace<S extends string> = S extends `${infer First} ${infer Rest}`
132
+ ? [First, ...SplitBySpace<Rest>]
133
+ : S extends ''
134
+ ? []
135
+ : [S]
136
+
137
+ /**
138
+ * Extract parameter names from CLI parameter string
139
+ * Example: "<env> [region]" => ["env", "region"]
140
+ */
141
+ type ExtractParameterNames<S extends string> = {
142
+ [K in keyof SplitBySpace<S>]: SplitBySpace<S>[K] extends string
143
+ ? StripBrackets<SplitBySpace<S>[K]>
144
+ : never
145
+ }
146
+
147
+ /**
148
+ * Validate that all parameter names are valid keys of the function input
149
+ */
150
+ export type ValidateParameters<Params extends string, Input> =
151
+ ExtractParameterNames<Params> extends (infer P)[]
152
+ ? P extends keyof Input
153
+ ? Params
154
+ : never
155
+ : never
156
+
157
+ /**
158
+ * Extract output type from a Pikku function config type
159
+ */
160
+ export type ExtractFunctionOutput<Func> =
161
+ Func extends CorePikkuFunctionConfig<infer FuncType>
162
+ ? FuncType extends
163
+ | CorePikkuFunction<any, infer Output, any, any, any>
164
+ | CorePikkuFunctionSessionless<any, infer Output, any, any, any>
165
+ ? Output
166
+ : never
167
+ : never
168
+
169
+ /**
170
+ * CLI command configuration that infers options from function input type.
171
+ * This is a helper type for creating type-safe CLI commands.
172
+ */
173
+ export interface CoreCLICommandConfig<
174
+ Func,
175
+ PikkuMiddleware extends CorePikkuMiddleware<any> = CorePikkuMiddleware<any>,
176
+ PikkuCLIRender extends CorePikkuCLIRender<any, any, any> = CorePikkuCLIRender<
177
+ any,
178
+ any
179
+ >,
180
+ > {
181
+ parameters?: ValidateParameters<string, ExtractFunctionInput<Func>>
182
+ func?: Func
183
+ description?: string
184
+ render?: PikkuCLIRender
185
+ options?: Partial<
186
+ Record<
187
+ keyof ExtractFunctionInput<Func>,
188
+ {
189
+ description?: string
190
+ short?: string
191
+ default?: ExtractFunctionInput<Func>[keyof ExtractFunctionInput<Func>]
192
+ }
193
+ >
194
+ > &
195
+ Record<
196
+ string,
197
+ {
198
+ description?: string
199
+ short?: string
200
+ default?: any
201
+ }
202
+ >
203
+ middleware?: PikkuMiddleware[]
204
+ subcommands?: Record<
205
+ string,
206
+ CoreCLICommandConfig<any, PikkuMiddleware, PikkuCLIRender>
207
+ >
208
+ auth?: boolean
209
+ permissions?: any[]
210
+ }
211
+
212
+ /**
213
+ * CLI command definition
214
+ */
215
+ export interface CoreCLICommand<
216
+ In,
217
+ Out,
218
+ PikkuFunction extends CorePikkuFunctionConfig<
219
+ | CorePikkuFunction<In, Out, any, any, any>
220
+ | CorePikkuFunctionSessionless<In, Out, any, any, any>
221
+ >,
222
+ PikkuPermission extends CorePikkuPermission<any, any, any>,
223
+ PikkuMiddleware extends CorePikkuMiddleware,
224
+ Options = any,
225
+ Subcommands extends Record<
226
+ string,
227
+ CoreCLICommandConfig<any, any, any>
228
+ > = Record<string, CoreCLICommandConfig<any, any, any>>,
229
+ > {
230
+ parameters?: string
231
+ func: PikkuFunction
232
+ render?: CorePikkuCLIRender<Out>
233
+ description?: string
234
+ options?: CLIOptions<Options>
235
+ middleware?: PikkuMiddleware[]
236
+ permissions?: Record<string, PikkuPermission | PikkuPermission[]>
237
+ auth?: boolean
238
+ docs?: PikkuDocs
239
+ subcommands?: Subcommands
240
+ }
241
+
242
+ /**
243
+ * Shorthand command definition (just a function)
244
+ */
245
+ export type CLICommandShorthand<
246
+ In,
247
+ Out,
248
+ PikkuFunction extends CorePikkuFunctionConfig<
249
+ | CorePikkuFunction<In, Out, any, any, any>
250
+ | CorePikkuFunctionSessionless<In, Out, any, any, any>
251
+ >,
252
+ > = PikkuFunction
253
+
254
+ /**
255
+ * Command definition (either full or shorthand)
256
+ */
257
+ export type CLICommandDefinition<
258
+ In,
259
+ Out,
260
+ PikkuFunction extends CorePikkuFunctionConfig<
261
+ | CorePikkuFunction<In, Out, any, any, any>
262
+ | CorePikkuFunctionSessionless<In, Out, any, any, any>
263
+ >,
264
+ PikkuPermission extends CorePikkuPermission<any, any, any>,
265
+ PikkuMiddleware extends CorePikkuMiddleware,
266
+ Options = any,
267
+ Subcommands extends Record<
268
+ string,
269
+ CoreCLICommandConfig<any, any, any>
270
+ > = Record<string, CoreCLICommandConfig<any, any, any>>,
271
+ > =
272
+ | CoreCLICommand<
273
+ In,
274
+ Out,
275
+ PikkuFunction,
276
+ PikkuPermission,
277
+ PikkuMiddleware,
278
+ Options,
279
+ Subcommands
280
+ >
281
+ | CLICommandShorthand<In, Out, PikkuFunction>
282
+
283
+ /**
284
+ * CLI wiring configuration
285
+ */
286
+ export interface CoreCLI<
287
+ Commands extends Record<string, CoreCLICommandConfig<any, any, any>>,
288
+ Options,
289
+ PikkuMiddleware,
290
+ PikkuCLIRender,
291
+ > {
292
+ program: string
293
+ description?: string
294
+ commands: Commands
295
+ options?: CLIOptions<Options>
296
+ middleware?: PikkuMiddleware[]
297
+ render?: PikkuCLIRender
298
+ docs?: PikkuDocs
299
+ tags?: string[]
300
+ }
301
+
302
+ /**
303
+ * Generic renderer type that can transform data into any output format.
304
+ * Can be used across different wirings for flexible output handling.
305
+ *
306
+ * @template Data - The input data type to be rendered
307
+ * @template Output - The output type after rendering
308
+ * @template Services - The services available to the renderer
309
+ * @template Session - The user session type
310
+ */
311
+ export type CorePikkuRender<
312
+ Data,
313
+ Output,
314
+ Services extends CoreSingletonServices = CoreServices,
315
+ Session extends CoreUserSession = CoreUserSession,
316
+ > = (
317
+ services: Services,
318
+ data: Data,
319
+ session?: Session
320
+ ) => Output | Promise<Output>