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

@schematichq/schematic-typescript-node

v1.1.7

Published

## Installation and Setup

Downloads

1,830

Readme

schematic-typescript-node

Installation and Setup

  1. Install the TypeScript library using your package manager of choice:
npm install @schematichq/schematic-typescript-node
# or
yarn add @schematichq/schematic-typescript-node
# or
pnpm add @schematichq/schematic-typescript-node
  1. Issue an API key for the appropriate environment using the Schematic app. Be sure to capture the secret key when you issue the API key; you'll only see this key once, and this is what you'll use with schematic-typescript-node.

  2. Using this secret key, initialize a client in your application:

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({ apiKey });

// interactions with the client

client.close();

By default, the client will do some local caching for flag checks. If you would like to change this behavior, you can do so using an initialization option to specify the max size of the cache (in terms of number of records) and the max age of the cache (in milliseconds):

import { LocalCache, SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const cacheSize = 100;
const cacheTTL = 1000; // in milliseconds
const client = new SchematicClient({
    apiKey,
    cacheProviders: {
        flagChecks: [new LocalCache<boolean>({ size: cacheSize, ttl: cacheTTL })],
    },
});

// interactions with the client

client.close();

You can also disable local caching entirely with an initialization option; bear in mind that, in this case, every flag check will result in a network request:

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({
    apiKey,
    cacheProviders: {
        flagChecks: [],
    },
});

// interactions with the client

client.close();

You may want to specify default flag values for your application, which will be used if there is a service interruption or if the client is running in offline mode (see below). You can do this using an initialization option:

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({
    apiKey,
    flagDefaults: {
        "some-flag-key": true,
    },
});

// interactions with the client

client.close();

Usage examples

Sending identify events

Create or update users and companies using identify events.

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({ apiKey });

client.identify({
    company: {
        id: "your-company-id",
    },
    keys: {
        email: "[email protected]",
        userId: "your-user-id",
    },
    name: "Wile E. Coyote",
    traits: {
        city: "Atlanta",
        loginCount: 24,
        isStaff: false,
    },
});

client.close();

This call is non-blocking and there is no response to check.

Sending track events

Track activity in your application using track events; these events can later be used to produce metrics for targeting.

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({ apiKey });

client.track({
    event: "some-action",
    company: {
        id: "your-company-id",
    },
    user: {
        email: "[email protected]",
        userId: "your-user-id",
    },
});

client.close();

This call is non-blocking and there is no response to check.

Creating and updating companies

Although it is faster to create companies and users via identify events, if you need to handle a response, you can use the companies API to upsert companies. Because you use your own identifiers to identify companies, rather than a Schematic company ID, creating and updating companies are both done via the same upsert operation:

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({ apiKey });

const body = {
    keys: {
        id: "your-company-id",
    },
    name: "Acme Widgets, Inc.",
    traits: {
        city: "Atlanta",
        highScore: 25,
        isActive: true,
    },
};

client.companies
    .upsertCompany(body)
    .then((response) => {
        console.log(response.data);
    })
    .catch(console.error);

client.close();

You can define any number of company keys; these are used to address the company in the future, for example by updating the company's traits or checking a flag for the company.

You can also define any number of company traits; these can then be used as targeting parameters.

Creating and updating users

Similarly, you can upsert users using the Schematic API, as an alternative to using identify events. Because you use your own identifiers to identify users, rather than a Schematic user ID, creating and updating users are both done via the same upsert operation:

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({ apiKey });

const body = {
    keys: {
        email: "[email protected]",
        userId: "your-user-id",
    },
    company: { id: "your-company-id" },
    name: "Wile E. Coyote",
    traits: {
        city: "Atlanta",
        loginCount: 24,
        isStaff: false,
    },
};

client.companies
    .upsertUser(body)
    .then((response) => {
        console.log(response.data);
    })
    .catch(console.error);

client.close();

You can define any number of user keys; these are used to address the user in the future, for example by updating the user's traits or checking a flag for the user.

You can also define any number of user traits; these can then be used as targeting parameters.

Checking flags

When checking a flag, you'll provide keys for a company and/or keys for a user. You can also provide no keys at all, in which case you'll get the default value for the flag.

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const apiKey = process.env.SCHEMATIC_API_KEY;
const client = new SchematicClient({ apiKey });

const evaluationCtx = {
    company: { id: "your-company-id" },
    user: {
        email: "[email protected]",
        userId: "your-user-id",
    },
};

client
    .checkFlag(evaluationCtx, "some-flag-key")
    .then((isFlagOn) => {
        if (isFlagOn) {
            // Flag is on
        } else {
            // Flag is off
        }
    })
    .catch(console.error);

client.close();

Other API operations

The Schematic API supports many operations beyond these, accessible via the API modules on the client, Accounts, Billing, Companies, Entitlements, Events, Features, and Plans.

Testing

Offline mode

In development or testing environments, you may want to avoid making network requests to the Schematic API. You can run Schematic in offline mode by specifying the offline option; in this case, it does not matter what API key you specify:

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const client = new SchematicClient({ offline: true });

client.close();

Offline mode works well with flag defaults:

import { SchematicClient } from "@schematichq/schematic-typescript-node";

const client = new SchematicClient({
    flagDefaults: { "some-flag-key": true },
    offline: true,
});

// interactions with the client

client.close();