@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.
Files changed (62) hide show
  1. package/README.md +90 -0
  2. package/package.json +70 -0
  3. package/src/apis/actions/index.ts +113 -0
  4. package/src/apis/actions/schemas.ts +59 -0
  5. package/src/apis/actions/types.ts +77 -0
  6. package/src/apis/certificates/index.ts +326 -0
  7. package/src/apis/certificates/schemas.ts +140 -0
  8. package/src/apis/certificates/types.ts +176 -0
  9. package/src/apis/common/schemas.ts +19 -0
  10. package/src/apis/dns/index.ts +961 -0
  11. package/src/apis/dns/schemas.ts +437 -0
  12. package/src/apis/dns/types.ts +397 -0
  13. package/src/apis/firewalls/index.ts +469 -0
  14. package/src/apis/firewalls/schemas.ts +274 -0
  15. package/src/apis/firewalls/types.ts +205 -0
  16. package/src/apis/floating-ips/index.ts +466 -0
  17. package/src/apis/floating-ips/schemas.ts +203 -0
  18. package/src/apis/floating-ips/types.ts +207 -0
  19. package/src/apis/images/index.ts +195 -0
  20. package/src/apis/images/schemas.ts +113 -0
  21. package/src/apis/images/types.ts +124 -0
  22. package/src/apis/isos/index.ts +91 -0
  23. package/src/apis/isos/schemas.ts +43 -0
  24. package/src/apis/isos/types.ts +60 -0
  25. package/src/apis/load-balancers/index.ts +892 -0
  26. package/src/apis/load-balancers/schemas.ts +561 -0
  27. package/src/apis/load-balancers/types.ts +361 -0
  28. package/src/apis/locations/index.ts +176 -0
  29. package/src/apis/locations/schemas.ts +83 -0
  30. package/src/apis/locations/types.ts +113 -0
  31. package/src/apis/networks/index.ts +544 -0
  32. package/src/apis/networks/schemas.ts +279 -0
  33. package/src/apis/networks/types.ts +243 -0
  34. package/src/apis/placement-groups/index.ts +212 -0
  35. package/src/apis/placement-groups/schemas.ts +90 -0
  36. package/src/apis/placement-groups/types.ts +99 -0
  37. package/src/apis/pricing/index.ts +42 -0
  38. package/src/apis/pricing/schemas.ts +93 -0
  39. package/src/apis/pricing/types.ts +71 -0
  40. package/src/apis/primary-ips/index.ts +467 -0
  41. package/src/apis/primary-ips/schemas.ts +221 -0
  42. package/src/apis/primary-ips/types.ts +221 -0
  43. package/src/apis/server-types/index.ts +93 -0
  44. package/src/apis/server-types/schemas.ts +29 -0
  45. package/src/apis/server-types/types.ts +43 -0
  46. package/src/apis/servers/index.ts +378 -0
  47. package/src/apis/servers/schemas.ts +771 -0
  48. package/src/apis/servers/types.ts +538 -0
  49. package/src/apis/ssh-keys/index.ts +204 -0
  50. package/src/apis/ssh-keys/schemas.ts +84 -0
  51. package/src/apis/ssh-keys/types.ts +106 -0
  52. package/src/apis/volumes/index.ts +452 -0
  53. package/src/apis/volumes/schemas.ts +195 -0
  54. package/src/apis/volumes/types.ts +197 -0
  55. package/src/auth/index.ts +26 -0
  56. package/src/base/index.ts +10 -0
  57. package/src/client/index.ts +388 -0
  58. package/src/config/index.ts +34 -0
  59. package/src/errors/index.ts +38 -0
  60. package/src/index.ts +799 -0
  61. package/src/types/index.ts +37 -0
  62. 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
+ }