SAP / cloud-sdk-js

Use the SAP Cloud SDK for JavaScript / TypeScript to reduce development effort when building applications on SAP Business Technology Platform that communicate with SAP solutions and services such as SAP S/4HANA Cloud, SAP SuccessFactors, and many others.
Apache License 2.0
167 stars 57 forks source link

OpenAPI AuthorizationAPI in CAP Node JS #3190

Closed Showkath closed 1 year ago

Showkath commented 1 year ago

Hi,

As explained in https://api.sap.com/api/AuthorizationAPI/cloud-sdk/JavaScript use "/sap/rest/authorization/v2/rolecollections" AuthorizationAPI for below use case in CAP Node JS

My Github : https://github.com/Showkath/shopping-cart

1) Get role collection details 2) Get Users List = Users Who are part of role collection

Idea is to use JavaScript type-safe client instead of axios.request(...);

const responseData = await RoleCollectionsApi.getRoleCollections().execute({ destinationName: 'myDestinationName' }); Note : I have reference to Official Documents "Installation & Generation Steps" from https://api.sap.com/api/AuthorizationAPI/cloud-sdk/JavaScript https://sap.github.io/cloud-sdk/docs/js/features/openapi/generate-client

I have included below dependencies in CAP Node JS & ran "npm install" https://github.com/Showkath/shopping-cart/blob/master/package.json#L22-L23 "@sap-cloud-sdk/generator": "2.12.0", "@sap-cloud-sdk/openapi-generator": "2.12.0" In https://github.com/Showkath/shopping-cart/blob/master/srv/product-catalog-service.js#L8 //import { RoleCollectionsApi } from './generated/AuthorizationAPI'; //const RoleCollectionsApi = require('./generated/AuthorizationAPI'); const RoleCollectionsApi = require('./gen/AuthorizationAPI');

I tried different syntax but facing below error

[ERROR] Cannot find module 'AuthorizationAPI'

FrankEssenberger commented 1 year ago

Good morning @Showkath,

great that you want to use the SDK with a typesafe client. The error cannot find module 'XYZ' means that in the folder where you do the require/import from no export with the given name is present. I checked the repo and there is a few lines (commented out) like:

const RoleCollectionsApi = require('./generated/AuthorizationAPI');

but there is no folder called generated next to the file. Perhaps you did not check them in, which is fine and also the safe way for public repositories but when you exectue the code you need a folder generated in which a index should be present exporting the AuthorizationAPI. Is this present? You can try also with a isolated script file and just run node my-scrip.js to see if the require/imports work.

Also note that for calls to standard BTP services the SDK will provide some extra convenience that if you pass the service name as destinationName a dummy destination is build for you under the hood with client credential grant token being fetched see here.

Best Frank

FrankEssenberger commented 1 year ago

I also made a quick test and I could generate the service and use it as expected:(the path for the require depends of course where you stored the generated client)

const api = require('./../test-services-openapi/AuthorizationAPI')

const request = api.RoleCollectionsApi.getRoleCollections()
console.log(JSON.stringify(request,null,2))

returns the expected content:

{
  "method": "get",
  "pathPattern": "/sap/rest/authorization/v2/rolecollections",
  "parameters": {},
  "customHeaders": {},
  "customRequestConfiguration": {},
  "_fetchCsrfToken": true
}
jjtang1985 commented 1 year ago

Hi @Showkath,

as promised in the call, here is the tutorial about executing an request against the "API Business Hub Sandbox" system: https://sap.github.io/cloud-sdk/docs/js/tutorials/getting-started/execute-an-odata-request#execute-an-odata-request

Please open the page and search for "sandbox", so you find an example about how to add an api key.

Please share your error message during the generation, if you need follow up.

Thank you.

Best regards, Junjie

Showkath commented 1 year ago

Hi @Showkath,

as promised in the call, here is the tutorial about executing an request against the "API Business Hub Sandbox" system: https://sap.github.io/cloud-sdk/docs/js/tutorials/getting-started/execute-an-odata-request#execute-an-odata-request

Please open the page and search for "sandbox", so you find an example about how to add an api key.

Please share your error message during the generation, if you need follow up.

Thank you.

Best regards, Junjie

Thanks @jjtang1985 Normal command works for TypeScript npx openapi-generator --input resources/service-specs --outputDir src/generated When i tried below command with Transpile options

npx openapi-generator --input resources/service-specs --outputDir src/generated -t

I am getting below errors ,however typesafe client are generated

node -v

v16.16.0

npm -v 8.11.0

Attaching error details

user: shopping-cart $ npx openapi-generator --input resources/service-specs --outputDir src/generated -t [2022-12-09T14:36:22.835Z] WARN (openapi-generator): Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime. [2022-12-09T14:36:22.838Z] WARN (openapi-generator): Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime. [2022-12-09T14:36:22.839Z] WARN (openapi-generator): Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime. [2022-12-09T14:36:22.839Z] WARN (openapi-generator): Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime. [2022-12-09T14:36:22.839Z] WARN (openapi-generator): Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime. [2022-12-09T14:36:22.840Z] WARN (openapi-generator): Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime. [2022-12-09T14:36:22.840Z] WARN (openapi-generator): Could not parse media type, because it is not 'application/json'. Generation will continue with 'any'. This might lead to errors at runtime. [2022-12-09T14:36:24.711Z] INFO (openapi-generator): Finished generation. Don't forget to add @sap-cloud-sdk/openapi to your dependencies. [2022-12-09T14:36:24.711Z] ERROR (openapi-generator): ErrorWithCause: Did you forget to install "@sap-cloud-sdk/openapi"? at generateWithParsedOptions (/home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/openapi-generator/dist/generator.js:63:19) at async parseCmdArgs (/home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/openapi-generator/dist/cli.js:25:13) Caused by: Error: Could not generate client. Errors: [ Error: Compilation Errors: /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/http-agent/agent-config.d.ts:1:22 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/http-agent/agent-config.d.ts:1:53 - error TS2307: Cannot find module 'http' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/http-agent/http-agent.d.ts:1:22 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/http-agent/http-agent.d.ts:1:48 - error TS2307: Cannot find module 'http' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/scp-cf/destination/http-proxy-util.d.ts:1:22 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/scp-cf/destination/http-proxy-util.d.ts:1:60 - error TS2307: Cannot find module 'https' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/scp-cf/jwt.d.ts:1:22 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/connectivity/dist/scp-cf/jwt.d.ts:1:63 - error TS2307: Cannot find module 'http' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/http-client/dist/http-client-types.d.ts:1:22 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/http-client/dist/http-client-types.d.ts:1:53 - error TS2307: Cannot find module 'http' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/util/dist/fs.d.ts:1:22 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/util/dist/fs.d.ts:1:56 - error TS2307: Cannot find module 'fs' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/winston-transport/index.d.ts:1:241 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/winston-transport/index.d.ts:1:275 - error TS2307: Cannot find module 'stream' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/winston/index.d.ts:1:111 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/winston/index.d.ts:1:153 - error TS2307: Cannot find module 'stream' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/winston/index.d.ts:6:3769 - error TS2503: Cannot find namespace 'NodeJS'. /home/user/projects/shopping-cart/node_modules/winston/index.d.ts:6:5228 - error TS2503: Cannot find namespace 'NodeJS'. /home/user/projects/shopping-cart/node_modules/winston/lib/winston/config/index.d.ts:1:111 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/winston/lib/winston/transports/index.d.ts:1:111 - error TS2688: Cannot find type definition file for 'node'. /home/user/projects/shopping-cart/node_modules/winston/lib/winston/transports/index.d.ts:1:143 - error TS2307: Cannot find module 'http' or its corresponding type declarations. /home/user/projects/shopping-cart/node_modules/winston/lib/winston/transports/index.d.ts:3:619 - error TS2503: Cannot find namespace 'NodeJS'. /home/user/projects/shopping-cart/node_modules/winston/lib/winston/transports/index.d.ts:3:2052 - error TS2503: Cannot find namespace 'NodeJS'. ] at finishAll (/home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/util/dist/promise.js:22:15) at async generateWithParsedOptions (/home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/openapi-generator/dist/generator.js:59:9) at async parseCmdArgs (/home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/openapi-generator/dist/cli.js:25:13)

FrankEssenberger commented 1 year ago

Hi @Showkath,

there are many errors I know but the relevant one I think is:

[2022-12-09T14:36:24.711Z] ERROR (openapi-generator): ErrorWithCause: Did you forget to install "@sap-cloud-sdk/openapi"?

Is this dependency part of your package.json? This is needed for transpilation as described here.

Best Frank

Showkath commented 1 year ago

https://github.com/Showkath/shopping-cart

Thanks @FrankEssenberger for hint

As i was getting below error so addition to openapi dependency ,i have added node dependency

/home/user/projects/shopping-cart/node_modules/@sap-cloud-sdk/http-client/dist/http-client-types.d.ts:1:53 - error TS2307: Cannot find module 'http' or its corresponding type declarations.

npm install -D @sap-cloud-sdk/openapi npm install -D @types/node

My latest changes for others reference

https://github.com/Showkath/shopping-cart/blob/master/package.json#L17-L19

"@sap-cloud-sdk/openapi": "^2.12.0",
"@types/node": "^18.11.13",

BR, Showkath.