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

feat(snippet): upgrading httsnippet for some JS/Node modernizations #899

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/oas-to-snippet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ npm run build && node bin/generate-target-markdown-table.js
| JavaScript | `javascript` | [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest), [Axios](https://github.com/axios/axios), [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch), [jQuery](http://api.jquery.com/jquery.ajax/)
| JSON | `json` | [Native JSON](https://www.json.org/json-en.html)
| Kotlin | `kotlin` | [OkHttp](http://square.github.io/okhttp/)
| Node.js | `node` | [HTTP](http://nodejs.org/api/http.html#http_http_request_options_callback), [Request](https://github.com/request/request), [Unirest](http://unirest.io/nodejs.html), [Axios](https://github.com/axios/axios), [Fetch](https://github.com/bitinn/node-fetch)
| Node.js | `node` | [HTTP](http://nodejs.org/api/http.html#http_http_request_options_callback), [Axios](https://github.com/axios/axios), [fetch](https://nodejs.org/docs/latest/api/globals.html#fetch)
| Objective-C | `objectivec` | [NSURLSession](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html)
| OCaml | `ocaml` | [CoHTTP](https://github.com/mirage/ocaml-cohttp)
| PHP | `php` | [cURL](http://php.net/manual/en/book.curl.php), [Guzzle](http://docs.guzzlephp.org/en/stable/), [HTTP v1](http://php.net/manual/en/book.http.php), [HTTP v2](http://devel-m6w6.rhcloud.com/mdref/http)
Expand Down
2 changes: 1 addition & 1 deletion packages/oas-to-snippet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"test": "vitest run --coverage"
},
"dependencies": {
"@readme/httpsnippet": "^10.0.5",
"@readme/httpsnippet": "^10.1.0",
"@readme/oas-to-har": "file:../oas-to-har"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/oas-to-snippet/src/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ const DEFAULT_LANGUAGES: SupportedLanguages = {
default: 'fetch',
targets: {
axios: { name: 'Axios' },
fetch: { name: 'node-fetch' },
fetch: { name: 'fetch' },
native: { name: 'http' },
request: { name: 'Request' },
},
},
},
Expand Down
66 changes: 17 additions & 49 deletions packages/oas-to-snippet/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ exports[`oas-to-snippet > supported languages > javascript > should generate cod
};

fetch('http://petstore.swagger.io/v2/pet', options)
.then(response => response.json())
.then(response => console.log(response))
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));",
"highlightMode": "javascript",
"install": false,
Expand All @@ -353,12 +353,8 @@ const options = {

axios
.request(options)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});",
.then(res => console.log(res.data))
.catch(err => console.error(err));",
"highlightMode": "javascript",
"install": "npm install axios --save",
}
Expand All @@ -369,8 +365,8 @@ exports[`oas-to-snippet > supported languages > javascript > targets > fetch > s
"code": "const options = {method: 'GET', headers: {accept: 'application/json'}};

fetch('http://petstore.swagger.io/v2/user/login?username=woof&password=barkbarkbark', options)
.then(response => response.json())
.then(response => console.log(response))
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));",
"highlightMode": "javascript",
"install": false,
Expand All @@ -389,8 +385,8 @@ exports[`oas-to-snippet > supported languages > javascript > targets > jquery >
}
};

$.ajax(settings).done(function (response) {
console.log(response);
$.ajax(settings).done(res => {
console.log(res);
});",
"highlightMode": "javascript",
"install": false,
Expand Down Expand Up @@ -473,9 +469,7 @@ val response = client.newCall(request).execute()",

exports[`oas-to-snippet > supported languages > node > should generate code for the default target 1`] = `
{
"code": "const fetch = require('node-fetch');

const url = 'http://petstore.swagger.io/v2/pet';
"code": "const url = 'http://petstore.swagger.io/v2/pet';
const options = {
method: 'POST',
headers: {'content-type': 'application/json'},
Expand All @@ -485,9 +479,9 @@ const options = {
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));",
.catch(err => console.error(err));",
"highlightMode": "javascript",
"install": "npm install node-fetch@2 --save",
"install": false,
}
`;

Expand Down Expand Up @@ -517,7 +511,7 @@ sdk.loginUser({username: 'woof', password: 'barkbarkbark'})

exports[`oas-to-snippet > supported languages > node > targets > axios > should support snippet generation 1`] = `
{
"code": "const axios = require('axios');
"code": "import axios from 'axios';

const options = {
method: 'GET',
Expand All @@ -527,30 +521,24 @@ const options = {

axios
.request(options)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});",
.then(res => console.log(res.data))
.catch(err => console.error(err));",
"highlightMode": "javascript",
"install": "npm install axios --save",
}
`;

exports[`oas-to-snippet > supported languages > node > targets > fetch > should support snippet generation 1`] = `
{
"code": "const fetch = require('node-fetch');

const url = 'http://petstore.swagger.io/v2/user/login?username=woof&password=barkbarkbark';
"code": "const url = 'http://petstore.swagger.io/v2/user/login?username=woof&password=barkbarkbark';
const options = {method: 'GET', headers: {accept: 'application/json'}};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));",
.catch(err => console.error(err));",
"highlightMode": "javascript",
"install": "npm install node-fetch@2 --save",
"install": false,
}
`;

Expand Down Expand Up @@ -587,26 +575,6 @@ req.end();",
}
`;

exports[`oas-to-snippet > supported languages > node > targets > request > should support snippet generation 1`] = `
{
"code": "const request = require('request');

const options = {
method: 'GET',
url: 'http://petstore.swagger.io/v2/user/login?username=woof&password=barkbarkbark',
headers: {accept: 'application/json'}
};

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});",
"highlightMode": "javascript",
"install": "npm install request --save",
}
`;

exports[`oas-to-snippet > supported languages > objectivec > should generate code for the default target 1`] = `
{
"code": "#import <Foundation/Foundation.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,11 @@ exports[`#getSupportedLanguages > should retrieve our default supported language
"name": "Axios",
},
"fetch": {
"install": "npm install node-fetch@2 --save",
"name": "node-fetch",
"name": "fetch",
},
"native": {
"name": "http",
},
"request": {
"install": "npm install request --save",
"name": "Request",
},
},
},
},
Expand Down
Loading