forked from crossplane/function-sdk-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.ts
More file actions
194 lines (181 loc) · 6.06 KB
/
resource.ts
File metadata and controls
194 lines (181 loc) · 6.06 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Resource utilities for working with Kubernetes resources and protobuf conversion
import { Ready, Resource } from '../proto/run_function.js';
// Type aliases for better readability
export type ConnectionDetails = { [key: string]: Buffer };
/**
* Composite represents a Crossplane composite resource (XR) with its state
*/
export interface Composite {
resource: Resource;
connectionDetails: ConnectionDetails;
ready: Ready;
}
/**
* ObservedComposed represents the observed state of a composed resource
*/
export interface ObservedComposed {
resource: Resource;
connectionDetails: ConnectionDetails;
}
/**
* DesiredComposed represents the desired state of a composed resource
*/
export interface DesiredComposed {
resource: Resource;
ready: Ready;
}
/**
* Create a new empty DesiredComposed resource
*/
export function newDesiredComposed(): DesiredComposed {
return {
resource: Resource.fromJSON({}),
ready: Ready.READY_UNSPECIFIED,
};
}
/**
* Convert a protobuf Struct to a Kubernetes object (plain JavaScript object)
* This is a more efficient conversion that avoids JSON round-trips when possible
*
* Note: This function is ported from the Go SDK for API compatibility.
* In TypeScript, this is a pass-through operation since JavaScript objects
* work directly with the protobuf library. This function may be deprecated
* in a future version once usage patterns are better understood.
*
* @param struct - The protobuf Struct to convert
* @returns A plain JavaScript object representing the Kubernetes resource
*/
export function asObject(struct: Record<string, unknown> | undefined): Record<string, unknown> {
if (!struct) {
return {};
}
// The struct is already a plain object in our TypeScript implementation
// In the Go SDK, this does actual protobuf conversion
return struct;
}
/**
* Convert a Kubernetes object to a protobuf Struct
* This is used when creating Resource objects from plain JavaScript objects
*
* Note: This function is ported from the Go SDK for API compatibility.
* In TypeScript, this is a pass-through operation since JavaScript objects
* work directly with the protobuf library. This function may be deprecated
* in a future version once usage patterns are better understood.
*
* @param obj - The plain JavaScript object to convert
* @returns A protobuf Struct representation
*/
export function asStruct(obj: Record<string, unknown>): Record<string, unknown> {
// In our TypeScript implementation, this is essentially a pass-through
// The actual conversion happens in the protobuf serialization layer
return obj;
}
/**
* Helper function for tests: Convert an object to a Struct, panics on failure
* Only use this in test code
*
* Note: This function is ported from the Go SDK for API compatibility.
* In TypeScript, this simply calls asStruct() which is a pass-through operation.
* This function may be deprecated in a future version once usage patterns are
* better understood.
*
* @param obj - The object to convert
* @returns A Struct representation
* @throws Error if conversion fails
*/
export function mustStructObject(obj: Record<string, unknown>): Record<string, unknown> {
try {
return asStruct(obj);
} catch (error) {
throw new Error(
`Failed to convert object to struct: ${
error instanceof Error ? error.message : String(error)
}`
);
}
}
/**
* Helper function for tests: Parse a JSON string into a Struct, panics on failure
* Only use this in test code
*
* Note: This function is ported from the Go SDK for API compatibility.
* In TypeScript, this parses JSON and calls asStruct() which is a pass-through operation.
* Consider using JSON.parse() directly. This function may be deprecated in a future
* version once usage patterns are better understood.
*
* @param json - The JSON string to parse
* @returns A Struct representation
* @throws Error if parsing or conversion fails
*/
export function mustStructJSON(json: string): Record<string, unknown> {
try {
const obj = JSON.parse(json) as Record<string, unknown>;
return asStruct(obj);
} catch (error) {
throw new Error(
`Failed to parse JSON to struct: ${error instanceof Error ? error.message : String(error)}`
);
}
}
/**
* Create a Resource from a plain JavaScript object
* This is a convenience wrapper around Resource.fromJSON
*
* @param obj - The resource object
* @param connectionDetails - Optional connection details
* @param ready - Optional ready status
* @returns A Resource
*/
export function fromObject(
obj: Record<string, unknown>,
connectionDetails?: ConnectionDetails,
ready?: Ready
): Resource {
return Resource.fromJSON({
resource: obj,
connectionDetails: connectionDetails || {},
ready: ready !== undefined ? ready : Ready.READY_UNSPECIFIED,
});
}
/**
* Get the resource object from a Resource
* This extracts the plain JavaScript object from the Resource wrapper
*
* @param resource - The Resource to extract from
* @returns The plain JavaScript object, or undefined if not present
*/
export function toObject(resource: Resource): Record<string, unknown> | undefined {
return resource.resource;
}
/**
* Create a Resource from a kubernetes-models object
* This is a convenience function for objects with a toJSON() method
* (like kubernetes-models objects)
*
* For plain JavaScript objects, use fromObject() instead.
*
* @param obj - A kubernetes-models object with a toJSON() method
* @param connectionDetails - Optional connection details
* @param ready - Optional ready status
* @returns A Resource
*
* @example
* ```typescript
* import { Pod } from "kubernetes-models/v1";
*
* const pod = new Pod({
* metadata: { name: "my-pod" },
* spec: { containers: [{ name: "app", image: "nginx" }] }
* });
*
* // Automatically calls toJSON() on the model object
* const resource = fromModel(pod);
* ```
*/
export function fromModel<T extends Record<string, unknown>>(
obj: { toJSON: () => T },
connectionDetails?: ConnectionDetails,
ready?: Ready
): Resource {
return fromObject(obj.toJSON() as Record<string, unknown>, connectionDetails, ready);
}