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

@toebean/npc-vortex-api

v0.2.1

Published

A utility package for npc for Vortex.

Downloads

9

Readme

npc-vortex-api 🛠️

A utility package for npc for Vortex.

npm package version npm package downloads typedocs license

pnpm test publish package publish docs

github twitter GitHub Sponsors donation button PayPal donation button

Description

This package exposes a number of utility functions for calling the built-in procedures of npc for Vortex, as well as type information for working with the extension in TypeScript.

Table of contents

Installation

pnpm

pnpm add @toebean/npc-vortex-api

yarn

yarn add @toebean/npc-vortex-api

npm

npm i @toebean/npc-vortex-api

Usage

Registering npc procedures in a Vortex extension

import { join } from "path";
import { RegisterNpcApi } from "@toebean/npc-vortex-api";
import { types } from "vortex-api";
import { z } from "zod";

export default main(context: types.IExtensionContext) {
  // always call registerNpcApi from within context.once
  context.once(async () => {
    // apply type information to the registerNpcApi function
    const registerNpcApi: RegisterNpcApi = context.api.ext.registerNpcApi;

    // this is the function which we will expose as an npc procedure
    const callback = Math.sqrt;

    // the path at which we will expose the procedure
    // it is recommended to always namespace your procedures
    // by the name of your extension
    const path = join("myExtensionName", "getSquareRoot");

    // it is recommended to validate the input arguments being
    // sent to your procedure with middleware - in this case
    // we are using Zod to validate the input argument is a number
    const middleware = z.number().parse;

    // now register the npc procedure
    const endpoint = await registerNpcApi?.(path, callback, middleware);
    // endpoint = "vortex\\myExtensionName\\getSquareRoot"

    // the npc procedure will now be callable via npc
    // at the endpoint: vortex\myExtensionName\getSquareRoot

    if (!endpoint) {
        // if endpoint is undefined, this is likely because the user
        // does not have npc for Vortex installed
    }
  });
}
  • See https://zod.dev for more information on Zod.

Calling external npc procedures from a Vortex extension

import { join } from "path";
import { call } from "@toebean/npc";
import { types } from "vortex-api";
import { z } from "zod";

async function getSquareRoot(n: number) {
  const result = await call(join("myNodeApp", "getSquareRoot"), n);
  // we should validate the result matches our expectations,
  // for this purpose we recommend using Zod
  return z.number().parse(result);
}

const getSquareRoot = async (n: number) => {
}z.number().parse(await call(join("myNodeApp", "getSquareRoot"), n));

export default async function main(context: types.IExtensionContext) {
  try {
    const root = await getSquareRoot(64);
    // if the endpoint was available, the input value 64 was
    // acceptable by the procedure at the endpoint,
    // and the output value meets our own validation,
    // then root will now be the result of the externally defined
    // function `myNodeApp\getSquareRoot`: 8
  } catch (error) {
    // if the npc endpoint `myNodeApp\getSquareRoot` is not available,
    // or if our input arguments/output did not pass validation,
    // an error will be thrown, so we should handle that error here
  }
}
  • See npc for more information about the call function.
  • See https://zod.dev for more information on Zod.

Registering npc procedures in a separate Node.js application

import { join } from "path";
import { create } from "@toebean/npc";
import { z } from "zod";

// create an npc procedure which calculates the square root of a
// numeric input value, with middleware which will validate the
// input value is a number using Zod
const npc = create(Math.sqrt, z.number().parse);
await npc.listen(join("myNodeApp", "getSquareRoot"));

// the npc procedure is now available to be called at the endpoint:
// myNodeApp\getSquareRoot
  • See npc for more information about the create function.
  • See https://zod.dev for more information on Zod.

Calling Vortex npc procedures from a separate Node.js application

Registered by extensions

import { join } from "path";
import { call } from "@toebean/npc";
import { z } from "zod";

async function getSquareRoot(n: number) {
  const result = await call({
    endpoint: join("vortex", "myExtensionName", "getSquareRoot"),
    input: n,
  });
  // we should validate the result matches our expectations,
  // for this purpose we recommend using Zod
  return z.number().parse(result);
}

(async () => {
  try {
    const root = await getSquareRoot(64);
    // if the endpoint was available, the input value 64 was
    // acceptable by the procedure at the endpoint,
    // and the output value meets our own validation,
    // then root will now be the result of the externally defined
    // function `myNodeApp\getSquareRoot`: 8
  } catch (error) {
    // if the npc endpoint `myNodeApp\getSquareRoot` is not available,
    // or if our input arguments/output did not pass validation,
    // an error will be thrown, so we should handle that error here
  }
})();
  • See npc for more information about the call function.
  • See https://zod.dev for more information on Zod.

npc for Vortex built-ins

npc-vortex-api exposes helper methods to call the built-in npc procedures of npc for Vortex:

import { inspect } from "util";
import { getLatestUpdated } from "@toebean/npc-vortex-api/nexus";
import { getCurrentGame } from "@toebean/npc-vortex-api/vortex";

(async () => {
  try {
    const currentGame = await getCurrentGame();

    if (currentGame.id !== "__placeholder") {
      const latest = await getLatestUpdated({
        input: { gameId: currentGame.id },
      });
      console.log(inspect(latest, false, null, true));
      // prints a list of the latest updated mods on Nexus Mods
      // for the game which the user is currently managing in Vortex
    }
  } catch (error) {
    // handle any errors
  }
})();

All of the helper methods use Zod to validate the output of each function, so you do not need to do this yourself.

Complete listing of all helper methods:

Calling npc for Vortex built-ins from a Vortex extension

It is not necessary to use npc to call the npc for Vortex built-ins from a Vortex extension, as they are exposed via the Vortex extension API in the same manner as registerNpcApi:

import { NexusApi, VortexApi } from "@toebean/npc-vortex-api";
import { types } from "vortex-api";

export default main(context: types.IExtensionContext) {
  // always call Vortex extension API functions from within context.once
  context.once(async () => {
    const nexusApi: NexusApi = context.api.ext.createNexusApi?.();
    const vortexApi: VortexApi = context.api.ext.createVortexApi?.();

    // if vortexApi or nexusApi are undefined,
    // the npc for Vortex extension is likely not installed
    if (vortexApi && nexusApi) {
        try {
          const currentGame = await vortexApi.getCurrentGame();

          if (currentGame.id !== '__placeholder') {
              const latest = await nexus.getLatestUpdated({
                  input: { gameId: currentGame.id },
              });
              // latest now contains a list of the latest updated
              // mods from Nexus Mods for the game which the user
              // is currently managing in Vortex
          }
        } catch (error) {
          // handle any errors here
        }
      }
  });
}

All of the built-ins use Zod to validate the output of each function, so you do not need to do this yourself.

Complete listing of npc for Vortex built-ins:

License

npc-vortex-api is licensed under MIT © 2023 Tobey Blaber.