@@ -10,12 +10,10 @@ import blurIndigoImage from '@/images/blur-indigo.png'
1010
1111const codeLanguage = 'typescript'
1212
13- const code = `import { safe, createSafe } from '@cometloop/safe'
14- import { fetchUserAsync, fetchPostsAsync } from '@features'
15- import { errorParser, resultParser } from '@lib/utils'
16- import { errorHook, successHook } from '@lib/hooks'
13+ const code = `// One-off wrapping — zero config
14+ const safeJSONParse = safe.wrap(JSON.parse)
15+ const [parsed, parseErr] = safeJSONParse(rawInput)
1716
18- // One-off wrapping — zero config
1917const fetchUserOnce = safe.wrapAsync(fetchUserAsync)
2018const [basicUser, basicError] = await fetchUserOnce('admin-456')
2119
@@ -25,21 +23,34 @@ const apiSafe = createSafe({
2523 parseResult: resultParser,
2624 onError: errorHook,
2725 onSuccess: successHook,
26+ onSettled: settledHook,
2827})
2928
3029const fetchUser = apiSafe.wrapAsync(fetchUserAsync)
3130const fetchPosts = apiSafe.wrapAsync(fetchPostsAsync)
3231
33- // Same API. More power .
32+ // Same shared config. Full type narrowing .
3433const [user, userError] = await fetchUser('123')
3534if (userError) return
3635
3736const [posts, postsError] = await fetchPosts(user.id)
3837if (postsError) return
3938
40- // Fully type-narrowed — never undefined
4139console.log(user.name)
42- console.log(posts.length)`
40+ console.log(posts.length)
41+
42+ // Add resilience — retry with backoff + auto-cancel
43+ const fetchUserRetry = apiSafe.wrapAsync(fetchUserAsync, {
44+ retry: { times: 3, waitBefore: (n) => n * 1000 },
45+ abortAfter: 5000,
46+ onRetry: (err, attempt) => console.log(\`Retry #\${attempt}...\`),
47+ })
48+
49+ // Prefer objects? One call to switch.
50+ const objSafe = withObjects(apiSafe)
51+ const fetchPostsObj = objSafe.wrapAsync(fetchPostsAsync)
52+ const { ok, data, error } = await fetchPostsObj('123')
53+ `
4354
4455// const code = `import { safe } from '@cometloop/safe'
4556// import { fetchUserAsync } from '@lib/api'
0 commit comments