Skip to content

An easy to use plugin for vite that adds api server capabilities

License

Notifications You must be signed in to change notification settings

j-o-sh/vite-api-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vite Api Server

Create a simple api server as a vite plugin that is easy to maintain.

NOTE The example usage below is to be considered a goal vor v1. It doesn't work yet ;)

vite.config.js

import { plugin as server, pathParams } from 'vite-api-server'
import api from './api'

/** @type {import('vite').UserConfig} */
export default {
  plugins: [server({
    '/todos': { 
      get: json(api.list),
      post: jsonBodyParam(api.add)
    },
    [pathParams('/todos/[id]')]: {
      get: json(api.get),
      put: json(jsonBodyParam(api.change, { index: 1 })),
      delete: json(api.remove)
    }
  })]
}
export default

api.js

/**
 * @typedef {description: string, done: boolean} Todo
 */

/**
 * @type {[id: string]: Todo}
 */
const todos = {}

export function list() {
  return todos
}

export function add(todo) {
  todos.push(todo)
}

export function get(id) {
  return todos[id]
}

export function change(id, todo) {
  todos[id] = todo
}

export function remove(id) {
  delete todos[id]
}

About

An easy to use plugin for vite that adds api server capabilities

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published