2.1.46 • Published 1 year ago

svetch.ts v2.1.46

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

npm svetch-chan (1)

šŸš€ Svetch.ts: Zero-Effort client/types/schema/OpenAPI Schema/Docs generator for your API endpoints

Typesafety, Minus the typing

šŸ‘‰ Check it out @ (https://svetch-dev.vercel.app/) Effortlessly generate a typesafe Fetch client for your Svelte/Sveltekit applications, complete with automatic input/output zod validation and autogenerated types & API Docs.

What is this?

Svetch automatically scans your +server.ts files in /src/routes/api (or whatever directory you specify) and generates a typesafe Fetch client that has intellisense for path, query, body parameters & all the possible responses (and errors)

šŸ§™ā€ā™‚ļø Automatic Detection

  • ā“ Query Params => Detected using any declarations of url.searchParams.get
  • šŸ“‚ Path Params => Detected from the file directory
  • šŸ“¦ Payload Definition => inferred from const payload: X = await request.json or as X
  • šŸ’¬ Response Definition => inferred from any return statement with json(X) (sveltekit utility) or new Response(X)
  • šŸ“› Error Definitions => inferred from any throw statement with throw error() or throw new Error or return error(HTTP_CODE, error_message)

ā¬ How to run

$ npx svetch.ts@latest

Make sure you have a path alias for src in your svelte.config.js

import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://kit.svelte.dev/docs/integrations#preprocessors
	// for more information about preprocessors
	preprocess: vitePreprocess(),
	kit: {
		// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
		// If your environment is not supported or you settled on a specific environment, switch out the adapter.
		// See https://kit.svelte.dev/docs/adapters for more information about adapters.
		adapter: adapter(),
+		alias: {
+			"src": "./src",
		},
	}
};

export default config;

🌟 Advantages

  • šŸ˜ ALMOST no code changes required to your existing API code
  • šŸš€ Almost no learning curve, Augments a regular FETCH client with data and error along with the rest of the fetch client properties, you can use it just like a regular fetch client.
  • šŸ”Ž Intellisense for your api paths.
  • āœ… Typesafety for api inputs & outputs.
  • šŸ“š Generates an OpenAPI Schema for your API, alongside with Swagger docs.
  • šŸ“ƒ Can use JSDocs to add more info to the endpoint documentation (WIP)
  • šŸ¤– Handles multiple possible responses per http code.

Autogenerated Docs

The library will generate an OpenAPI compatible schema, alongside with swagger docs, by default in /docs/+page.svelte

āš™ Config

.svetchrc

{
  "framework": "sveltekit", // currently the only option
  "input": "src/routes/api", // the directory you want the generator to traverse
  "out": "src/lib/api", // the directory to output the types & the client to
  "docs": "src/routes/docs", // where to output docs
  "tsconfig": "tsconfig.json" // your tsconfig directory
}

šŸ”Ž Detection

  1. Svetch will traverse your input directory, will scan for any +server.ts with exported GET/POST/PUT/DELETE functions.
  2. For each endpoint it will detect...
    1. path parameters: based on the file name, e.g. user/[user_id].ts will have a path parameter of user_id
    2. query parameters: based on any parameters instantiated like url.searchParams.get('param')
    3. body parameters: based on the type of the payload Must be assigned to a const called payload ⚠ IMPORTANT
    4. response types: will pickup any top-level return statement that is instantiated like json(xxx) or new Response(xxx) along with status code

In client code

import { Svetch } from "src/lib/api/client"; // or wherever you chose to generate the client

const svetch = new Svetch({
  baseUrl: "/api", // default is '/api'
  fetch, // default is window.fetch, pass the global fetch to it in node, etc...
  validate: false, // default is false, uses Zod to validate payload + response (ON CLIENT THIS CAN MAKE THE IMPORT SIZE HUGE)
});

await svetch
  .post("user/[user_id]", {
    path: {
      user_id: 1,
    },
    body: {
      text: foodInput,
      journalId: $journal.id,
      today: new Date(),
    },
  })
  .then((response) => {
    if (response.error) throw new Error(response.error);
    food = response.data;
    loading = false;
  })
  .catch((error) => {
    throw new Error(error);
  });

In load functions

import { Svetch } from "src/lib/api/client"; // or wherever you chose to generate the client

