---
name: svelte-store-refactoring-tool
description: "Reshapes tangled Svelte store code into typed, single-purpose modules: side effects pulled into helpers, mixed concerns split into separate stores, and a change list that explains each move. Use when a Svelte store has grown into a tangle of API calls, UI flags, and business rules and needs splitting into typed single-purpose modules. Do not use for diagnosing a store bug without restructuring, or converting stores to Svelte 5 runes. Related topics: coding, svelte."
---

# Svelte Store Refactoring Tool

Reshapes tangled Svelte store code into typed, single-purpose modules: side effects pulled into helpers, mixed concerns split into separate stores, and a change list that explains each move.

## When to use

Use this skill when a Svelte store has grown into a tangle of API calls, UI flags, and business rules and needs splitting into typed single-purpose modules. Do not use it for diagnosing a store bug without restructuring, or converting stores to Svelte 5 runes.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. List every concern the current code mixes (fetching, persistence, derived values, UI flags, rules); the list is complete before any code moves.
3. Decide the module split: one store per concern, derived stores for computed values, utilities for side effects; each concern has a named home.
4. Move the code into the new modules while keeping the store names and exported functions the components already use, noting any forced rename.
5. Apply the chosen error handling at the side-effect boundary and add types at the requested level, so every export has a signature.
6. Write the Structural Changes list with one bullet per move and the reason, and name the tests that would reach the coverage target.

## Inputs

- **Current store code** (longer free text)
- **State it holds** (choose any: User data, UI settings, Todo list, Auth token, Cart items, Theme preference, Other)
- **Dependencies** (free text, e.g. "External libraries or APIs the store calls")
- **Error handling** (choose one: Silent fail, Log to console, Throw exception, Return null)
- **Type safety level** (choose one: Basic JSDoc types, Full TypeScript, Leave untyped)
- **Test coverage required** (number from 0 to 100)

## Output

One code block per refactored file with its path as the first comment, a Utilities section for extracted helpers, then a Structural Changes list of one bullet per move with its reason.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Svelte Store Refactoring Tool

Refactor the store code below into clean, typed, single-purpose modules.

- Current store code: {{Current store code}}
- State it holds: {{State it holds}}
- Dependencies: {{Dependencies}}
- Error handling: {{Error handling}}
- Type safety level: {{Type safety level}}
- Test coverage required: {{Test coverage required}}

Read the code once for structure before changing anything. Identify each concern it mixes: data fetching, persistence, derived values, UI flags, and business rules. Decide the module split: one store per concern, derived stores for values computed from other stores, and plain utility functions for side effects such as API calls and localStorage, so each store stays synchronous and testable. Keep the public surface the components already use (store names and exported function names) unless a rename is required, and say so when it is.

Apply the chosen error handling consistently at the boundary where side effects happen, never inside subscribers. Add types at the requested level: the value type, the exported function signatures, and typed custom store return objects. When test coverage is required, structure the code so the pure parts can be tested without a DOM and name the tests that would reach the target.

### Output
Return the refactored store modules in one code block per file with the file path as the first comment. Follow with a Utilities section for any extracted helpers, then a Structural Changes list with one bullet per move and the reason for it. All imports explicit and ready to drop into a Svelte project.

## Pitfalls

- Renaming the store or its exports during the split, which breaks every component that imports them.
- Leaving an async fetch inside a derived store callback, which makes the derived value flicker between stale and fresh.
- Catching errors inside subscribers instead of at the side-effect boundary, so failures are swallowed where nobody can act on them.
