0. Get Started - Refzlund/sveltekit-zero-api GitHub Wiki

Requirements

  • TypeScript >5 in your SvelteKit project

Install

  1. Add to project: npm i sveltekit-zero-api -D/pnpm add sveltekit-zero-api -D
  2. Add Vite Plugin to your vite.config.ts:
    import { zeroAPI } from 'sveltekit-zero-api'
    
    const config: UserConfig = {
       plugins: [
          sveltekit(),
          zeroAPI()
       ]
    }
    
    // .gitignore
    **/sveltekit-zero-api.d.ts
  3. Run the project npm run dev/pnpm dev — running the server generates the types for the project.

Configuration

Vite Plugin Config

Property Description Default
routesDir The directory where we may locate +server.ts files 'src/routes'
outputDir Where api.ts (to import) will be written 'src'
apiName The name of the output-file API config 'api'


The output-file will be imported in your front-end to access ZeroAPI


api.ts (default name) config

Will be generated at npm run dev at ↑ outputDir

Property Description Default
baseData Data (body/query) which will be prepended to every request. Takes same inputs as the fetch function's 2nd param undefined
format Default format for all responses 'json'
baseUrl Prepended to API url ''
onError Function to be called on error undefined
onSuccess Function to be called on success undefined
stringifyQueryObjects If a query value in a key-value pair is an object, it will be stringified automatically true
prependCallbacks Prepends callbacks. Ex.: (method) => method.ServerError(errorHandler) undefined
sendEmptyBodyAsJSON If this is true, it will send the body of each request as an empty JSON to provide a backend error message when trying to use request.json() true
⚠️ **GitHub.com Fallback** ⚠️