@highstate/contract 0.1.1 → 0.2.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.
package/package.json CHANGED
@@ -1,24 +1,32 @@
1
1
  {
2
2
  "name": "@highstate/contract",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
+ "exports": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.mjs"
14
+ }
15
+ },
10
16
  "publishConfig": {
11
17
  "access": "public"
12
18
  },
13
19
  "scripts": {
14
- "build": "pkgroll",
15
- "dev": "pkgroll --watch"
20
+ "build": "pkgroll --clean-dist",
21
+ "test": "jest"
16
22
  },
17
23
  "dependencies": {
18
- "zod": "^3.23.8"
24
+ "@sinclair/typebox": "^0.34.11",
25
+ "remeda": "^2.17.4"
19
26
  },
20
27
  "devDependencies": {
28
+ "jest": "^29.7.0",
21
29
  "pkgroll": "^2.5.1"
22
30
  },
23
- "gitHead": "63cce0eb6a93b6efff80882017271c4a4ba0c4a7"
31
+ "gitHead": "41f789ef9d6da623923812bba9783c216605495d"
24
32
  }
package/dist/index.d.ts DELETED
@@ -1,140 +0,0 @@
1
- import { z, ZodType, ZodObject } from 'zod';
2
-
3
- type StackDependency = Readonly<{
4
- project: string;
5
- stack: string;
6
- }>;
7
- type StackRegistration = Readonly<{
8
- project: string;
9
- stack: string;
10
- config: Readonly<Record<string, unknown>>;
11
- secrets: readonly string[];
12
- dependencies: readonly StackDependency[];
13
- }>;
14
- interface EvaluationContext {
15
- registerStack(registration: StackRegistration): void;
16
- getSecret(name: string): string | undefined;
17
- }
18
- declare function runEvaluation<T>(context: EvaluationContext, fn: () => T): T;
19
- declare function getCurrentEvaluationContext(): EvaluationContext;
20
-
21
- type ListRequiredKeys<T extends Record<string, unknown>> = {
22
- [K in keyof T]: undefined extends T[K] ? never : K;
23
- }[keyof T];
24
- type PickOptionalRecords<T extends Record<string, Record<string, unknown>>> = {
25
- [K in keyof T]: ListRequiredKeys<T[K]> extends never ? K : never;
26
- }[keyof T];
27
- type PickRequiredRecords<T extends Record<string, Record<string, unknown>>> = {
28
- [K in keyof T]: ListRequiredKeys<T[K]> extends never ? never : K;
29
- }[keyof T];
30
- type OptionalEmptyFields<T extends Record<string, Record<string, unknown>>> = {
31
- [K in PickOptionalRecords<T>]?: T[K];
32
- } & {
33
- [K in PickRequiredRecords<T>]: T[K];
34
- };
35
-
36
- declare const componentArgType: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodArray<z.ZodString, "many">, z.ZodUndefined]>;
37
- declare const secretType: z.ZodUnion<[z.ZodString, z.ZodUndefined]>;
38
- declare const inputType: z.ZodObject<{
39
- type: z.ZodString;
40
- }, "strip", z.ZodTypeAny, {
41
- type: string;
42
- }, {
43
- type: string;
44
- }>;
45
- declare const outputRefType: z.ZodObject<{
46
- $output: z.ZodString;
47
- project: z.ZodString;
48
- stack: z.ZodString;
49
- }, "strip", z.ZodTypeAny, {
50
- $output: string;
51
- project: string;
52
- stack: string;
53
- }, {
54
- $output: string;
55
- project: string;
56
- stack: string;
57
- }>;
58
- type Input<T = unknown> = T extends (infer U)[] ? (U | OutputRef)[] : T | OutputRef;
59
- type Inputs<T extends Record<string, unknown> = Record<string, unknown>> = {
60
- [K in keyof T]: Input<T[K]>;
61
- };
62
- type ComponentArg = z.infer<typeof componentArgType>;
63
- type Secret = z.infer<typeof secretType>;
64
- type InputType = z.infer<typeof inputType>;
65
- type OutputRef = z.infer<typeof outputRefType>;
66
-
67
- interface ComponentParams<TArgs extends Record<string, unknown> = Record<string, unknown>, TSecrets extends Record<string, Secret> = Record<string, string>, TInputs extends Record<string, unknown> = Record<string, unknown>> {
68
- name: string;
69
- args: TArgs;
70
- secrets: TSecrets;
71
- inputs: Inputs<TInputs>;
72
- context: EvaluationContext;
73
- }
74
- type InputComponentParams<TArgs extends Record<string, ComponentArg>, TInputs extends Record<string, unknown>> = {
75
- name: string;
76
- } & OptionalEmptyFields<{
77
- args: TArgs;
78
- inputs: Inputs<TInputs>;
79
- }>;
80
- declare const componentMetaType: z.ZodObject<{
81
- displayName: z.ZodString;
82
- description: z.ZodString;
83
- primaryIcon: z.ZodOptional<z.ZodString>;
84
- primaryIconColor: z.ZodOptional<z.ZodString>;
85
- secondaryIcon: z.ZodOptional<z.ZodString>;
86
- secondaryIconColor: z.ZodOptional<z.ZodString>;
87
- category: z.ZodOptional<z.ZodString>;
88
- color: z.ZodOptional<z.ZodString>;
89
- }, "strip", z.ZodTypeAny, {
90
- displayName: string;
91
- description: string;
92
- primaryIcon?: string | undefined;
93
- primaryIconColor?: string | undefined;
94
- secondaryIcon?: string | undefined;
95
- secondaryIconColor?: string | undefined;
96
- category?: string | undefined;
97
- color?: string | undefined;
98
- }, {
99
- displayName: string;
100
- description: string;
101
- primaryIcon?: string | undefined;
102
- primaryIconColor?: string | undefined;
103
- secondaryIcon?: string | undefined;
104
- secondaryIconColor?: string | undefined;
105
- category?: string | undefined;
106
- color?: string | undefined;
107
- }>;
108
- type ComponentMeta = z.infer<typeof componentMetaType>;
109
- type ComponentOptions<TArgs extends Record<string, ComponentArg>, TSecrets extends Record<string, Secret>, TInputs extends Record<string, unknown>, TOutputs extends Record<string, unknown>> = {
110
- type: string;
111
- meta: ComponentMeta;
112
- args?: ZodType<TArgs>;
113
- inputs?: ZodType<TInputs>;
114
- secrets?: ZodType<TSecrets>;
115
- outputs: ZodType<TOutputs>;
116
- create: (params: ComponentParams<TArgs, TSecrets, TInputs>) => Inputs<NoInfer<TOutputs>>;
117
- };
118
- type UnitOptions<TArgs extends Record<string, ComponentArg>, TSecrets extends Record<string, Secret>, TInputs extends Record<string, InputType>, TOutputs extends Record<string, InputType>> = Omit<ComponentOptions<TArgs, TSecrets, TInputs, TOutputs>, "create">;
119
- type Component<TArgs extends Record<string, ComponentArg> = {}, TSecrets extends Record<string, unknown> = {}, TInputs extends Record<string, InputType> = {}, TOutputs extends Record<string, InputType> = {}> = {
120
- type: string;
121
- meta: ComponentMeta;
122
- args: ZodObject<{
123
- [K in keyof TArgs]: ZodType<unknown>;
124
- }>;
125
- inputs: ZodObject<{
126
- [K in keyof TInputs]: ZodType<unknown>;
127
- }>;
128
- secrets: ZodObject<{
129
- [K in keyof TSecrets]: ZodType<Secret>;
130
- }>;
131
- outputs: ZodObject<{
132
- [K in keyof TOutputs]: ZodType<unknown>;
133
- }>;
134
- (context: InputComponentParams<TArgs, TInputs>): Inputs<TOutputs>;
135
- };
136
- declare function isComponent(arg: unknown): arg is Component;
137
- declare function defineComponent<TArgs extends Record<string, ComponentArg> = {}, TSecrets extends Record<string, Secret> = {}, TInputs extends Record<string, InputType> = {}, TOutputs extends Record<string, InputType> = {}>(options: ComponentOptions<TArgs, TSecrets, TInputs, TOutputs>): Component<TArgs, TSecrets, TInputs, TOutputs>;
138
- declare function defineUnit<TArgs extends Record<string, ComponentArg>, TSecrets extends Record<string, Secret>, TInputs extends Record<string, InputType>, TOutputs extends Record<string, InputType>>(options: UnitOptions<TArgs, TSecrets, TInputs, TOutputs>): Component<TArgs, TSecrets, TInputs, TOutputs>;
139
-
140
- export { type Component, type ComponentMeta, type ComponentOptions, type ComponentParams, type EvaluationContext, type InputComponentParams, type StackDependency, type StackRegistration, type UnitOptions, componentMetaType, defineComponent, defineUnit, getCurrentEvaluationContext, isComponent, runEvaluation };
package/dist/index.mjs DELETED
@@ -1,114 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- let evaluationContext;
4
- function runEvaluation(context, fn) {
5
- evaluationContext = context;
6
- try {
7
- return fn();
8
- } finally {
9
- evaluationContext = void 0;
10
- }
11
- }
12
- function getCurrentEvaluationContext() {
13
- if (!evaluationContext) {
14
- throw new Error("This method must be called while evaluating a configuration.");
15
- }
16
- return evaluationContext;
17
- }
18
-
19
- z.union([
20
- z.string(),
21
- z.number(),
22
- z.boolean(),
23
- z.array(z.string()),
24
- z.undefined()
25
- ]);
26
- z.union([z.string(), z.undefined()]);
27
- z.object({ type: z.string() });
28
- const outputRefType = z.object({
29
- $output: z.string(),
30
- project: z.string(),
31
- stack: z.string()
32
- });
33
- function isOutputRef(value) {
34
- return outputRefType.safeParse(value).success;
35
- }
36
-
37
- const componentMetaType = z.object({
38
- displayName: z.string(),
39
- description: z.string(),
40
- primaryIcon: z.string().optional(),
41
- primaryIconColor: z.string().optional(),
42
- secondaryIcon: z.string().optional(),
43
- secondaryIconColor: z.string().optional(),
44
- category: z.string().optional(),
45
- color: z.string().optional()
46
- });
47
- function isComponent(arg) {
48
- return typeof arg === "function" && "type" in arg && "meta" in arg;
49
- }
50
- function defineComponent(options) {
51
- function create(params) {
52
- const { name, args, inputs } = params;
53
- const context = getCurrentEvaluationContext();
54
- const secrets = retreiveSecrets(options.secrets, context);
55
- return options.create({
56
- name,
57
- args: args ?? {},
58
- secrets,
59
- inputs: inputs ?? {},
60
- context
61
- });
62
- }
63
- create.type = options.type;
64
- create.meta = options.meta ?? {};
65
- create.args = options.args ?? z.object({});
66
- create.inputs = options.inputs ?? z.object({});
67
- create.secrets = options.secrets ?? z.object({});
68
- create.outputs = options.outputs ?? z.object({});
69
- return create;
70
- }
71
- function defineUnit(options) {
72
- return defineComponent({
73
- ...options,
74
- create({ name, args, secrets, inputs, context }) {
75
- context.registerStack({
76
- project: options.type,
77
- stack: name,
78
- config: args,
79
- secrets: Object.keys(secrets),
80
- dependencies: Object.values(inputs).flatMap((value) => {
81
- if (isOutputRef(value)) {
82
- return [{ project: value.project, stack: value.stack }];
83
- }
84
- return [];
85
- })
86
- });
87
- return fillUnitOutputs(name, options.type, options.outputs);
88
- }
89
- });
90
- }
91
- function retreiveSecrets(secrets, context) {
92
- if (!secrets) {
93
- return {};
94
- }
95
- const secretValues = {};
96
- const secretType = secrets;
97
- for (const key of Object.keys(secretType.shape)) {
98
- const secret = context.getSecret(key);
99
- if (secret) {
100
- secretValues[key] = secret;
101
- }
102
- }
103
- return secrets.parse(secretValues);
104
- }
105
- function fillUnitOutputs(name, type, optionsOutputs) {
106
- const outputs = {};
107
- const outputType = optionsOutputs;
108
- for (const key of Object.keys(outputType.shape)) {
109
- outputs[key] = { $output: key, project: type, stack: name };
110
- }
111
- return outputs;
112
- }
113
-
114
- export { componentMetaType, defineComponent, defineUnit, getCurrentEvaluationContext, isComponent, runEvaluation };