-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_pricing_subscriptions.ts
More file actions
61 lines (57 loc) · 2.09 KB
/
get_pricing_subscriptions.ts
File metadata and controls
61 lines (57 loc) · 2.09 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Client, buildQuery } from '../../client.js'
import { throwIfNotOk } from '../../errors.js'
import { ZooProductSubscription } from '../../models.js'
interface GetPricingSubscriptionsInput {
client?: Client
}
type GetPricingSubscriptionsReturn = Record<string, ZooProductSubscription[]>
/**
* Get the pricing for our subscriptions.
*
* This is the ultimate source of truth for the pricing of our subscriptions.
*
* Tags: meta, hidden
*
* @param params Function parameters.
* @property {Client} [client] Optional client with auth token.
* @returns {Promise<GetPricingSubscriptionsReturn>} successful operation
*
* Possible return types: ZooProductSubscription
*/
export default async function get_pricing_subscriptions(
{ client }: GetPricingSubscriptionsInput = {} as GetPricingSubscriptionsInput
): Promise<GetPricingSubscriptionsReturn> {
const path = `/pricing/subscriptions`
const qs = buildQuery({})
const url = path + qs
// Backwards compatible for the BASE_URL env variable
// That used to exist in only this lib, ZOO_HOST exists in the all the other
// sdks and the CLI.
const urlBase =
client?.baseUrl ||
process?.env?.ZOO_HOST ||
process?.env?.BASE_URL ||
'https://api.zoo.dev'
const fullUrl = urlBase + url
// The other sdks use to use KITTYCAD_API_TOKEN, now they still do for
// backwards compatibility, but the new standard is ZOO_API_TOKEN.
// For some reason only this lib supported KITTYCAD_TOKEN, so we need to
// check for that as well.
const kittycadToken = client
? client.token || process.env.ZOO_API_TOKEN || ''
: process.env.KITTYCAD_TOKEN ||
process.env.KITTYCAD_API_TOKEN ||
process.env.ZOO_API_TOKEN ||
''
const headers: Record<string, string> = {}
if (kittycadToken) headers.Authorization = `Bearer ${kittycadToken}`
const fetchOptions: RequestInit = {
method: 'GET',
headers,
}
const _fetch = client?.fetch || fetch
const response = await _fetch(fullUrl, fetchOptions)
await throwIfNotOk(response)
const result = (await response.json()) as GetPricingSubscriptionsReturn
return result
}