Skip to content

Commit 78a78c4

Browse files
committed
feat: replace postman codegen with httpsnippet
1 parent afa156a commit 78a78c4

5 files changed

Lines changed: 149 additions & 138 deletions

File tree

packages/docusaurus-theme-openapi-docs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
"file-saver": "^2.0.5",
5454
"lodash": "^4.17.21",
5555
"pako": "^2.1.0",
56-
"postman-code-generators": "^2.0.0",
5756
"postman-collection": "^5.0.2",
57+
"httpsnippet-lite": "^3.0.5",
58+
"@har-sdk/postman": "^2.4.7",
5859
"prism-react-renderer": "^2.4.1",
5960
"process": "^0.11.10",
6061
"react-hook-form": "^7.59.0",

packages/docusaurus-theme-openapi-docs/src/postman-code-generators.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsx

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
* ========================================================================== */
77

8-
import React, { useState, useEffect } from "react";
8+
import React, { useEffect, useState } from "react";
99

1010
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
11+
import { postman2har } from "@har-sdk/postman";
1112
import ApiCodeBlock from "@theme/ApiExplorer/ApiCodeBlock";
1213
import buildPostmanRequest from "@theme/ApiExplorer/buildPostmanRequest";
1314
import CodeTabs from "@theme/ApiExplorer/CodeTabs";
1415
import { useTypedSelector } from "@theme/ApiItem/hooks";
16+
import HTTPSnippet from "httpsnippet-lite";
1517
import cloneDeep from "lodash/cloneDeep";
16-
import codegen from "postman-code-generators";
1718
import * as sdk from "postman-collection";
1819

1920
import { CodeSample, Language } from "./code-snippets-types";
@@ -150,43 +151,29 @@ function CodeSnippets({
150151
setCodeSampleCodeText(getCodeSampleSourceFromLanguage(language));
151152
}
152153

153-
if (language && !!language.options) {
154-
codegen.convert(
155-
language.language,
156-
language.variant,
157-
cleanedPostmanRequest,
158-
language.options,
159-
(error: any, snippet: string) => {
160-
if (error) {
161-
return;
162-
}
163-
setCodeText(snippet);
164-
}
165-
);
166-
} else if (language && !language.options) {
154+
async function generateSnippet() {
155+
if (!language) {
156+
setCodeText("");
157+
return;
158+
}
167159
const langSource = mergedLangs.filter(
168160
(lang) => lang.language === language.language
169161
);
170-
171-
// Merges user-defined language with default languageSet
172-
// This allows users to define only the minimal properties necessary in languageTabs
173-
// User-defined properties should override languageSet properties
174-
const mergedLanguage = { ...langSource[0], ...language };
175-
codegen.convert(
162+
const mergedLanguage = language.options
163+
? language
164+
: { ...langSource[0], ...language };
165+
const collection = new sdk.Collection({
166+
item: [{ name: "request", request: cleanedPostmanRequest }],
167+
});
168+
const [harRequest] = await postman2har(collection.toJSON());
169+
const snippet = new HTTPSnippet(harRequest).convert(
176170
mergedLanguage.language,
177171
mergedLanguage.variant,
178-
cleanedPostmanRequest,
179-
mergedLanguage.options,
180-
(error: any, snippet: string) => {
181-
if (error) {
182-
return;
183-
}
184-
setCodeText(snippet);
185-
}
172+
mergedLanguage.options
186173
);
187-
} else {
188-
setCodeText("");
174+
setCodeText(typeof snippet === "string" ? snippet : "");
189175
}
176+
generateSnippet();
190177
}, [
191178
accept,
192179
body,
@@ -203,19 +190,25 @@ function CodeSnippets({
203190
]);
204191
// no dependencies was intentionally set for this particular hook. it's safe as long as if conditions are set
205192
useEffect(function onSelectedVariantUpdate() {
206-
if (selectedVariant && selectedVariant !== language?.variant) {
207-
codegen.convert(
208-
language.language,
209-
selectedVariant,
210-
cleanedPostmanRequest,
211-
language.options,
212-
(error: any, snippet: string) => {
213-
if (error) {
214-
return;
215-
}
216-
setCodeText(snippet);
217-
}
218-
);
193+
if (selectedVariant && selectedVariant !== language?.variant && language) {
194+
(async () => {
195+
const langSource = mergedLangs.filter(
196+
(lang) => lang.language === language.language
197+
);
198+
const mergedLanguage = language.options
199+
? language
200+
: { ...langSource[0], ...language };
201+
const collection = new sdk.Collection({
202+
item: [{ name: "request", request: cleanedPostmanRequest }],
203+
});
204+
const [harRequest] = await postman2har(collection.toJSON());
205+
const snippet = new HTTPSnippet(harRequest).convert(
206+
mergedLanguage.language,
207+
selectedVariant,
208+
mergedLanguage.options
209+
);
210+
setCodeText(typeof snippet === "string" ? snippet : "");
211+
})();
219212
}
220213
});
221214

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/languages.ts

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
* ========================================================================== */
77

8+
import { availableTargets } from "httpsnippet-lite";
89
import find from "lodash/find";
910
import mergeWith from "lodash/mergeWith";
1011
import unionBy from "lodash/unionBy";
11-
import codegen from "postman-code-generators";
1212

13-
import { CodeSample, Language } from "./code-snippets-types";
13+
import {
14+
CodeSample,
15+
Language,
16+
CodeSampleLanguage,
17+
} from "./code-snippets-types";
1418

1519
export function mergeCodeSampleLanguage(
1620
languages: Language[],
@@ -73,23 +77,42 @@ export function getCodeSampleSourceFromLanguage(language: Language) {
7377

7478
export function generateLanguageSet() {
7579
const languageSet: Language[] = [];
76-
codegen.getLanguageList().forEach((language: any) => {
77-
const variants: any = [];
78-
language.variants.forEach((variant: any) => {
79-
variants.push(variant.key);
80-
});
80+
const languageMap: Record<string, CodeSampleLanguage> = {
81+
c: "C",
82+
csharp: "C#",
83+
go: "Go",
84+
java: "Java",
85+
javascript: "JavaScript",
86+
kotlin: "Kotlin",
87+
node: "JavaScript",
88+
objc: "Objective-C",
89+
ocaml: "OCaml",
90+
php: "PHP",
91+
powershell: "PowerShell",
92+
python: "Python",
93+
r: "R",
94+
ruby: "Ruby",
95+
rust: "Rust",
96+
shell: "Shell",
97+
swift: "Swift",
98+
};
99+
const highlightMap: Record<string, string> = {
100+
node: "javascript",
101+
};
102+
availableTargets().forEach((target) => {
103+
const codeSample = languageMap[target.key];
104+
if (!codeSample) {
105+
return;
106+
}
107+
const variants = target.clients.map((client: any) => client.key);
81108
languageSet.push({
82-
highlight: language.syntax_mode,
83-
language: language.key,
84-
codeSampleLanguage: language.label,
85-
logoClass: language.key,
86-
options: {
87-
longFormat: false,
88-
followRedirect: true,
89-
trimRequestBody: true,
90-
},
109+
highlight: highlightMap[target.key] ?? target.key,
110+
language: target.key,
111+
codeSampleLanguage: codeSample,
112+
logoClass: target.key,
113+
options: {},
91114
variant: variants[0],
92-
variants: variants,
115+
variants,
93116
});
94117
});
95118
return languageSet;

0 commit comments

Comments
 (0)