newOS for Developers

Back to Overview

Client Configuration

TypeScript

API client setup, token management, and cache configuration.

packages/newgraph-signals/src/index.ts, actions/auth.ts

newgraphClientManager

Singleton API client for all Newgraph API calls. Manages authentication tokens and base URL configuration.

import { NewgraphApi } from "./clients/newgraph";
import { newgraphBaseUrl } from "./config";

export const newgraphClientManager = NewgraphApi();
export const newgraphClient = newgraphClientManager;

// Initialize with base URL on module load
newgraphClientManager.initialize(newgraphBaseUrl);

// Update auth token (called after login)
newgraphClientManager.updateToken(`newsafe ${jwt}`);

// Get current token
newgraphClient.getCurrentToken();

// Make API calls
await newgraphClient.api.user.currentList();
await newgraphClient.api.mood.moodList({ id: "..." });
MethodDescription
initialize(baseUrl)Set the API base URL
updateToken(token)Set authentication token (prefixed with "newsafe ")
getCurrentToken()Get currently set token
api.*Access to all API endpoints (user, mood, post, etc.)

SessionTokenResponse

JWT token wrapper used for authentication state management.

interface SessionTokenResponse {
  jwt?: string;  // JWT token (prefixed with "newsafe " when set)
}

// Token signal for reactive auth state
export const token: Signal<SessionTokenResponse> = signal({
  jwt: maybeToken(storedJwt)  // Initialized from localStorage
});

// Token sources (in priority order):
// 1. URL parameter "newsafe_jwt" 
// 2. URL parameter "token"
// 3. localStorage "newsafe-auth-token"
Token Format: Tokens must be prefixed with "newsafe " (with space) when passed to updateToken(). The helper maybeToken() handles this.

Cache Configuration

IndexedDB cache tables for offline support and performance.

type Tables = {
  post: DelayedTable<EnrichedPost, string>;       // Posts by ID
  user: DelayedTable<UserReadPublicResponse, string>;  // Users by ID
  poweredInUser: DelayedTable<UserReadPublicResponse, string>;  // Stakers
  poweredOutUser: DelayedTable<UserReadPublicResponse, string>; // Staked on
  folder: DelayedTable<EnrichedFolder, string>;   // Folders by ID
};

// Index definitions for querying
const indices = {
  post: "id,created,author.username",
  user: "id,created,username",
  poweredInUser: "id",
  poweredOutUser: "id",
  folder: "id,created,username,score"
};

// Create and export cache instance
export const cache = Store<Tables>("iosdk-cache", indices);
export const mainAppCache = cache;
TableTypeIndices
postEnrichedPostid, created, author.username
userUserReadPublicResponseid, created, username
folderEnrichedFolderid, created, username, score
poweredInUserUserReadPublicResponseid
poweredOutUserUserReadPublicResponseid

AppBoot State

Application initialization state signal. Tracks async setup for cache and auth.

export const appBoot = signal({
  ready: false,                    // True when all conditions met

  readyConditions: {
    dbOpened: false,               // IndexedDB cache opened
    authChecked: false             // Auth token validated
  },

  promise: undefined as Promise<any> | undefined,  // Boot promise
  error: undefined as Error | undefined,           // Boot error
  dbBlocked: false,                // IndexedDB blocked by other tabs

  startTime: Date.now()            // Boot start timestamp
});

// Usage: Wait for app to be ready
await appBoot.value.promise;
if (appBoot.value.ready) {
  // Safe to use cache and make authenticated API calls
}

Boot Sequence

  1. Check cache version, reset if deprecated
  2. Open IndexedDB cache → dbOpened: true
  3. Validate stored JWT token
  4. Fetch current user if token valid → authChecked: true
  5. Set ready: true when all conditions met
DB Blocked: If dbBlocked is true, another browser tab has an incompatible IndexedDB version open. Prompt user to close other tabs.

Cache Versioning

Automatic cache migration when the schema changes.

const CACHE_VERSION_STORE = "__iosdk_cache_version";

const DEPRECATED_CACHE_VERSIONS = [
  1210103023, 1030232100, 202306281530, 
  202306281550, 202408082144, 202501232204
];
const CACHE_VERSION = 202501232210;

// On boot: check stored version
// If deprecated or old → delete cache, set new version, reload
// This ensures clients always have compatible schema