Skip to content

Merge ErikWittern/openapi-snippet #13

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

Merged
merged 18 commits into from
Jan 15, 2025
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ For example:
"snippets": [
{
"id": "node",
"mimeType": "application/json", // Only set for methods with a request body
"title": "Node + Native",
"content": "var http = require(\"https\");\n\nvar options = {..."
}
Expand All @@ -83,10 +84,11 @@ For example:
```

## Targets
Currently, OpenAPI Snippet supports the following targets (depending on the HTTP Snippet library):
Currently, OpenAPI Snippet supports the following [targets](https://github.com/Kong/httpsnippet/tree/master/src/targets) (depending on the HTTP Snippet library):

* `c_libcurl` (default)
* `csharp_restsharp` (default)
* `csharp_httpclient`
* `go_native` (default)
* `java_okhttp`
* `java_unirest` (default)
Expand Down
89 changes: 51 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,27 @@ const getEndpointSnippets = function (openApi, path, method, targets, values) {
values = {};
}

const har = OpenAPIToHar.getEndpoint(openApi, path, method, values);

const snippet = new HTTPSnippet(har);
const hars = OpenAPIToHar.getEndpoint(openApi, path, method, values);

const snippets = [];
for (let j in targets) {
const target = formatTarget(targets[j]);
if (!target) throw new Error('Invalid target: ' + targets[j]);
snippets.push({
id: targets[j],
title: target.title,
content: snippet.convert(
target.language,
typeof target.library !== 'undefined' ? target.library : null
),
});
for (const har of hars) {
const snippet = new HTTPSnippet(har);
snippets.push(
...getSnippetsForTargets(
targets,
snippet,
har.comment ? har.comment : undefined
)
);
}

// use first element since method, url, and description
// are the same for all elements
return {
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
method: hars[0].method,
url: hars[0].url,
description: hars[0].description,
resource: getResourceName(hars[0].url),
snippets: snippets,
};
};
Expand All @@ -63,33 +61,23 @@ const getEndpointSnippets = function (openApi, path, method, targets, values) {
* ['cURL', 'Node']
*/
const getSnippets = function (openApi, targets) {
const harList = OpenAPIToHar.getAll(openApi);
const endpointHarInfoList = OpenAPIToHar.getAll(openApi);

const results = [];
for (let i in harList) {
for (let i in endpointHarInfoList) {
// create HTTPSnippet object:
const har = harList[i];
const snippet = new HTTPSnippet(har.har);

const harInfo = endpointHarInfoList[i];
const snippets = [];
for (let j in targets) {
const target = formatTarget(targets[j]);
if (!target) throw new Error('Invalid target: ' + targets[j]);
snippets.push({
id: targets[j],
title: target.title,
content: snippet.convert(
target.language,
typeof target.library !== 'undefined' ? target.library : null
),
});
for (const har of harInfo.hars) {
const snippet = new HTTPSnippet(har);
snippets.push(...getSnippetsForTargets(targets, snippet, har.comment));
}

results.push({
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
method: harInfo.method,
url: harInfo.url,
description: harInfo.description,
resource: getResourceName(harInfo.url),
snippets,
});
}
Expand Down Expand Up @@ -195,6 +183,31 @@ const formatTarget = function (targetStr) {
};
};

/**
* Generate code snippets for each of the supplied targets
*
* @param targets {array} List of language targets to generate code for
* @param snippet {Object} Snippet object from httpsnippet to convert into the target objects
* @param mimeType {string | undefined} Additional information to add uniqueness to the produced snippets
*/
const getSnippetsForTargets = function (targets, snippet, mimeType) {
const snippets = [];
for (let j in targets) {
const target = formatTarget(targets[j]);
if (!target) throw new Error('Invalid target: ' + targets[j]);
snippets.push({
id: targets[j],
...(mimeType !== undefined && { mimeType: mimeType }),
title: target.title,
content: snippet.convert(
target.language,
typeof target.library !== 'undefined' ? target.library : null
),
});
}
return snippets;
};

const capitalizeFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
Expand Down
Loading