File size: 949 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { HttpsProxyAgent } from "https-proxy-agent"
import fetch from "node-fetch"
import {
  compositionFileSchema,
  compositionIndexSchema,
  processEnvSchema,
} from "./schema"

const env = processEnvSchema.parse(process.env)

const agent = env.HTTPS_PROXY ? new HttpsProxyAgent(env.HTTPS_PROXY) : undefined

export async function fetchCompositions() {
  const res = await fetch(`${env.REGISTRY_URL}/compositions/index.json`, {
    agent,
  })
  const json = await res.json()
  return compositionIndexSchema.parse(json)
}

export async function fetchComposition(id: string) {
  try {
    const res = await fetch(`${env.REGISTRY_URL}/compositions/${id}.json`, {
      agent,
    })
    const json = await res.json()
    return compositionFileSchema.parse(json)
  } catch (error) {
    throw new Error(
      `Failed to fetch snippet "${id}". Make sure the id is correct or run @chakra-ui/cli snippet list to see available snippets.`,
    )
  }
}