scalar icon indicating copy to clipboard operation
scalar copied to clipboard

feat(api-reference): `RequestExample` embed

Open hanspagel opened this issue 1 year ago • 4 comments

This PR adds a standalone RequestExample Vue component.

This and all future components will need to dereference the OpenAPI document. We probably don’t want to do this in every embed, so I’ve also embed an OpenApiDocument which does the data wrangling and can then be used by one or multiple instances of the ExampleRequest component.

In order to bring this to life, the components need to be really encapsulated, so I’ve had to move some code. I think that’s a great way to improve our code quality. :)

Supports the relevant parts of the configuration: proxy, spec, hiddenClients, defaultHttpClient, baseServerUrl, servers, authentication.

Usage

Note: While this PR adds the embeds, it doesn’t expose them yet. Let’s polish them a little bite more before making them available to everyone. :)

<script setup>
// TODO: The package doesn’t have this entrypoint yet.
import { OpenApiDocument, ExampleRequest } from '@scalar/api-reference/embeds'
</script>

<template>
  <OpenApiDocument
    v-slot="{ configuration, parsedSpec }"
    :configuration="{
      proxy: 'https://proxy.scalar.com',
      spec: {
        url: 'https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.json',
      }
    }">
  
    <ExampleRequest
      :configuration="configuration"
      :operation="parsedSpec.tags?.[1]?.operations?.[1]"
      :parsedSpec="parsedSpec" />
  
  </OpenApiDocument>
</template>

Playground

cd packages/api-reference
pnpm playground:embed

Preview

Screenshot 2024-08-09 at 21 23 02

hanspagel avatar Aug 09 '24 14:08 hanspagel

🦋 Changeset detected

Latest commit: 65233d20e1d2402617031473b644d0b2284148d5

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

This PR includes changesets to release 10 packages
Name Type
@scalar/api-reference Patch
@scalar/api-reference-editor Patch
@scalar/api-reference-react Patch
@scalar/cli Patch
@scalar/express-api-reference Patch
@scalar/fastify-api-reference Patch
@scalar/nestjs-api-reference Patch
@scalar/nuxt Patch
@scalar/play-button Patch
@scalar/docusaurus Patch

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

changeset-bot[bot] avatar Aug 09 '24 14:08 changeset-bot[bot]

#4149 Bundle Size — 2.09MiB (~+0.01%).

65233d2(current) vs b9afc73 main#4145(baseline)

[!WARNING] Bundle contains 4 duplicate packages – View duplicate packages

Bundle metrics  Change 2 changes Regression 1 regression
                 Current
#4149
     Baseline
#4145
Regression  Initial JS 2.09MiB(~+0.01%) 2.09MiB
No change  Initial CSS 0B 0B
Change  Cache Invalidation 100% 0%
No change  Chunks 1 1
No change  Assets 1 1
No change  Modules 1236 1236
No change  Duplicate Modules 0 0
No change  Duplicate Code 0% 0%
No change  Packages 169 169
No change  Duplicate Packages 4 4
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#4149
     Baseline
#4145
Regression  JS 2.09MiB (~+0.01%) 2.09MiB

Bundle analysis reportBranch feat/embedsProject dashboard


Generated by RelativeCIDocumentationReport issue

relativeci[bot] avatar Aug 09 '24 19:08 relativeci[bot]

Cloudflare Preview for the API Client

https://066edb6a.client-980.pages.dev

github-actions[bot] avatar Aug 09 '24 19:08 github-actions[bot]

@hanspagel I think the approach around OpenApiDocument needs to be discussed a bit more. Looking at this the plan looks to be using it in third party consumers to provision specs for different types of embedded components.

I think we can do this better with a hook that exposes both the Vue reactive objects and an event based system that would be easier to interface with in other applications.

// Usage anywhere in the app. 
const { configuration, parsedSpec, addSpec  } = useOASDocument({
  key: 'some-spec-id',
  /** Event based interface for non-vue frameworks. */ 
  onSpecChange: (spec) => console.log(spec),
})

// Function used to add a spec to the app using a namespaced key
// This allows a spec to be uploaded/parsed once and referenced everywhere using the same key
addSpec({
  key: 'some-spec-id',
    ...baseConfig,
})

With your above point about tidying up the code by keeping code encapsulated I think this could help a lot with writing broader parts of the reference codebase as we can just provide a key to components and they can call useOASDocument() internally to get the data they need.

geoffgscott avatar Aug 19 '24 18:08 geoffgscott

True and I fully agree. It was just slightly easier/faster to move the code from a component to another component and I wanted it to be hidden, but merged before I’m OOO.

I just rebased the PR and would love to get this in (still hidden) and work on a new PR with a better approach to expose this new functionally. Fine for everyone?

hanspagel avatar Aug 26 '24 09:08 hanspagel

Preview deployed to https://807dca5b-24df-451a-95e6-8f2348776371--scalar-deploy-preview.netlify.app

github-actions[bot] avatar Sep 02 '24 12:09 github-actions[bot]

Cool! I’ll merge this and take over the responsibility to further improve the implementation according to @geoffgscott’s feedback. 🤝

hanspagel avatar Sep 02 '24 12:09 hanspagel