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

salt-net-api

v1.4.1

Published

Salt Net API is a TypeScript client for SaltStack to add essential type checking and validation, as well as make working the somewhat convoluted data structures of SaltStack much easier.

Downloads

21

Readme

Salt Net API

Salt Net API is a TypeScript client for SaltStack to add essential type checking and validation, as well as make working the somewhat convoluted data structures of SaltStack much easier.

This library heavily utilizes types to provide a clean interface for interacting with minimal logical code to manage.

Missing Types

SaltStack has an extremely expansive amount of functions and modules available. Initially, I'm mostly adding them as I need manually and thus providing a way for you to easily extend types that are missing.

I welcome PRs to add more typings around more functions.

Eventually when things look more solid I might generate these automatically from the Salt API documentation.

Using This Client

This client provides a few abstractions around the matching Salt clients.

import { LocalClient, WheelClient, Core, Modules, Runner } from "salt-net-api";

// Create a connection to the Local salt client
export const local = new LocalClient({
  endpoint: process.env.SALT_ENDPOINT!,
  username: process.env.SALT_USERNAME!,
  password: process.env.SALT_PASSWORD!,
  eauth: "file",
});

// Define the request type and the expected response type.
const a2 = await local.exec<Modules.Test.IPingRequest, Modules.Test.IPingResult>({
  fun: "test.ping",
  tgt: "test-site",
});

// You can also execute against the async client where available
const asyncResponse: Core.ILocalAsyncResponse = await local.execAsync<Modules.Service.IRestartRequest>({
  fun: "service.restart",
  kwarg: {
    // The type inference will validate that you are passing this required argument
    name: "service-name",
  },
});

// Other clients are also available
export const wheel = new WheelClient({
  endpoint: process.env.SALT_ENDPOINT!,
  username: process.env.SALT_USERNAME!,
  password: process.env.SALT_PASSWORD!,
  eauth: "file",
});

// This also works for other clients
const a1 = await wheel.exec<Runner.Key.IListRequest, Runner.Key.IListResponse>({
  fun: "key.list",
  match: "pre",
});

// If a type is missing you can easily specify it yourself
interface INginxStatusRequest extends Core.ILocalRequest {
  kwarg: {
    url: string;
  }
}
interface INginxStatusResponse {
  [key: string]: {
    connections: number;
    status: boolean;
  }
}

const customTypeResponse = await local.exec<INginxStatusRequest, INginxStatusResponse>({
  fun: "nginx.status",
  kwarg: {
    url: "http://localhost",
  }
});

Events Client

The SaltStack Event Bus is an incredibly useful tool for building event driven automation and functionality from. The events client allows you to easily leverage this functionality.

import { EventsClient } from 'salt-net-api';

const client = new EventsClient({
  endpoint: process.env.SALT_ENDPOINT!,
  username: process.env.SALT_USERNAME!,
  password: process.env.SALT_PASSWORD!,
  eauth: "file",
});

client.subscribe({
  tag: "salt/auth",
  handler: (event: any) => {
    console.log(event);
  }
});

await client.start();

Why

I wanted to create a way to easily interact with the SaltStack Net API while also taking advantage of TypeScript's ability to type things to make it have access to required argument validation and auto completion. The SaltStack API has a massive amount of formatting and data differences between modules and even within a single module.

Originally I started by implementing functions within each client type for every module but it very quickly became far too much essentially boiler plate code that was just really wrapping types and executing a simple request.

Eventually I deprecated that version and decided to almost entirely rely on TypeScript's ability to type check and generics. This minimizes the amount of code that actually needs to be unit tested, thus making everything much simpler. It also makes it easy for people to easy create their own types for modules that may be missing, or custom modules.