forked from bump-sh/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
81 lines (63 loc) · 2.67 KB
/
index.ts
File metadata and controls
81 lines (63 loc) · 2.67 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
import {Config} from '@oclif/core'
import axios, {AxiosError, AxiosInstance, AxiosResponse} from 'axios'
import APIError from './error.js'
import {
DiffRequest,
DiffResponse,
PingResponse,
PreviewRequest,
PreviewResponse,
VersionRequest,
VersionResponse,
WithDiff,
} from './models.js'
import {vars} from './vars.js'
class BumpApi {
protected readonly client: AxiosInstance
public getDiff = (diffId: string, format: string): Promise<AxiosResponse<DiffResponse>> =>
this.client.get<DiffResponse>(`/diffs/${diffId}`, {
params: {formats: [format]},
})
public getPing = (): Promise<AxiosResponse<PingResponse>> => this.client.get<PingResponse>('/ping')
public getVersion = (versionId: string, token: string): Promise<AxiosResponse<VersionResponse & WithDiff>> =>
this.client.get<VersionResponse & WithDiff>(`/versions/${versionId}`, {
headers: this.authorizationHeader(token),
})
public postDiff = (body: DiffRequest): Promise<AxiosResponse<DiffResponse>> =>
this.client.post<PreviewResponse>('/diffs', body)
public postPreview = (body?: PreviewRequest): Promise<AxiosResponse<PreviewResponse>> =>
this.client.post<PreviewResponse>('/previews', body)
public postValidation = (body: VersionRequest, token: string): Promise<AxiosResponse<void>> =>
this.client.post<void>('/validations', body, {
headers: this.authorizationHeader(token),
})
public postVersion = (body: VersionRequest, token: string): Promise<AxiosResponse<VersionResponse>> =>
this.client.post<VersionResponse>('/versions', body, {
headers: this.authorizationHeader(token),
})
public putPreview = (versionId: string, body?: PreviewRequest): Promise<AxiosResponse<PreviewResponse>> =>
this.client.put<PreviewResponse>(`/previews/${versionId}`, body)
private authorizationHeader = (token: string) => ({
Authorization: `Basic ${Buffer.from(token).toString('base64')}`,
})
private handleError = (error: AxiosError) => Promise.reject(new APIError(error))
private initializeResponseInterceptor = () => {
this.client.interceptors.response.use((data) => data, this.handleError)
}
// Check https://oclif.io/docs/config for details about Config.IConfig
public constructor(protected config?: Config) {
const baseURL = `${vars.apiUrl}${vars.apiBasePath}`
const userAgent = config?.userAgent || 'bump-cli'
const headers: {Authorization?: string; 'User-Agent': string} = {
'User-Agent': vars.apiUserAgent(userAgent),
}
this.client = axios.create({
baseURL,
headers,
})
this.initializeResponseInterceptor()
}
}
export {default as APIError} from './error.js'
export {BumpApi}
export * from './models.js'