Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 61252ab

Browse files
authored
fix: max call stack exceeded (#116)
* chore: skip if no plugin has been found * chore: use list of references, if it’s there already * chore: deal with strange function return * test: enable max call stack exceeded files * fix: build * refactor: define types for a few more utils, rename pipeline to openapi * refactor: streamline all returns * docs: update examples * docs(changeset): refactor!: most functions return an object now * docs(changeset): fix: max call stack exceeded error * fix: benchmarks * chore: clean up * fix: types * chore: add alias again, add json viewer component * chore: allow more alias nodes * fix: TS issues
1 parent c9dd499 commit 61252ab

29 files changed

+550
-235
lines changed

.changeset/sweet-mangos-lay.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@scalar/openapi-parser': minor
3+
---
4+
5+
refactor!: most functions return an object now

.changeset/young-coins-run.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@scalar/openapi-parser': patch
3+
---
4+
5+
fix: max call stack exceeded error

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ const file = `{
4444
"paths": {}
4545
}`
4646

47-
const result = await validate(file)
47+
const { valid, errors } = await validate(file)
4848

49-
console.log(result.valid)
49+
console.log(valid)
5050

51-
if (!result.valid) {
52-
console.log(result.errors)
51+
if (!valid) {
52+
console.log(errors)
5353
}
5454
```
5555

@@ -67,7 +67,7 @@ const specification = `{
6767
"paths": {}
6868
}`
6969

70-
const result = await dereference(specification)
70+
const { schema, errors } = await dereference(specification)
7171
```
7272

7373
### Modify an OpenAPI specification
@@ -84,7 +84,7 @@ const specification = `{
8484
"paths": {}
8585
}`
8686

87-
const result = filter(specification, (schema) => !schema?.['x-internal'])
87+
const { specification } = filter(specification, (schema) => !schema?.['x-internal'])
8888
```
8989

9090
### Upgrade your OpenAPI specification
@@ -96,7 +96,7 @@ There’s an `upgrade` command to upgrade all your OpenAPI specifications to the
9696
```ts
9797
import { upgrade } from '@scalar/openapi-parser'
9898

99-
const result = upgrade({
99+
const { specification } = upgrade({
100100
openapi: '3.0.0',
101101
info: {
102102
title: 'Hello World',
@@ -105,7 +105,7 @@ const result = upgrade({
105105
paths: {},
106106
})
107107

108-
console.log(result.openapi)
108+
console.log(specification.openapi)
109109
// Output: 3.1.0
110110
```
111111

@@ -141,7 +141,7 @@ import {
141141
} from '@scalar/openapi-parser'
142142

143143
// Load a file and all referenced files
144-
const filesystem = await load('./openapi.yaml', {
144+
const { filesystem } = await load('./openapi.yaml', {
145145
plugins: [readFilesPlugin(), fetchUrlsPlugin()],
146146
})
147147

demo/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@vitejs/plugin-vue": "^5.0.5",
1818
"typescript": "^5.2.2",
1919
"vite": "^5.2.12",
20+
"vue-json-viewer": "3",
2021
"vue-tsc": "^2.0.19"
2122
}
2223
}

demo/src/App.vue

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<script setup lang="ts">
2-
import {
3-
dereference,
4-
fetchUrlsPlugin,
5-
load,
6-
toJson, // validate,
7-
} from '@scalar/openapi-parser'
2+
import { dereference, fetchUrlsPlugin, load } from '@scalar/openapi-parser'
83
import { watchDebounced } from '@vueuse/core'
94
import { onMounted, ref, watch } from 'vue'
5+
// @ts-ignore
6+
import JsonViewer from 'vue-json-viewer'
107
118
const value = ref(
129
JSON.stringify(
@@ -35,7 +32,7 @@ const value = ref(
3532
),
3633
)
3734
38-
const result = ref('')
35+
const result = ref({})
3936
4037
onMounted(() => {
4138
const savedValue = window.localStorage.getItem('value')
@@ -52,16 +49,14 @@ watch(value, async (newValue) => {
5249
watchDebounced(
5350
value,
5451
async (newValue) => {
55-
const content = await load(newValue, {
52+
const { filesystem } = await load(newValue, {
5653
plugins: [fetchUrlsPlugin()],
5754
})
5855
59-
// console.log(await validate(content))
60-
61-
const { schema } = await dereference(content)
56+
const { schema } = await dereference(filesystem)
6257
6358
if (schema) {
64-
result.value = toJson(schema)
59+
result.value = schema
6560
}
6661
},
6762
{
@@ -84,7 +79,9 @@ watchDebounced(
8479
v-model="value" />
8580
</div>
8681
<div class="preview-right">
87-
<pre id="output">{{ result }}</pre>
82+
<json-viewer
83+
:value="result"
84+
:expand-depth="3"></json-viewer>
8885
</div>
8986
</div>
9087
</template>
@@ -110,11 +107,9 @@ watchDebounced(
110107
width: 50%;
111108
height: 100%;
112109
font-size: 1rem;
113-
padding: 20px;
114110
border-radius: 5px;
115-
background: #000;
116-
color: #fff;
117111
overflow: auto;
112+
border: 1px solid #ccc;
118113
}
119114
120115
textarea {
@@ -125,13 +120,18 @@ textarea {
125120
resize: none;
126121
padding: 0;
127122
margin: 0;
128-
font-size: 1rem;
123+
font-size: 0.9rem;
124+
line-height: 1.4;
129125
font-family: monospace;
130126
border: 1px solid #ccc;
131-
padding: 20px;
127+
padding: 15px 20px;
132128
border-radius: 5px;
133129
}
134130
131+
.jv-container .jv-code {
132+
padding: 20px;
133+
}
134+
135135
pre {
136136
margin: 0;
137137
}

demo/vite.config.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ export default defineConfig({
1111
// external: [...builtinModules, ...builtinModules.map((m) => `node:${m}`)],
1212
// },
1313
// },
14-
// resolve: {
15-
// alias: [
16-
// // Resolve the uncompiled source code for all @scalar packages
17-
// // It’s working with the alias, too. It’s just required to enable HMR.
18-
// // It also does not match components since we want the built version
19-
// {
20-
// // Resolve the uncompiled source code for @scalar/openapi-parser packages
21-
// find: '@scalar/openapi-parser',
22-
// replacement: path.resolve(
23-
// __dirname,
24-
// // '../packages/openapi-parser/dist/index.js',
25-
// '../packages/openapi-parser/src/index.ts',
26-
// ),
27-
// },
28-
// ],
29-
// },
14+
resolve: {
15+
alias: [
16+
// Resolve the uncompiled source code for all @scalar packages
17+
// It’s working with the alias, too. It’s just required to enable HMR.
18+
// It also does not match components since we want the built version
19+
{
20+
// Resolve the uncompiled source code for @scalar/openapi-parser packages
21+
find: '@scalar/openapi-parser',
22+
replacement: path.resolve(
23+
__dirname,
24+
// '../packages/openapi-parser/dist/index.js',
25+
'../packages/openapi-parser/src/index.ts',
26+
),
27+
},
28+
],
29+
},
3030
})

packages/openapi-parser/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ const file = `{
4747
"paths": {}
4848
}`
4949

50-
const result = await validate(file)
50+
const { valid, errors } = await validate(file)
5151

52-
console.log(result.valid)
52+
console.log(valid)
5353

54-
if (!result.valid) {
55-
console.log(result.errors)
54+
if (!valid) {
55+
console.log(errors)
5656
}
5757
```
5858

@@ -70,7 +70,7 @@ const file = `{
7070
"paths": {}
7171
}`
7272

73-
const result = await dereference(file)
73+
const { version } = await dereference(file)
7474

75-
console.log(result.version)
75+
console.log(version)
7676
```

0 commit comments

Comments
 (0)