forked from supabase/fly-preview
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.ts
More file actions
197 lines (183 loc) · 5.13 KB
/
client.ts
File metadata and controls
197 lines (183 loc) · 5.13 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import crossFetch from 'cross-fetch'
import { App } from './lib/app'
import { Machine } from './lib/machine'
import { Network } from './lib/network'
import { Organization } from './lib/organization'
import { Secret } from './lib/secret'
import { Volume } from './lib/volume'
import { Regions } from './lib/regions'
import { APIResponse } from './lib/utils'
export const FLY_API_GRAPHQL = 'https://api.fly.io'
export const FLY_API_HOSTNAME = 'https://api.machines.dev'
interface GraphQLRequest<T> {
query: string
variables?: Record<string, T>
}
interface GraphQLResponse<T> {
data: T
errors?: {
message: string
locations: {
line: number
column: number
}[]
}[]
}
interface ClientConfig {
graphqlUrl?: string
apiUrl?: string
}
class Client {
private graphqlUrl: string
private apiUrl: string
private apiKey: string
App: App
Machine: Machine
Regions: Regions
Network: Network
Organization: Organization
Secret: Secret
Volume: Volume
constructor(apiKey: string, { graphqlUrl, apiUrl }: ClientConfig = {}) {
if (!apiKey) {
throw new Error('Fly API Key is required')
}
this.graphqlUrl = graphqlUrl || FLY_API_GRAPHQL
this.apiUrl = apiUrl || FLY_API_HOSTNAME
this.apiKey = apiKey
this.App = new App(this)
this.Machine = new Machine(this)
this.Network = new Network(this)
this.Regions = new Regions(this)
this.Organization = new Organization(this)
this.Secret = new Secret(this)
this.Volume = new Volume(this)
}
getApiKey(): string {
return this.apiKey
}
getApiUrl(): string {
return this.apiUrl
}
getGraphqlUrl(): string {
return this.graphqlUrl
}
async gqlPostOrThrow<U, V>(payload: GraphQLRequest<U>): Promise<V> {
const token = this.apiKey
const resp = await crossFetch(`${this.graphqlUrl}/graphql`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
const text = await resp.text()
if (!resp.ok) {
throw new Error(`${resp.status}: ${text}`)
}
const { data, errors }: GraphQLResponse<V> = JSON.parse(text)
if (errors) {
throw new Error(JSON.stringify(errors))
}
return data
}
// TODO: make sure these methods using this method are using return values correctly
async safeGqlPost<U, V>(payload: GraphQLRequest<U>): Promise<APIResponse<V>> {
try {
const token = this.apiKey
const resp = await crossFetch(`${this.graphqlUrl}/graphql`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
if (!resp.ok) {
const text = await resp.text()
return {
data: undefined,
error: { status: resp.status, message: text },
}
}
const { data, errors }: GraphQLResponse<V> = await resp.json()
if (errors) {
return {
data: undefined,
error: { status: 500, message: JSON.stringify(errors) },
}
}
return { data, error: undefined }
} catch (e) {
if (e instanceof Error) {
return { data: undefined, error: { status: 500, message: e.message } }
}
if (typeof e === 'string') {
return { data: undefined, error: { status: 500, message: e } }
}
return {
data: undefined,
error: { status: 500, message: 'An unknown error occurred.' },
}
}
}
async restOrThrow<U, V>(
path: string,
method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'GET',
body?: U
): Promise<V> {
const resp = await crossFetch(`${this.apiUrl}/v1/${path}`, {
method,
headers: {
Authorization: `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
const text = await resp.text()
if (!resp.ok) {
throw new Error(`${resp.status}: ${text}`)
}
return text ? JSON.parse(text) : undefined
}
async safeRest<U, V>(
path: string,
method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'GET',
body?: U,
headers?: Record<string, string>
): Promise<APIResponse<V>> {
try {
const resp = await crossFetch(`${this.apiUrl}/v1/${path}`, {
method,
headers: {
Authorization: `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify(body),
})
if (!resp.ok) {
const text = await resp.text()
return {
data: undefined,
error: { status: resp.status, message: text },
}
}
const data = await resp.json()
return { data, error: undefined }
} catch (e) {
if (e instanceof Error) {
return { data: undefined, error: { status: 500, message: e.message } }
}
if (typeof e === 'string') {
return { data: undefined, error: { status: 500, message: e } }
}
return {
data: undefined,
error: { status: 500, message: 'An unknown error occurred.' },
}
}
}
}
export default Client