@highstate/pulumi 0.9.16 → 0.9.19

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/src/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Input, type Unwrap, type Output, output, all } from "@pulumi/pulumi"
1
+ import { type Input, type Output, output, type Unwrap } from "@pulumi/pulumi"
2
2
 
3
3
  /**
4
4
  * The input type for an array of inputs.
@@ -7,182 +7,97 @@ import { type Input, type Unwrap, type Output, output, all } from "@pulumi/pulum
7
7
  export type InputArray<T> = Input<Input<T>[]>
8
8
 
9
9
  /**
10
- * The input type for a map of inputs.
10
+ * The input type for a record of inputs.
11
11
  * The same as `Input<Record<string, Input<T>>>`, but more readable.
12
12
  */
13
- export type InputMap<T> = Input<Readonly<Record<string, Input<T>>>>
13
+ export type InputRecord<T> = Input<Record<string, Input<T>>>
14
14
 
15
15
  /**
16
16
  * The input or input array type for a value.
17
17
  */
18
18
  export type InputOrArray<T> = Input<T> | InputArray<T>
19
19
 
20
+ type LeafValue = string | number | boolean | null | undefined
21
+ type IsUnknown<T> = unknown extends T ? (T extends unknown ? true : false) : false
22
+
20
23
  /**
21
- * The input of inputs of inputs of inputs, so you got the idea.
24
+ * The recursive input type for a value.
22
25
  */
23
- export type DeepInput<T> = [T] extends [Record<string, unknown> | undefined]
24
- ? [T] extends [infer U | undefined]
25
- ? Input<{ [K in keyof U]: DeepInput<U[K]> } | undefined>
26
+ export type DeepInput<T> = [T] extends [LeafValue]
27
+ ? Input<T>
28
+ : IsUnknown<T> extends true
29
+ ? Input<unknown>
26
30
  : Input<{ [K in keyof T]: DeepInput<T[K]> }>
27
- : [T] extends [Array<unknown> | undefined]
28
- ? [T] extends [(infer U)[] | undefined]
29
- ? Input<DeepInput<U>[] | undefined>
30
- : Input<DeepInput<T>[]>
31
- : Input<T>
32
31
 
33
32
  /**
34
- * Merges the given array of `InputOrArray` values into a single flat output array.
33
+ * Transforms an input value to a promise that resolves to the unwrapped value.
35
34
  *
36
- * @param values The values to merge.
37
- * @returns The merged output array.
35
+ * @param input The input value to transform.
36
+ * @returns A promise that resolves to the unwrapped value.
38
37
  */
