bvanderlaan / swagger-ui-restify

Adds middleware to your restify app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
3 stars 10 forks source link

DeprecationWarning: Invalid 'main' field in 'swagger-ui-restify/package.json' #9

Open Allyhere opened 1 year ago

Allyhere commented 1 year ago

Im using swagger-ui-restify in the latest version (3.0.8 at the time of this issue) with node v16.19.0 and npm v8.19.3 and got the following warning

 [DEP0128] DeprecationWarning: Invalid 'main' field in 'my-project/node_modules/swagger-ui-restify/package.json' of './lib/index.js'. Please either fix that or report it to the module author

It seems that the main field references to a unresolvable path

"main": "./lib/index.js",

The swagger-ui-restify folder tree in my node_modules:

swagger-ui-restify
  | - static
  index.js
  indexTemplate.html

  LICENSE
  package.json
  README.md
  swagger-ui-init.js
devlinjunker commented 1 year ago

Seems like this repo isn't supported anymore...

I am just using swagger-ui-dist and doing some static hosting myself (I'm still on restify v7)

const pathToSwaggerUi = require('swagger-ui-dist').getAbsoluteFSPath();

// serve swagger-ui
server.get('/swagger/*', function (req, res) {
  const filePath: string = req.params['*'] || 'index.html';
  try {
    const file = fs.readFileSync(path.join(pathToSwaggerUi, filePath));
    res.write(file);
    res.end();
  } catch {
    res.send(500);
  }
  res.end();
});

You can also point at your own OpenAPI file and update the other configuration params for swagger-ui :

// serve openapi file
const OPENAPI_PATH = '/openapi.yaml';
server.get(OPENAPI_PATH, function (req, res) {
  const swaggerDoc = fs.readFileSync(OPENAPI_FILE_PATH);
  res.write(swaggerDoc);
  res.end();
});

// overwrite swagger initializer config
const pathToSwaggerUi = require('swagger-ui-dist').getAbsoluteFSPath();
const indexContent = fs
  .readFileSync(path.join(pathToSwaggerUi, 'swagger-initializer.js'))
  .toString()
  // to point at local OpenAPI File
  .replace('https://petstore.swagger.io/v2/swagger.json', OPENAPI_PATH)

  // and to set other configs from https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
  .replace(
    'layout: "StandaloneLayout",',
    `layout: "StandaloneLayout",
    <config_key>: <configvalue>
  `
  );
server.get('/swagger/swagger-initializer.js', (req, res) => {
  res.write(indexContent);
  res.end();
});

You'll want to place that second snippet before server.get('/swagger/*', ...) in the first