-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
128 lines (108 loc) · 3.56 KB
/
index.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import fetch from "cross-fetch"
export type Attributes = Record<string, number | string>
export type JsonValue =
| number
| string
| null
| Array<JsonValue>
| { [key: string]: JsonValue }
export type Features = Record<string, JsonValue>
export interface FeaturesClientOptions {
apiKey: string | null
apiUrl?: string
headers?: { [key: string]: string }
scope?: string
}
const DEFAULT_API_URL = "https://www.featurescope.io"
const DEFAULT_SCOPE = "_"
export class FeaturesClientInitError extends Error {
constructor(message: string) {
super(`Could not instantiate features client: ${message}`)
}
}
export class FeaturesClientNotImplementedError extends Error {
constructor(method: string) {
super(`Method "${method}" not implemented`)
}
}
export function init(options: FeaturesClientOptions | string | null) {
if (typeof options === "string") {
options = {
apiKey: options,
apiUrl: DEFAULT_API_URL,
headers: {},
scope: DEFAULT_SCOPE,
}
}
const apiKey = options?.apiKey ?? null
const apiUrl = options?.apiUrl ?? DEFAULT_API_URL
const headers: { [key: string]: string } = {
...(options?.headers || {}),
"Content-Type": "application/json",
}
const scope = options?.scope ?? DEFAULT_SCOPE
if (apiKey) {
headers.Authorization = `Bearer ${apiKey}`
}
const fetcher = (path: string, options: RequestInit = {}): Promise<any> =>
fetch(`${apiUrl}${path}`, {
...options,
headers,
method: options.method || "get",
})
.then((response) => {
if (response.ok) return response.json()
return Promise.reject({ errors: ["Unknown error"] })
})
.then(({ data, errors }) => {
if (Array.isArray(errors) && errors.length) {
return Promise.reject(errors[0])
}
return data
})
const createFeatureVariation = () => {
throw new FeaturesClientNotImplementedError("createFeatureVariations")
}
const createFeatureVariations = () => {
throw new FeaturesClientNotImplementedError("createFeatureVariations")
}
const getAllVariationsForFeature = () => {
throw new FeaturesClientNotImplementedError("getAllVariationsForFeature")
}
const getAllVariationsForFeaturesList = () => {
throw new FeaturesClientNotImplementedError("createFeatureVariations")
}
const listScopesForUser = (): Promise<Array<string>> =>
fetcher("/api/v1/scopes")
const listFeaturesForScope = (scope: string): Promise<Array<string>> =>
fetcher(`/api/v1/features?scope=${scope}`)
const findFeaturesListVariationsByAttributes = (
attributes: Attributes = {},
options?: { featureIds?: Array<string> },
): Promise<Features> => {
const params = new URLSearchParams({ scope, ...attributes })
if (options && Array.isArray(options.featureIds)) {
params.set("featureIds", options.featureIds.join(","))
}
return fetcher(`/api/v1/variations?${params}`)
}
const findFeatureVariationByAttributes = (
featureId: string,
attributes: Attributes = {},
): Promise<JsonValue> =>
findFeaturesListVariationsByAttributes(attributes, {
featureIds: [featureId],
}).then((variationsByFeatureId) => variationsByFeatureId[featureId])
return {
createFeatureVariation,
createFeatureVariations,
findFeatureVariationByAttributes,
findFeaturesListVariationsByAttributes,
getAllVariationsForFeature,
getAllVariationsForFeaturesList,
getFeature: findFeatureVariationByAttributes,
getFeatures: findFeaturesListVariationsByAttributes,
listFeaturesForScope,
listScopesForUser,
}
}