@arrirpc/codegen-utils 0.85.0 → 0.86.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/dist/index.cjs CHANGED
@@ -71,7 +71,40 @@ function normalizeWhitespace(input) {
71
71
  }
72
72
  return result;
73
73
  }
74
+ function matchSchemaForms(schema, matcher) {
75
+ if (typeDefs.isSchemaFormType(schema)) return matcher.type(schema);
76
+ if (typeDefs.isSchemaFormEnum(schema)) return matcher.enum(schema);
77
+ if (typeDefs.isSchemaFormProperties(schema)) return matcher.properties(schema);
78
+ if (typeDefs.isSchemaFormElements(schema)) return matcher.elements(schema);
79
+ if (typeDefs.isSchemaFormValues(schema)) return matcher.values(schema);
80
+ if (typeDefs.isSchemaFormDiscriminator(schema)) return matcher.discriminator(schema);
81
+ if (typeDefs.isSchemaFormUnion(schema)) return matcher.union(schema);
82
+ if (typeDefs.isSchemaFormRef(schema)) return matcher.ref(schema);
83
+ return matcher.empty(schema);
84
+ }
85
+ function getUnionFallbackKey(variants) {
86
+ const keys = Object.keys(variants);
87
+ return keys.find((key) => !isRecursiveSchema(variants[key])) ?? keys[0];
88
+ }
89
+ function isRecursiveSchema(schema) {
90
+ if (schema.isNullable) return false;
91
+ return matchSchemaForms(schema, {
92
+ type: () => false,
93
+ enum: () => false,
94
+ empty: () => false,
95
+ elements: () => false,
96
+ values: () => false,
97
+ ref: () => true,
98
+ properties: (s) => Object.values(s.properties).some(isRecursiveSchema),
99
+ // nested unions fall back to their first non-recursive variant
100
+ discriminator: (s) => Object.values(s.mapping).every(isRecursiveSchema),
101
+ union: (s) => Object.values(s.union).every(isRecursiveSchema)
102
+ });
103
+ }
74
104
 
105
+ exports.getUnionFallbackKey = getUnionFallbackKey;
106
+ exports.isRecursiveSchema = isRecursiveSchema;
107
+ exports.matchSchemaForms = matchSchemaForms;
75
108
  exports.normalizeWhitespace = normalizeWhitespace;
76
109
  exports.removeDisallowedChars = removeDisallowedChars;
77
110
  exports.setNestedObjectProperty = setNestedObjectProperty;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppDefinition, RpcDefinition, ServiceDefinition } from '@arrirpc/type-defs';
1
+ import { SchemaFormType, SchemaFormEnum, SchemaFormProperties, SchemaFormElements, SchemaFormValues, SchemaFormDiscriminator, SchemaFormUnion, SchemaFormRef, Schema, AppDefinition, RpcDefinition, ServiceDefinition } from '@arrirpc/type-defs';
2
2
  export * from '@arrirpc/type-defs';
3
3
  export * from 'scule';
4
4
 
@@ -8,5 +8,33 @@ declare const removeDisallowedChars: (input: string, disallowedChars: string) =>
8
8
  declare const stringStartsWithNumber: (input: string) => boolean;
9
9
  declare function setNestedObjectProperty<T>(targetProp: string, value: T, object: Record<any, any>): Record<any, any>;
10
10
  declare function normalizeWhitespace(input: string): string;
11
+ type SchemaMatcher<T> = {
12
+ type: (schema: SchemaFormType) => T;
13
+ enum: (schema: SchemaFormEnum) => T;
14
+ properties: (schema: SchemaFormProperties) => T;
15
+ elements: (schema: SchemaFormElements) => T;
16
+ values: (schema: SchemaFormValues) => T;
17
+ /**
18
+ * @deprecated
19
+ */
20
+ discriminator: (schema: SchemaFormDiscriminator) => T;
21
+ union: (schema: SchemaFormUnion) => T;
22
+ ref: (schema: SchemaFormRef) => T;
23
+ empty: (schema: SchemaFormElements) => T;
24
+ };
25
+ declare function matchSchemaForms<T = void>(schema: Schema, matcher: SchemaMatcher<T>): T;
26
+ /**
27
+ * Returns the key of the variant that clients should fall back to when a union (or discriminator) can't be decoded.
28
+ * This is the first variant unless it's recursive, in which case it's the next non-recursive variant.
29
+ * If every variant is recursive the first variant is returned.
30
+ */
31
+ declare function getUnionFallbackKey(variants: Record<string, Schema>): string | undefined;
32
+ /**
33
+ * A schema is recursive when building its fallback value would loop back to itself.
34
+ * Refs are only used for self-references, so this is the case when the fallback reaches a non-nullable ref.
35
+ * Nullable schemas, arrays, records, and optional properties fall back to an empty value so they never recurse.
36
+ */
37
+ declare function isRecursiveSchema(schema: Schema): boolean;
11
38
 
