-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate oas libraries of ours into this new monorepo (#811)
* refactor(monorepo): moving the library over to a monorepo structure * refactor(oas-extensions): moving `oas-extensions` into this monorepo * refactor(oas-normalize): moving `oas-normalize` into this monorepo * refactor(oas-to-har): moving `oas-to-har` into this monorepo * fix: adding a test i forgot to commit * fix: prettier issues * ci: fix build ordering for linting * fix: out of date dep * refactor: build cleanup (#812) * refactor: remove pretest scripts that way we're not double linting * refactor: build using lerna * chore: oops --------- Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com>
- Loading branch information
1 parent
0ab5932
commit 2554cfa
Showing
84 changed files
with
33,905 additions
and
6,029 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,5 @@ jobs: | |
steps: | ||
- uses: actions/checkout@v4 | ||
- run: npm ci | ||
- run: npm run build | ||
- run: npm run lint |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
coverage/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": ["@readme/eslint-config", "@readme/eslint-config/typescript"], | ||
"root": true, | ||
"rules": { | ||
"@typescript-eslint/consistent-type-imports": "error", | ||
"@typescript-eslint/explicit-module-boundary-types": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
test/ | ||
.eslint* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright © 2023 ReadMe | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any purpose | ||
with or without fee is hereby granted, provided that the above copyright notice | ||
and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS | ||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | ||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF | ||
THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# @readme/oas-extensions | ||
|
||
A list of extensions that ReadMe has added to extend the OpenAPI Specification. | ||
|
||
https://docs.readme.com/docs/openapi-extensions | ||
|
||
[![Build](https://github.com/readmeio/oas/workflows/CI/badge.svg)](https://github.com/readmeio/oas/tree/main/packages/oas-extensions) | ||
|
||
[![](https://raw.githubusercontent.com/readmeio/.github/main/oss-header.png)](https://readme.io) | ||
|
||
## Installation | ||
|
||
``` | ||
npm install --save @readme/oas-extensions | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"name": "@readme/oas-extensions", | ||
"description": "A list of extensions that ReadMe has added to extend the OpenAPI Specification", | ||
"version": "20.0.1", | ||
"author": "Jon Ursenbach <jon@readme.io>", | ||
"license": "ISC", | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.js" | ||
} | ||
}, | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.cts", | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/readmeio/oas.git", | ||
"directory": "packages/oas-extensions" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/readmeio/oas/issues" | ||
}, | ||
"scripts": { | ||
"build": "tsup", | ||
"lint": "npm run lint:types && npm run lint:js", | ||
"lint:js": "eslint . --ext .js,.ts", | ||
"lint:types": "tsc --noEmit", | ||
"prebuild": "rm -rf dist/", | ||
"prepack": "npm run build", | ||
"test": "vitest run --coverage" | ||
}, | ||
"peerDependencies": { | ||
"oas": "file:../oas" | ||
}, | ||
"devDependencies": { | ||
"@readme/oas-examples": "^5.12.0", | ||
"@vitest/coverage-v8": "^0.34.3", | ||
"eslint": "^8.48.0", | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.3" | ||
}, | ||
"prettier": "@readme/eslint-config/prettier" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import type Oas from 'oas'; | ||
import type Operation from 'oas/operation'; | ||
|
||
export const CODE_SAMPLES = 'code-samples'; | ||
export const EXPLORER_ENABLED = 'explorer-enabled'; | ||
export const HEADERS = 'headers'; | ||
export const METRICS_ENABLED = 'metrics-enabled'; | ||
export const PROXY_ENABLED = 'proxy-enabled'; | ||
export const SAMPLES_ENABLED = 'samples-enabled'; | ||
export const SAMPLES_LANGUAGES = 'samples-languages'; | ||
export const SEND_DEFAULTS = 'send-defaults'; | ||
export const SIMPLE_MODE = 'simple-mode'; | ||
|
||
// Make sure you document any changes on here: | ||
// https://docs.readme.com/docs/openapi-extensions | ||
export interface Extensions { | ||
[CODE_SAMPLES]: { | ||
code: string; | ||
install?: string; | ||
language: string; | ||
name?: string; | ||
}; | ||
[EXPLORER_ENABLED]: boolean; | ||
[HEADERS]: Record<string, string | number>[]; | ||
[METRICS_ENABLED]: boolean; | ||
[PROXY_ENABLED]: boolean; | ||
[SAMPLES_ENABLED]: boolean; // @deprecated | ||
[SAMPLES_LANGUAGES]: string[]; | ||
[SEND_DEFAULTS]: boolean; // @deprecated | ||
[SIMPLE_MODE]: boolean; | ||
} | ||
|
||
export const defaults: Extensions = { | ||
[CODE_SAMPLES]: undefined, | ||
[EXPLORER_ENABLED]: true, | ||
[HEADERS]: undefined, | ||
[METRICS_ENABLED]: true, | ||
[PROXY_ENABLED]: true, | ||
[SAMPLES_ENABLED]: true, | ||
[SAMPLES_LANGUAGES]: ['shell', 'node', 'ruby', 'php', 'python', 'java', 'csharp'], | ||
[SEND_DEFAULTS]: false, | ||
[SIMPLE_MODE]: true, | ||
}; | ||
|
||
/** | ||
* With one of our custom OpenAPI extensions, look for it in either an instance of `oas` or an | ||
* instance of the `Operation` class in `oas`. | ||
* | ||
* Our custom extensions can either be nestled inside of an `x-readme` object or at the root level | ||
* with an `x-` prefix. | ||
* | ||
* @see {@link https://npm.im/oas} | ||
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions} | ||
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specificationExtensions} | ||
* @param extension Specification extension to lookup. | ||
* @param oas An instance of the `oas` library class. | ||
* @param operation An instsance of the `Operation` class from the `oas` library. | ||
* @returns Contents of the extension if found, otherwise the extensions default value. | ||
*/ | ||
export function getExtension(extension: keyof Extensions, oas: Oas, operation?: Operation) { | ||
if (operation) { | ||
if (operation.hasExtension('x-readme')) { | ||
const data = operation.getExtension('x-readme') as Extensions; | ||
if (data && typeof data === 'object' && extension in data) { | ||
return data[extension]; | ||
} | ||
} | ||
|
||
if (operation.hasExtension(`x-${extension}`)) { | ||
return operation.getExtension(`x-${extension}`); | ||
} | ||
} | ||
|
||
// Because our `code-samples` extension is intended for operation-level use, if it's instead | ||
// placed at the OAS-level we should ignore it. | ||
if (extension === CODE_SAMPLES) { | ||
return defaults[extension]; | ||
} | ||
|
||
if (oas.hasExtension('x-readme')) { | ||
const data = oas.getExtension('x-readme') as Extensions; | ||
if (data && typeof data === 'object' && extension in data) { | ||
return data[extension]; | ||
} | ||
} | ||
|
||
if (oas.hasExtension(`x-${extension}`)) { | ||
return oas.getExtension(`x-${extension}`); | ||
} | ||
|
||
return defaults[extension]; | ||
} | ||
|
||
/** | ||
* With one of our custom OpenAPI extensions, determine if it's valid on a given instance of `oas`. | ||
* | ||
* @todo add support for validating on operations. | ||
* | ||
* @see {@link https://npm.im/oas} | ||
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions} | ||
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specificationExtensions} | ||
* @param extension Specification extension to lookup. | ||
* @param oas An instance of the `oas` library class. | ||
*/ | ||
export function validateExtension(extension: keyof Extensions, oas: Oas) { | ||
if (oas.hasExtension('x-readme')) { | ||
const data = oas.getExtension('x-readme') as Extensions; | ||
if (typeof data !== 'object' || Array.isArray(data) || data === null) { | ||
throw new TypeError('"x-readme" must be of type "Object"'); | ||
} | ||
|
||
if (extension in data) { | ||
if ([CODE_SAMPLES, HEADERS, SAMPLES_LANGUAGES].includes(extension)) { | ||
if (!Array.isArray(data[extension])) { | ||
throw new TypeError(`"x-readme.${extension}" must be of type "Array"`); | ||
} | ||
} else if (typeof data[extension] !== 'boolean') { | ||
throw new TypeError(`"x-readme.${extension}" must be of type "Boolean"`); | ||
} | ||
} | ||
} | ||
|
||
// If the extension isn't grouped under `x-readme`, we need to look for them with `x-` prefixes. | ||
if (oas.hasExtension(`x-${extension}`)) { | ||
const data = oas.getExtension(`x-${extension}`); | ||
if ([CODE_SAMPLES, HEADERS, SAMPLES_LANGUAGES].includes(extension)) { | ||
if (!Array.isArray(data)) { | ||
throw new TypeError(`"x-${extension}" must be of type "Array"`); | ||
} | ||
} else if (typeof data !== 'boolean') { | ||
throw new TypeError(`"x-${extension}" must be of type "Boolean"`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@readme/eslint-config/testing/vitest" | ||
} |
Oops, something went wrong.