1.0.81 • Published 1 day ago

ts-cactus v1.0.81

Weekly downloads
-
License
MIT
Repository
-
Last release
1 day ago

ts-cactus

Create Typescript types from Open API Specification 3 (OAS3)

Features:

  • ✅ Convert OAS3 to Typescript
  • ✅ Typesafe API
  • ✅ Svelte Adapter
  • ✅ Works in any client ecosystem
  • ✅ Lightweight
  • ✅ IDE Autocompletion

Quick Start

Install ts-cactus 👇

bun add ts-cactus

Add to your package.json 👇 Make sure the "lib" folder exists, if not, create it.

{
  "name": "client-site",
  "private": true,
  "type": "module",
  "scripts": {
    "gen-users": "bun run ./node_modules/.bin/ts-cactus --url OAS3_JSON_URL --to ./src/lib/usersSchema.ts",
    "gen-accounts": "bun run ./node_modules/.bin/ts-cactus --url OAS3_JSON_URL --to ./src/lib/accountsSchema.ts",
    "gen-all": "bun run gen-users && bun run gen-accounts"
  }
}

Generate two schemas 👇

bun run gen-all

Create services.ts file in any place of the src directory 👇

import { createApi } from "ts-cactus";
import { apiSchema as usersSchema } from "./usersSchema.ts";
import { apiSchema as accountsSchema } from "./accountsSchema.ts";

const users = createApi("https://domain.com", usersSchema);
const accounts = createApi("https://domain.com", accountsSchema);

export const services = { users, accounts };

In your api.ts 👇

import { services } from "./services.ts";

const getUsers = async () => {
  const [status, res] = await services.users.get.users({
    page: 1,
    perPage: 20,
  });

  if (status) {
    console.log(res); // response
  } else {
    console.log(res); // error
  }
};

Svelte

Usage, here is an example component with an adapter for svelte, but you can use any other framework 👇

<script lang="ts">
  import { load } from "ts-cactus/svelte";
  import { services } from "./lib/services.ts";

  let params = {
    page: 1,
  };

  const getAccounts = async () => {
    return await services.accounts.get.adminAccounts(params);
  };

  let accounts = load(getAccounts);
</script>

<button
  on:click="{() => {
    $accounts.res = undefined;
    $accounts.reload();
  }}">Reload</button
>

<button
  on:click="{() => {
    params.page += 1;
    $accounts.reload();
  }}">Next</button
>

{#if $accounts?.loading}
  Loading
{/if}

{#each $accounts?.res?.data || [] as account}
  <div>
    <span>{account.clientName}</span>
  </div>
{/each}

Adapter - load function is needed just for convenient data getting, it’s not required to use this function. However, if you use load, you must set the following option in your tsconfig.json: "moduleResolution": "nodenext".

You can also add events for more control 👇

const users = createApi("https://domain.com", usersSchema, {
  beforeRequest: async (request) => {
    console.log(request.headers.get("header"));
    // set request headers: req.headers.append("xxx", "value")
  },
  afterRequest: async (request, response) => {
    // logic
  },
});

Need to abort the request? Easy! 👇

const getAccounts = async () => {
  const controller = new AbortController();
  const signal = controller.signal;

  window.setTimeout(() => {
    controller.abort();
  }, 2000);

  return await services.accounts.get.adminAccounts(params, {
    signal,
  });
};

Any method has options where you can pass a signal.

Upload File 👇

const doc1 = await fetch("doc.pdf");
const doc2 = new File([], "test");

// uploadPassport - multipart/form-data
await services.accounts.post.uploadPassport({
  file: await doc1.blob(), // good type
});

// uploadPassport - multipart/form-data
await services.accounts.post.uploadPassport({
  file: doc2, // good type
});

You don't need to wrap in try/catch 👇

const createUser = async () => {
	const [status, res] = await services.users.post.createUser({...});

	console.log(status, res);
}

Author

Vladislav Yemelyanov

Released under the MIT License.

1.0.81

1 day ago

1.0.73

24 days ago

1.0.72

24 days ago

1.0.71

24 days ago

1.0.77

24 days ago

1.0.76

24 days ago

1.0.75

24 days ago

1.0.74

24 days ago

1.0.79

24 days ago

1.0.78

24 days ago

1.0.62

9 months ago

1.0.61

10 months ago

1.0.60

10 months ago

1.0.65

9 months ago

1.0.64

9 months ago

1.0.69

8 months ago

1.0.68

9 months ago

1.0.67

9 months ago

1.0.70

8 months ago

1.0.39

12 months ago

1.0.40

12 months ago

1.0.44

11 months ago

1.0.43

11 months ago

1.0.42

12 months ago

1.0.41

12 months ago

1.0.48

11 months ago

1.0.47

11 months ago

1.0.46

11 months ago

1.0.45

11 months ago

1.0.49

11 months ago

1.0.51

10 months ago

1.0.50

10 months ago

1.0.55

10 months ago

1.0.54

10 months ago

1.0.53

10 months ago

1.0.52

10 months ago

1.0.59

10 months ago

1.0.58

10 months ago

1.0.57

10 months ago

1.0.56

10 months ago

1.0.38

12 months ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago