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

chore: reduce bundle size #100

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strong-students-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@scalar/openapi-parser': patch
---

chore: reduce bundle size
23 changes: 14 additions & 9 deletions demo/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<script setup lang="ts">
import { fetchUrlsPlugin, openapi } from '@scalar/openapi-parser'
import {
dereference,
fetchUrlsPlugin,
load,
toJson,
} from '@scalar/openapi-parser'
import { watchDebounced } from '@vueuse/core'
import { onMounted, ref, watch } from 'vue'

Expand Down Expand Up @@ -47,14 +52,14 @@ watch(value, async (newValue) => {
watchDebounced(
value,
async (newValue) => {
result.value = (
await openapi()
.load(newValue, {
plugins: [fetchUrlsPlugin()],
})
.dereference()
.get()
)?.schema
const content = await load(newValue, {
plugins: [fetchUrlsPlugin()],
})
const { schema } = await dereference(content)

if (schema) {
result.value = toJson(schema)
}
},
{
debounce: 500,
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@types/node": "^20.12.12"
},
"dependencies": {
"@humanwhocodes/momoa": "^3.0.2",
"ajv": "^8.13.0",
"ajv-draft-04": "^1.0.0",
"ajv-formats": "^3.0.1",
Expand Down
11 changes: 0 additions & 11 deletions packages/openapi-parser/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import Ajv04 from 'ajv-draft-04'
import Ajv2020 from 'ajv/dist/2020'

import Swagger20 from '../schemas/v2.0/schema.json'
import OpenApi30 from '../schemas/v3.0/schema.json'
import OpenApi31 from '../schemas/v3.1/schema.json'
Expand All @@ -20,14 +17,6 @@ export const OpenApiVersions = Object.keys(
OpenApiSpecifications,
) as OpenApiVersion[]

/**
* Configure available JSON Schema versions
*/
export const jsonSchemaVersions = {
'http://json-schema.org/draft-04/schema#': Ajv04,
'https://json-schema.org/draft/2020-12/schema': Ajv2020,
}

/**
* List of error messages used in the Validator
*/
Expand Down
11 changes: 10 additions & 1 deletion packages/openapi-parser/src/lib/Validator/Validator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import Ajv04 from 'ajv-draft-04'
import addFormats from 'ajv-formats'
import Ajv2020 from 'ajv/dist/2020'

import {
ERRORS,
OpenApiSpecifications,
type OpenApiVersion,
OpenApiVersions,
jsonSchemaVersions,
} from '../../configuration'
import type { AnyObject, Filesystem, ValidateResult } from '../../types'
import { details as getOpenApiVersion } from '../../utils'
import { resolveReferences } from '../../utils/resolveReferences'
import { transformErrors } from '../../utils/transformErrors'

/**
* Configure available JSON Schema versions
*/
export const jsonSchemaVersions = {
'http://json-schema.org/draft-04/schema#': Ajv04,
'https://json-schema.org/draft/2020-12/schema': Ajv2020,
}

export class Validator {
public version: string

Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-parser/src/pipeline.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import YAML from 'yaml'
import { stringify } from 'yaml'

import { openapi } from '.'
import { readFilesPlugin } from './utils/load/plugins/readFilesPlugin'
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('pipeline', () => {
it('toYaml', async () => {
const result = await openapi().load(specification).toYaml()

expect(result).toBe(YAML.stringify(specification))
expect(result).toBe(stringify(specification))
})

it('dereference', async () => {
Expand Down
4 changes: 0 additions & 4 deletions packages/openapi-parser/src/utils/betterAjvErrors/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { parse } from '@humanwhocodes/momoa'

import prettify from './helpers'

type BetterAjvErrorsOptions = {
Expand Down Expand Up @@ -28,13 +26,11 @@ export function betterAjvErrors(
const { indent = null, json = null } = options

const jsonRaw = json || JSON.stringify(data, null, indent)
const jsonAst = parse(jsonRaw)

const customErrorToStructure = (error) => error.getError()
const customErrors = prettify(errors, {
data,
schema,
jsonAst,
jsonRaw,
})

Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-parser/src/utils/isYaml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import YAML from 'yaml'
import { parse } from 'yaml'

export function isYaml(value: string) {
// Line breaks
Expand All @@ -7,7 +7,7 @@ export function isYaml(value: string) {
}

try {
YAML.parse(value)
parse(value)
return true
} catch (error) {
console.log('ADSAS', error)
Expand Down
10 changes: 5 additions & 5 deletions packages/openapi-parser/src/utils/load/load.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import { describe, expect, it } from 'vitest'
import YAML from 'yaml'
import { stringify } from 'yaml'

import { getEntrypoint } from '../getEntrypoint'
import { load } from './load'
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('load', async () => {

it('loads YAML string', async () => {
const filesystem = await load(
YAML.stringify({
stringify({
openapi: '3.1.0',
info: {
title: 'Hello World',
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('load', async () => {
// @ts-expect-error only partially patched
global.fetch = async () => ({
text: async () =>
YAML.stringify({
stringify({
openapi: '3.1.0',
info: {
title: 'Hello World',
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('load', async () => {
if (url === 'https://example.com/openapi.yaml') {
return {
text: async () =>
YAML.stringify({
stringify({
openapi: '3.1.0',
info: {
title: 'Hello World',
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('load', async () => {
}

const filesystem = await load(
YAML.stringify({
stringify({
openapi: '3.1.0',
info: {
title: 'Hello World',
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-parser/src/utils/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import YAML from 'yaml'
import { parse } from 'yaml'

import { AnyObject, Filesystem } from '../types'
import { isFilesystem } from './isFilesystem'
Expand All @@ -18,7 +18,7 @@ export function normalize(
try {
return JSON.parse(specification)
} catch (error) {
return YAML.parse(specification)
return parse(specification)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-parser/src/utils/toYaml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import YAML from 'yaml'
import { stringify } from 'yaml'

import { AnyObject } from '../types'

export const toYaml = (value: AnyObject) => YAML.stringify(value)
export const toYaml = (value: AnyObject) => stringify(value)
9 changes: 8 additions & 1 deletion packages/openapi-parser/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ export default defineConfig({
formats: ['es'],
},
rollupOptions: {
external: [...builtinModules, ...builtinModules.map((m) => `node:${m}`)],
external: [
...builtinModules,
...builtinModules.map((m) => `node:${m}`),
'ajv/dist/2020',
'ajv-draft-04',
'ajv-formats',
'yaml',
],
},
},
test: {
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.