@toolproof-npm/shared 0.1.77 → 0.1.79

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.
@@ -12,70 +12,35 @@ export declare function generateContentHash(content: string): string;
12
12
  * @returns The path in format: {resourceTypeRef}/{contentHash}
13
13
  */
14
14
  export declare function generateContentAddressedPath(resourceTypeRef: ResourceTypeIdentityJson, content: string): string;
15
- /**
16
- * Options for creating a materialized resource from a potential-output
17
- * Provide either 'path' OR 'content' (which generates a hash-based path)
18
- */
19
- export type CreateMaterializedResourceOptions = Pick<ResourceJson, 'extractedData'> & {
20
- /** Explicit path (if provided, content is ignored) */
21
- path?: string;
22
- /** Content to hash for generating path (used if path not provided) */
23
- content?: string;
24
- /** Optional timestamp (defaults to current ISO timestamp) */
25
- timestamp?: string;
26
- };
27
15
  /**
28
16
  * Options for creating a materialized resource from scratch
29
- * Provide either 'path' OR 'content' (which generates a hash-based path)
30
- * Omits 'kind' (always set to 'materialized') and makes 'timestamp' optional
17
+ * Omits 'kind' (always set to 'materialized'), 'path' (auto-generated), and makes 'timestamp' optional
31
18
  */
32
19
  export type CreateMaterializedResourceFromScratchOptions = Omit<ResourceJson, 'kind' | 'timestamp' | 'path'> & {
33
- /** Explicit path (if provided, content is ignored) */
34
- path?: string;
35
- /** Content to hash for generating path (used if path not provided) */
36
- content?: string;
37
- /** Optional timestamp (defaults to current ISO timestamp) */
20
+ /** Optional timestamp (for replacing existing resources with same timestamp) */
38
21
  timestamp?: string;
39
22
  };
40
23
  /**
41
24
  * Creates a materialized resource from a potential-output resource.
42
- * Handles timestamp generation, extractedData normalization, path generation, and structural conversion.
25
+ * Converts the potential output to a materialized resource by generating a content-addressed path,
26
+ * assigning the content as extractedData, and creating a timestamp.
43
27
  *
44
28
  * @param potentialOutput The potential-output resource to materialize
45
- * @param options Configuration for the materialized resource
29
+ * @param content The actual content produced by the job (assigned directly to extractedData)
46
30
  * @returns A fully materialized ResourceJson
47
31
  *
48
32
  * @example
49
33
  * ```typescript
50
- * // With explicit path
51
- * const materialized = createMaterializedResource(potentialOutput, {
52
- * path: 'TYPE-Natural/abc123...',
53
- * extractedData: { identity: 42 },
54
- * timestamp: '2025-01-03T12:00:00.000Z'
55
- * });
56
- *
57
- * // With content (generates hash-based path)
58
- * const materialized = createMaterializedResource(potentialOutput, {
59
- * content: '{"identity": 42}',
60
- * extractedData: { identity: 42 }
61
- * });
34
+ * const materialized = createMaterializedResourceFromPotentialOutput(
35
+ * potentialOutput,
36
+ * { identity: 42, someData: "result" }
37
+ * );
62
38
  * ```
63
39
  */
64
- export declare function createMaterializedResource(potentialOutput: ResourcePotentialOutputJson, options: CreateMaterializedResourceOptions): ResourceJson;
65
- /**
66
- * Normalizes extractedData to ensure consistent structure with an identity field.
67
- * Handles various input formats:
68
- * - Primitives (numbers, strings): wrapped in { identity: value }
69
- * - Objects with identity: preserved
70
- * - Objects without identity: identity extracted from first suitable field
71
- *
72
- * @param data The raw extracted data
73
- * @returns Normalized extractedData with identity field
74
- */
75
- export declare function normalizeExtractedData(data: JsonDataJson): JsonDataJson;
40
+ export declare function createMaterializedResourceFromPotentialOutput(potentialOutput: ResourcePotentialOutputJson, content: JsonDataJson): ResourceJson;
76
41
  /**
77
42
  * Creates a materialized resource from scratch (for manual resource creation).
78
- * Handles timestamp generation, extractedData normalization, path generation, and structural validation.
43
+ * Generates a content-addressed path from the extractedData and creates a timestamp.
79
44
  * Use this when creating resources manually without a potential-output template.
80
45
  *
81
46
  * @param options All required fields for creating a materialized resource
@@ -83,7 +48,6 @@ export declare function normalizeExtractedData(data: JsonDataJson): JsonDataJson
83
48
  *
84
49
  * @example
85
50
  * ```typescript
86
- * // With explicit path
87
51
  * const materialized = createMaterializedResourceFromScratch({
88
52
  * identity: 'RESOURCE-abc123',
89
53
  * resourceTypeRef: 'TYPE-Natural',
@@ -91,21 +55,8 @@ export declare function normalizeExtractedData(data: JsonDataJson): JsonDataJson
91
55
  * resourceRoleRef: 'ROLE-Manual',
92
56
  * executionRef: 'EXECUTION-Genesis'
93
57
  * },
94
- * path: 'TYPE-Natural/def456...',
95
58
  * extractedData: { identity: 42 },
96
- * timestamp: '2025-01-03T12:00:00.000Z'
97
- * });
98
- *
99
- * // With content (generates hash-based path)
100
- * const materialized = createMaterializedResourceFromScratch({
101
- * identity: 'RESOURCE-abc123',
102
- * resourceTypeRef: 'TYPE-Natural',
103
- * creationContext: {
104
- * resourceRoleRef: 'ROLE-Manual',
105
- * executionRef: 'EXECUTION-Genesis'
106
- * },
107
- * content: '{"identity": 42}',
108
- * extractedData: { identity: 42 }
59
+ * timestamp: '2025-01-03T12:00:00.000Z' // optional
109
60
  * });
110
61
  * ```
111
62
  */
