Skip to content

Commit

Permalink
Merge pull request #10 from UnofficialDathost/Development
Browse files Browse the repository at this point in the history
Added option to pass Axios config with File.download & File.upload
  • Loading branch information
WardPearce committed Jun 15, 2021
2 parents 166424a + 291e563 commit 0875a5d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "dathost",
"version": "0.0.8",
"version": "0.0.9",
"description": "TypeScript wrapper for Dathost's API",
"main": "src/index.ts",
"files": [
Expand Down
21 changes: 9 additions & 12 deletions src/http.ts
Expand Up @@ -28,24 +28,21 @@ export default class HTTP {
)
}

public async get(url: string, raw = false): Promise<any> {
if (raw) {
return await this.request.get(url, { responseType: 'blob' })
}
return await this.request.get(url)
public async get(url: string, config: AxiosRequestConfig = {}): Promise<any> {
return await this.request.get(url, config)
}

public async delete(url: string): Promise<any> {
return await this.request.delete(url)
public async delete(url: string, config: AxiosRequestConfig = {}): Promise<any> {
return await this.request.delete(url, config)
}

public async post(url: string, data?: URLSearchParams | FormData): Promise<any> {
public async post(url: string, data?: URLSearchParams | FormData, config: AxiosRequestConfig = {}): Promise<any> {
if (data instanceof FormData)
return await this.request.post(url, data, {headers: {'Content-Type': 'multipart/form-data'}})
return await this.request.post(url, data)
return await this.request.post(url, data, {headers: {'Content-Type': 'multipart/form-data'}, ...config})
return await this.request.post(url, data, config)
}

public async put(url: string, data: URLSearchParams): Promise<any> {
return await this.request.put(url, data)
public async put(url: string, data: URLSearchParams, config: AxiosRequestConfig = {}): Promise<any> {
return await this.request.put(url, data, config)
}
}
14 changes: 10 additions & 4 deletions src/server/file.ts
@@ -1,3 +1,5 @@
import { AxiosRequestConfig } from 'axios'

import HTTP from '../http'
import FormData from '../helpers/formData'

Expand Down Expand Up @@ -36,13 +38,17 @@ export default class File {
await this.http.put(this.url, payload)
}

public async download(asText = false): Promise<Blob | string> {
return await this.http.get(`${this.url}?as_text=${asText.toString()}`, !asText)
public async download(asText = false, config: AxiosRequestConfig = {}): Promise<Blob | string> {
if (!asText) {
config.responseType = 'blob'
}

return await this.http.get(`${this.url}?as_text=${asText.toString()}`, config)
}

public async upload(data: Blob): Promise<void> {
public async upload(data: Blob, config: AxiosRequestConfig = {}): Promise<void> {
const payload = new FormData()
payload.append('file', data)
await this.http.post(`https://upload.dathost.net/api/0.1/game-servers/${this.serverId}/files/${this.path}`, payload)
await this.http.post(`https://upload.dathost.net/api/0.1/game-servers/${this.serverId}/files/${this.path}`, payload, config)
}
}

0 comments on commit 0875a5d

Please sign in to comment.