{"library":"pear-api","title":"Pear API Base Class (Holepunch)","description":"The `pear-api` package provides the foundational base class for interacting with the Pear API, which is part of the Holepunch peer-to-peer (P2P) runtime and development platform. Currently at version 1.30.0, this library enables developers to build P2P applications, particularly focusing on User Interface integrations by abstracting complex underlying P2P mechanisms. The Pear ecosystem is actively maintained, with regular updates to its 1.x branch and a significant transition underway to a version 2, which introduces notable breaking changes and a shift towards modern JavaScript module standards. Key differentiators include its focus on enabling local-first, offline-first P2P applications and its design for extensibility within UI environments like Electron.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install pear-api"],"cli":null},"imports":["import { PearAPI } from 'pear-api';","import { Config } from 'pear-api';","import { PearError } from 'pear-api';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { PearAPI } from 'pear-api';\n\ninterface MyApiOptions {\n  authToken: string;\n  appId: string;\n}\n\ninterface MyP2PData {\n  id: string;\n  message: string;\n  timestamp: number;\n}\n\nclass MyPearAppAPI extends PearAPI {\n  private token: string;\n  private appId: string;\n\n  constructor(options: MyApiOptions) {\n    super(); // Initialize the base PearAPI class\n    this.token = options.authToken;\n    this.appId = options.appId;\n    console.log(`MyPearAppAPI initialized for app: ${this.appId}`);\n  }\n\n  async connect(): Promise<boolean> {\n    console.log('Attempting to connect to Pear network...');\n    // Simulate P2P network connection logic\n    await new Promise(resolve => setTimeout(resolve, 1000));\n    if (!this.token) {\n      console.error('Authentication token is missing.');\n      return false;\n    }\n    console.log('Successfully connected to Pear network.');\n    return true;\n  }\n\n  async publishData(data: MyP2PData): Promise<void> {\n    if (!(await this.connect())) {\n      throw new Error('Failed to connect, cannot publish data.');\n    }\n    console.log(`Publishing data [${data.id}]: ${data.message} at ${new Date(data.timestamp).toISOString()}`);\n    // Simulate data publishing to the P2P network\n    await new Promise(resolve => setTimeout(resolve, 500));\n    console.log('Data published.');\n  }\n\n  static async createAndRun(token: string): Promise<MyPearAppAPI> {\n    const api = new MyPearAppAPI({ authToken: token, appId: 'my-p2p-app' });\n    const connected = await api.connect();\n    if (connected) {\n      await api.publishData({ id: 'msg-1', message: 'Hello P2P world!', timestamp: Date.now() });\n    }\n    return api;\n  }\n}\n\n// Example usage with a dummy token\nMyPearAppAPI.createAndRun(process.env.PEAR_AUTH_TOKEN ?? 'dummy_auth_token_123')\n  .then(() => console.log('Pear application flow completed.'))\n  .catch(error => console.error('Pear application error:', error.message));","lang":"typescript","description":"This quickstart demonstrates how to extend the `PearAPI` base class to create a custom P2P application API, handle connection, and publish data, including error handling and simulated asynchronous operations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}