scalar / openapi-parser

Modern OpenAPI parser written in TypeScript
MIT License
34 stars 1 forks source link

feat: fetch urls, fix #67 #95

Closed hanspagel closed 3 months ago

hanspagel commented 3 months ago

With this PR references to URLs can be resolved.

To achieve this, I’ve introduced the new utility load utility, which imports JSON stings, YAML strings, JS objects, files and urls now. It even comes with a tiny plugin API, which means you can add whatever data source you’d like, or not enable file support (in browser envs).

I’ve had to make the load utility async, which made the pipeline syntax really ugly (await (await openapi().load()).resolve()), so I’ve refactored it to put all actions in a queue and work through it at the end, which enables a way nicer syntax:

Pipeline syntax

import { openapi, fetchUrlsPlugin, readFilesPlugin } from '@scalar/openapi-parser'

const result = await openapi()
  .load('./openapi.yaml', {
    plugins: [readFilesPlugin, fetchUrlsPlugin],
  })
  .dereference()
  .get()

Modular syntax

import {
  dereference,
  fetchUrlsPlugin,
  load,
  readFilesPlugin,
} from '@scalar/openapi-parser'

// Load a file and all referenced files
const filesystem = await load('./openapi.yaml', {
  plugins: [readFilesPlugin, fetchUrlsPlugin],
})

// Instead of just passing a single specification, pass the whole “filesystem”
const result = await dereference(filesystem)

Additional improvements

Example

{
  "openapi": "3.1.0",
  "info": {
    "title": "HTTP Reference"
  },
  "paths": {
    "/foobar": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.json#/components/schemas/Planet"
              }
            }
          }
        }
      }
    }
  }
}
changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: 78cd8437365be4ae4c79ad50987866ef8badd024

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ---------------------- | ----- | | @scalar/openapi-parser | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

relativeci[bot] commented 3 months ago

#44 Bundle Size — 477.72KiB (-0.58%).

78cd843(current) vs e3d7bba main#42(baseline)

Bundle metrics  Change 3 changes Improvement 1 improvement
                 Current
#44
     Baseline
#42
Improvement  Initial JS 477.72KiB(-0.58%) 480.51KiB
No change  Initial CSS 0B 0B
Change  Cache Invalidation 100% 0%
No change  Chunks 1 1
No change  Assets 1 1
Change  Modules 306(+0.33%) 305
No change  Duplicate Modules 0 0
No change  Duplicate Code 0% 0%
No change  Packages 10 10
No change  Duplicate Packages 0 0

Bundle size by type  Change 1 change Improvement 1 improvement
|            |       Current
[#44](https://app.relative-ci.com/projects/hXQ54UQKluDUZAKvpt9b/jobs/44-oAHC1oInE6TE9tMvDnmx?utm_source=github&utm_medium=pr-report "View bundle analysis report") |      Baseline
[#42](https://app.relative-ci.com/projects/hXQ54UQKluDUZAKvpt9b/jobs/42-LYqv1hMR19XLvUg7iVSz?utm_source=github&utm_medium=pr-report "View baseline bundle analysis report") | |:--|--:|--:| | Improvement  [JS](https://app.relative-ci.com/projects/hXQ54UQKluDUZAKvpt9b/jobs/44-oAHC1oInE6TE9tMvDnmx/assets?ba=%7B%22filters%22%3A%22ft.CSS-0_ft.JS-1_ft.IMG-0_ft.MEDIA-0_ft.FONT-0_ft.HTML-0_ft.OTHER-0%22%7D "View JS assets") | `477.72KiB` (`-0.58%`) | `480.51KiB` |

Bundle analysis reportBranch feat/fetch-urlsProject dashboard