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

Commit 72703c8

Browse files
authored
chore: improve types (#122)
* chore: remove some @ts-ignore * chore: remove more @ts-ignores * chore: remove some @ts-expect-error comments * chore: restore deleted code
1 parent c6944f2 commit 72703c8

File tree

8 files changed

+46
-59
lines changed

8 files changed

+46
-59
lines changed

demo/src/App.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { dereference, fetchUrlsPlugin, load } from '@scalar/openapi-parser'
33
import { watchDebounced } from '@vueuse/core'
44
import { onMounted, ref, watch } from 'vue'
5-
// @ts-ignore
5+
// @ts-expect-error Package doesn’t come with types
66
import JsonViewer from 'vue-json-viewer'
77
88
const value = ref(

packages/openapi-parser/src/utils/dereference.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ it('resolves a simple reference', async () => {
147147

148148
// Original
149149
expect(
150-
// @ts-ignore
151150
result.specification.paths['/test'].get.responses['200'].content[
152151
'application/json'
153152
].schema,
@@ -157,7 +156,6 @@ it('resolves a simple reference', async () => {
157156

158157
// Resolved references
159158
expect(
160-
// @ts-ignore
161159
result.schema.paths['/test'].get.responses['200'].content[
162160
'application/json'
163161
].schema,

packages/openapi-parser/src/utils/load/load.test.ts

+19-23
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,18 @@ describe('load', async () => {
137137
})
138138

139139
it('loads url', async () => {
140-
// @ts-expect-error only partially patched
141-
global.fetch = async () => ({
142-
text: async () =>
143-
stringify({
144-
openapi: '3.1.0',
145-
info: {
146-
title: 'Hello World',
147-
version: '1.0.0',
148-
},
149-
paths: {},
150-
}),
151-
})
140+
global.fetch = async () =>
141+
({
142+
text: async () =>
143+
stringify({
144+
openapi: '3.1.0',
145+
info: {
146+
title: 'Hello World',
147+
version: '1.0.0',
148+
},
149+
paths: {},
150+
}),
151+
}) as Response
152152

153153
const { filesystem } = await load('https://example.com/openapi.yaml', {
154154
plugins: [readFilesPlugin(), fetchUrlsPlugin()],
@@ -209,12 +209,10 @@ describe('load', async () => {
209209
})
210210

211211
it('limits the number of requests', async () => {
212-
// @ts-expect-error only partially patched
213-
global.fetch = async () => {
214-
return {
212+
global.fetch = async () =>
213+
({
215214
text: async () => 'FOOBAR',
216-
}
217-
}
215+
}) as unknown as Response
218216

219217
const { filesystem } = await load(
220218
{
@@ -252,7 +250,6 @@ describe('load', async () => {
252250
})
253251

254252
it('loads referenced urls', async () => {
255-
// @ts-expect-error only partially patched
256253
global.fetch = async (url: string) => {
257254
if (url === 'https://example.com/openapi.yaml') {
258255
return {
@@ -273,7 +270,7 @@ describe('load', async () => {
273270
},
274271
},
275272
}),
276-
}
273+
} as Response
277274
}
278275

279276
if (url === 'https://example.com/foobar.json') {
@@ -289,7 +286,7 @@ describe('load', async () => {
289286
},
290287
},
291288
}),
292-
}
289+
} as Response
293290
}
294291
}
295292

@@ -327,8 +324,7 @@ describe('load', async () => {
327324
})
328325

329326
it('loads string with url reference', async () => {
330-
// @ts-expect-error only partially patched
331-
global.fetch = async (url: string) => {
327+
global.fetch = async () => {
332328
return {
333329
text: async () =>
334330
JSON.stringify({
@@ -341,7 +337,7 @@ describe('load', async () => {
341337
},
342338
},
343339
}),
344-
}
340+
} as Response
345341
}
346342

347343
const { filesystem } = await load(

packages/openapi-parser/src/utils/load/plugins/fetchUrlsPlugin.test.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ describe('fetchUrlsPlugin', async () => {
2626
})
2727

2828
it('fetches the URL', async () => {
29-
// @ts-expect-error only partially patched
30-
global.fetch = async (url: string) => ({
31-
text: async () => {
32-
if (url === 'http://example.com/specification/openapi.yaml') {
33-
return 'OK'
34-
}
29+
global.fetch = async (url: string) =>
30+
({
31+
text: async () => {
32+
if (url === 'http://example.com/specification/openapi.yaml') {
33+
return 'OK'
34+
}
3535

36-
throw new Error('Not found')
37-
},
38-
})
36+
throw new Error('Not found')
37+
},
38+
}) as Response
3939

4040
expect(
4141
await fetchUrlsPlugin().get(
@@ -45,16 +45,16 @@ describe('fetchUrlsPlugin', async () => {
4545
})
4646

4747
it('rewrites the URL', async () => {
48-
// @ts-expect-error only partially patched
49-
global.fetch = async (url: string) => ({
50-
text: async () => {
51-
if (url === 'http://foobar.com/specification/openapi.yaml') {
52-
return 'OK'
53-
}
48+
global.fetch = async (url: string) =>
49+
({
50+
text: async () => {
51+
if (url === 'http://foobar.com/specification/openapi.yaml') {
52+
return 'OK'
53+
}
5454

55-
throw new Error('Not found')
56-
},
57-
})
55+
throw new Error('Not found')
56+
},
57+
}) as Response
5858

5959
expect(
6060
await fetchUrlsPlugin({

packages/openapi-parser/src/utils/resolveReferences.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ describe('resolveReferences', () => {
375375
const { schema } = resolveReferences(specification)
376376

377377
// Assertion
378-
// @ts-ignore
379378
expect(schema.swagger).toBe('2.0')
380379
expect(
381380
schema.paths['/foobar'].post.responses[200].schema.properties.dictionaries
@@ -519,7 +518,6 @@ describe('resolveReferences', () => {
519518
const { schema } = resolveReferences(filesystem)
520519

521520
expect(
522-
// @ts-ignore
523521
schema.paths['/foobar'].post.requestBody.content['application/json']
524522
.schema.example,
525523
).toBe('foobar')
@@ -572,7 +570,6 @@ describe('resolveReferences', () => {
572570

573571
const { schema } = resolveReferences(filesystem)
574572
expect(
575-
// @ts-ignore
576573
schema.paths['/foobar'].post.requestBody.content['application/json']
577574
.schema.example,
578575
).toBe('foobar')
@@ -634,7 +631,6 @@ describe('resolveReferences', () => {
634631

635632
const { schema } = resolveReferences(filesystem)
636633
expect(
637-
// @ts-ignore
638634
schema.paths['/foobar'].post.requestBody.content['application/json']
639635
.schema.example,
640636
).toBe('foobar')

packages/openapi-parser/src/utils/upgradeFromThreeToThreeOne.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,16 @@ export function upgradeFromThreeToThreeOne(specification: AnyObject) {
5959
// Multipart file uploads with a binary file
6060
specification = traverse(specification, (schema) => {
6161
if (schema.type === 'object' && schema.properties !== undefined) {
62-
for (const [_, value] of Object.entries(schema.properties)) {
62+
// Types
63+
const entries: [string, any][] = Object.entries(schema.properties)
64+
65+
for (const [_, value] of entries) {
6366
if (
64-
value !== undefined &&
65-
// @ts-ignore
67+
typeof value === 'object' &&
6668
value.type === 'string' &&
67-
// @ts-ignore
6869
value.format === 'binary'
6970
) {
70-
// @ts-ignore
7171
value.contentEncoding = 'application/octet-stream'
72-
// @ts-ignore
7372
delete value.format
7473
}
7574
}

packages/openapi-parser/tests/openapi3-examples/3.0/fail/schemaProperties.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ describe('schemaProperties', () => {
1111

1212
expect(errors).not.toBe(undefined)
1313
expect(errors).not.toStrictEqual([])
14-
// @ts-ignore
1514
expect(errors[0]?.message).toBe(
1615
'Can’t resolve external reference: ../resources/myobject.yml',
1716
)

packages/openapi-parser/tests/openapi3-examples/3.0/pass/cyclical.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ describe('cyclical', () => {
4646
)
4747
const category =
4848
result.schema.components.schemas.top.properties.cat.properties
49-
expect(
50-
// @ts-ignore
51-
category.subcategories.items.properties.subcategories.type,
52-
).toEqual('array')
49+
expect(category.subcategories.items.properties.subcategories.type).toEqual(
50+
'array',
51+
)
5352
})
5453

5554
it.todo('resolves circular dependencies in referenced files', async () => {

0 commit comments

Comments
 (0)