Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content

Storage

The storage driver controls where Kubb writes generated files. Set it through the storage option in kubb.config.ts. Leave storage out and Kubb uses fsStorage(), the built-in filesystem driver. Swap in memoryStorage() for tests, or implement the Storage interface to target any backend.

Storage interface

Every driver implements the Storage interface exported from @kubb/core:

Member Signature Purpose
name string Identifier used in logs and debug output
hasItem (key: string) => Promise<boolean> Returns true when the key exists
getItem (key: string) => Promise<string | null> Returns the stored value, or null when missing
setItem (key: string, value: string) => Promise<void> Persists a value under key
removeItem (key: string) => Promise<void> Removes the entry for key
getKeys (base?: string) => Promise<Array<string>> Lists all keys, optionally filtered by prefix
clear (base?: string) => Promise<void> Removes all entries, optionally scoped to a prefix
dispose () => Promise<void> (optional) Optional teardown hook for the backend to flush buffers or close connections

Keys are root-relative paths, for example src/gen/api/getPets.ts.

Built-in drivers

fsStorage

fsStorage writes files to the local filesystem. Kubb uses it when no storage option is set.

kubb.config.ts
typescript
import {  } from 'kubb'
import {  } from '@kubb/core'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen' },
  : (),
})

Keys resolve against process.cwd(), so root-relative paths land in the right place without extra setup. The driver skips writes when file content is already identical and creates missing parent directories. Calling clear() without a base argument is a no-op. Pass a path prefix to remove a specific subtree.

memoryStorage

memoryStorage stores all output in a Map. Nothing is written to disk, which suits testing, CI validation, and dry runs.

kubb.config.ts
typescript
import {  } from 'kubb'
import {  } from '@kubb/core'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen' },
  : (),
})

Each call to memoryStorage() creates an independent Map. Read the generated output back with storage.getKeys() and storage.getItem(key) after the build completes.

Creating a custom driver

createStorage from @kubb/core wraps any backend. Pass a factory that receives your options and returns a Storage implementation.

s3Storage.ts
typescript
import {  } from '@kubb/core'

export const  = <{ : string }>(() => {
  return {
    : 's3',
    async () {
      return false
    },
    async () {
      return null
    },
    async (, ) {
      // upload to S3
    },
    async () {
      // delete from S3
    },
    async () {
      return []
    },
    async () {
      // batch delete from S3
    },
    async () {
      // close any open connections
    },
  }
})

Pass it in kubb.config.ts:

kubb.config.ts
typescript
import {  } from 'kubb'
import {  } from './s3Storage.ts'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen' },
  : ({ : ..! }),
})

Testing with memoryStorage

Use memoryStorage in tests to capture generated files without writing to disk:

plugin.test.ts
typescript
import { ,  } from '@kubb/core'

const  = ()

const  = ({
  : { : './petStore.yaml' },
  : { : './src/gen' },
  ,
  : [],
})

const {  } = await .()

if (!) {
  const  = await .()
  const  = await .([0]!)
  .()
}

See Testing for a complete plugin test setup.