Skill站外收录 @skilllib/skill-2014279426328965122
apollo-sdk-patterns
提供 Apollo.io API 集成的标准化实现方案,涵盖类型安全的客户端封装、带退避策略的自动重试、支持流式消费的分页迭代、批量请求协调机制,以及可区分捕获的自定义错误体系,适用于构建高可靠性、易维护的销售开发与数据 enrichment 应用。
软件开发 AI 技能
Skills.MD
# Apollo SDK Patterns
## Overview
Production-ready patterns for Apollo.io API integration with type safety, error handling, and retry logic.
## Prerequisites
- Completed `apollo-install-auth` setup
- Familiarity with async/await patterns
- Understanding of TypeScript generics
## Pattern 1: Type-Safe Client Singleton
```typescript
// src/lib/apollo/client.ts
import axios, { AxiosInstance, AxiosError } from 'axios';
import { z } from 'zod';
// Response schemas
const PersonSchema = z.object({
id: z.string(),
name: z.string(),
first_name: z.string().optional(),
last_name: z.string().optional(),
title: z.string().optional(),
email: z.string().email().optional(),
linkedin_url: z.string().url().optional(),
organization: z.object({
id: z.string(),
name: z.string(),
domain: z.string().optional(),
}).optional(),
});
const PeopleSearchResponseSchema = z.object({
people: z.array(PersonSchema),
pagination: z.object({
page: z.number(),
per_page: z.number(),
total_entries: z.number(),
total_pages: z.number(),
}),
});
export type Person = z.infer<typeof PersonSchema>;
export type PeopleSearchResponse = z.infer<typeof PeopleSearchResponseSchema>;
class ApolloClient {
private static instance: ApolloClient;
private client: AxiosInstance;
private constructor() {
this.client = axios.create({
baseURL: 'https://api.apollo.io/v1',
timeout: 30000,
headers: { 'Content-Type': 'application/json' },
params: { api_key: process.env.APOLLO_API_KEY },
});
this.setupInterceptors();
}
static getInstance(): ApolloClient {
if (!ApolloClient.instance) {
ApolloClient.instance = new ApolloClient();
}
return ApolloClient.instance;
}
private setupInterceptors() {
this.client.interceptors.response.use(
(response) => response,
this.handleError.bind(this)
);
}
private handleError(error: AxiosError) {
if (error.response?.status === 429) {
throw new ApolloRateLimitError('Rate limit exceeded');
}
if (error.response?.status === 401) {
throw new ApolloAuthError('Invalid API key');
}
throw error;
}
async searchPeople(params: PeopleSearchParams): Promise<PeopleSearchResponse> {
const { data } = await this.client.post('/people/search', params);
return PeopleSearchResponseSchema.parse(data);
}
}
export const apollo = ApolloClient.getInstance();
```
## Pattern 2: Retry with Exponential Backoff
```typescript
// src/lib/apollo/retry.ts
interface RetryConfig {
maxRetries: number;
baseDelay: number;
maxDelay: number;
}
const defaultConfig: RetryConfig = {
maxRetries: 3,
baseDelay: 1000,
maxDelay: 30000,
};
export async function withRetry<T>(
fn: () => Promise<T>,
config: Partial<RetryConfig> = {}
): Promise<T> {
const { maxRetries, baseDelay, maxDelay } = { ...defaultConfig, ...config };
let lastError: Error;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error as Error;
if (error instanceof ApolloAuthError) {
throw error; // Don't retry auth errors
}
if (attempt < maxRetries) {
const delay = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}
throw lastError!;
}
// Usage
const people = await withRetry(() => apollo.searchPeople({ domain: 'stripe.com' }));
```
## Pattern 3: Paginated Iterator
```typescript
// src/lib/apollo/pagination.ts
export async function* paginateSearch(
searchFn: (page: number) => Promise<PeopleSearchResponse>,
options: { maxPages?: number } = {}
): AsyncGenerator<Person[], void, unknown> {
const maxPages = options.maxPages || Infinity;
let page = 1;
let totalPages = 1;
while (page <= Math.min(totalPages, maxPages)) {
const response = await searchFn(page);
totalPages = response.pagination.total_pages;
yield response.people;
page++;
// Respect rate limits
await new Promise((resolve) => setTimeout(resolve, 100));
}
}
// Usage
async function getAllPeople(domain: string): Promise<Person[]> {
const allPeople: Person[] = [];
for await (const batch of paginateSearch(
(page) => apollo.searchPeople({ q_organization_domains: [domain], page, per_page: 100 })
)) {
allPeople.push(...batch);
}
return allPeople;
}
```
## Pattern 4: Request Batching
```typescript
// src/lib/apollo/batch.ts
class ApolloBatcher {
private queue: Array<{ domain: string; resolve: Function; reject: Function }> = [];
private timeout: NodeJS.Timeout | null = null;
private readonly batchSize = 10;
private readonly batchDelay = 100;
async enrichCompany(domain: string): Promise<Organization> {
return new Promise((resolve, reject) => {
this.queue.push({ domain, resolve, reject });
this.scheduleBatch();
});
}
private scheduleBatch() {
if (this.timeout) return;
this.timeout = setTimeout(async () => {
this.timeout = null;
const batch = this.queue.splice(0, this.batchSize);
try {
// Apollo doesn't have batch endpoint, process sequentially with rate limiting
for (const item of batch) {
try {
const result = await apollo.enrichOrganization(item.domain);
item.resolve(result);
} catch (error) {
item.reject(error);
}
await new Promise((r) => setTimeout(r, 50)); // Rate limit spacing
}
} catch (error) {
batch.forEach((item) => item.reject(error));
}
if (this.queue.length > 0) {
this.scheduleBatch();
}
}, this.batchDelay);
}
}
export const apolloBatcher = new ApolloBatcher();
```
## Pattern 5: Custom Error Classes
```typescript
// src/lib/apollo/errors.ts
export class ApolloError extends Error {
constructor(message: string, public readonly code?: string) {
super(message);
this.name = 'ApolloError';
}
}
export class ApolloRateLimitError extends ApolloError {
constructor(message: string = 'Rate limit exceeded') {
super(message, 'RATE_LIMIT');
this.name = 'ApolloRateLimitError';
}
}
export class ApolloAuthError extends ApolloError {
constructor(message: string = 'Authentication failed') {
super(message, 'AUTH_ERROR');
this.name = 'ApolloAuthError';
}
}
export class ApolloValidationError extends ApolloError {
constructor(message: string, public readonly details?: unknown) {
super(message, 'VALIDATION_ERROR');
this.name = 'ApolloValidationError';
}
}
```
## Output
- Type-safe client singleton with Zod validation
- Robust error handling with custom error classes
- Automatic retry with exponential backoff
- Async pagination iterator
- Request batching for bulk operations
## Error Handling
| Pattern | When to Use |
|---------|-------------|
| Singleton | Always - ensures single client instance |
| Retry | Network errors, 429/500 responses |
| Pagination | Large result sets (>100 records) |
| Batching | Multiple enrichment calls |
| Custom Errors | Distinguish error types in catch blocks |
## Resources
- [Zod Documentation](https://zod.dev/)
- [Axios Interceptors](https://axios-http.com/docs/interceptors)
- [TypeScript Generics](https://www.typescriptlang.org/docs/handbook/2/generics.html)
## Next Steps
Proceed to `apollo-core-workflow-a` for lead search implementation.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「apollo-sdk-patterns」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次