Détail du package

@scalar/workspace-store

scalar0MIT0.7.0

Store interface for openapi documents

workspace, store, openapi, scalar

readme

@scalar/workspace-store

A powerful data store for managing OpenAPI documents. This package provides a flexible solution for handling multiple OpenAPI documents in a workspace environment

Server-Side workspace store

Server side data store which enables document chunking to reduce initial loading time specially when working with large openapi documents

Usage

Create a new store in SSR mode

// Create the store
const store = await createServerWorkspaceStore({
  baseUrl: 'example.com',
  mode: 'ssr',
  meta: { 'x-scalar-active-document': 'document-name' },
  documents: [
    {
      name: 'document-name',
      meta: {},
      document: {
        openapi: '3.1.1',
        info: {
          title: 'Hello World',
          version: '1.0.0',
        },
        components: {
          schemas: {
            Person: {
              type: 'object',
              properties: {
                name: { type: 'string' },
              },
            },
            User: {
              $ref: '#/components/schemas/Person',
            },
          },
        },
      },
    },
  ],
})

// Add a new document to the store
await store.addDocument({
  openapi: '3.1.1',
  info: {
    title: 'Hello World',
    version: '1.0.0',
  },
  components: {
    schemas: {
      Person: {
        type: 'object',
        properties: {
          name: { type: 'string' },
        },
      },
      User: {
        $ref: '#/components/schemas/Person',
      },
    },
  },
}, {
  name: 'document-2',
  "x-scalar-active-server": "server1"
})

// Get the workspace
// Workspace is going to keep all the sparse documents
const workspace = store.getWorkspace()

// Get chucks using json pointers
const chunk = store.get('#/document-name/components/schemas/Person')

Create a new store in static mode

// Create the store
const store = await createServerWorkspaceStore({
  directory: 'assets',
  mode: 'static',
  meta: { 'x-scalar-active-document': 'document-name' },
  documents: [
    {
      name: 'document-name',
      meta: {},
      document: {
        openapi: '3.1.1',
        info: {
          title: 'Hello World',
          version: '1.0.0',
        },
        components: {
          schemas: {
            Person: {
              type: 'object',
              properties: {
                name: { type: 'string' },
              },
            },
            User: {
              $ref: '#/components/schemas/Person',
            },
          },
        },
      },
    },
  ],
})

// Add a new document to the store
await store.addDocument({
  openapi: '3.1.1',
  info: {
    title: 'Hello World',
    version: '1.0.0',
  },
  components: {
    schemas: {
      Person: {
        type: 'object',
        properties: {
          name: { type: 'string' },
        },
      },
      User: {
        $ref: '#/components/schemas/Person',
      },
    },
  },
}, {
  name: 'document-2',
  "x-scalar-active-server": "server1"
})

// Generate the workspace file system
// This will write in the filesystem the workspace and all the chucks
// which can be resolved by the consumer
const workspace = await store.generateWorkspaceChunks()

Load documents from external sources

// Initialize the store with documents from external sources
const store = await createServerWorkspaceStore({
  mode: 'static',
  documents: [
    {
      name: 'remoteFile',
      url: 'http://localhost/document.json'
    },
    {
      name: 'fsFile',
      path: './document.json'
    }
  ]
})

// Output: { openapi: 'x.x.x', ... }
console.log(store.getWorkspace().documents.remoteFile)

// Output: { openapi: 'x.x.x', ... }
console.log(store.getWorkspace().documents.fsFile)

Client-Side Workspace Store

A reactive workspace store for managing OpenAPI documents with automatic reference resolution and chunked loading capabilities. Works seamlessly with server-side stores to handle large documents efficiently.

Usage


// Initialize a new workspace store with default document
const store = createWorkspaceStore({
  documents: [
    {
      name: 'default',
      document: {
        info: {
          title: 'OpenApi document',
          version: '1.0.0',
        },
      },
    },
  ],
  meta: {
    'x-scalar-active-document': 'default',
  },
})

// Add another OpenAPI document to the workspace
store.addDocumentSync({
  document: {
    info: {
      title: 'OpenApi document',
      version: '1.0.0',
    },
  },
  name: 'document',
})

// Get the currently active document
store.workspace.activeDocument

// Retrieve a specific document by name
store.workspace.documents['document']

// Update global workspace settings
store.update('x-scalar-dark-mode', true)

// Update settings for the active document
store.updateDocument('active', "x-scalar-active-auth", '<value>')

