0.0.47 • Published 1 month ago

@sociogram-dev/mini-apps-sdk v0.0.47

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Sociogram Mini Apps SDK

A TypeScript SDK for creating mini apps that integrate with Sociogram.

Installation

npm install @sociogram-dev/mini-apps-sdk
# or
yarn add @sociogram-dev/mini-apps-sdk
# or
pnpm add @sociogram-dev/mini-apps-sdk

TypeScript Support

This SDK is written in TypeScript and provides full type definitions. You can use it in your TypeScript projects with full type safety and autocompletion.

Usage

Basic Setup

import { Sociogram } from '@sociogram-dev/mini-apps-sdk';

// The SDK is automatically initialized and available globally
const miniApp = Sociogram.MiniApp;

// Access initial data
console.log(miniApp.initData);

// Get SDK version
console.log(miniApp.version);

User Interactions

// Get user's followers
const followersRequestId = miniApp.getFollowers(
  { limit: 10, cursor: 'next_page_cursor' },
  (response) => {
    if (response.error) {
      console.error(response.error);
      return;
    }
    console.log('Followers:', response.rows);
    console.log('Next cursor:', response.cursor);
  }
);

// Get users that a user is following
const followingRequestId = miniApp.getFollowing(
  { limit: 10 },
  (response) => {
    if (response.error) {
      console.error(response.error);
      return;
    }
    console.log('Following:', response.rows);
  }
);

// Get user's friends
const friendsRequestId = miniApp.getFriends(
  { limit: 10 },
  (response) => {
    if (response.error) {
      console.error(response.error);
      return;
    }
    console.log('Friends:', response.rows);
  }
);

// Follow a user
miniApp.followUser('user_address');

Post Actions

// Like a post
miniApp.openLikeModal({
  postId: 'post_id'
});

// Tip a post
miniApp.openTipModal({
  postId: 'post_id'
});

// Reward a post
miniApp.openRewardModal({
  postId: 'post_id'
});

Navigation and Sharing

// Open a link in the browser
miniApp.openLink('https://example.com', {
  // Optional parameters
  target: '_blank'
});

// Share content
miniApp.share({
  text: 'Check this out!',
  url: 'https://example.com'
});

Clipboard Operations

// Read text from clipboard
miniApp.readTextFromClipboard('text_to_read');

Invoice Handling

// Open an invoice
const invoiceId = miniApp.openInvoice(
  {
    title: 'Purchase Item',
    price: 10,
    currency: 'usd', // or 'sol'
    invoicePayload: {
      // Your custom payload data
      itemId: '123',
      description: 'Premium subscription'
    }
  },
  (status) => {
    if (status === 'success') {
      console.log('Payment successful!');
    } else {
      console.log('Payment failed');
    }
  }
);

WebView Communication

// Access WebView API directly
const webView = Sociogram.WebView;

// Post an event
webView.postEvent('custom_event', { data: 'value' });

// Listen for events
webView.onEvent('custom_event', (eventType, eventData) => {
  console.log(`Received ${eventType}:`, eventData);
});

// Remove event listener
webView.offEvent('custom_event', callback);

// Check if running in iframe
const isIframe = webView.isIframe;

// Access initialization parameters
const initParams = webView.initParams;

Utility Functions

const { urlSafeDecode, urlParseQueryString, safeParseUrlParams } = Sociogram.Utils;

// Decode URL-safe strings
const decoded = urlSafeDecode('encoded_string');

// Parse query string
const params = urlParseQueryString('?key=value');

// Safely parse URL parameters
const safeParams = safeParseUrlParams();

Type Definitions

The SDK provides comprehensive TypeScript type definitions. Here are the main types:

User Interface

interface User {
  _id: string;
  createdAt: string;
  id: string;
  address: string;
  domain: string | null;
  name: string;
  avatar: string;
  verified: boolean;
  subscription: {
    following: number;
    followers: number;
  };
  twitter: {
    twitterId: string | null;
    name: string | null;
    followers: number;
  };
  isFollowed: boolean;
}

API Response Types

interface UsersResponse {
  cursor: string | null;
  rows: User[];
  error?: string;
}

interface PostActionData {
  postId: string;
}

interface InvoiceData {
  invoicePayload: Record<string, unknown>;
  title: string;
  price: number;
  currency: CurrencyType;
}

enum CurrencyType {
  USD = 'usd',
  SOL = 'sol'
}

Event Handling

type EventType = string;
type EventData = unknown;
type EventCallback = (eventType: EventType, eventData: EventData) => void;

// Register event handler
Sociogram.WebView.onEvent('event_type', (type, data) => {
  console.log(`Event ${type}:`, data);
});

// Remove event handler
Sociogram.WebView.offEvent('event_type', callback);

Environment Detection

The SDK automatically detects the environment it's running in:

  • iframe: When running inside an iframe
  • react-native: When running in a React Native WebView
  • web: When running in a regular web browser
const isIframe = Sociogram.WebView.isIframe;

Error Handling

The SDK provides error handling through callbacks and response objects:

// Error handling in user list responses
miniApp.getFollowers({}, (response) => {
  if (response.error) {
    console.error('Error fetching followers:', response.error);
    return;
  }
  // Handle successful response
});

// Error handling in invoice callbacks
miniApp.openInvoice(invoiceData, (status) => {
  if (status === 'failed') {
    console.error('Payment failed');
    return;
  }
  // Handle successful payment
});

Development

Building

# Install dependencies
yarn install

# Build the SDK
yarn build

# Watch mode for development
yarn build:watch

Testing

# Run tests
yarn test

# Watch mode for tests
yarn test:watch

License

MIT

0.0.47

1 month ago

0.0.46

1 month ago

0.0.45

2 months ago

0.0.44

2 months ago

0.0.43

2 months ago

0.0.42

2 months ago

0.0.41

2 months ago

0.0.40

2 months ago

0.0.39

2 months ago

0.0.38

2 months ago

0.0.37

2 months ago

0.0.36

2 months ago

0.0.35

2 months ago

0.0.34

2 months ago

0.0.33

2 months ago

0.0.32

2 months ago

0.0.31

2 months ago

0.0.30

2 months ago

0.0.29

2 months ago

0.0.28

2 months ago

0.0.27

2 months ago

0.0.26

2 months ago

0.0.25

2 months ago

0.0.24

2 months ago

0.0.23

2 months ago

0.0.22

2 months ago

0.0.21

2 months ago

0.0.20

2 months ago

0.0.18

3 months ago

0.0.17

3 months ago

0.0.16

3 months ago

0.0.15

3 months ago

0.0.13

3 months ago

0.0.12

3 months ago

0.0.11

3 months ago

0.0.10

3 months ago

0.0.9

3 months ago

0.0.8

3 months ago

0.0.7

3 months ago

0.0.6

3 months ago

0.0.5

3 months ago

0.0.4

3 months ago

0.0.2

3 months ago

0.0.1

3 months ago