Skip to content

Commit c415f0f

Browse files
authored
Initial plugin refactor (#86)
* update openapi types * start refactoring parser
1 parent 56e6b80 commit c415f0f

File tree

10 files changed

+1217
-639
lines changed

10 files changed

+1217
-639
lines changed

jest.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* ============================================================================
2+
* Copyright (c) Cloud Annotations
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+
module.exports = {
9+
preset: "ts-jest",
10+
testEnvironment: "node",
11+
roots: [
12+
"<rootDir>/packages/docusaurus-plugin-openapi/src",
13+
"<rootDir>/packages/docusaurus-preset-openapi/src",
14+
"<rootDir>/packages/docusaurus-theme-openapi/src",
15+
],
16+
};

packages/docusaurus-plugin-openapi/src/markdown/createParamsTable.ts

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

8-
import { ApiItem, ExampleObject, ParameterObject } from "../types";
8+
import { ApiItem } from "../types";
99
import { createDescription } from "./createDescription";
1010
import { createFullWidthTable } from "./createFullWidthTable";
1111
import { getSchemaName } from "./schema";
@@ -20,9 +20,7 @@ export function createParamsTable({ parameters, type }: Props) {
2020
if (parameters === undefined) {
2121
return undefined;
2222
}
23-
const params = parameters.filter(
24-
(param: any) => param?.in === type
25-
) as ParameterObject[];
23+
const params = parameters.filter((param) => param?.in === type);
2624
if (params.length === 0) {
2725
return undefined;
2826
}
@@ -79,9 +77,7 @@ export function createParamsTable({ parameters, type }: Props) {
7977
style: { marginTop: "var(--ifm-table-cell-padding)" },
8078
children: Object.entries(examples).map(([k, v]) =>
8179
create("div", {
82-
children: `Example (${k}): ${
83-
(v as ExampleObject).value
84-
}`,
80+
children: `Example (${k}): ${v.value}`,
8581
})
8682
),
8783
})

packages/docusaurus-plugin-openapi/src/markdown/createSchemaTable.ts

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

8-
import { Map, MediaTypeObject, SchemaObject } from "../types";
8+
import { MediaTypeObject, SchemaObject } from "../openapi/types";
99
import { createDescription } from "./createDescription";
1010
import { createFullWidthTable } from "./createFullWidthTable";
1111
import { getSchemaName } from "./schema";
@@ -64,11 +64,13 @@ function createRows({ schema }: RowsProps): string | undefined {
6464
marginBottom: "0px",
6565
},
6666
children: create("tbody", {
67-
children: Object.keys(schema.properties).map((key) =>
67+
children: Object.entries(schema.properties).map(([key, val]) =>
6868
createRow({
6969
name: key,
70-
schema: schema.properties[key],
71-
required: schema.required?.includes(key),
70+
schema: val,
71+
required: Array.isArray(schema.required)
72+
? schema.required.includes(key)
73+
: false,
7274
})
7375
),
7476
}),
@@ -91,11 +93,13 @@ interface RowsRootProps {
9193
function createRowsRoot({ schema }: RowsRootProps) {
9294
// object
9395
if (schema.properties !== undefined) {
94-
return Object.keys(schema.properties).map((key) =>
96+
return Object.entries(schema.properties).map(([key, val]) =>
9597
createRow({
9698
name: key,
97-
schema: schema.properties[key],
98-
required: schema.required?.includes(key),
99+
schema: val,
100+
required: Array.isArray(schema.required)
101+
? schema.required.includes(key)
102+
: false,
99103
})
100104
);
101105
}
@@ -128,7 +132,9 @@ interface Props {
128132
style?: any;
129133
title: string;
130134
body: {
131-
content: Map<MediaTypeObject>;
135+
content: {
136+
[key: string]: MediaTypeObject;
137+
};
132138
description?: string;
133139
required?: boolean;
134140
};
@@ -144,7 +150,11 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
144150

145151
const randomFirstKey = Object.keys(body.content)[0];
146152

147-
const firstBody = body.content[randomFirstKey].schema as SchemaObject;
153+
const firstBody = body.content[randomFirstKey].schema;
154+
155+
if (firstBody === undefined) {
156+
return undefined;
157+
}
148158

149159
// we don't show the table if there is no properties to show
150160
if (Object.keys(firstBody.properties ?? {}).length === 0) {

0 commit comments

Comments
 (0)