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

SwaggerClient and data:url's #2381

Closed
poluyanov opened this issue Dec 24, 2021 · 5 comments · Fixed by #3137
Closed

SwaggerClient and data:url's #2381

poluyanov opened this issue Dec 24, 2021 · 5 comments · Fixed by #3137

Comments

@poluyanov
Copy link

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?

@jimmywarting
Copy link
Contributor

i guess a update to node-fetch@3 would add support for dataurls

@char0n char0n self-assigned this Jan 14, 2022
@char0n
Copy link
Member

char0n commented Jan 14, 2022

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 #2414 and #2415 are processed this issue will autoresolve.

@artgibson
Copy link

+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.

char0n added a commit that referenced this issue Sep 12, 2023
@char0n
Copy link
Member

char0n commented Sep 12, 2023

Addressed by 9f7e0bc in #3137

@char0n char0n closed this as completed Sep 12, 2023
char0n added a commit that referenced this issue Sep 12, 2023
swagger-client requires Node.js >=12.20.0 and uses different fetch 
implementation depending on Node.js version:

>=12.20.0 <16.8 - node-fetch@3
>=16.8 <18 - undici
>=18 - native Node.js fetch

Closes #1220
Closes #2736
Closes #2415
Closes #2381
Closes #2187
Closes #2291
swagger-bot pushed a commit that referenced this issue Sep 12, 2023
# [3.21.0](v3.20.2...v3.21.0) (2023-09-12)

### Features

* replace node-fetch with undici ([#3137](#3137)) ([bc7ca17](bc7ca17)), closes [#1220](#1220) [#2736](#2736) [#2415](#2415) [#2381](#2381) [#2187](#2187) [#2291](#2291)
@swagger-bot
Copy link
Contributor

🎉 This issue has been resolved in version 3.21.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants