npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-cactus

v1.0.82

Published

Create Typescript types from Open API Specification 3 (OAS3)

Downloads

1,738

Readme

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.