Skip to content

Commit

Permalink
Use @cap-js/openapi instead of @sap/cds-dk (#134)
Browse files Browse the repository at this point in the history
Also test w/ Node 22
  • Loading branch information
chgeo authored Jul 15, 2024
1 parent dca82c4 commit 2fdd8a0
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 4,178 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: [20.x, 18.x]
node-version: [22.x, 20.x, 18.x]

steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
registry=https://registry.npmjs.org/
@sap:registry=https://registry.npmjs.org/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).

The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.9.0 - 2024-07-15

### Changed

- Use `@cap-js/openapi` instead of `@sap/cds-dk`, leading to lighter dependencies

## Version 0.8.0 - 2024-01-18

### Added
Expand Down
23 changes: 3 additions & 20 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const cds = requireCdsOpenAPI()
const cds = require('@sap/cds')
const openapi = require('@cap-js/openapi')
const debug = cds.debug('swagger')

const swaggerUi = require('swagger-ui-express')
Expand Down Expand Up @@ -36,7 +37,7 @@ module.exports = (options={}, swaggerUiOptions={}) => {
const cache = {}
function toOpenApiDoc (service, options = {}) {
if (!cache[service.name]) {
cache[service.name] = cds.compile.to.openapi(service.model, {
cache[service.name] = openapi.compile(service.model, {
service: service.name,
'odata-version': options.odataVersion,
'openapi:url': join('/', options.apiPath, service.path),
Expand All @@ -56,24 +57,6 @@ function addLinkToIndexHtml (service, apiPath) {
service.$linkProviders ? service.$linkProviders.push(provider) : service.$linkProviders = [provider]
}

/**
* Loads the compile.to.openapi function from @sap/cds-dk or throws an error if not installed.
* Will get simpler in the future when we have a dedicated openapi plugin.
*
* @returns { import('@sap/cds-dk') }
*/
function requireCdsOpenAPI () {
try {
return require('@sap/cds-dk')
} catch (err) {
const cds = require('@sap/cds')
if (!cds.compile.to.openapi) {
throw new Error(`'@sap/cds-dk' is not installed. Add it as a (dev) dependency to use this plugin`, { cause: err })
}
return cds
}
}

/**
* @typedef {Object} CdsSwaggerOptions
* @property {string} basePath - the root path to mount the middleware on
Expand Down
Loading

0 comments on commit 2fdd8a0

Please sign in to comment.