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

@nmbrco/sdk

v0.6.4

Published

This is an SDK for Nmbr's APIs meant for internal use only.

Downloads

16

Readme

@nmbr/sdk [internal]

This is an SDK for Nmbr's APIs meant for internal use only.

Everything about it is subject to change. We do not officially support it for external use. If you would like to use it in your project, please reach out to the Nmbr team.

TODO

  • [ ] T-4012 Decouple SDK from API spec codegen

Clients

Nmbr Client [server-to-server only]

import { fetchWithCachedResponse } from "@example/fetch-compatible-lib";
import Nmbr, * as nmbr from "nmbr/server";

const nmbr = new Nmbr({
  apiKey: process.env.NMBR_API_KEY,
  apiSecret: process.env.NMBR_API_SECRET,
  // optional; defaults to `nmbr.utils.fetchJSON`
  fetcher: (path, init) =>
      fetchWithCachedResponse(path, init).then(nmbr.utils.expectJSONResponse),

  // [more optional parameters]:
  // apiHost = "https://staging.nmbr.co",
  // apiPath = "/services/payroll",
  // tokenStorage?: {
  //   getItem: (companyId: string) => Promise<Record<string, unknown>>;
  //   setItem: (companyId: string, token: Record<string, unknown>) => Promise<void>;
  // }
});

// example usage
const company = await nmbr.companies.create({ name: "Acme Burgers" });
const businessEntity = await nmbr.forCompany(company)
  .businessEntities.create({ name: "Acme Burgers" });
  • Implements core REST API [full API coming soon]
  • Accepts credentials (public & secret); optional API host & path; optional fetcher (default fetch) and optional token storage (default none)
  • Manages token re/issuance lifecycle for company-scoped resources
  • Supports addl partner-level resources, e.g. webhooks [coming soon]

Company Client [browser & server-to-server]

// example browser usage
// servers access via `nmbr.forCompany(companyOrCompanyId)`
import * as nmbr from "@nmbrco/sdk";
import { fetchWithCustomAuthHeaders } from "@example/fetch-compatible-lib";

const client = new nmbr.CompanyClient({
  baseUrl: "/api/nmbr",
  // optional; defaults to nmbr.utils.fetchJSON
  fetcher: fetchWithCustomAuthHeaders.then(nmbr.utils.expectJSONResponse),
  company: {
    id: window.authenticatedNmbrCompanyId, // required
    // businessEntities: [...],
    // employees: [...],
    // ...more initial data
  },
});

// example usage — subscribe to observable dbs
client.businessEntities.db$.subscribe((values) =>
  console.log("emitted new business entities", values)
);
client.businessEntities.payrolls.db$.subscribe((values) =>
  console.log("emitted new payrolls", values)
);

// example usage - `findAll` on an entity, with expands
client.businessEntities
  .findAll({ expand: ["payrolls.pay_stubs.line_items.recurrence"] })
  .then(console.log);

// expected console output:
// => [returned businessEntities]
// => "emitted new business entities", [returned businessEntities]
// => "emitted new payrolls", [returned payrolls, flattened]
  • Provides typesafe find, findAll, create, update, destroy for core company-scoped resources, e.g. employees, business entities, payrolls…
  • Maintains a local cache linked to related resources; updates mutate cache objects & emit on an observable
  • In server-to-server environments, fetcher provided automatically when accessed through Nmbr Client. In browser environments, fetcher should use a fetch authed to the host app

💡 For browser CompanyClients, we suggest setting up middleware on a generic path (e.g. api.myapp.com/nmbr/*) to authorize and proxy the request.

// the `@nmbrco/sdk/connect` interface provides helpers for express.js & other connect-based apps
import { initialize } from "@nmbrco/sdk/connect";
import express from "express";

const app = express();
const nmbr = initialize({
  apiKey: process.env.NMBR_API_KEY,
  apiSecret: process.env.NMBR_API_SECRET,
});

// authenticate & authorize the request; in this case on all `/api` requests.
// assume this throws an error, or otherwise sets `res.locals.authorizedUser`
app.use("/api", function authorizeMiddleware(_req, _res, next) {
  next();
});

// prepare a `res.locals.nmbr` client for the authorized request
app.use("/api/nmbr", (res) => {
  const { nmbrCompanyId } = res.locals.authorizedUser;

  res.locals.nmbr = nmbr.forCompany(nmbrCompanyId);
});

// override any routes before falling through to a proxy
// app.post("/api/nmbr/employments", myCustomHandler);
app.use(
  "/api/nmbr",
  nmbr.proxyWithCompanyClient((req, res) => res.locals.nmbr)
);

HTTP Client [internal; browser & server-to-server]

  • Used by Nmbr Client, Company Client, and Resource Clients [also elsewhere in the demo site]
  • Abstract class, extend it to provide typesafe get, post, put, delete methods
  • Constructed with a baseUrl and a generic fetcher: <ResponseType = Response>(path: string | URL, init?: RequestInit) => ResponseType
  • Request paths are resolved against baseUrl and converted to ResponseType

[fin]