技能 编程开发 Gamma SDK 模式指南

Gamma SDK 模式指南

v20260313
gamma-sdk-patterns
涵盖 Gamma SDK 的单例客户端、构建器、错误处理和模板工厂等模式,帮助构建稳定的异步演示流程。
获取技能
433 次下载
概览

Gamma SDK Patterns

Overview

Learn idiomatic patterns and best practices for the Gamma SDK to build robust presentation automation.

Prerequisites

  • Completed gamma-local-dev-loop setup
  • Familiarity with async/await patterns
  • TypeScript recommended

Instructions

Step 1: Client Singleton

// lib/gamma.ts
import { GammaClient } from '@gamma/sdk';

let client: GammaClient | null = null;

export function getGammaClient(): GammaClient {
  if (!client) {
    client = new GammaClient({
      apiKey: process.env.GAMMA_API_KEY,
      timeout: 30000,  # 30000: 30 seconds in ms
      retries: 3,
    });
  }
  return client;
}

Step 2: Presentation Builder

// lib/presentation-builder.ts
import { getGammaClient } from './gamma';

interface SlideContent {
  title: string;
  content: string;
  layout?: 'title' | 'content' | 'image' | 'split';
}

export class PresentationBuilder {
  private slides: SlideContent[] = [];
  private title: string = '';
  private style: string = 'professional';

  setTitle(title: string): this {
    this.title = title;
    return this;
  }

  addSlide(slide: SlideContent): this {
    this.slides.push(slide);
    return this;
  }

  setStyle(style: string): this {
    this.style = style;
    return this;
  }

  async build() {
    const gamma = getGammaClient();
    return gamma.presentations.create({
      title: this.title,
      slides: this.slides,
      style: this.style,
    });
  }
}

Step 3: Error Handling Wrapper

// lib/safe-gamma.ts
import { GammaError } from '@gamma/sdk';

export async function safeGammaCall<T>(
  fn: () => Promise<T>
): Promise<{ data: T; error: null } | { data: null; error: string }> {
  try {
    const data = await fn();
    return { data, error: null };
  } catch (err) {
    if (err instanceof GammaError) {
      return { data: null, error: err.message };
    }
    throw err;
  }
}

Step 4: Template Factory

// lib/templates.ts
type TemplateType = 'pitch-deck' | 'report' | 'tutorial' | 'proposal';

const TEMPLATES: Record<TemplateType, object> = {
  'pitch-deck': { slides: 10, style: 'bold' },
  'report': { slides: 15, style: 'professional' },
  'tutorial': { slides: 8, style: 'friendly' },
  'proposal': { slides: 12, style: 'corporate' },
};

export function fromTemplate(type: TemplateType, title: string) {
  return { ...TEMPLATES[type], title };
}

Output

  • Reusable client singleton
  • Fluent builder pattern
  • Type-safe error handling
  • Template factory system

Error Handling

Pattern Use Case Benefit
Singleton Multiple modules Consistent config
Builder Complex presentations Readable code
Safe Call Error boundaries Graceful failures
Factory Repeated templates DRY code

Resources

Next Steps

Proceed to gamma-core-workflow-a for presentation generation workflows.

Examples

Basic usage: Apply gamma sdk patterns to a standard project setup with default configuration options.

Advanced scenario: Customize gamma sdk patterns for production environments with multiple constraints and team-specific requirements.

信息
Category 编程开发
Name gamma-sdk-patterns
版本 v20260313
大小 3.76KB
更新时间 2026-03-14
语言