Get Started - Refzlund/sveltekit-zero-api Wiki

Requirements

  • TypeScript in your SvelteKit project
  • >= @next.489

Install

  1. Add to project: npm i sveltekit-zero-api -D/pnpm add sveltekit-zero-api -D
  2. Add watchAPI at the top of svelte.config.js:
     import { watchAPI } from 'sveltekit-zero-api'
     if (process.env.NODE_ENV !== 'production') {
         watchAPI()
     }
  3. Run the project npm run dev/pnpm dev — running the server generates the types for the project.

Configuration

watchAPI

Property Description Default
watchDir The directory which will be watched for changes '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** ⚠️