12
- export { normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
39
+ export { getUnionFallbackKey, isRecursiveSchema, matchSchemaForms, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
40
+ export type { SchemaMatcher };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppDefinition, RpcDefinition, ServiceDefinition } from '@arrirpc/type-defs';
1
+ import { SchemaFormType, SchemaFormEnum, SchemaFormProperties, SchemaFormElements, SchemaFormValues, SchemaFormDiscriminator, SchemaFormUnion, SchemaFormRef, Schema, AppDefinition, RpcDefinition, ServiceDefinition } from '@arrirpc/type-defs';
2
2
  export * from '@arrirpc/type-defs';
3
3
  export * from 'scule';
4
4
 
@@ -8,5 +8,33 @@ declare const removeDisallowedChars: (input: string, disallowedChars: string) =>
8
8
  declare const stringStartsWithNumber: (input: string) => boolean;
9
9
  declare function setNestedObjectProperty<T>(targetProp: string, value: T, object: Record<any, any>): Record<any, any>;
10
10
  declare function normalizeWhitespace(input: string): string;
11
+ type SchemaMatcher<T> = {
12
+ type: (schema: SchemaFormType) => T;
13
+ enum: (schema: SchemaFormEnum) => T;
14
+ properties: (schema: SchemaFormProperties) => T;
15
+ elements: (schema: SchemaFormElements) => T;
16
+ values: (schema: SchemaFormValues) => T;
17
+ /**
18
+ * @deprecated
19
+ */
20
+ discriminator: (schema: SchemaFormDiscriminator) => T;
21
+ union: (schema: SchemaFormUnion) => T;
22
+ ref: (schema: SchemaFormRef) => T;
23
+ empty: (schema: SchemaFormElements) => T;
24
+ };
25
+ declare function matchSchemaForms<T = void>(schema: Schema, matcher: SchemaMatcher<T>): T;
26
+ /**
27
+ * Returns the key of the variant that clients should fall back to when a union (or discriminator) can't be decoded.
28
+ * This is the first variant unless it's recursive, in which case it's the next non-recursive variant.
29
+ * If every variant is recursive the first variant is returned.
30
+ */
31
+ declare function getUnionFallbackKey(variants: Record<string, Schema>): string | undefined;
32
+ /**
33
+ * A schema is recursive when building its fallback value would loop back to itself.
34
+ * Refs are only used for self-references, so this is the case when the fallback reaches a non-nullable ref.
35
+ * Nullable schemas, arrays, records, and optional properties fall back to an empty value so they never recurse.
36
+ */
37
+ declare function isRecursiveSchema(schema: Schema): boolean;
11
38
 
12
- export { normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
39
+ export { getUnionFallbackKey, isRecursiveSchema, matchSchemaForms, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
40
+ export type { SchemaMatcher };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppDefinition, RpcDefinition, ServiceDefinition } from '@arrirpc/type-defs';
1
+ import { SchemaFormType, SchemaFormEnum, SchemaFormProperties, SchemaFormElements, SchemaFormValues, SchemaFormDiscriminator, SchemaFormUnion, SchemaFormRef, Schema, AppDefinition, RpcDefinition, ServiceDefinition } from '@arrirpc/type-defs';
2
2
  export * from '@arrirpc/type-defs';
3
3
  export * from 'scule';
4
4
 
@@ -8,5 +8,33 @@ declare const removeDisallowedChars: (input: string, disallowedChars: string) =>
8
8
  declare const stringStartsWithNumber: (input: string) => boolean;
9
9
  declare function setNestedObjectProperty<T>(targetProp: string, value: T, object: Record<any, any>): Record<any, any>;
10
10
  declare function normalizeWhitespace(input: string): string;
11
+ type SchemaMatcher<T> = {
12
+ type: (schema: SchemaFormType) => T;
13
+ enum: (schema: SchemaFormEnum) => T;
14
+ properties: (schema: SchemaFormProperties) => T;
15
+ elements: (schema: SchemaFormElements) => T;
16
+ values: (schema: SchemaFormValues) => T;
17
+ /**
18
+ * @deprecated
19
+ */
20
+ discriminator: (schema: SchemaFormDiscriminator) => T;
21
+ union: (schema: SchemaFormUnion) => T;
22
+ ref: (schema: SchemaFormRef) => T;
23
+ empty: (schema: SchemaFormElements) => T;
24
+ };
25
+ declare function matchSchemaForms<T = void>(schema: Schema, matcher: SchemaMatcher<T>): T;
26
+ /**
27
+ * Returns the key of the variant that clients should fall back to when a union (or discriminator) can't be decoded.
28
+ * This is the first variant unless it's recursive, in which case it's the next non-recursive variant.
29
+ * If every variant is recursive the first variant is returned.
30
+ */
31
+ declare function getUnionFallbackKey(variants: Record<string, Schema>): string | undefined;
32
+ /**
33
+ * A schema is recursive when building its fallback value would loop back to itself.
34
+ * Refs are only used for self-references, so this is the case when the fallback reaches a non-nullable ref.
35
+ * Nullable schemas, arrays, records, and optional properties fall back to an empty value so they never recurse.
36
+ */
37
+ declare function isRecursiveSchema(schema: Schema): boolean;
11
38
 
12
- export { normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
39
+ export { getUnionFallbackKey, isRecursiveSchema, matchSchemaForms, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
40
+ export type { SchemaMatcher };
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { isSchemaFormType, isSchemaFormEnum, isSchemaFormProperties, isSchemaFormElements, isSchemaFormValues, isSchemaFormDiscriminator, isSchemaFormUnion, isSchemaFormRef } from '@arrirpc/type-defs';
1
2
  export * from '@arrirpc/type-defs';
2
3
  export * from 'scule';
3
4
 
@@ -69,5 +70,35 @@ function normalizeWhitespace(input) {
69
70
  }
70
71
  return result;
71
72
  }
73
+ function matchSchemaForms(schema, matcher) {
74
+ if (isSchemaFormType(schema)) return matcher.type(schema);
75
+ if (isSchemaFormEnum(schema)) return matcher.enum(schema);
76
+ if (isSchemaFormProperties(schema)) return matcher.properties(schema);
77
+ if (isSchemaFormElements(schema)) return matcher.elements(schema);
78
+ if (isSchemaFormValues(schema)) return matcher.values(schema);
79
+ if (isSchemaFormDiscriminator(schema)) return matcher.discriminator(schema);
80
+ if (isSchemaFormUnion(schema)) return matcher.union(schema);
81
+ if (isSchemaFormRef(schema)) return matcher.ref(schema);
82
+ return matcher.empty(schema);
83
+ }
84
+ function getUnionFallbackKey(variants) {
85
+ const keys = Object.keys(variants);
86
+ return keys.find((key) => !isRecursiveSchema(variants[key])) ?? keys[0];
87
+ }
88
+ function isRecursiveSchema(schema) {
89
+ if (schema.isNullable) return false;
90
+ return matchSchemaForms(schema, {
91
+ type: () => false,
92
+ enum: () => false,
93
+ empty: () => false,
94
+ elements: () => false,
95
+ values: () => false,
96
+ ref: () => true,
97
+ properties: (s) => Object.values(s.properties).some(isRecursiveSchema),
98
+ // nested unions fall back to their first non-recursive variant
99
+ discriminator: (s) => Object.values(s.mapping).every(isRecursiveSchema),
100
+ union: (s) => Object.values(s.union).every(isRecursiveSchema)
101
+ });
102
+ }
72
103
 
73
- export { normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
104
+ export { getUnionFallbackKey, isRecursiveSchema, matchSchemaForms, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/codegen-utils",
3
- "version": "0.85.0",
3
+ "version": "0.86.0",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "joshmossas",
@@ -23,7 +23,7 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "scule": "^1.3.0",
26
- "@arrirpc/type-defs": "0.85.0"
26
+ "@arrirpc/type-defs": "0.86.0"
27
27
  },
28
28
  "devDependencies": {}
29
29
  }