|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +// We use z.any() for services that are interfaces with methods, |
| 4 | +// as Zod cannot easily validate function signatures at runtime. |
| 5 | +export const PluginContextSchema = z.object({ |
| 6 | + ql: z.object({ |
| 7 | + object: z.function().returns(z.any()), // Return any to allow method chaining |
| 8 | + query: z.function().returns(z.any()), |
| 9 | + }).passthrough().describe('ObjectQL Engine Interface'), |
| 10 | + |
| 11 | + os: z.object({ |
| 12 | + getCurrentUser: z.function().returns(z.any()), |
| 13 | + getConfig: z.function().returns(z.any()), |
| 14 | + }).passthrough().describe('ObjectStack Kernel Interface'), |
| 15 | + |
| 16 | + logger: z.object({ |
| 17 | + debug: z.function().returns(z.void()), |
| 18 | + info: z.function().returns(z.void()), |
| 19 | + warn: z.function().returns(z.void()), |
| 20 | + error: z.function().returns(z.void()), |
| 21 | + }).passthrough().describe('Logger Interface'), |
| 22 | + |
| 23 | + storage: z.object({ |
| 24 | + get: z.function().returns(z.any()), |
| 25 | + set: z.function().returns(z.promise(z.void())), |
| 26 | + delete: z.function().returns(z.promise(z.void())), |
| 27 | + }).passthrough().describe('Storage Interface'), |
| 28 | + |
| 29 | + i18n: z.object({ |
| 30 | + t: z.function().returns(z.string()), |
| 31 | + getLocale: z.function().returns(z.string()), |
| 32 | + }).passthrough().describe('Internationalization Interface'), |
| 33 | + |
| 34 | + metadata: z.record(z.any()), |
| 35 | + events: z.record(z.any()), |
| 36 | + |
| 37 | + app: z.object({ |
| 38 | + router: z.object({ |
| 39 | + get: z.function().returns(z.any()), |
| 40 | + post: z.function().returns(z.any()), |
| 41 | + use: z.function().returns(z.any()), |
| 42 | + }).passthrough() |
| 43 | + }).passthrough().describe('App Framework Interface'), |
| 44 | + |
| 45 | + drivers: z.object({ |
| 46 | + register: z.function().returns(z.void()), |
| 47 | + }).passthrough().describe('Driver Registry'), |
| 48 | +}); |
| 49 | + |
| 50 | +export type PluginContextData = z.infer<typeof PluginContextSchema>; |
| 51 | + |
| 52 | +export const PluginLifecycleSchema = z.object({ |
| 53 | + onInstall: z.function() |
| 54 | + .args(PluginContextSchema) |
| 55 | + .returns(z.promise(z.void())) |
| 56 | + .optional(), |
| 57 | + |
| 58 | + onEnable: z.function() |
| 59 | + .args(PluginContextSchema) |
| 60 | + .returns(z.promise(z.void())) |
| 61 | + .optional(), |
| 62 | + |
| 63 | + onDisable: z.function() |
| 64 | + .args(PluginContextSchema) |
| 65 | + .returns(z.promise(z.void())) |
| 66 | + .optional(), |
| 67 | + |
| 68 | + onUninstall: z.function() |
| 69 | + .args(PluginContextSchema) |
| 70 | + .returns(z.promise(z.void())) |
| 71 | + .optional(), |
| 72 | + |
| 73 | + onUpgrade: z.function() |
| 74 | + .args(PluginContextSchema, z.string(), z.string()) |
| 75 | + .returns(z.promise(z.void())) |
| 76 | + .optional(), |
| 77 | +}); |
| 78 | + |
| 79 | +export type PluginLifecycleHooks = z.infer<typeof PluginLifecycleSchema>; |
| 80 | + |
| 81 | +export const PluginSchema = PluginLifecycleSchema.extend({ |
| 82 | + id: z.string().min(1).describe('Unique Plugin ID (e.g. com.example.crm)'), |
| 83 | + version: z.string().regex(/^\d+\.\d+\.\d+$/).describe('Semantic Version'), |
| 84 | + description: z.string().optional(), |
| 85 | + author: z.string().optional(), |
| 86 | + homepage: z.string().url().optional(), |
| 87 | +}); |
| 88 | + |
| 89 | +export type PluginDefinition = z.infer<typeof PluginSchema>; |
0 commit comments