技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
apify-sdk-patterns【数据与研究】Production-ready patterns for Apify SDK and apify-client in TypeScript. Use when building Actors with Crawlee, managing datasets/KV stores, or implementing robust client wrappers with retry and validation. Trigger with "apify SDK patterns", "apify best practices", "apify client wrapper", "crawlee patterns", "idiomatic apify".
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: apify-sdk-patterns description: 'Production-ready patterns for Apify SDK and apify-client in TypeScript.
Use when building Actors with Crawlee, managing datasets/KV stores, or implementing robust client wrappers with retry and validation.
Trigger with "apify SDK patterns", "apify best practices", "apify client wrapper", "crawlee patterns", "idiomatic apify".
' allowed-tools: Read, Write, Edit version: 1.5.0 license: MIT author: Jeremy Longshore jeremy@intentsolutions.io tags:
Production patterns for both the apify SDK (building Actors) and apify-client (calling Actors remotely). Covers Crawlee crawler selection, data storage, proxy configuration, and typed client wrappers. This skill gives you the essential skeletons inline; the full eight-pattern catalog and two worked scenarios live in references/ for progressive drill-down.
apify-client for calling Actors remotely, or apify + crawlee for building Actors. Both can coexist in one project.APIFY_TOKEN in the environment — read it via process.env.APIFY_TOKEN, never hard-code it. This is the only credential these patterns require (Apify uses a personal API token, not OAuth).ts-node or a compiled build.Use the two skeletons below to start, then reach into the reference catalog for the pattern that matches your task.
Create one lazily-initialized, token-validated ApifyClient and reuse it everywhere. A resetClient() hook keeps it testable.
// src/apify/client.ts
import { ApifyClient } from 'apify-client';
let instance: ApifyClient | null = null;
export function getApifyClient(): ApifyClient {
if (!instance) {
const token = process.env.APIFY_TOKEN;
if (!token) throw new Error('APIFY_TOKEN is required');
instance = new ApifyClient({ token });
}
return instance;
}
// Reset for testing
export function resetClient(): void {
instance = null;
}
Choose the crawler that matches the page, not the other way around:
import { CheerioCrawler, PlaywrightCrawler, PuppeteerCrawler } from 'crawlee';
// CHEERIO — Fast, lightweight, no JavaScript rendering
// Use for: static HTML, server-rendered pages, APIs
// PLAYWRIGHT — Full browser, all engines, modern API
// Use for: SPAs, JavaScript-heavy pages, complex interactions
// PUPPETEER — Chromium-only browser automation
// Use for: when you need Chromium specifically or legacy Puppeteer code
The remaining six patterns are moved verbatim into patterns.md so this file stays scannable. Pick the one you need:
Actor.main() wrapping input validation, conditional proxy, and a failedRequestHandler.Result<T> around remote calls.See patterns.md for the complete code of all six.
Applying these patterns yields:
ApifyClient instance with fail-fast token validation.Result<T> — callers branch on error instead of catching exceptions.{ url, error, '#isFailed': true } rows rather than aborting the crawl.| Pattern | Use Case | Benefit |
|---|---|---|
Actor.main() | Actor entry point | Auto init/exit + error reporting |
failedRequestHandler | Per-request failures | Log failures without stopping crawl |
| Safe wrapper | External calls | Prevents uncaught exceptions |
| Router | Multi-page scrapes | Clean separation of page types |
| Proxy rotation | Anti-bot sites | Higher success rate |
Two runnable end-to-end scenarios live in examples.md:
apify/web-scraper and read its dataset without ever throwing.Apply these patterns in apify-core-workflow-a for a complete build-and-deploy web scraping workflow, or pair them with apify-common-errors when hardening an Actor for production.