@@ -19,79 +19,38 @@ export function generateContentAddressedPath(resourceTypeRef, content) {
19
19
  }
20
20
  /**
21
21
  * Creates a materialized resource from a potential-output resource.
22
- * Handles timestamp generation, extractedData normalization, path generation, and structural conversion.
22
+ * Converts the potential output to a materialized resource by generating a content-addressed path,
23
+ * assigning the content as extractedData, and creating a timestamp.
23
24
  *
24
25
  * @param potentialOutput The potential-output resource to materialize
25
- * @param options Configuration for the materialized resource
26
+ * @param content The actual content produced by the job (assigned directly to extractedData)
26
27
  * @returns A fully materialized ResourceJson
27
28
  *
28
29
  * @example
29
30
  * ```typescript
30
- * // With explicit path
31
- * const materialized = createMaterializedResource(potentialOutput, {
32
- * path: 'TYPE-Natural/abc123...',
33
- * extractedData: { identity: 42 },
34
- * timestamp: '2025-01-03T12:00:00.000Z'
35
- * });
36
- *
37
- * // With content (generates hash-based path)
38
- * const materialized = createMaterializedResource(potentialOutput, {
39
- * content: '{"identity": 42}',
40
- * extractedData: { identity: 42 }
41
- * });
31
+ * const materialized = createMaterializedResourceFromPotentialOutput(
32
+ * potentialOutput,
33
+ * { identity: 42, someData: "result" }
34
+ * );
42
35
  * ```
43
36
  */