const svetch = new Svetch({
  baseUrl: "/api", // default is '/api'
  fetch, // pass the load function's fetch to it
});

export async function load({ fetch, session }) {
  const user = await svetch.get("user").then((response) => {
    if (response.error) throw new Error(response.error);
    return response.data;
  });
  return {
    props: {
      user,
    },
  };
}

License

This library is Free for personal use, If it's useful to you, please consider purchasing a license @ https://petrasolutions.lemonsqueezy.com/checkout/buy/19210e05-ae3c-41a0-920c-324e3083618d Redistribution/Forking is Not Allowed.

2.1.39

1 year ago

2.1.45

1 year ago

2.1.46

1 year ago

2.1.43

1 year ago

2.1.44

1 year ago

2.1.41

1 year ago

2.1.42

1 year ago

2.1.40

1 year ago

2.0.28

1 year ago

2.0.29

1 year ago

2.0.37

1 year ago

2.0.3

1 year ago

2.0.38

1 year ago

2.0.2

1 year ago

2.0.35

1 year ago

2.0.5

1 year ago

2.0.36

1 year ago

2.0.4

1 year ago

2.0.33

1 year ago

2.0.7

1 year ago

2.0.34

1 year ago

2.0.6

1 year ago

2.0.31

1 year ago

2.0.9

1 year ago

2.0.32

1 year ago

2.0.8

1 year ago

2.0.30

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

2.1.9

1 year ago

2.0.39

1 year ago

2.1.16

1 year ago

2.0.48

1 year ago

2.1.17

1 year ago

2.0.49

1 year ago

2.1.14

1 year ago

2.0.46

1 year ago

2.1.15

1 year ago

2.0.47

1 year ago

2.1.12

1 year ago

2.0.44

1 year ago

2.1.13

1 year ago

2.0.45

1 year ago

2.1.10

1 year ago

2.0.42

1 year ago

2.1.11

1 year ago

2.0.43

1 year ago

2.0.40

1 year ago

2.0.41

1 year ago

2.1.18

1 year ago

2.1.19

1 year ago

2.1.27

1 year ago

2.0.59

1 year ago

2.1.28

1 year ago

2.1.25

1 year ago

2.0.57

1 year ago

2.1.26

1 year ago

2.0.58

1 year ago

2.1.23

1 year ago

2.0.55

1 year ago

2.1.24

1 year ago

2.0.56

1 year ago

2.1.21

1 year ago

2.0.53

1 year ago

2.1.22

1 year ago

2.0.54

1 year ago

2.0.51

1 year ago

2.1.20

1 year ago

2.0.52

1 year ago

2.0.50

1 year ago

2.1.29

1 year ago

2.1.36

1 year ago

2.0.68

1 year ago

2.1.37

1 year ago

2.0.69

1 year ago

2.1.34

1 year ago

2.0.66

1 year ago

2.1.35

1 year ago

2.0.67

1 year ago

2.1.32

1 year ago

2.0.64

1 year ago

2.1.33

1 year ago

2.0.65

1 year ago

2.1.30

1 year ago

2.0.62

1 year ago

2.1.31

1 year ago

2.0.63

1 year ago

2.0.60

1 year ago

2.0.61

1 year ago

2.0.75

1 year ago

2.0.73

1 year ago

2.0.74

1 year ago

2.0.71

1 year ago

2.0.72

1 year ago

2.0.70

1 year ago

2.1.2

1 year ago

2.0.15

1 year ago

2.1.1

1 year ago

2.0.16

1 year ago

2.1.4

1 year ago

2.0.13

1 year ago

2.1.3

1 year ago

2.0.14

1 year ago

2.1.6

1 year ago

2.0.11

1 year ago

2.1.5

1 year ago

2.0.12

1 year ago

2.1.8

1 year ago

2.1.7

1 year ago

2.0.10

1 year ago

2.1.0

1 year ago

2.0.19

1 year ago

2.0.17

1 year ago

2.0.18

1 year ago

2.0.26

1 year ago

2.0.27

1 year ago

2.0.24

1 year ago

2.0.25

1 year ago

2.0.22

1 year ago

2.0.23

1 year ago

2.0.20

1 year ago

2.0.21

1 year ago

1.0.48

2 years ago

1.0.49

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.11

2 years ago