@nilovonjs/hcloud-js 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -0
- package/package.json +70 -0
- package/src/apis/actions/index.ts +113 -0
- package/src/apis/actions/schemas.ts +59 -0
- package/src/apis/actions/types.ts +77 -0
- package/src/apis/certificates/index.ts +326 -0
- package/src/apis/certificates/schemas.ts +140 -0
- package/src/apis/certificates/types.ts +176 -0
- package/src/apis/common/schemas.ts +19 -0
- package/src/apis/dns/index.ts +961 -0
- package/src/apis/dns/schemas.ts +437 -0
- package/src/apis/dns/types.ts +397 -0
- package/src/apis/firewalls/index.ts +469 -0
- package/src/apis/firewalls/schemas.ts +274 -0
- package/src/apis/firewalls/types.ts +205 -0
- package/src/apis/floating-ips/index.ts +466 -0
- package/src/apis/floating-ips/schemas.ts +203 -0
- package/src/apis/floating-ips/types.ts +207 -0
- package/src/apis/images/index.ts +195 -0
- package/src/apis/images/schemas.ts +113 -0
- package/src/apis/images/types.ts +124 -0
- package/src/apis/isos/index.ts +91 -0
- package/src/apis/isos/schemas.ts +43 -0
- package/src/apis/isos/types.ts +60 -0
- package/src/apis/load-balancers/index.ts +892 -0
- package/src/apis/load-balancers/schemas.ts +561 -0
- package/src/apis/load-balancers/types.ts +361 -0
- package/src/apis/locations/index.ts +176 -0
- package/src/apis/locations/schemas.ts +83 -0
- package/src/apis/locations/types.ts +113 -0
- package/src/apis/networks/index.ts +544 -0
- package/src/apis/networks/schemas.ts +279 -0
- package/src/apis/networks/types.ts +243 -0
- package/src/apis/placement-groups/index.ts +212 -0
- package/src/apis/placement-groups/schemas.ts +90 -0
- package/src/apis/placement-groups/types.ts +99 -0
- package/src/apis/pricing/index.ts +42 -0
- package/src/apis/pricing/schemas.ts +93 -0
- package/src/apis/pricing/types.ts +71 -0
- package/src/apis/primary-ips/index.ts +467 -0
- package/src/apis/primary-ips/schemas.ts +221 -0
- package/src/apis/primary-ips/types.ts +221 -0
- package/src/apis/server-types/index.ts +93 -0
- package/src/apis/server-types/schemas.ts +29 -0
- package/src/apis/server-types/types.ts +43 -0
- package/src/apis/servers/index.ts +378 -0
- package/src/apis/servers/schemas.ts +771 -0
- package/src/apis/servers/types.ts +538 -0
- package/src/apis/ssh-keys/index.ts +204 -0
- package/src/apis/ssh-keys/schemas.ts +84 -0
- package/src/apis/ssh-keys/types.ts +106 -0
- package/src/apis/volumes/index.ts +452 -0
- package/src/apis/volumes/schemas.ts +195 -0
- package/src/apis/volumes/types.ts +197 -0
- package/src/auth/index.ts +26 -0
- package/src/base/index.ts +10 -0
- package/src/client/index.ts +388 -0
- package/src/config/index.ts +34 -0
- package/src/errors/index.ts +38 -0
- package/src/index.ts +799 -0
- package/src/types/index.ts +37 -0
- package/src/validation/index.ts +109 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for Hetzner Cloud API
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Error details field structure from Hetzner Cloud API
|
|
7
|
+
*/
|
|
8
|
+
export interface ErrorDetailsField {
|
|
9
|
+
name: string;
|
|
10
|
+
messages: string[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Error response structure from Hetzner Cloud API
|
|
15
|
+
* @see https://docs.hetzner.cloud/reference/cloud#errors
|
|
16
|
+
*/
|
|
17
|
+
export interface ApiErrorResponse {
|
|
18
|
+
error: {
|
|
19
|
+
message: string;
|
|
20
|
+
code: string;
|
|
21
|
+
details?: {
|
|
22
|
+
fields?: ErrorDetailsField[];
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* HTTP request options
|
|
30
|
+
*/
|
|
31
|
+
export interface RequestOptions {
|
|
32
|
+
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
33
|
+
path: string;
|
|
34
|
+
body?: unknown;
|
|
35
|
+
params?: Record<string, string | number | boolean | string[] | undefined>;
|
|
36
|
+
headers?: Record<string, string>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic validation utilities for Hetzner Cloud API
|
|
3
|
+
* Provides reusable validation functions that can be used across all API modules
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { HCloudError } from "../errors/index";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Options for validation error messages
|
|
11
|
+
*/
|
|
12
|
+
export interface ValidateOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Name/context for the validation error message
|
|
15
|
+
* Example: "Image", "Server", "List images response"
|
|
16
|
+
*/
|
|
17
|
+
context: string;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to include detailed path information in error messages
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
detailed?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Generic validation function that validates data against a Zod schema
|
|
27
|
+
*
|
|
28
|
+
* @param schema - Zod schema to validate against
|
|
29
|
+
* @param data - Data to validate
|
|
30
|
+
* @param options - Validation options (context name for error messages)
|
|
31
|
+
* @returns Validated data with proper TypeScript typing
|
|
32
|
+
* @throws {HCloudError} If validation fails
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { validate } from '../validation/index.js';
|
|
37
|
+
* import { imageSchema } from './schemas.js';
|
|
38
|
+
*
|
|
39
|
+
* const image = validate(imageSchema, data, { context: 'Image' });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function validate<T extends z.ZodTypeAny>(
|
|
43
|
+
schema: T,
|
|
44
|
+
data: unknown,
|
|
45
|
+
options: ValidateOptions,
|
|
46
|
+
): z.infer<T> {
|
|
47
|
+
try {
|
|
48
|
+
return schema.parse(data) as z.infer<T>;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error instanceof z.ZodError) {
|
|
51
|
+
const detailed = options.detailed ?? true;
|
|
52
|
+
let errorMessage: string;
|
|
53
|
+
|
|
54
|
+
if (detailed) {
|
|
55
|
+
// Provide detailed error messages with paths
|
|
56
|
+
const errorMessages = error.errors.map((e) => {
|
|
57
|
+
const path = e.path.length > 0 ? e.path.join(".") : "root";
|
|
58
|
+
return `${path}: ${e.message}`;
|
|
59
|
+
});
|
|
60
|
+
errorMessage = `${options.context} validation failed: ${errorMessages.join(", ")}`;
|
|
61
|
+
} else {
|
|
62
|
+
// Simple error message
|
|
63
|
+
errorMessage = `${options.context} validation failed: ${error.errors.map((e) => e.message).join(", ")}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw new HCloudError(errorMessage, "VALIDATION_ERROR", 0, { errors: error.errors });
|
|
67
|
+
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Safe validation - returns a result object instead of throwing
|
|
74
|
+
*
|
|
75
|
+
* @param schema - Zod schema to validate against
|
|
76
|
+
* @param data - Data to validate
|
|
77
|
+
* @returns Result object with success flag and data/error
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* import { safeValidate } from '../validation/index.js';
|
|
82
|
+
* import { imageSchema } from './schemas.js';
|
|
83
|
+
*
|
|
84
|
+
* const result = safeValidate(imageSchema, data);
|
|
85
|
+
* if (result.success) {
|
|
86
|
+
* console.log(result.data);
|
|
87
|
+
* } else {
|
|
88
|
+
* console.error(result.error);
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function safeValidate<T extends z.ZodTypeAny>(
|
|
93
|
+
schema: T,
|
|
94
|
+
data: unknown,
|
|
95
|
+
):
|
|
96
|
+
| {
|
|
97
|
+
success: true;
|
|
98
|
+
data: z.infer<T>;
|
|
99
|
+
}
|
|
100
|
+
| {
|
|
101
|
+
success: false;
|
|
102
|
+
error: z.ZodError;
|
|
103
|
+
} {
|
|
104
|
+
const result = schema.safeParse(data);
|
|
105
|
+
if (result.success) {
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|