docusaurus-theme-openapi-docs 2.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/lib/markdown/utils.d.ts +2 -2
  2. package/lib/markdown/utils.js +4 -4
  3. package/lib/markdown/utils.test.d.ts +1 -0
  4. package/lib/markdown/utils.test.js +43 -0
  5. package/lib/theme/ApiExplorer/Accept/index.d.ts +2 -2
  6. package/lib/theme/ApiExplorer/Authorization/index.d.ts +2 -2
  7. package/lib/theme/ApiExplorer/Authorization/slice.d.ts +1 -1
  8. package/lib/theme/ApiExplorer/Body/index.d.ts +2 -2
  9. package/lib/theme/ApiExplorer/Body/slice.d.ts +3 -3
  10. package/lib/theme/ApiExplorer/CodeSnippets/code-snippets-types.d.ts +1 -1
  11. package/lib/theme/ApiExplorer/CodeSnippets/index.d.ts +2 -2
  12. package/lib/theme/ApiExplorer/CodeSnippets/index.js +10 -11
  13. package/lib/theme/ApiExplorer/CodeSnippets/languages.d.ts +1 -0
  14. package/lib/theme/ApiExplorer/CodeSnippets/languages.js +17 -1
  15. package/lib/theme/ApiExplorer/CodeTabs/index.js +8 -6
  16. package/lib/theme/ApiExplorer/ContentType/index.d.ts +2 -2
  17. package/lib/theme/ApiExplorer/Export/index.d.ts +2 -2
  18. package/lib/theme/ApiExplorer/FloatingButton/index.d.ts +1 -1
  19. package/lib/theme/ApiExplorer/FormFileUpload/index.d.ts +2 -2
  20. package/lib/theme/ApiExplorer/FormItem/index.d.ts +1 -1
  21. package/lib/theme/ApiExplorer/FormMultiSelect/index.d.ts +1 -1
  22. package/lib/theme/ApiExplorer/FormSelect/index.d.ts +1 -1
  23. package/lib/theme/ApiExplorer/FormTextInput/index.d.ts +1 -1
  24. package/lib/theme/ApiExplorer/MethodEndpoint/index.d.ts +2 -2
  25. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamArrayFormItem.d.ts +2 -2
  26. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamBooleanFormItem.d.ts +2 -2
  27. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamMultiSelectFormItem.d.ts +2 -2
  28. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamSelectFormItem.d.ts +2 -2
  29. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamTextFormItem.d.ts +2 -2
  30. package/lib/theme/ApiExplorer/ParamOptions/index.d.ts +2 -2
  31. package/lib/theme/ApiExplorer/ParamOptions/slice.d.ts +1 -1
  32. package/lib/theme/ApiExplorer/Request/index.d.ts +2 -2
  33. package/lib/theme/ApiExplorer/Response/index.d.ts +2 -2
  34. package/lib/theme/ApiExplorer/SecuritySchemes/index.d.ts +2 -2
  35. package/lib/theme/ApiExplorer/Server/index.d.ts +2 -2
  36. package/lib/theme/ApiExplorer/buildPostmanRequest.d.ts +1 -1
  37. package/lib/theme/ApiExplorer/index.d.ts +2 -2
  38. package/lib/theme/ApiExplorer/storage-utils.d.ts +1 -1
  39. package/lib/theme/ApiItem/index.js +24 -0
  40. package/lib/theme/ApiItem/store.d.ts +6 -6
  41. package/lib/theme/SchemaItem/index.js +5 -8
  42. package/lib/types.d.ts +4 -4
  43. package/package.json +3 -3
  44. package/src/markdown/utils.test.ts +48 -0
  45. package/src/markdown/utils.ts +4 -4
  46. package/src/theme/ApiExplorer/CodeSnippets/index.tsx +13 -13
  47. package/src/theme/ApiExplorer/CodeSnippets/languages.ts +16 -0
  48. package/src/theme/ApiExplorer/CodeTabs/index.js +8 -6
  49. package/src/theme/ApiItem/index.tsx +20 -0
  50. package/src/theme/SchemaItem/index.js +5 -8
