Skip to content

Commit dec0e4b

Browse files
erunionjonlucakanadgupta
authored
feat(parser): rewriting the library in typescript (#932)
## 🐳 Context Now that our fork of [@apidevtools/swagger-parser](https://npm.im/@apidevtools/swagger-parser) has fully deviated and was moved here from [our other repository](https://github.com/readmeio/openapi-parser) I have built off of JonLuca's[^1] starting work over in APIDevTools/swagger-parser#253 and rewritten the library in TS. This is all going to be the basis for a slew of forthcoming changes in order to support error levels. Because this is a full rewrite, and a lot of things have changed, this will incur a major version bump. ## 🧰 Changes * [x] Rewrote our OpenAPI parser in TS. * [x] Added in dual support for CJS and ESM. * [x] Resolved a number of test quirks and invalid false positive assertions. [^1]: Because I adapted heavily from his draft PR of this rewrite over in the swagger-parser repository I've credited him as a co-author of this work. --------- Co-authored-by: JonLuca De Caro <[email protected]> Co-authored-by: Kanad Gupta <[email protected]>
1 parent f24ed0f commit dec0e4b

File tree

82 files changed

+3083
-3184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+3083
-3184
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": ["@readme/eslint-config", "@readme/eslint-config/typescript", "@readme/eslint-config/esm"],
3-
"root": true
3+
"root": true,
44
}

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages/parser/test/specs/large-file-memory-leak/cloudflare-stringified.json

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll": "explicit"
5+
},
6+
"editor.formatOnSave": true
7+
}

package-lock.json

Lines changed: 1661 additions & 1143 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"./packages/*"
2828
],
2929
"devDependencies": {
30-
"@readme/eslint-config": "^14.0.0",
30+
"@readme/eslint-config": "^14.2.0",
3131
"@vitest/coverage-v8": "^3.0.4",
3232
"alex": "^11.0.1",
3333
"eslint": "^8.57.0",

packages/oas-normalize/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
"eslint": "^8.57.0",
7878
"nock": "^14.0.0",
7979
"tsup": "^8.0.2",
80-
"typescript": "^5.1.6"
80+
"typescript": "^5.1.6",
81+
"vitest": "^3.0.5"
8182
},
8283
"prettier": "@readme/eslint-config/prettier"
8384
}

packages/oas-normalize/src/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Options } from './lib/types.js';
2+
import type { ParserOptions } from '@readme/openapi-parser';
23
import type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';
34

45
import fs from 'node:fs';
56

6-
import openapiParser from '@readme/openapi-parser';
7+
import { OpenAPIParser } from '@readme/openapi-parser';
78
import postmanToOpenAPI from '@readme/postman-to-openapi';
89
import converter from 'swagger2openapi';
910

@@ -108,9 +109,8 @@ export default class OASNormalize {
108109

109110
return schema;
110111
})
111-
.then(schema => openapiParser.bundle(schema))
112+
.then(schema => new OpenAPIParser().bundle(schema))
112113
.then(bundle => {
113-
// @ts-expect-error The typings on the parser are messed up rigth now while a rewrite is in progress.
114114
this.cache.bundle = bundle;
115115
return bundle;
116116
});
@@ -134,9 +134,8 @@ export default class OASNormalize {
134134

135135
return schema;
136136
})
137-
.then(schema => openapiParser.dereference(schema))
137+
.then(schema => new OpenAPIParser().dereference(schema))
138138
.then(dereferenced => {
139-
// @ts-expect-error The typings on the parser are messed up rigth now while a rewrite is in progress.
140139
this.cache.deref = dereferenced;
141140
return dereferenced;
142141
});
@@ -181,7 +180,7 @@ export default class OASNormalize {
181180
*/
182181
async validate(
183182
opts: {
184-
parser?: openapiParser.Options;
183+
parser?: ParserOptions;
185184
} = {},
186185
): Promise<true> {
187186
const parserOptions = opts.parser || {};
@@ -208,7 +207,7 @@ export default class OASNormalize {
208207
}
209208

210209
/**
211-
* `openapiParser.validate()` dereferences schemas at the same time as validation, mutating
210+
* `OpenAPIParser.validate()` dereferences schemas at the same time as validation, mutating
212211
* the supplied parameter in the process, and does not give us an option to disable this.
213212
* As we already have a dereferencing method on this library, and this method just needs to
214213
* tell us if the API definition is valid or not, we need to clone the schema before
@@ -217,8 +216,7 @@ export default class OASNormalize {
217216
// eslint-disable-next-line try-catch-failsafe/json-parse
218217
const clonedSchema = JSON.parse(JSON.stringify(schema));
219218

220-
// @ts-expect-error The typings on the parser are messed up rigth now while a rewrite is in progress.
221-
return openapiParser.validate(clonedSchema, parserOptions).then(() => {
219+
return new OpenAPIParser().validate(clonedSchema, parserOptions).then(() => {
222220
// The API definition, whatever its format or specification, is valid.
223221
return true;
224222
});

packages/oas-to-har/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"jest-expect-har": "^7.1.0",
6363
"tsup": "^8.0.2",
6464
"type-fest": "^4.18.3",
65-
"typescript": "^5.2.2"
65+
"typescript": "^5.2.2",
66+
"vitest": "^3.0.5"
6667
},
6768
"prettier": "@readme/eslint-config/prettier"
6869
}

packages/oas-to-snippet/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"oas": "file:../oas",
6363
"tsup": "^8.0.2",
6464
"type-fest": "^4.18.3",
65-
"typescript": "^5.2.2"
65+
"typescript": "^5.2.2",
66+
"vitest": "^3.0.5"
6667
},
6768
"prettier": "@readme/eslint-config/prettier"
6869
}

packages/oas/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
"@types/memoizee": "^0.4.11",
106106
"@types/node": "^22.7.6",
107107
"tsup": "^8.0.2",
108-
"typescript": "^5.4.4"
108+
"typescript": "^5.4.4",
109+
"vitest": "^3.0.5"
109110
},
110111
"prettier": "@readme/eslint-config/prettier"
111112
}

0 commit comments

Comments
 (0)