39
- export function flattenInputs<T>(...values: (InputOrArray<T> | undefined)[]): Output<T[]> {
40
- return all(values).apply(allValues => {
41
- const result: T[] = []
42
- for (const value of allValues) {
43
- if (Array.isArray(value)) {
44
- result.push(...(value as T[]))
45
- } else if (value) {
46
- result.push(value as T)
47
- }
48
- }
49
- return result
50
- })
38
+ export function toPromise<T>(input: Input<T>): Promise<Unwrap<T>> {
39
+ return new Promise(resolve => output(input).apply(resolve))
51
40
  }
52
41
 
53
42
  /**
54
- * Maps each element of an input array to a new value.
55
- * Produces an output array with the same length.
43
+ * Receives an item and a collection, and returns an array containing the item and the collection.
44
+ *
45
+ * Excludes the item if it is undefined.
56
46
  *
57
- * @param array The input array.
58
- * @param fn The mapping function.
59
- * @returns The output array.
47
+ * @param item The single item input.
48
+ * @param collection The collection of items input.
60
49
  */
61
- export function mapInputs<T, U>(
62
- array: InputArray<T>,
63
- fn: (v: Unwrap<T>, index: number, all: Unwrap<T>[]) => U,
64
- ): Output<U[]> {
65
- return output(array).apply(array => {
66
- return array?.map((v, index) => fn(v as Unwrap<T>, index, array as Unwrap<T>[])) ?? []
67
- })
68
- }
50
+ export function normalize<T>(item: T | undefined, collection: T[] | undefined): T[] {
51
+ if (item && collection) {
52
+ return [item, ...collection]
53
+ }
69
54
 
70
- export function flatMapInput<T, U>(
71
- v1: InputOrArray<T>,
72
- v2: InputOrArray<T>,
73
- fn: (v: Unwrap<T>, index: number, all: Unwrap<T>[]) => U,
74
- ): Output<U[]>
55
+ if (item) {
56
+ return [item]
57
+ }
75
58
 
76
- export function flatMapInput<T, U>(
77
- v1: InputOrArray<T>,
78
- v2: InputOrArray<T>,
79
- v3: InputOrArray<T>,
80
- fn: (v: Unwrap<T>, index: number, all: Unwrap<T>[]) => U,
81
- ): Output<U[]>
59
+ return collection ?? []
60
+ }
82
61
 
83
62
  /**
84
- * Merges the given array of `InputOrArray` values into a single flat output array and maps each element to a new value.
63
+ * The same as `normalize`, but accepts inputs and returns output.
85
64
  *
86
- * @param values The values to merge.
87
- * @param fn The mapping function.
65
+ * @param item The single item input.
66
+ * @param collection The collection of items input.
88
67
  */
89
- export function flatMapInput<T, U>(
90
- ...args: (InputOrArray<T> | ((v: Unwrap<T>, index: number, all: Unwrap<T>[]) => U))[]
91
- ): Output<U[]> {
92
- const fn = args.pop() as (v: Unwrap<T>, index: number, all: Unwrap<T>[]) => U
93
- const values = args as InputOrArray<T>[]
94
-
95
- return mapInputs(flattenInputs(...values), fn)
68
+ export function normalizeInputs<T>(
69
+ item: Input<T> | undefined,
70
+ collection: InputArray<T> | undefined,
71
+ ): Output<Unwrap<T>[]> {
72
+ return (
73
+ output({ item, collection })
74
+ //
75
+ .apply(({ item, collection }) => normalize(item, collection)) as Output<Unwrap<T>[]>
76
+ )
96
77
  }
97
78
 
98
79
  /**
99
- * Map an optional value to another optional value.
80
+ * The convenience function to normalize inputs and map them to a new type.
100
81
  *
101
- * @param input The input value.
102
- * @param func The function to apply to the input value.
103
- * @returns The output value, or `undefined` if the input value is `undefined`.
82
+ * @param item The single item input.
83
+ * @param collection The collection of items input.
84
+ * @param mapFn The function to map each item to a new type.
104
85
  */
105
- export function mapOptional<T, U>(input: T | undefined, func: (value: T) => U): U | undefined {
106
- if (input === undefined) {
107
- return undefined
108
- }
109
-
110
- return func(input)
111
- }
112
-
113
- export function toPromise<T>(input: Input<T>): Promise<Unwrap<T>> {
114
- return new Promise(resolve => output(input).apply(resolve))
115
- }
116
-
117
- export function singleton<T>(factory: () => T): () => T {
118
- let instance: T | undefined
119
- return () => {
120
- if (instance === undefined) {
121
- instance = factory()
122
- }
123
-
124
- return instance
125
- }
126
- }
127
-
128
- export function providerFactory<TInput>(
129
- factory: (name: string) => TInput,
130
- ): (name: string) => TInput {
131
- const instances = new Map<string, TInput>()
132
- return name => {
133
- if (!instances.has(name)) {
134
- instances.set(name, factory(name))
135
- }
136
-
137
- return instances.get(name)!
138
- }
86
+ export function normalizeInputsAndMap<T, U>(
87
+ item: Input<T> | undefined,
88
+ collection: InputArray<T> | undefined,
89
+ mapFn: (value: Unwrap<T>) => U,
90
+ ): Output<U[]> {
91
+ return normalizeInputs(item, collection).apply(values => values.map(mapFn))
139
92
  }
140
93
 
141
- export function mergeInputObjects<
142
- T1 extends Record<string, unknown>,
143
- T2 extends Record<string, unknown>,
144
- >(obj1: Input<T1 | undefined> | undefined, obj2: Input<T2 | undefined> | undefined): Output<T1 & T2>
145
-
146
- export function mergeInputObjects<
147
- T1 extends Record<string, unknown>,
148
- T2 extends Record<string, unknown>,
149
- T3 extends Record<string, unknown>,
150
- >(
151
- obj1: Input<T1 | undefined> | undefined,
152
- obj2: Input<T2 | undefined> | undefined,
153
- obj3: Input<T3 | undefined> | undefined,
154
- ): Output<T1 & T2 & T3>
155
-
156
94
  /**
157
- * Merges the given input objects into a single output object.
95
+ * Applies a function to the input and returns an output.
96
+ *
97
+ * Can be used in `remeda` pipelines.
158
98
  *
159
- * @param objects The input objects.
160
- * @returns The output object.
99
+ * @param fn The function to apply to the input.
161
100
  */
162
- export function mergeInputObjects(
163
- ...objects: Input<Record<string, unknown>>[]
164
- ): Output<Record<string, unknown>> {
165
- return output(objects).apply(array => {
166
- return Object.assign({}, ...array) as Record<string, unknown>
167
- })
168
- }
169
-
170
- export function normalize<T>(item: T | undefined, collection: T[] | undefined): T[] {
171
- if (item && collection) {
172
- return [item, ...collection]
173
- }
174
-
175
- if (item) {
176
- return [item]
177
- }
178
-
179
- return collection ?? []
180
- }
181
-
182
101
  export function apply<T, U>(fn: (value: Unwrap<T>) => U): (input: Input<T>) => Output<U> {
183
102
  return input => output(input).apply(fn)
184
103
  }
185
-
186
- export function applyMap<T, U>(fn: (value: Unwrap<T>) => U): (input: Input<T[]>) => Output<U[]> {
187
- return input => output(input).apply(array => array.map(fn))
188
- }
package/src/secret.ts DELETED
@@ -1,30 +0,0 @@
1
- import { secret, type Input, type Output } from "@pulumi/pulumi"
2
-
3
- export const createdSecrets: Record<string, Output<unknown>> = {}
4
-
5
- export function getOrCreateSecret<
6
- TSecrets extends Record<string, unknown>,
7
- TResult extends TSecrets[keyof TSecrets],
8
- >(secrets: Output<TSecrets>, key: keyof TSecrets, create: () => Input<TResult>): Output<TResult> {
9
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
10
- return secrets[key as any].apply(value => {
11
- if (value !== undefined) {
12
- // if the secret is accessed via "getOrCreateSecret" with assume it automatically created (even if it was not)
13
- // in order to provide stable stack outputs
14
- createdSecrets[key as string] = value as Output<unknown>
15
- return value as TResult
16
- }
17
-
18
- const secretValue = createdSecrets[key as string] ?? secret(create())
19
- createdSecrets[key as string] = secretValue
20
-
21
- return secretValue
22
- }) as Output<TResult>
23
- }
24
-
25
- export function storeSecret<
26
- TSecrets extends Record<string, unknown>,
27
- TResult extends TSecrets[keyof TSecrets],
28
- >(_secrets: Output<TSecrets>, key: keyof TSecrets, value: Input<TResult>): void {
29
- createdSecrets[key as string] = value as Output<unknown>
30
- }