|
| 1 | +import { MotiaPlugin, MotiaPluginContext, Step, isApiStep } from '@motiadev/core' |
| 2 | +import { EndpointsStream, mapEndpoint } from './endpoints-stream' |
| 3 | + |
| 4 | +export function plugin({ lockedData }: MotiaPluginContext): MotiaPlugin { |
| 5 | + const stream = lockedData.createStream({ |
| 6 | + filePath: '__motia.api-endpoints.ts', |
| 7 | + hidden: true, |
| 8 | + config: { |
| 9 | + name: '__motia.api-endpoints', |
| 10 | + baseConfig: { storageType: 'custom', factory: () => new EndpointsStream(lockedData) }, |
| 11 | + schema: null as never, |
| 12 | + }, |
| 13 | + })() |
| 14 | + |
| 15 | + const apiStepCreated = (step: Step) => { |
| 16 | + if (isApiStep(step)) { |
| 17 | + stream.set('default', step.filePath, { |
| 18 | + id: step.filePath, |
| 19 | + method: step.config.method, |
| 20 | + path: step.config.path, |
| 21 | + description: step.config.description, |
| 22 | + queryParams: step.config.queryParams, |
| 23 | + }) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + const apiStepUpdated = (step: Step) => { |
| 28 | + if (isApiStep(step)) { |
| 29 | + stream.set('default', step.filePath, mapEndpoint(step)) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const apiStepRemoved = (step: Step) => { |
| 34 | + if (isApiStep(step)) { |
| 35 | + stream.delete('default', step.filePath) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + lockedData.onStep('step-created', apiStepCreated) |
| 40 | + lockedData.onStep('step-updated', apiStepUpdated) |
| 41 | + lockedData.onStep('step-removed', apiStepRemoved) |
| 42 | + |
| 43 | + return { |
| 44 | + workbench: [ |
| 45 | + { |
| 46 | + packageName: '@motiadev/plugin-endpoint', |
| 47 | + cssImports: ['@motiadev/plugin-endpoint/dist/plugin-endpoint.css'], |
| 48 | + label: 'Endpoints', |
| 49 | + position: 'top', |
| 50 | + componentName: 'EndpointsPage', |
| 51 | + labelIcon: 'link-2', |
| 52 | + }, |
| 53 | + ], |
| 54 | + } |
| 55 | +} |
0 commit comments