gamma-sdk-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Gamma SDK Patterns

Gamma SDK 模式

Overview

概述

Learn idiomatic patterns and best practices for the Gamma SDK to build robust presentation automation.
学习Gamma SDK的惯用模式和最佳实践,以构建稳健的演示自动化系统。

Prerequisites

前提条件

  • Completed
    gamma-local-dev-loop
    setup
  • Familiarity with async/await patterns
  • TypeScript recommended
  • 已完成
    gamma-local-dev-loop
    设置
  • 熟悉async/await模式
  • 推荐使用TypeScript

Instructions

说明

Pattern 1: Client Singleton

模式1:客户端单例

typescript
// 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,
      retries: 3,
    });
  }
  return client;
}
typescript
// 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,
      retries: 3,
    });
  }
  return client;
}

Pattern 2: Presentation Builder

模式2:演示文稿构建器

typescript
// 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,
    });
  }
}
typescript
// 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,
    });
  }
}

Pattern 3: Error Handling Wrapper

模式3:错误处理包装器

typescript
// 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;
  }
}
typescript
// 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;
  }
}

Pattern 4: Template Factory

模式4:模板工厂

typescript
// 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 };
}
typescript
// 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

错误处理

PatternUse CaseBenefit
SingletonMultiple modulesConsistent config
BuilderComplex presentationsReadable code
Safe CallError boundariesGraceful failures
FactoryRepeated templatesDRY code
模式使用场景优势
单例多模块场景配置一致
构建器复杂演示文稿代码可读性高
安全调用错误边界处理优雅容错
工厂重复使用模板遵循DRY原则,代码简洁

Resources

资源

Next Steps

后续步骤

Proceed to
gamma-core-workflow-a
for presentation generation workflows.
继续学习
gamma-core-workflow-a
以了解演示文稿生成工作流。