Skip to content

@skystate/core

@skystate/core is the framework-agnostic SkyState client library. It handles HTTP fetching, caching, pub/sub notification, and auto-registration of missing config keys.


Installation

bash
npm install @skystate/core

Public API

Classes

ExportDescription
ConfigStoreThe main client class. Fetches config, caches it, and notifies subscribers when values change.
AutoRegistrarBatches and debounces missing-key registrations in development mode. Used internally by ConfigStore.
HttpClientHandles HTTP fetch with Cache-Control scheduling and tab visibility re-fetching. Used internally by ConfigStore.
ConfigCacheIn-memory cache with structural sharing for stable object references. Used internally by ConfigStore.
PubSubEmitterPath-keyed subscription registry. Used internally by ConfigStore.
SkyStateErrorTyped error class with a code string and optional HTTP status.

Functions

ExportDescription
getOrCreateStore(options)Get or create a singleton ConfigStore for the given (apiUrl, accountSlug, projectSlug, environment) tuple.

Constants

ExportDescription
VALID_ENVIRONMENTS['development', 'staging', 'production'] — the allowed values for environment slugs.

Types

ExportDescription
Environment'development' | 'staging' | 'production'
ConfigStoreOptionsOptions passed to getOrCreateStore.
ConfigEnvelopeThe shape of a config response from the API: { version, lastModified, config }.
VersionSemantic version object: { major, minor, patch }.
SkyStateConfigBasic config identity: { apiUrl, accountSlug, projectSlug, environmentSlug }.
HttpClientOptionsOptions for the HttpClient constructor.
OnUpdateCallback(envelope: ConfigEnvelope) => void
OnErrorCallback(error: Error) => void

Sections

  • ConfigStore — initialization, reading values, and subscribing to changes
  • Environments — how build-time environment variables map to server environment slugs
  • Auto-registration — automatic key creation in development mode

Error Handling

All SDK errors are thrown as SkyStateError instances:

typescript
import { SkyStateError } from '@skystate/core';

try {
  const store = getOrCreateStore({ /* ... */ });
} catch (err) {
  if (err instanceof SkyStateError) {
    console.error(err.code);    // e.g. 'invalid_environment'
    console.error(err.message); // human-readable description
    console.error(err.status);  // HTTP status if applicable, otherwise undefined
  }
}

Error Codes

CodeWhen thrown
environment_resolution_failedresolveEnvironment() threw an exception
invalid_environmentresolveEnvironment() returned a value not in VALID_ENVIRONMENTS
fetch_failedThe HTTP GET for config returned a non-2xx status
patch_failedThe HTTP PATCH for auto-registration returned a non-2xx, non-409 status

Built with VitePress