|
1 | 1 | import OriginalSwaggerParser from '@apidevtools/swagger-parser'
|
2 |
| -import { describe, expect, it } from 'vitest' |
| 2 | +import path from 'node:path' |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
3 | 4 |
|
4 | 5 | import { dereference } from '../src/utils/dereference'
|
| 6 | +import { load } from '../src/utils/load' |
| 7 | +import { fetchUrls } from '../src/utils/load/plugins/fetchUrls' |
| 8 | +import { readFiles } from '../src/utils/load/plugins/readFiles' |
5 | 9 | import { validate } from '../src/utils/validate'
|
6 | 10 |
|
7 | 11 | const myAPI = JSON.stringify({
|
@@ -30,19 +34,33 @@ const myAPI = JSON.stringify({
|
30 | 34 |
|
31 | 35 | class SwaggerParser {
|
32 | 36 | static async validate(api: string, callback: (err: any, api: any) => void) {
|
33 |
| - validate(api, { |
34 |
| - throwOnError: true, |
35 |
| - }) |
36 |
| - .then((result) => { |
37 |
| - callback(null, result.schema) |
| 37 | + try { |
| 38 | + const { filesystem } = await load(api, { |
| 39 | + plugins: [fetchUrls(), readFiles()], |
| 40 | + throwOnError: true, |
38 | 41 | })
|
39 |
| - .catch((error) => { |
40 |
| - callback(error, null) |
| 42 | + |
| 43 | + validate(filesystem, { |
| 44 | + throwOnError: true, |
41 | 45 | })
|
| 46 | + .then((result) => { |
| 47 | + callback(null, result.schema) |
| 48 | + }) |
| 49 | + .catch((error) => { |
| 50 | + callback(error, null) |
| 51 | + }) |
| 52 | + } catch (error) { |
| 53 | + callback(error, null) |
| 54 | + } |
42 | 55 | }
|
43 | 56 |
|
44 | 57 | static async dereference(api: string) {
|
45 |
| - return dereference(api).then((result) => result.schema) |
| 58 | + const { filesystem } = await load(api, { |
| 59 | + plugins: [fetchUrls(), readFiles()], |
| 60 | + throwOnError: true, |
| 61 | + }) |
| 62 | + |
| 63 | + return dereference(filesystem).then((result) => result.schema) |
46 | 64 | }
|
47 | 65 | }
|
48 | 66 |
|
@@ -85,4 +103,38 @@ describe('dereference', () => {
|
85 | 103 | // so you can easily access any part of the API using simple dot notation
|
86 | 104 | expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({})
|
87 | 105 | })
|
| 106 | + |
| 107 | + it('dereferences URLs', async () => { |
| 108 | + global.fetch = async (url: string) => |
| 109 | + ({ |
| 110 | + text: async () => { |
| 111 | + if (url === 'http://example.com/specification/openapi.yaml') { |
| 112 | + return myAPI |
| 113 | + } |
| 114 | + |
| 115 | + throw new Error('Not found') |
| 116 | + }, |
| 117 | + }) as Response |
| 118 | + |
| 119 | + let api = await SwaggerParser.dereference( |
| 120 | + 'http://example.com/specification/openapi.yaml', |
| 121 | + ) |
| 122 | + |
| 123 | + // The `api` object is a normal JavaScript object, |
| 124 | + // so you can easily access any part of the API using simple dot notation |
| 125 | + expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) |
| 126 | + }) |
| 127 | + |
| 128 | + it('dereferences files', async () => { |
| 129 | + const EXAMPLE_FILE = path.join( |
| 130 | + new URL(import.meta.url).pathname, |
| 131 | + '../../tests/migration-layer.json', |
| 132 | + ) |
| 133 | + |
| 134 | + let api = await SwaggerParser.dereference(EXAMPLE_FILE) |
| 135 | + |
| 136 | + // The `api` object is a normal JavaScript object, |
| 137 | + // so you can easily access any part of the API using simple dot notation |
| 138 | + expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) |
| 139 | + }) |
88 | 140 | })
|
0 commit comments