Git Product home page Git Product logo

svelte-zero-api's Introduction

svelte-zero-api

Only 2kb size (not gzip)

Easy change Svelte Kit APIs to Zero API.

Use Svelte Kit APIs like call function, support Typescript.

Install

npm install svelte-zero-api

Getting started

1. Edit svelte.config.js, example:

import preprocess from "svelte-preprocess";

// 1. import
import zeroApiWatch from "svelte-zero-api/watch";

// 2. add watch by change watchPath files, auto create api files:
if (process.env.NODE_ENV !== "production") {
  zeroApiWatch();
}

export default {
  preprocess: [preprocess({ postcss: true })],
  kit: { target: "#svelte" },
};

2. Use all api function in front-end pages, example:

at src/routes/index.svelte

<script lang="ts">
  import { zeroApi } from "../zeroApi";

  // We can use api before onMount, because api function only run in browser.
  // like front end function, and have Typescrit point out.
  let helloPost = zeroApi.api.hello.post({ body: { name: "Dog" } });
</script>

{#await helloPost}
	<div>loading...</div>
{:then res}
	<div>{res.body.world}</div>
{/await}

API Example

at src/routes/api/hello.ts

import type { QueryGet } from "svelte-zero-api";

interface Get {
  query: {
    name: string;
  };
}

interface Post {
  body: {
    name: string;
  };
}

// Need return a Promise
// use `Get & QueryGet<Get>` definition types add query.get(...);
export const get = async ({ query }: Get & QueryGet<Get>) => {
  return { body: { world: "I'm a " + query.get("name") } };
};

// Need return a Promise
export const post = async ({ body }: Post) => {
  return { body: { world: "I'm a " + body.name } };
};

Other


That's all, Thanks read my broken English.

svelte-zero-api's People

Contributors

refzlund avatar ymzuiku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

svelte-zero-api's Issues

fs.watch recursive

Awesome work!
However, fs.watch recursive [./watch.js(96)] does not work in Linux, and does not work in Node 14

The feature watch recursively is unavailable on the current platform, which is being used to run Node.js TypeError [ERR_FEATURE_UNAVAILABLE_ON_PLATFORM]: The feature watch recursively is unavailable on the current platform, which is being used to run Node.js at Object.watch (fs.js:1578:11) at default (file:///home/arthur/Gits/cuppy-sveltekit/node_modules/svelte-zero-api/watch.js:96:6) at file:///home/arthur/Gits/cuppy-sveltekit/svelte.config.js:7:2 at ModuleJob.run (internal/modules/esm/module_job.js:152:23) at async Loader.import (internal/modules/esm/loader.js:177:24) at async load_config (file:///home/arthur/Gits/cuppy-sveltekit/node_modules/@sveltejs/kit/dist/cli.js:502:17) at async get_config (file:///home/arthur/Gits/cuppy-sveltekit/node_modules/@sveltejs/kit/dist/cli.js:557:10) at async file:///home/arthur/Gits/cuppy-sveltekit/node_modules/@sveltejs/kit/dist/cli.js:615:18

https://stackoverflow.com/a/61829462

Perhaps https://stackoverflow.com/a/13705878 could help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.