Effect, 무료 TypeScript 라이브러리 — TS용 누락된 표준 라이브러리
Source: Dev.to
문제: TypeScript에 표준 라이브러리가 없음
Python은 os, json, datetime, collections, itertools 같은 모듈을 기본으로 제공합니다.
Go는 net/http, encoding/json, fmt 같은 패키지를 제공합니다.
반면 TypeScript는 npm 생태계에 의존합니다:
- 재시도가 필요합니까? 패키지를 설치하세요.
- 스키마 검증이 필요합니까? 또 다른 패키지를 설치하세요.
- 적절한 오류 처리가 필요합니까? 또 하나의 패키지를 설치하세요.
- 동시성 제어가 필요합니까? 또 하나의 패키지를 설치하세요.
Effect: 포괄적인 TypeScript 라이브러리
Effect는 언어에 부족한 표준 라이브러리를 제공하는 무료 TypeScript 라이브러리입니다. 다음을 다룹니다:
- 오류 (완전한 타입 안전성 포함)
- 동시성 및 병렬성
- 재시도 및 백오프 전략
- 스트리밍
- 의존성 주입
- 그리고 더 많은 기능…
오류가 타입의 일부가 된다
// In regular TypeScript, errors are invisible.
async function getUser(id: string): Promise {
const res = await fetch(`/api/users/${id}`) // NetworkError?
const data = await res.json() // ParseError?
return UserSchema.parse(data) // ValidationError?
}// With Effect, errors are part of the type signature.
import { Effect } from 'effect'
const getUser = (id: string): Effect.Effect => { /* implementation */ }TypeScript가 오류 처리를 강제한다
const result = getUser('123').pipe(
Effect.catchTag('NetworkError', () => fallbackUser),
Effect.catchTag('ParseError', () => Effect.fail(new AppError()))
)예시
지수 백오프를 이용한 재시도
import { Effect, Schedule } from 'effect'
const fetchWithRetry = fetchData.pipe(
Effect.retry(
Schedule.exponential('1 second').pipe(
Schedule.compose(Schedule.recurs(3))
)
)
)
// Retries 3 times with exponential backoff. Type‑safe. No extra library.동시성 제한을 둔 병렬 처리
const results = Effect.forEach(
items,
(item) => processItem(item),
{ concurrency: 10 }
)Context와 Layer를 이용한 의존성 주입
import { Context, Effect, Layer } from 'effect'
class Database extends Context.Tag('Database') Effect.Effect } // (original snippet)
// Your code declares what it needs
const getUsers = Database.pipe(
Effect.flatMap(db => db.query('SELECT * FROM users'))
)
// Tests provide a mock
const testLayer = Layer.succeed(Database, {
query: () => Effect.succeed([{ id: 1, name: 'Test' }])
})
// Production provides the real thing
const prodLayer = Layer.succeed(Database, {
query: (sql) => pgPool.query(sql)
})전형적인 사용 사례
- 다양한 실패 모드가 존재하는 복잡한 비즈니스 로직
- 재시도, 타임아웃, 동시성 제어가 필요한 데이터 파이프라인
- 구조화된 오류 처리가 필요한 백엔드 서비스
- 런타임에서 발생하는 예기치 않은 상황을 줄이고 싶은 팀
- 간단한 CRUD 애플리케이션
- 프론트엔드 컴포넌트 및 UI 로직
- 스크립트 및 일회성 작업
- 함수형 프로그래밍 개념에 익숙하지 않은 팀
설치
npm install effect연락처
맞춤형 솔루션이 필요하거나 질문이 있으면 언제든지 연락 주세요.
데이터 파이프라인을 구축하고 계신가요? Apify에서 이미 88개 이상의 프로덕션 스크래퍼가 재시도, 속도 제한, 안티봇 방어 등을 대신 처리하고 있습니다.