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