@@ -1,5 +1,5 @@
1
- export declare type Children = string | undefined | (string | undefined)[];
2
- export declare type Props = Record<string, any> & {
1
+ export type Children = string | undefined | (string | undefined)[];
2
+ export type Props = Record<string, any> & {
3
3
  children?: Children;
4
4
  };
5
5
  export declare function create(tag: string, props: Props): string;
@@ -17,11 +17,11 @@ function create(tag, props) {
17
17
  }
18
18
  exports.create = create;
19
19
  function guard(value, cb) {
20
- if (!!value) {
21
- const children = cb(value);
22
- return render(children);
20
+ if (value === undefined || value === "") {
21
+ return "";
23
22
  }
24
- return "";
23
+ const children = cb(value);
24
+ return render(children);
25
25
  }
26
26
  exports.guard = guard;
27
27
  function render(children) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ /* ============================================================================
3
+ * Copyright (c) Palo Alto Networks
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ * ========================================================================== */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const utils_1 = require("./utils");
10
+ describe("guard", () => {
11
+ it("should guard empty strings", () => {
12
+ const actual = (0, utils_1.guard)("", (_) => {
13
+ throw new Error("Should not be called");
14
+ });
15
+ expect(actual).toBe("");
16
+ });
17
+ it("should guard undefined", () => {
18
+ const actual = (0, utils_1.guard)(undefined, (value) => {
19
+ throw new Error("Should not be called");
20
+ });
21
+ expect(actual).toBe("");
22
+ });
23
+ it("should not guard strings", () => {
24
+ const actual = (0, utils_1.guard)("hello", (value) => value);
25
+ expect(actual).toBe("hello");
26
+ });
27
+ it("should not guard numbers", () => {
28
+ const actual = (0, utils_1.guard)(1, (value) => `${value}`);
29
+ expect(actual).toBe("1");
30
+ });
31
+ it("should not guard numbers equals to 0", () => {
32
+ const actual = (0, utils_1.guard)(0, (value) => `${value}`);
33
+ expect(actual).toBe("0");
34
+ });
35
+ it("should not guard false booleans", () => {
36
+ const actual = (0, utils_1.guard)(false, (value) => `${value}`);
37
+ expect(actual).toBe("false");
38
+ });
39
+ it("should not guard true booleans", () => {
40
+ const actual = (0, utils_1.guard)(true, (value) => `${value}`);
41
+ expect(actual).toBe("true");
42
+ });
43
+ });
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- declare function Accept(): JSX.Element | null;
1
+ import React from "react";
2
+ declare function Accept(): React.JSX.Element | null;
3
3
  export default Accept;
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- declare function Authorization(): JSX.Element | null;
1
+ import React from "react";
2
+ declare function Authorization(): React.JSX.Element | null;
3
3
  export default Authorization;
@@ -8,7 +8,7 @@ export declare function createAuth({ security, securitySchemes, options, }: {
8
8
  };
9
9
  options?: ThemeConfig["api"];
10
10
  }): AuthState;
11
- export declare type Scheme = {
11
+ export type Scheme = {
12
12
  key: string;
13
13
  scopes: string[];
14
14
  } & SecuritySchemeObject;
@@ -1,4 +1,4 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { RequestBodyObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
3
3
  export interface Props {
4
4
  jsonRequestBodyExample: string;
@@ -6,5 +6,5 @@ export interface Props {
6
6
  methods?: any;
7
7
  required?: boolean;
8
8
  }
9
- declare function BodyWrap({ requestBodyMetadata, jsonRequestBodyExample, methods, required, }: Props): JSX.Element | null;
9
+ declare function BodyWrap({ requestBodyMetadata, jsonRequestBodyExample, methods, required, }: Props): React.JSX.Element | null;
10
10
  export default BodyWrap;
@@ -10,7 +10,7 @@ export interface StringContent {
10
10
  type: "string";
11
11
  value?: string;
12
12
  }
13
- export declare type Content = FileContent | StringContent | undefined;
13
+ export type Content = FileContent | StringContent | undefined;
14
14
  export interface FormBody {
15
15
  type: "form";
16
16
  content: {
@@ -24,8 +24,8 @@ export interface RawBody {
24
24
  export interface EmptyBody {
25
25
  type: "empty";
26
26
  }
27
- export declare type Body = EmptyBody | FormBody | RawBody;
28
- export declare type State = Body;
27
+ export type Body = EmptyBody | FormBody | RawBody;
28
+ export type State = Body;
29
29
  export declare const slice: import("@reduxjs/toolkit").Slice<EmptyBody | FormBody | RawBody, {
30
30
  clearRawBody: (_state: import("immer/dist/internal").WritableDraft<EmptyBody> | import("immer/dist/internal").WritableDraft<FormBody> | import("immer/dist/internal").WritableDraft<RawBody>) => {
31
31
  type: "empty";
@@ -1,4 +1,4 @@
1
- export declare type CodeSampleLanguage = "C" | "C#" | "C++" | "CoffeeScript" | "CSS" | "Dart" | "DM" | "Elixir" | "Go" | "Groovy" | "HTML" | "Java" | "JavaScript" | "Kotlin" | "Objective-C" | "Perl" | "PHP" | "PowerShell" | "Python" | "Ruby" | "Rust" | "Scala" | "Shell" | "Swift" | "TypeScript";
1
+ export type CodeSampleLanguage = "C" | "C#" | "C++" | "CoffeeScript" | "CSS" | "Dart" | "DM" | "Elixir" | "Go" | "Groovy" | "HTML" | "Java" | "JavaScript" | "Kotlin" | "Objective-C" | "Perl" | "PHP" | "PowerShell" | "Python" | "Ruby" | "Rust" | "Scala" | "Shell" | "Swift" | "TypeScript";
2
2
  export interface Language {
3
3
  highlight: string;
4
4
  language: string;
@@ -1,4 +1,4 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import sdk from "@paloaltonetworks/postman-collection";
3
3
  import { CodeSample, Language } from "./code-snippets-types";
4
4
  export declare const languageSet: Language[];
@@ -6,5 +6,5 @@ export interface Props {
6
6
  postman: sdk.Request;
7
7
  codeSamples: CodeSample[];
8
8
  }
9
- declare function CodeSnippets({ postman, codeSamples }: Props): JSX.Element | null;
9
+ declare function CodeSnippets({ postman, codeSamples }: Props): React.JSX.Element | null;
10
10
  export default CodeSnippets;
@@ -246,19 +246,14 @@ function CodeSnippets({ postman, codeSamples }) {
246
246
  return defaultLang[0] ?? mergedLangs[0];
247
247
  });
248
248
  const [codeText, setCodeText] = (0, react_1.useState)("");
249
- const [codeSampleCodeText, setCodeSampleCodeText] = (0, react_1.useState)("");
249
+ const [codeSampleCodeText, setCodeSampleCodeText] = (0, react_1.useState)(
250
+ () => (0, languages_1.getCodeSampleSourceFromLanguage)(language)
251
+ );
250
252
  (0, react_1.useEffect)(() => {
251
- // initial active language is custom code sample
252
- if (
253
- language &&
254
- language.sample &&
255
- language.samples &&
256
- language.samplesSources
257
- ) {
258
- const sampleIndex = language.samples.findIndex(
259
- (smp) => smp === language.sample
253
+ if (language && !!language.sample) {
254
+ setCodeSampleCodeText(
255
+ (0, languages_1.getCodeSampleSourceFromLanguage)(language)
260
256
  );
261
- setCodeSampleCodeText(language.samplesSources[sampleIndex]);
262
257
  }
263
258
  if (language && !!language.options) {
264
259
  const postmanRequest = (0, buildPostmanRequest_1.default)(postman, {
@@ -388,7 +383,9 @@ function CodeSnippets({ postman, codeSamples }) {
388
383
  action: {
389
384
  setLanguage: setLanguage,
390
385
  setSelectedVariant: setSelectedVariant,
386
+ setSelectedSample: setSelectedSample,
391
387
  },
388
+ languageSet: mergedLangs,
392
389
  lazy: true,
393
390
  },
394
391
  mergedLangs.map((lang) => {
@@ -414,6 +411,7 @@ function CodeSnippets({ postman, codeSamples }) {
414
411
  includeSample: true,
415
412
  currentLanguage: lang.language,
416
413
  defaultValue: selectedSample,
414
+ languageSet: mergedLangs,
417
415
  lazy: true,
418
416
  },
419
417
  lang.samples.map((sample, index) => {
@@ -452,6 +450,7 @@ function CodeSnippets({ postman, codeSamples }) {
452
450
  includeVariant: true,
453
451
  currentLanguage: lang.language,
454
452
  defaultValue: selectedVariant,
453
+ languageSet: mergedLangs,
455
454
  lazy: true,
456
455
  },
457
456
  lang.variants.map((variant, index) => {
@@ -1,2 +1,3 @@
1
1
  import { CodeSample, Language } from "./code-snippets-types";
2
2
  export declare function mergeCodeSampleLanguage(languages: Language[], codeSamples: CodeSample[]): Language[];
3
+ export declare function getCodeSampleSourceFromLanguage(language: Language): string;
@@ -6,7 +6,8 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  * ========================================================================== */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.mergeCodeSampleLanguage = void 0;
9
+ exports.getCodeSampleSourceFromLanguage = exports.mergeCodeSampleLanguage =
10
+ void 0;
10
11
  function mergeCodeSampleLanguage(languages, codeSamples) {
11
12
  return languages.map((language) => {
12
13
  const languageCodeSamples = codeSamples.filter(
@@ -30,3 +31,18 @@ function mergeCodeSampleLanguage(languages, codeSamples) {
30
31
  });
31
32
  }
32
33
  exports.mergeCodeSampleLanguage = mergeCodeSampleLanguage;
34
+ function getCodeSampleSourceFromLanguage(language) {
35
+ if (
36
+ language &&
37
+ language.sample &&
38
+ language.samples &&
39
+ language.samplesSources
40
+ ) {
41
+ const sampleIndex = language.samples.findIndex(
42
+ (smp) => smp === language.sample
43
+ );
44
+ return language.samplesSources[sampleIndex];
45
+ }
46
+ return "";
47
+ }
48
+ exports.getCodeSampleSourceFromLanguage = getCodeSampleSourceFromLanguage;
@@ -12,12 +12,12 @@ import {
12
12
  useTabs,
13
13
  } from "@docusaurus/theme-common/internal";
14
14
  import useIsBrowser from "@docusaurus/useIsBrowser";
15
- import { languageSet } from "@theme/ApiExplorer/CodeSnippets";
16
15
  import clsx from "clsx";
17
16
 
18
17
  function TabList({
19
18
  action,
20
19
  currentLanguage,
20
+ languageSet,
21
21
  includeVariant,
22
22
  includeSample,
23
23
  className,
@@ -48,16 +48,18 @@ function TabList({
48
48
  )[0];
49
49
  newLanguage.variant = newTabValue;
50
50
  action.setSelectedVariant(newTabValue.toLowerCase());
51
+ } else if (currentLanguage && includeSample) {
52
+ newLanguage = languageSet.filter(
53
+ (lang) => lang.language === currentLanguage
54
+ )[0];
55
+ newLanguage.sample = newTabValue;
56
+ action.setSelectedSample(newTabValue);
51
57
  } else {
52
58
  newLanguage = languageSet.filter(
53
59
  (lang) => lang.language === newTabValue
54
60
  )[0];
55
61
  action.setSelectedVariant(newLanguage.variant.toLowerCase());
56
- }
57
-
58
- if (currentLanguage && includeSample) {
59
- newLanguage.sample = newTabValue;
60
- action.setSelectedSample(newTabValue.toLowerCase());
62
+ action.setSelectedSample(newLanguage.sample);
61
63
  }
62
64
 
63
65
  action.setLanguage(newLanguage);
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- declare function ContentType(): JSX.Element | null;
1
+ import React from "react";
2
+ declare function ContentType(): React.JSX.Element | null;
3
3
  export default ContentType;
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- declare function Export({ url, proxy }: any): JSX.Element;
1
+ import React from "react";
2
+ declare function Export({ url, proxy }: any): React.JSX.Element;
3
3
  export default Export;
@@ -4,5 +4,5 @@ export interface Props {
4
4
  onClick?: React.MouseEventHandler<HTMLButtonElement>;
5
5
  children?: React.ReactNode;
6
6
  }
7
- declare function FloatingButton({ label, onClick, children }: Props): JSX.Element;
7
+ declare function FloatingButton({ label, onClick, children }: Props): React.JSX.Element;
8
8
  export default FloatingButton;
@@ -1,7 +1,7 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  export interface Props {
3
3
  placeholder: string;
4
4
  onChange?(file?: File): any;
5
5
  }
6
- declare function FormFileUpload({ placeholder, onChange }: Props): JSX.Element;
6
+ declare function FormFileUpload({ placeholder, onChange }: Props): React.JSX.Element;
7
7
  export default FormFileUpload;
@@ -6,5 +6,5 @@ export interface Props {
6
6
  children?: React.ReactNode;
7
7
  className?: string;
8
8
  }
9
- declare function FormItem({ label, type, required, children, className }: Props): JSX.Element;
9
+ declare function FormItem({ label, type, required, children, className }: Props): React.JSX.Element;
10
10
  export default FormItem;
@@ -5,5 +5,5 @@ export interface Props {
5
5
  onChange?: React.ChangeEventHandler<HTMLSelectElement>;
6
6
  showErrors?: boolean;
7
7
  }
8
- declare function FormMultiSelect({ value, options, onChange, showErrors }: Props): JSX.Element | null;
8
+ declare function FormMultiSelect({ value, options, onChange, showErrors }: Props): React.JSX.Element | null;
9
9
  export default FormMultiSelect;
@@ -4,5 +4,5 @@ export interface Props {
4
4
  options?: string[];
5
5
  onChange?: React.ChangeEventHandler<HTMLSelectElement>;
6
6
  }
7
- declare function FormSelect({ value, options, onChange }: Props): JSX.Element | null;
7
+ declare function FormSelect({ value, options, onChange }: Props): React.JSX.Element | null;
8
8
  export default FormSelect;
@@ -5,5 +5,5 @@ export interface Props {
5
5
  password?: boolean;
6
6
  onChange?: React.ChangeEventHandler<HTMLInputElement>;
7
7
  }
8
- declare function FormTextInput({ isRequired, value, placeholder, password, onChange, paramName, }: Props): JSX.Element;
8
+ declare function FormTextInput({ isRequired, value, placeholder, password, onChange, paramName, }: Props): React.JSX.Element;
9
9
  export default FormTextInput;
@@ -1,7 +1,7 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  export interface Props {
3
3
  method: string;
4
4
  path: string;
5
5
  }
6
- declare function MethodEndpoint({ method, path }: Props): JSX.Element;
6
+ declare function MethodEndpoint({ method, path }: Props): React.JSX.Element;
7
7
  export default MethodEndpoint;
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { Param } from "@theme/ApiExplorer/ParamOptions/slice";
3
3
  export interface ParamProps {
4
4
  param: Param;
5
5
  }
6
- export default function ParamArrayFormItem({ param }: ParamProps): JSX.Element;
6
+ export default function ParamArrayFormItem({ param }: ParamProps): React.JSX.Element;
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { Param } from "@theme/ApiExplorer/ParamOptions/slice";
3
3
  export interface ParamProps {
4
4
  param: Param;
5
5
  }
6
- export default function ParamBooleanFormItem({ param }: ParamProps): JSX.Element;
6
+ export default function ParamBooleanFormItem({ param }: ParamProps): React.JSX.Element;
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { Param } from "@theme/ApiExplorer/ParamOptions/slice";
3
3
  export interface ParamProps {
4
4
  param: Param;
5
5
  }
6
- export default function ParamMultiSelectFormItem({ param }: ParamProps): JSX.Element;
6
+ export default function ParamMultiSelectFormItem({ param }: ParamProps): React.JSX.Element;
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { Param } from "@theme/ApiExplorer/ParamOptions/slice";
3
3
  export interface ParamProps {
4
4
  param: Param;
5
5
  }
6
- export default function ParamSelectFormItem({ param }: ParamProps): JSX.Element;
6
+ export default function ParamSelectFormItem({ param }: ParamProps): React.JSX.Element;
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { Param } from "@theme/ApiExplorer/ParamOptions/slice";
3
3
  export interface ParamProps {
4
4
  param: Param;
5
5
  }
6
- export default function ParamTextFormItem({ param }: ParamProps): JSX.Element;
6
+ export default function ParamTextFormItem({ param }: ParamProps): React.JSX.Element;
@@ -1,7 +1,7 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { Param } from "./slice";
3
3
  export interface ParamProps {
4
4
  param: Param;
5
5
  }
6
- declare function ParamOptions(): JSX.Element;
6
+ declare function ParamOptions(): React.JSX.Element;
7
7
  export default ParamOptions;
@@ -1,6 +1,6 @@
1
1
  import { PayloadAction } from "@reduxjs/toolkit";
2
2
  import { ParameterObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
3
- export declare type Param = ParameterObject & {
3
+ export type Param = ParameterObject & {
4
4
  value?: string[] | string;
5
5
  };
6
6
  export interface State {
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { ApiItem } from "docusaurus-plugin-openapi-docs/src/types";
3
3
  declare function Request({ item }: {
4
4
  item: NonNullable<ApiItem>;
5
- }): JSX.Element | null;
5
+ }): React.JSX.Element | null;
6
6
  export default Request;
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { ApiItem } from "docusaurus-plugin-openapi-docs/src/types";
3
3
  declare function Response({ item }: {
4
4
  item: NonNullable<ApiItem>;
5
- }): JSX.Element | null;
5
+ }): React.JSX.Element | null;
6
6
  export default Response;
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- declare function SecuritySchemes(props: any): JSX.Element | null;
1
+ import React from "react";
2
+ declare function SecuritySchemes(props: any): React.JSX.Element | null;
3
3
  export default SecuritySchemes;
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- declare function Server(): JSX.Element | null;
1
+ import React from "react";
2
+ declare function Server(): React.JSX.Element | null;
3
3
  export default Server;
@@ -2,7 +2,7 @@ import sdk from "@paloaltonetworks/postman-collection";
2
2
  import { AuthState } from "@theme/ApiExplorer/Authorization/slice";
3
3
  import { Body } from "@theme/ApiExplorer/Body/slice";
4
4
  import { ParameterObject, ServerObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
5
- declare type Param = {
5
+ type Param = {
6
6
  value?: string | string[];
7
7
  } & ParameterObject;
8
8
  interface Options {
@@ -1,7 +1,7 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { ApiItem } from "docusaurus-plugin-openapi-docs/src/types";
3
3
  declare function ApiExplorer({ item, infoPath, }: {
4
4
  item: NonNullable<ApiItem>;
5
5
  infoPath: string;
6
- }): JSX.Element;
6
+ }): React.JSX.Element;
7
7
  export default ApiExplorer;
@@ -1,4 +1,4 @@
1
1
  export declare function hashArray(arr: string[]): string;
2
- declare type Persistance = false | "localStorage" | "sessionStorage" | undefined;
2
+ type Persistance = false | "localStorage" | "sessionStorage" | undefined;
3
3
  export declare function createStorage(persistance: Persistance): Storage;
4
4
  export {};
@@ -40,6 +40,7 @@ function ApiItem(props) {
40
40
  const { frontMatter } = MDXComponent;
41
41
  const { info_path: infoPath } = frontMatter;
42
42
  let { api } = frontMatter;
43
+ const { schema } = frontMatter;
43
44
  // decompress and parse
44
45
  if (api) {
45
46
  api = JSON.parse(
@@ -163,6 +164,29 @@ function ApiItem(props) {
163
164
  )
164
165
  )
165
166
  );
167
+ } else if (schema) {
168
+ return react_1.default.createElement(
169
+ DocProvider,
170
+ { content: props.content },
171
+ react_1.default.createElement(
172
+ theme_common_1.HtmlClassNameProvider,
173
+ { className: docHtmlClassName },
174
+ react_1.default.createElement(Metadata_1.default, null),
175
+ react_1.default.createElement(
176
+ Layout_1.default,
177
+ null,
178
+ react_1.default.createElement(
179
+ "div",
180
+ { className: (0, clsx_1.default)("row", "theme-api-markdown") },
181
+ react_1.default.createElement(
182
+ "div",
183
+ { className: "col col--12" },
184
+ react_1.default.createElement(MDXComponent, null)
185
+ )
186
+ )
187
+ )
188
+ )
189
+ );
166
190
  }
167
191
  // Non-API docs
168
192
  return react_1.default.createElement(
@@ -7,8 +7,8 @@ declare const rootReducer: import("redux").Reducer<import("redux").CombinedState
7
7
  params: unknown;
8
8
  auth: unknown;
9
9
  }>, import("redux").Action<any>>;
10
- export declare type RootState = ReturnType<typeof rootReducer>;
11
- export declare const createStoreWithState: (preloadedState: RootState, middlewares: any[]) => import("@reduxjs/toolkit/dist/configureStore").ToolkitStore<import("redux").CombinedState<{
10
+ export type RootState = ReturnType<typeof rootReducer>;
11
+ export declare const createStoreWithState: (preloadedState: RootState, middlewares: any[]) => import("@reduxjs/toolkit/dist/configureStore").ToolkitStore<import("redux").EmptyObject & {
12
12
  accept: unknown;
13
13
  contentType: unknown;
14
14
  response: unknown;
@@ -16,7 +16,7 @@ export declare const createStoreWithState: (preloadedState: RootState, middlewar
16
16
  body: unknown;
17
17
  params: unknown;
18
18
  auth: unknown;
19
- }>, import("redux").Action<any>, import("@reduxjs/toolkit").MiddlewareArray<[import("@reduxjs/toolkit").ThunkMiddleware<import("redux").CombinedState<{
19
+ }, import("redux").Action<any>, import("@reduxjs/toolkit").MiddlewareArray<[import("@reduxjs/toolkit").ThunkMiddleware<import("redux").CombinedState<{
20
20
  accept: unknown;
21
21
  contentType: unknown;
22
22
  response: unknown;
@@ -25,7 +25,7 @@ export declare const createStoreWithState: (preloadedState: RootState, middlewar
25
25
  params: unknown;
26
26
  auth: unknown;
27
27
  }>, import("redux").AnyAction, undefined>, ...any[]]>>;
28
- export declare const createStoreWithoutState: (preloadedState: {}, middlewares: any[]) => import("@reduxjs/toolkit/dist/configureStore").ToolkitStore<import("redux").CombinedState<{
28
+ export declare const createStoreWithoutState: (preloadedState: {}, middlewares: any[]) => import("@reduxjs/toolkit/dist/configureStore").ToolkitStore<import("redux").EmptyObject & {
29
29
  accept: unknown;
30
30
  contentType: unknown;
31
31
  response: unknown;
@@ -33,7 +33,7 @@ export declare const createStoreWithoutState: (preloadedState: {}, middlewares:
33
33
  body: unknown;
34
34
  params: unknown;
35
35
  auth: unknown;
36
- }>, import("redux").Action<any>, import("@reduxjs/toolkit").MiddlewareArray<[import("@reduxjs/toolkit").ThunkMiddleware<import("redux").CombinedState<{
36
+ }, import("redux").Action<any>, import("@reduxjs/toolkit").MiddlewareArray<[import("@reduxjs/toolkit").ThunkMiddleware<import("redux").CombinedState<{
37
37
  accept: unknown;
38
38
  contentType: unknown;
39
39
  response: unknown;
@@ -42,5 +42,5 @@ export declare const createStoreWithoutState: (preloadedState: {}, middlewares:
42
42
  params: unknown;
43
43
  auth: unknown;
44
44
  }>, import("redux").AnyAction, undefined>, ...any[]]>>;
45
- export declare type AppDispatch = ReturnType<typeof createStoreWithState>["dispatch"];
45
+ export type AppDispatch = ReturnType<typeof createStoreWithState>["dispatch"];
46
46
  export {};
@@ -79,14 +79,11 @@ function SchemaItem({
79
79
  </div>
80
80
  ));
81
81
 
82
- const renderDefaultValue = guard(
83
- typeof defaultValue === "boolean" ? defaultValue.toString() : defaultValue,
84
- (value) => (
85
- <div className="">
86
- <ReactMarkdown children={`**Default value:** \`${value}\``} />
87
- </div>
88
- )
89
- );
82
+ const renderDefaultValue = guard(defaultValue, (value) => (
83
+ <div className="">
84
+ <ReactMarkdown children={`**Default value:** \`${value}\``} />
85
+ </div>
86
+ ));
90
87
 
91
88
  const schemaContent = (
92
89
  <div>
package/lib/types.d.ts CHANGED
@@ -9,8 +9,8 @@ export interface ThemeConfig {
9
9
  interface Map<T> {
10
10
  [key: string]: T;
11
11
  }
12
- export declare type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
13
- export declare type SchemaObject = Omit<JSONSchema, "type" | "allOf" | "oneOf" | "anyOf" | "not" | "items" | "properties" | "additionalProperties"> & {
12
+ export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
13
+ export type SchemaObject = Omit<JSONSchema, "type" | "allOf" | "oneOf" | "anyOf" | "not" | "items" | "properties" | "additionalProperties"> & {
14
14
  type?: "string" | "number" | "integer" | "boolean" | "object" | "array";
15
15
  allOf?: SchemaObject[];
16
16
  oneOf?: SchemaObject[];
@@ -43,14 +43,14 @@ export interface ExternalDocumentationObject {
43
43
  description?: string;
44
44
  url: string;
45
45
  }
46
- export declare type FileChange = {
46
+ export type FileChange = {
47
47
  author?: string;
48
48
  /** Date can be any
49
49
  * [parsable date string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse).
50
50
  */
51
51
  date?: Date | string;
52
52
  };
53
- export declare type DocFrontMatter = {
53
+ export type DocFrontMatter = {
54
54
  /**
55
55
  * The last part of the doc ID (will be refactored in the future to be the
56
56
  * full ID instead)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-theme-openapi-docs",
3
3
  "description": "OpenAPI theme for Docusaurus.",
4
- "version": "2.0.4",
4
+ "version": "2.1.0",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -43,7 +43,7 @@
43
43
  "clsx": "^1.1.1",
44
44
  "copy-text-to-clipboard": "^3.1.0",
45
45
  "crypto-js": "^4.1.1",
46
- "docusaurus-plugin-openapi-docs": "^2.0.4",
46
+ "docusaurus-plugin-openapi-docs": "^2.1.0",
47
47
  "docusaurus-plugin-sass": "^0.2.3",
48
48
  "file-saver": "^2.0.5",
49
49
  "lodash": "^4.17.20",
@@ -68,5 +68,5 @@
68
68
  "engines": {
69
69
  "node": ">=14"
70
70
  },
71
- "gitHead": "71012fa9a8e21d7d83beaf6368b93381298a3e2e"
71
+ "gitHead": "9064dd22c8e18125f838fd88caba6364e5484b68"
72
72
  }
@@ -0,0 +1,48 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ import { guard } from "./utils";
9
+
10
+ describe("guard", () => {
11
+ it("should guard empty strings", () => {
12
+ const actual = guard("", (_) => {
13
+ throw new Error("Should not be called");
14
+ });
15
+ expect(actual).toBe("");
16
+ });
17
+
18
+ it("should guard undefined", () => {
19
+ const actual = guard(undefined, (value) => {
20
+ throw new Error("Should not be called");
21
+ });
22
+ expect(actual).toBe("");
23
+ });
24
+
25
+ it("should not guard strings", () => {
26
+ const actual = guard("hello", (value) => value);
27
+ expect(actual).toBe("hello");
28
+ });
29
+
30
+ it("should not guard numbers", () => {
31
+ const actual = guard(1, (value) => `${value}`);
32
+ expect(actual).toBe("1");
33
+ });
34
+
35
+ it("should not guard numbers equals to 0", () => {
36
+ const actual = guard(0, (value) => `${value}`);
37
+ expect(actual).toBe("0");
38
+ });
39
+
40
+ it("should not guard false booleans", () => {
41
+ const actual = guard(false, (value) => `${value}`);
42
+ expect(actual).toBe("false");
43
+ });
44
+ it("should not guard true booleans", () => {
45
+ const actual = guard(true, (value) => `${value}`);
46
+ expect(actual).toBe("true");
47
+ });
48
+ });
@@ -24,11 +24,11 @@ export function guard<T>(
24
24
  value: T | undefined | string,
25
25
  cb: (value: T) => Children
26
26
  ): string {
27
- if (!!value) {
28
- const children = cb(value as T);
29
- return render(children);
27
+ if (value === undefined || value === "") {
28
+ return "";
30
29
  }
31
- return "";
30
+ const children = cb(value as T);
31
+ return render(children);
32
32
  }
33
33
 
34
34
  export function render(children: Children): string {
@@ -17,7 +17,10 @@ import { useTypedSelector } from "@theme/ApiItem/hooks";
17
17
  import merge from "lodash/merge";
18
18
 
19
19
  import { CodeSample, Language } from "./code-snippets-types";
20
- import { mergeCodeSampleLanguage } from "./languages";
20
+ import {
21
+ getCodeSampleSourceFromLanguage,
22
+ mergeCodeSampleLanguage,
23
+ } from "./languages";
21
24
 
22
25
  export const languageSet: Language[] = [
23
26
  {
@@ -198,20 +201,13 @@ function CodeSnippets({ postman, codeSamples }: Props) {
198
201
  return defaultLang[0] ?? mergedLangs[0];
199
202
  });
200
203
  const [codeText, setCodeText] = useState<string>("");
201
- const [codeSampleCodeText, setCodeSampleCodeText] = useState<string>("");
204
+ const [codeSampleCodeText, setCodeSampleCodeText] = useState<
205
+ string | (() => string)
206
+ >(() => getCodeSampleSourceFromLanguage(language));
202
207
 
203
208
  useEffect(() => {
204
- // initial active language is custom code sample
205
- if (
206
- language &&
207
- language.sample &&
208
- language.samples &&
209
- language.samplesSources
210
- ) {
211
- const sampleIndex = language.samples.findIndex(
212
- (smp) => smp === language.sample
213
- );
214
- setCodeSampleCodeText(language.samplesSources[sampleIndex]);
209
+ if (language && !!language.sample) {
210
+ setCodeSampleCodeText(getCodeSampleSourceFromLanguage(language));
215
211
  }
216
212
 
217
213
  if (language && !!language.options) {
@@ -344,7 +340,9 @@ function CodeSnippets({ postman, codeSamples }: Props) {
344
340
  action={{
345
341
  setLanguage: setLanguage,
346
342
  setSelectedVariant: setSelectedVariant,
343
+ setSelectedSample: setSelectedSample,
347
344
  }}
345
+ languageSet={mergedLangs}
348
346
  lazy
349
347
  >
350
348
  {mergedLangs.map((lang) => {
@@ -367,6 +365,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
367
365
  includeSample={true}
368
366
  currentLanguage={lang.language}
369
367
  defaultValue={selectedSample}
368
+ languageSet={mergedLangs}
370
369
  lazy
371
370
  >
372
371
  {lang.samples.map((sample, index) => {
@@ -406,6 +405,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
406
405
  includeVariant={true}
407
406
  currentLanguage={lang.language}
408
407
  defaultValue={selectedVariant}
408
+ languageSet={mergedLangs}
409
409
  lazy
410
410
  >
411
411
  {lang.variants.map((variant, index) => {
@@ -35,3 +35,19 @@ export function mergeCodeSampleLanguage(
35
35
  return language;
36
36
  });
37
37
  }
38
+
39
+ export function getCodeSampleSourceFromLanguage(language: Language) {
40
+ if (
41
+ language &&
42
+ language.sample &&
43
+ language.samples &&
44
+ language.samplesSources
45
+ ) {
46
+ const sampleIndex = language.samples.findIndex(
47
+ (smp) => smp === language.sample
48
+ );
49
+ return language.samplesSources[sampleIndex];
50
+ }
51
+
52
+ return "";
53
+ }
@@ -12,12 +12,12 @@ import {
12
12
  useTabs,
13
13
  } from "@docusaurus/theme-common/internal";
14
14
  import useIsBrowser from "@docusaurus/useIsBrowser";
15
- import { languageSet } from "@theme/ApiExplorer/CodeSnippets";
16
15
  import clsx from "clsx";
17
16
 
18
17
  function TabList({
19
18
  action,
20
19
  currentLanguage,
20
+ languageSet,
21
21
  includeVariant,
22
22
  includeSample,
23
23
  className,
@@ -48,16 +48,18 @@ function TabList({
48
48
  )[0];
49
49
  newLanguage.variant = newTabValue;
50
50
  action.setSelectedVariant(newTabValue.toLowerCase());
51
+ } else if (currentLanguage && includeSample) {
52
+ newLanguage = languageSet.filter(
53
+ (lang) => lang.language === currentLanguage
54
+ )[0];
55
+ newLanguage.sample = newTabValue;
56
+ action.setSelectedSample(newTabValue);
51
57
  } else {
52
58
  newLanguage = languageSet.filter(
53
59
  (lang) => lang.language === newTabValue
54
60
  )[0];
55
61
  action.setSelectedVariant(newLanguage.variant.toLowerCase());
56
- }
57
-
58
- if (currentLanguage && includeSample) {
59
- newLanguage.sample = newTabValue;
60
- action.setSelectedSample(newTabValue.toLowerCase());
62
+ action.setSelectedSample(newLanguage.sample);
61
63
  }
62
64
 
63
65
  action.setLanguage(newLanguage);
@@ -44,12 +44,17 @@ interface ApiFrontMatter extends DocFrontMatter {
44
44
  readonly api?: ApiItemType;
45
45
  }
46
46
 
47
+ interface SchemaFrontMatter extends DocFrontMatter {
48
+ readonly schema?: boolean;
49
+ }
50
+
47
51
  export default function ApiItem(props: Props): JSX.Element {
48
52
  const docHtmlClassName = `docs-doc-id-${props.content.metadata.unversionedId}`;
49
53
  const MDXComponent = props.content;
50
54
  const { frontMatter } = MDXComponent;
51
55
  const { info_path: infoPath } = frontMatter as DocFrontMatter;
52
56
  let { api } = frontMatter as ApiFrontMatter;
57
+ const { schema } = frontMatter as SchemaFrontMatter;
53
58
  // decompress and parse
54
59
  if (api) {
55
60
  api = JSON.parse(
@@ -159,6 +164,21 @@ export default function ApiItem(props: Props): JSX.Element {
159
164
  </HtmlClassNameProvider>
160
165
  </DocProvider>
161
166
  );
167
+ } else if (schema) {
168
+ return (
169
+ <DocProvider content={props.content}>
170
+ <HtmlClassNameProvider className={docHtmlClassName}>
171
+ <DocItemMetadata />
172
+ <DocItemLayout>
173
+ <div className={clsx("row", "theme-api-markdown")}>
174
+ <div className="col col--12">
175
+ <MDXComponent />
176
+ </div>
177
+ </div>
178
+ </DocItemLayout>
179
+ </HtmlClassNameProvider>
180
+ </DocProvider>
181
+ );
162
182
  }
163
183
 
164
184
  // Non-API docs
@@ -79,14 +79,11 @@ function SchemaItem({
79
79
  </div>
80
80
  ));
81
81
 
82
- const renderDefaultValue = guard(
83
- typeof defaultValue === "boolean" ? defaultValue.toString() : defaultValue,
84
- (value) => (
85
- <div className="">
86
- <ReactMarkdown children={`**Default value:** \`${value}\``} />
87
- </div>
88
- )
89
- );
82
+ const renderDefaultValue = guard(defaultValue, (value) => (
83
+ <div className="">
84
+ <ReactMarkdown children={`**Default value:** \`${value}\``} />
85
+ </div>
86
+ ));
90
87
 
91
88
  const schemaContent = (
92
89
  <div>