Skip to content
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

chore: Sample CAP transpilation #273

Merged
merged 32 commits into from
Nov 15, 2024
Merged

chore: Sample CAP transpilation #273

merged 32 commits into from
Nov 15, 2024

Conversation

ZhongpinWang
Copy link
Contributor

@ZhongpinWang ZhongpinWang commented Nov 7, 2024

Context

AI/gen-ai-hub-sdk-js-backlog#160.

This PR configures the sample-cap app, so that CAP can be transpiled into ESM JavaScript. It also provides remote deployment details.

As a result, we confirm that CAP can be used together with our SDK.

Known Issue

Currently, I have to stop using @cap-js/cds-types for type reference since sometimes, even after installing @sap/cds and @cap-js/cds-types (even not as dev deps), I see the following error:

Could not find a declaration file for module '@sap/cds'. '****/ai-sdk-js/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sap/cds/lib/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/sap__cds` if it exists or add a new declaration (.d.ts) file containing `declare module '@sap/cds';`ts(7016)

This will also sometimes fail the check:public-api script as the package installs a symlink in the root directory pointing to a missing target.

A potential workaround could be using pnpm rebuild to re-link the missing @cap-js/cds-types package. But this would require adding workaround code to our pipeline just for this sample package.

An issue was already reported -> cap-js/cds-types#294 (comment).

Also reported here -> cap-js/cds-types#251 (comment)

Definition of Done

  • [ ] Code is tested (Unit, E2E)
  • [ ] Error handling created / updated & covered by the tests above
  • Documentation updated
  • [ ] (Optional) Aligned changes with the Java SDK
  • [ ] (Optional) Release notes updated

@ZhongpinWang ZhongpinWang changed the title fix: Sample CAP transpilation chore: Sample CAP transpilation Nov 8, 2024
@ZhongpinWang ZhongpinWang marked this pull request as ready for review November 8, 2024 12:05
@@ -31,7 +31,7 @@
"sample-code": "pnpm -F=@sap-ai-sdk/sample-code",
"sample-cap": "pnpm -F=@sap-ai-sdk/sample-cap",
"check:public-api": "pnpm -r check:public-api",
"check:deps": "pnpm -r -F !./tests/smoke-tests -F !./tests/schema-tests exec depcheck --ignores=\"nock,@jest/globals\" --quiet"
"check:deps": "pnpm -r -F !./tests/smoke-tests -F !./tests/schema-tests -F !./sample-cap exec depcheck --ignores=\"nock,@jest/globals\" --quiet"
Copy link
Contributor Author

@ZhongpinWang ZhongpinWang Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fyi, I removed deps check for this sample cap package because, e.g., xssec was added when using cds add xsuaa but our check complains that this package is not used. And also for dev deps @cap-js/cds-types.

},
"scripts": {
"prebuild": "rm -rf dist",
"build": "pnpm cds-build && pnpm compile && pnpm cleanup",
"cds-build": "cds build --production",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fyi, cds build only generates (copies) .ts file. We need to run tsc to transpile ts files into js and remove ts files afterwards.

"cds-build": "cds build --production",
"compile": "tsc",
"cleanup": "rm -f ./dist/srv/srv/**/*.ts ./dist/srv/package-lock.json",
"postbuild": "pushd ./dist/srv && npm i --package-lock-only && popd",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to remove the generated (copied) lock file and regenerate it in ./dist/srv using npm directly as for remote deployment, buildpack uses npm instead of pnpm. Otherwise, the lock file would only resolves into a bunch of symbolic links and a incomplete install when using buildpack.

@@ -1,4 +1,5 @@
@path: 'ai-api'
@requires: 'any'
Copy link
Contributor Author

@ZhongpinWang ZhongpinWang Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fyi, nodejs CAP by default authenticate all endpoints. Ideally, setting up an app-router to do authentication would be the right approach. For testing purpose, I just expose them all publicly.

STOP THE APPLICATION after testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would document this in the readme, as this is related to security topics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

sample-cap/package.json Outdated Show resolved Hide resolved
@ZhongpinWang
Copy link
Contributor Author

ZhongpinWang commented Nov 11, 2024

@kay-schmitteckert I have to remove @cap-js/cds-types and using any for Request again, since it also sometimes install an invalid symlink also directly to the root node_modules even if it is defined as a dev dependency in sample-cap package. This will fail our pipeline and add flakiness to every build in the future. Re-run the same job sometimes fix the issue. Check the failed job https://github.com/SAP/ai-sdk-js/actions/runs/11774540395/job/32793282331.

Issue already opened as mentioned above in the PR description. I really don't want to add too much workaround in our repository just for a known CAP issue related to types. Once it is fixed there, we can also update in our repo.

Copy link
Contributor

@jjtang1985 jjtang1985 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment for more documentation.
Otherwise, looks good to me.

@@ -1,4 +1,5 @@
@path: 'ai-api'
@requires: 'any'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would document this in the readme, as this is related to security topics.

@ZhongpinWang ZhongpinWang enabled auto-merge (squash) November 15, 2024 11:48
Copy link
Contributor

@deekshas8 deekshas8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ZhongpinWang ZhongpinWang merged commit f6e4e65 into main Nov 15, 2024
11 checks passed
@ZhongpinWang ZhongpinWang deleted the fix-sample-cap-build branch November 15, 2024 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants