Skip to content

Storage

The storage driver controls where Kubb writes generated files. Configure it via the storage option in kubb.config.ts. When storage is omitted, Kubb defaults to fsStorage(), the built-in filesystem driver. Swap it with memoryStorage() for tests or implement the Storage interface to target any backend.

Storage interface

Every driver must implement 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) Teardown hook called after the build completes

Keys are root-relative paths (e.g. src/gen/api/getPets.ts).

Built-in drivers

fsStorage

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

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

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

Keys resolve against the configured root (defaults to process.cwd()). The driver skips writes when file content is already identical and creates missing parent directories automatically. 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 makes it useful for testing, CI validation, and dry-run scenarios.

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 instance. Read back the generated output 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 function 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 your plugin locally for a complete plugin test setup.