44
- export function createMaterializedResource(potentialOutput, options) {
45
- const { path, content, extractedData, timestamp } = options;
46
- // Determine path: use explicit path or generate from content
47
- const finalPath = path ?? (content ? generateContentAddressedPath(potentialOutput.resourceTypeRef, content) : '');
48
- if (!finalPath) {
49
- throw new Error('Either path or content must be provided');
50
- }
51
- // Normalize extractedData to ensure it has an identity field
52
- const normalizedExtractedData = normalizeExtractedData(extractedData);
37
+ export function createMaterializedResourceFromPotentialOutput(potentialOutput, content) {
38
+ // Generate content-addressed path from the content
39
+ const contentString = JSON.stringify(content);
40
+ const path = generateContentAddressedPath(potentialOutput.resourceTypeRef, contentString);
53
41
  return {
54
42
  identity: potentialOutput.identity,
55
43
  resourceTypeRef: potentialOutput.resourceTypeRef,
56
44
  creationContext: potentialOutput.creationContext,
57
45
  kind: 'materialized',
58
- path: finalPath,
59
- timestamp: timestamp ?? new Date().toISOString(),
60
- extractedData: normalizedExtractedData,
61
- };
62
- }
63
- /**
64
- * Normalizes extractedData to ensure consistent structure with an identity field.
65
- * Handles various input formats:
66
- * - Primitives (numbers, strings): wrapped in { identity: value }
67
- * - Objects with identity: preserved
68
- * - Objects without identity: identity extracted from first suitable field
69
- *
70
- * @param data The raw extracted data
71
- * @returns Normalized extractedData with identity field
72
- */
73
- export function normalizeExtractedData(data) {
74
- // If data is a primitive, wrap it
75
- if (typeof data !== 'object' || data === null) {
76
- return { identity: data };
77
- }
78
- // If data is an object
79
- const dataObj = data;
80
- // If it already has an identity field, preserve everything
81
- if ('identity' in dataObj) {
82
- return data;
83
- }
84
- // Otherwise, try to extract an identity value from common patterns
85
- // This handles cases where the data might have a value field or similar
86
- const identityValue = dataObj.value ?? dataObj.id ?? dataObj.result ?? 0;
87
- return {
88
- identity: identityValue,
89
- ...dataObj,
46
+ path,
47
+ timestamp: new Date().toISOString(),
48
+ extractedData: content,
90
49
  };
91
50
  }
92
51
  /**
93
52
  * Creates a materialized resource from scratch (for manual resource creation).
94
- * Handles timestamp generation, extractedData normalization, path generation, and structural validation.
53
+ * Generates a content-addressed path from the extractedData and creates a timestamp.
95
54
  * Use this when creating resources manually without a potential-output template.
96
55
  *
97
56
  * @param options All required fields for creating a materialized resource
@@ -99,7 +58,6 @@ export function normalizeExtractedData(data) {
99
58
  *
100
59
  * @example
101
60
  * ```typescript
102
- * // With explicit path
103
61
  * const materialized = createMaterializedResourceFromScratch({
104
62
  * identity: 'RESOURCE-abc123',
105
63
  * resourceTypeRef: 'TYPE-Natural',
@@ -107,40 +65,23 @@ export function normalizeExtractedData(data) {
107
65
  * resourceRoleRef: 'ROLE-Manual',
108
66
  * executionRef: 'EXECUTION-Genesis'
109
67
  * },
110
- * path: 'TYPE-Natural/def456...',
111
68
  * extractedData: { identity: 42 },
112
- * timestamp: '2025-01-03T12:00:00.000Z'
113
- * });
114
- *
115
- * // With content (generates hash-based path)
116
- * const materialized = createMaterializedResourceFromScratch({
117
- * identity: 'RESOURCE-abc123',
118
- * resourceTypeRef: 'TYPE-Natural',
119
- * creationContext: {
120
- * resourceRoleRef: 'ROLE-Manual',
121
- * executionRef: 'EXECUTION-Genesis'
122
- * },
123
- * content: '{"identity": 42}',
124
- * extractedData: { identity: 42 }
69
+ * timestamp: '2025-01-03T12:00:00.000Z' // optional
125
70
  * });
126
71
  * ```
127
72
  */
128
73
  export function createMaterializedResourceFromScratch(options) {
129
- const { identity, resourceTypeRef, creationContext, path, content, extractedData, timestamp } = options;
130
- // Determine path: use explicit path or generate from content
131
- const finalPath = path ?? (content ? generateContentAddressedPath(resourceTypeRef, content) : '');
132
- if (!finalPath) {
133
- throw new Error('Either path or content must be provided');
134
- }
135
- // Normalize extractedData to ensure it has an identity field
136
- const normalizedExtractedData = normalizeExtractedData(extractedData);
74
+ const { identity, resourceTypeRef, creationContext, extractedData, timestamp } = options;
75
+ // Generate content-addressed path from the extractedData
76
+ const contentString = JSON.stringify(extractedData);
77
+ const path = generateContentAddressedPath(resourceTypeRef, contentString);
137
78
  return {
138
79
  identity,
139
80
  resourceTypeRef,
140
81
  creationContext,
141
82
  kind: 'materialized',
142
- path: finalPath,
83
+ path,
143
84
  timestamp: timestamp ?? new Date().toISOString(),
144
- extractedData: normalizedExtractedData,
85
+ extractedData,
145
86
  };
146
87
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * as CONSTANTS from './constants.js';
2
2
  export * as TYPES from './_lib/types.js';
3
3
  export * as UTILS from './_lib/utils/utils.js';
4
+ export * as RESOURCE_CREATION from './_lib/utils/resourceCreation.js';
4
5
  export * from './firebaseAdminHelpers.js';
5
6
  export * from './firebaseAdminInit.js';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * as CONSTANTS from './constants.js';
2
2
  export * as TYPES from './_lib/types.js';
3
3
  export * as UTILS from './_lib/utils/utils.js';
4
+ export * as RESOURCE_CREATION from './_lib/utils/resourceCreation.js';
4
5
  export * from './firebaseAdminHelpers.js';
5
6
  export * from './firebaseAdminInit.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolproof-npm/shared",
3
- "version": "0.1.77",
3
+ "version": "0.1.79",
4
4
  "description": "Core library utilities for ToolProof",
5
5
  "keywords": [
6
6
  "toolproof",