SwaggerClient and data:url's
Hi folks!
I tried to handle data:url for spec instead and faced an exception.
The documentation said
var specUrl = 'http://petstore.swagger.io/v2/swagger.json'; // data urls are OK too 'data:application/json;base64,abc...'
but it does not work.
Example
const swaggerSpecBase64 = fs.readFileSync(__dirname + '/../swagger.yml').toString('base64');
client = await SwaggerClient(`data:text/yaml;base64,${swaggerSpecBase64}`);
throws
/source/node_modules/node-fetch/lib/index.js:1309
throw new TypeError('Only HTTP(S) protocols are supported');
^
TypeError: Only HTTP(S) protocols are supported
What is wrong here?
i guess a update to node-fetch@3 would add support for dataurls
Yes @jimmywarting is absolutely right. We need [email protected] to be able to add support for data URLs. Here is a POC:
import fs from 'node:fs';
import util from 'node:util';
import fetch from 'node-fetch';
const data = fs.readFileSync('./petstore.json').toString('base64');
const dataURL = `data:application/json;base64,${data}`;
const response = await fetch(dataURL);
const jsonBody = await response.json();
console.dir(jsonBody);
When https://github.com/swagger-api/swagger-js/issues/2414 and https://github.com/swagger-api/swagger-js/issues/2415 are processed this issue will autoresolve.
+1 This would be a great add to allow a local load of a swagger file vs relying on a hosted version. Looking to use this solution where adding an extra HTTP fetch is not ideal.
Addressed by https://github.com/swagger-api/swagger-js/pull/3137/commits/9f7e0bcffa34991cc21960f14e13478c911a060e in https://github.com/swagger-api/swagger-js/pull/3137
:tada: This issue has been resolved in version 3.21.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket: