@terraforge/terraform 0.0.19 → 0.0.21

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/dist/index.d.mts CHANGED
@@ -191,6 +191,13 @@ type GetDataProps<T = State> = {
191
191
  type: string;
192
192
  state: T;
193
193
  };
194
+ type RefreshStateProps<T = State> = {
195
+ type: string;
196
+ tag: 'resource' | 'data';
197
+ priorInputState: T;
198
+ priorOutputState: T;
199
+ refreshedState: T;
200
+ };
194
201
  interface Provider {
195
202
  ownResource(id: string): boolean;
196
203
  getResource(props: GetProps): Promise<{
@@ -214,6 +221,7 @@ interface Provider {
214
221
  getData?(props: GetDataProps): Promise<{
215
222
  state: State;
216
223
  }>;
224
+ isRefreshStateEqual?(props: RefreshStateProps): Promise<boolean> | boolean;
217
225
  destroy?(): Promise<void>;
218
226
  }
219
227
  //#endregion
@@ -284,6 +292,13 @@ declare class TerraformProvider implements Provider {
284
292
  }: GetDataProps): Promise<{
285
293
  state: State$1;
286
294
  }>;
295
+ isRefreshStateEqual({
296
+ type,
297
+ tag,
298
+ priorInputState,
299
+ priorOutputState,
300
+ refreshedState
301
+ }: RefreshStateProps): Promise<boolean>;
287
302
  }
288
303
  //#endregion
289
304
  //#region src/plugin/registry.d.ts
package/dist/index.mjs CHANGED
@@ -217,6 +217,16 @@ const generateValue = (prop, ctx) => {
217
217
 
218
218
  //#endregion
219
219
  //#region src/provider.ts
220
+ const areStatesEqual = (left, right) => {
221
+ const replacer = (_, value) => {
222
+ if (value !== null && value instanceof Object && !Array.isArray(value)) return Object.keys(value).sort().reduce((sorted, key) => {
223
+ sorted[key] = value[key];
224
+ return sorted;
225
+ }, {});
226
+ return value;
227
+ };
228
+ return JSON.stringify(left, replacer) === JSON.stringify(right, replacer);
229
+ };
220
230
  var TerraformProvider = class {
221
231
  configured;
222
232
  plugin;
@@ -305,6 +315,16 @@ var TerraformProvider = class {
305
315
  if (!data) throw new Error(`Data source not found ${type}`);
306
316
  return { state: data };
307
317
  }
318
+ async isRefreshStateEqual({ type, tag, priorInputState, priorOutputState, refreshedState }) {
319
+ if (tag === "data") return areStatesEqual(priorOutputState, refreshedState);
320
+ const plugin = await this.configure();
321
+ const mergedState = {
322
+ ...refreshedState,
323
+ ...priorInputState
324
+ };
325
+ const { requiresReplace, plannedState } = await plugin.planResourceChange(type, refreshedState, mergedState, priorInputState);
326
+ return requiresReplace.length === 0 && areStatesEqual(plannedState, refreshedState);
327
+ }
308
328
  };
309
329
 
310
330
  //#endregion
@@ -1997,6 +2017,16 @@ var IncorrectType = class extends TypeError {
1997
2017
  super(`${path.join(".")} should be a ${type}`);
1998
2018
  }
1999
2019
  };
2020
+ const isEmptyOutputValue = (value) => {
2021
+ if (value === null || typeof value === "undefined") return true;
2022
+ if (Array.isArray(value)) return value.length === 0;
2023
+ if (typeof value === "object") return Object.keys(value).length === 0;
2024
+ return false;
2025
+ };
2026
+ const shouldOmitOutputValue = (schema, value) => {
2027
+ if (!(schema.optional || schema.computed)) return false;
2028
+ return isEmptyOutputValue(value);
2029
+ };
2000
2030
  const formatInputState = (schema, state, includeSchemaFields = true, path = []) => {
2001
2031
  if (state === null) return null;
2002
2032
  if (typeof state === "undefined") return null;
@@ -2062,7 +2092,9 @@ const formatOutputState = (schema, state, path = []) => {
2062
2092
  const object = {};
2063
2093
  for (const [key, prop] of Object.entries(schema.properties)) {
2064
2094
  const value = state[key];
2065
- object[camelCase(key)] = formatOutputState(prop, value, [...path, key]);
2095
+ const formatted = formatOutputState(prop, value, [...path, key]);
2096
+ if (shouldOmitOutputValue(prop, formatted)) continue;
2097
+ object[camelCase(key)] = formatted;
2066
2098
  }
2067
2099
  return object;
2068
2100
  }
@@ -2073,7 +2105,9 @@ const formatOutputState = (schema, state, path = []) => {
2073
2105
  const object = {};
2074
2106
  for (const [key, prop] of Object.entries(schema.properties)) {
2075
2107
  const value = state[0][key];
2076
- object[camelCase(key)] = formatOutputState(prop, value, [...path, key]);
2108
+ const formatted = formatOutputState(prop, value, [...path, key]);
2109
+ if (shouldOmitOutputValue(prop, formatted)) continue;
2110
+ object[camelCase(key)] = formatted;
2077
2111
  }
2078
2112
  return object;
2079
2113
  } else return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terraforge/terraform",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",