Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ymzuiku/svelte-zero-api

Use Svelte Kit APIs like client functions, support Typescript.
https://github.com/ymzuiku/svelte-zero-api

svelte svelte-kit zero-api

Last synced: about 2 months ago
JSON representation

Use Svelte Kit APIs like client functions, support Typescript.

Awesome Lists containing this project

README

        

## How about dont't use style-zero-api/watch?

> only use coding and not edit `svelte.config.js`

1 - Create A POST api in `src/routes/api/hello.ts`:

```ts
interface Props {
body: {
name: string;
};
}

// Must export a Promise function
export const get = async ({ query }: Props) => {
return { world: { out: "I'm a " + query.name } };
};

// Must export a Promise function
export const post = async ({ body }: Props) => {
return { world: { out: "You are a " + body.name } };
};
```

2 - Create Some index.d.ts for APIs in `src/routes/api/index.d.ts`:

```ts
import * as hello from "./hello";

// Export default object tree must equal svelte kit api fetch route
export default { hello };
```

3 - Create api functions by `svelte-zero-api` in `src/api.ts`:

```ts
import { createZeroApi } from "./zeroApi";
import type API from "../routes/api";

export const api = createZeroApi({ baseUrl: "/api" });
```

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

at `src/routes/index.svelte`

```ts

import { api } from "../tools/api";

import Button from "../lib/Button.svelte";

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

{#await helloFetch}

loading...

{:then res}
{res.body.world}

{/await}

```

5 - If you have some api files like tree:

```
- src/routes/api
- hello.ts
- company
- user
- storage
```

`src/routes/api/index.d.ts` need like this:

```ts
import * as hello from "./hello";
import * as user from "./company/user";
import * as storage from "./company/storage";

// Export default object tree must equal svelte kit api fetch route
export default {
hello,
company: {
user,
storage,
},
};
```