// Resolve and load document chunks including any $ref references
await store.resolve(['paths', '/users', 'get'])

Load documents from external sources

You can only initialize the store with object literals but if you want to add documents from external sources you can use the addDocument function

const store = createWorkspaceStore()

// Load a document into the store from a remote url
await store.addDocument({
  name: 'default',
  url: 'http://localhost/document.json'
})

// Output: { openapi: 'x.x.x', ... }
console.log(store.workspace.documents.default)

Configuration

You can pass configuration object to the workspace store which is going to be applied to all the documents

const store = createWorkspaceStore({
  config: {
    "x-scalar-reference-config": {
      features: {
        showModels: true,
      },
      appearance: {
        layout: 'modern'
      },
    }
  }
})

You can override specific document configuration when you add the document to the store

store.addDocumentSync({
  name: 'example',
  document: {
    openapi: '3.0.0',
    info: { title: 'Example API', version: '1.0.0' },
    paths: {},
  },
  config: {
    features: {
      showModels: false,
    },
  }
})

To get the active document configuration you can use config getter\

// Get the configuration for the active document
console.log(store.config['x-scalar-reference-config'].features.showModels)

When no configuration is provided it will return the default configuration

changelog

@scalar/workspace-store

0.7.0

Minor Changes

  • a05d638: feat(workspace-store): create types and validator for reference config schema
  • 8d8e427: feat(api-reference): load documents on the new store
  • dbbdd70: feat(workspace-store) add configuration mapping to the workspace store

Patch Changes

  • 23b150b: fix: export and fix up some types from workspace store
  • Updated dependencies [533469b]
  • Updated dependencies [23b150b]
    • @scalar/openapi-types@0.3.4
    • @scalar/helpers@0.0.5
    • @scalar/openapi-parser@0.18.0
    • @scalar/types@0.2.5
    • @scalar/code-highlight@0.1.4

0.6.0

Minor Changes

  • b97c82a: feat(workspace-store): server store input document from different sources
  • 9f786d5: feat(workspace-store): preserve the original ref information

Patch Changes

  • @scalar/code-highlight@0.1.4
  • @scalar/openapi-parser@0.18.0

0.5.2

Patch Changes

  • 1703e42: fix(workspace-store): relative paths for ssg mode

0.5.1

Patch Changes

  • a567796: fix: make the workspace store reactive again
  • Updated dependencies [291f09d]
    • @scalar/openapi-parser@0.18.0

0.5.0

Minor Changes

  • b6a1624: feat(workspace-store): improve workspace store validation

0.4.1

Patch Changes

  • Updated dependencies [7913193]
    • @scalar/code-highlight@0.1.4

0.4.0

Minor Changes

  • 7338d56: feat(workspace-store): generate sidebar entries

Patch Changes

  • 4156f1d: Expand workspace store integration
  • Updated dependencies [221e35f]
  • Updated dependencies [166e298]
  • Updated dependencies [4156f1d]
  • Updated dependencies [a37df33]
  • Updated dependencies [f823d45]
  • Updated dependencies [37c90b8]
    • @scalar/openapi-types@0.3.3
    • @scalar/helpers@0.0.4
    • @scalar/openapi-parser@0.17.0
    • @scalar/code-highlight@0.1.3

0.3.2

Patch Changes

  • 11fabae: fix(openapi-parser): webpack bundle errors because of the barrel files
  • Updated dependencies [11fabae]
    • @scalar/openapi-parser@0.16.0

0.3.1

Patch Changes

  • 373214a: fix(workspace-store): remove readFiles plugin from the client store

0.3.0

Minor Changes

  • bbad7c7: feat(workspace-store): create a sync client store

0.2.0

Minor Changes

  • f4332eb: feat: external reference resolution and partial bundle of the openapi document

Patch Changes

  • Updated dependencies [17bc0d8]
  • Updated dependencies [b9dacba]
  • Updated dependencies [a73e9be]
  • Updated dependencies [49c04cf]
  • Updated dependencies [f4332eb]
  • Updated dependencies [05c22c7]
    • @scalar/openapi-parser@0.15.0
    • @scalar/openapi-types@0.3.2

0.1.1

Patch Changes

  • Updated dependencies [ee3eb77]
    • @scalar/openapi-parser@0.14.0

0.1.0

Minor Changes

  • f7474ed: feat(workspace-store): openapi server side store

Patch Changes

  • Updated dependencies [1e87feb]
    • @scalar/openapi-parser@0.13.0
    • @scalar/openapi-types@0.3.1