-
Notifications
You must be signed in to change notification settings - Fork 0
Making the plugin to js #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9c28c6b
moving cds-plugin implementation to lib/index
Kronprinz03 9ed691b
moved types folder to lib
Kronprinz03 8d42607
fixed import paths in index
Kronprinz03 ef74e52
Fixed compile to js
Kronprinz03 3934a0a
fixed imports
Kronprinz03 0e0b2df
added compile step
Kronprinz03 ba72c63
Added prebuild hook
Kronprinz03 d499a66
adding js wrapper for srv
Kronprinz03 af1be47
add back action for compile
Kronprinz03 aceca15
fix lint
Kronprinz03 ad4470d
added files
Kronprinz03 cafab5b
fixed files
Kronprinz03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ clients/ | |
| @cds-models/ | ||
| package-lock.json | ||
| .cdsrc-private.json | ||
| @cds-models | ||
| @cds-models | ||
| dist/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| tests/bookshop/workflows/* | ||
| tests/bookshop/workflows/* | ||
| dist/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| module.exports = require('./dist/cds-plugin'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,150 +1 @@ | ||
| import cds, { Results } from '@sap/cds'; | ||
| import { EntityEventCache } from './types/cds-plugin'; | ||
| import { | ||
| handleProcessCancel, | ||
| handleProcessResume, | ||
| handleProcessStart, | ||
| handleProcessSuspend, | ||
| ProcessValidationPlugin, | ||
| registerProcessServiceHandlers, | ||
| PROCESS_START_ID, | ||
| PROCESS_START_ON, | ||
| PROCESS_CANCEL_ON, | ||
| PROCESS_SUSPEND_ON, | ||
| PROCESS_RESUME_ON, | ||
| PROCESS_PREFIX, | ||
| CUD_EVENTS, | ||
| EntityRow, | ||
| addDeletedEntityToRequestCancel, | ||
| addDeletedEntityToRequestStart, | ||
| addDeletedEntityToRequestStartBusinessKey, | ||
| addDeletedEntityToRequestResume, | ||
| addDeletedEntityToRequestSuspend, | ||
| ProcessDeleteRequest, | ||
| } from './lib/index'; | ||
| import { importProcess } from './lib/processImport'; | ||
|
|
||
| // Register build plugin for annotation validation during cds build | ||
| cds.build?.register?.('process-validation', ProcessValidationPlugin); | ||
|
|
||
| // Register import handler for: cds import --from process | ||
| // @ts-expect-error: import does not exist on cds type | ||
| cds.import ??= {}; | ||
| //@ts-expect-error: cds type does not exist | ||
| cds.import.options ??= {}; | ||
| //@ts-expect-error: cds type does not exist | ||
| cds.import.options.process = { no_copy: true, as: 'cds', config: 'kind=process-service' }; | ||
| // @ts-expect-error: process does not exist on cds.import type | ||
| cds.import.from ??= {}; | ||
| // @ts-expect-error: from does not exist on cds.import type | ||
| cds.import.from.process = importProcess; | ||
|
|
||
| cds.on('serving', async (service: cds.Service) => { | ||
| if (service instanceof cds.ApplicationService == false) return; | ||
|
|
||
| const annotationCache = buildAnnotationCache(service); | ||
|
|
||
| service.before('DELETE', async (req: cds.Request) => { | ||
| const cacheKey = `${req.target.name}:${req.event}`; | ||
| const cached = annotationCache.get(cacheKey); | ||
|
|
||
| if (!cached) return; // Fast exit - no annotations | ||
| const results = await Promise.all( | ||
| [ | ||
| cached.hasStart && addDeletedEntityToRequestStart(req), | ||
| cached.hasStart && addDeletedEntityToRequestStartBusinessKey(req), | ||
| cached.hasCancel && addDeletedEntityToRequestCancel(req), | ||
| cached.hasResume && addDeletedEntityToRequestResume(req), | ||
| cached.hasSuspend && addDeletedEntityToRequestSuspend(req), | ||
| ].filter(Boolean), | ||
| ); | ||
| (req as ProcessDeleteRequest)._Process = Object.assign({}, ...results); | ||
| }); | ||
|
|
||
| service.after('*', async (results: Results, req: cds.Request) => { | ||
| if (!req.target) return; | ||
| const cacheKey = `${req.target.name}:${req.event}`; | ||
| const cached = annotationCache.get(cacheKey); | ||
|
|
||
| if (!cached) return; // Fast exit - no annotations | ||
|
|
||
| const rows: EntityRow[] = Array.isArray(results) ? results : [results]; | ||
| if (rows.length > 0) { | ||
| await Promise.all(rows.map((row) => dispatchProcessHandlers(cached, req, row))); | ||
| } else { | ||
| await dispatchProcessHandlers(cached, req, req.data); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| async function dispatchProcessHandlers( | ||
| cached: EntityEventCache, | ||
| req: cds.Request, | ||
| data: EntityRow, | ||
| ) { | ||
| if (cached.hasStart) { | ||
| await handleProcessStart(req, data); | ||
| } | ||
| if (cached.hasCancel) { | ||
| await handleProcessCancel(req, data); | ||
| } | ||
| if (cached.hasSuspend) { | ||
| await handleProcessSuspend(req, data); | ||
| } | ||
| if (cached.hasResume) { | ||
| await handleProcessResume(req, data); | ||
| } | ||
| } | ||
|
|
||
| function expandEvent(event: string | undefined, entity: cds.entity): string[] { | ||
| if (!event) return []; | ||
| if (event === '*') { | ||
| const boundActions = entity.actions ? Object.keys(entity.actions) : []; | ||
| return [...CUD_EVENTS, ...boundActions]; | ||
| } | ||
| return [event]; | ||
| } | ||
|
|
||
| function buildAnnotationCache(service: cds.Service) { | ||
| const cache = new Map<string, EntityEventCache>(); | ||
| for (const entity of Object.values(service.entities)) { | ||
| const startEvent = entity[PROCESS_START_ON]; | ||
| const cancelEvent = entity[PROCESS_CANCEL_ON]; | ||
| const suspendEvent = entity[PROCESS_SUSPEND_ON]; | ||
| const resumeEvent = entity[PROCESS_RESUME_ON]; | ||
|
|
||
| const events = new Set<string>(); | ||
| for (const ev of expandEvent(startEvent, entity)) events.add(ev); | ||
| for (const ev of expandEvent(cancelEvent, entity)) events.add(ev); | ||
| for (const ev of expandEvent(suspendEvent, entity)) events.add(ev); | ||
| for (const ev of expandEvent(resumeEvent, entity)) events.add(ev); | ||
|
|
||
| for (const event of events) { | ||
| const matchesEvent = (annotationEvent: string | undefined) => | ||
| annotationEvent === event || annotationEvent === '*'; | ||
|
|
||
| const hasStart = !!(matchesEvent(startEvent) && entity[PROCESS_START_ID]); | ||
| const hasCancel = !!matchesEvent(cancelEvent); | ||
| const hasSuspend = !!matchesEvent(suspendEvent); | ||
| const hasResume = !!matchesEvent(resumeEvent); | ||
|
|
||
| const cacheKey = `${entity.name}:${event}`; | ||
| cache.set(cacheKey, { | ||
| hasStart, | ||
| hasCancel, | ||
| hasSuspend, | ||
| hasResume, | ||
| }); | ||
| } | ||
| } | ||
| return cache; | ||
| } | ||
|
|
||
| cds.on('served', async (services) => { | ||
| const processServices = Object.values(services).filter( | ||
| (service) => service.definition?.[PROCESS_PREFIX], | ||
| ); | ||
| for (const service of processServices) { | ||
| registerProcessServiceHandlers(service); | ||
| } | ||
| }); | ||
| import './lib'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic Error: Side-effect plugin code (CDS event registrations, import handler setup) has been moved into
lib/index.ts, which is also a barrel re-export module. Any consumer who imports anything fromlib/index.ts(e.g. in tests or programmatically) will inadvertently triggercds.on('serving', ...),cds.on('served', ...), andcds.build?.register?.(...)as side effects.This breaks the separation of concerns that existed when the plugin code was isolated in
cds-plugin.ts. Consider moving the plugin registration logic back tocds-plugin.ts(importing from./libfor shared utilities) and keepinglib/index.tsas a pure export barrel.Please provide feedback on the review comment by checking the appropriate box: