hcloud-ts
v0.1.2
Published
Hetzner Cloud API Client
Downloads
2
Readme
Hetzner Cloud API Client
Typescript implementation of this api: https://docs.hetzner.cloud/
Following extensions are available
| API Extension | |-----------------------------------------------------------------| | Datacenter | | Firewall | | Image | | Iso | | Network | | Placement group | | Server |
Example with axios:
import axios from 'axios';
import HCloud from 'hcloud-ts';
const request: HttpClientRequestMethod = <T>(props: HttpClientRequestMethodProps<T>): Promise<T> => {
return axios.request<T>({
url: props.url,
data: props.data,
method: props.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(props.data),
Authorization: 'Bearer API_TOKEN'
}
}).then((response) => {
if (response.status !== 200) {
throw new Error(response.statusText)
}
return response.data
});
};
const hCloudClient = new HCloud(request);
Example with nodejs https:
import * as https from 'https';
import HCloud from 'hcloud-ts';
const request: HttpClientRequestMethod = <T>(props: HttpClientRequestMethodProps<T>): Promise<T> => {
return new Promise((resolve, reject) => {
const req = https.request({
hostname: props.url,
method: props.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(props.data),
Authorization: 'Bearer API_TOKEN'
}
}, (res) => {
let data = '';
res.on('data', (chunk) => {
data = data + chunk
});
res.on('end', () => {
if (res.statusCode !== 200) {
reject(new Error(res.statusMessage));
} else {
resolve(JSON.parse(data) as T);
}
});
});
req.on('error', (error) => {
reject(error);
})
req.write(props.data);
req.end();
});
}
const hCloudClient = new HCloud(request);
Example with fetch:
const request: HttpClientRequestMethod = <T>(props: HttpClientRequestMethodProps<T>): Promise<T> => {
const body = JSON.stringify(props.data);
return window.fetch(props.url, {
method: props.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': JSON.stringify(props.data).length,
Authorization: 'Bearer API_TOKEN'
},
body,
}).then((response) => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json() as Promise<T>
});
};
const hCloudClient = new HCloud(request);