1
- function concatAndDedup < T > ( left : T [ ] , right : T [ ] ) : T [ ] {
2
- return left . concat ( right ) . filter ( ( elem , index , self ) => {
3
- return index === self . indexOf ( elem ) ;
4
- } ) ;
5
- }
1
+ import { Directive } from './schema-base' ;
2
+ import { InterfaceType } from './schema-intermediate' ;
6
3
7
4
/**
8
5
* Utility enum for Schema class
@@ -12,6 +9,69 @@ export enum SchemaMode {
12
9
CODE = 'CODE' ,
13
10
} ;
14
11
12
+ /**
13
+ * Generates an addition to the schema
14
+ *
15
+ * ```
16
+ * prefix name interfaces directives {
17
+ * field
18
+ * field
19
+ * ...
20
+ * }
21
+ * ```
22
+ */
23
+ export interface SchemaAdditionOptions {
24
+ /**
25
+ * the prefix for this additon (type, interface, enum, input, schema)
26
+ */
27
+ readonly prefix : string ;
28
+ /**
29
+ * the name for this addition (some additions dont need this [i.e. schema])
30
+ *
31
+ * @default - no name
32
+ */
33
+ readonly name ?: string ;
34
+ /**
35
+ * the interface types if this is creating an object type
36
+ *
37
+ * @default - no interfaces
38
+ */
39
+ readonly interfaceTypes ?: InterfaceType [ ] ;
40
+ /**
41
+ * the directives for this type
42
+ *
43
+ * @default - no directives
44
+ */
45
+ readonly directives ?: Directive [ ] ;
46
+ /**
47
+ * the fields to reduce onto the addition
48
+ */
49
+ readonly fields : string [ ] ;
50
+ }
51
+
52
+ /**
53
+ * Generates an addition to the schema
54
+ *
55
+ * @param options the options to produced a stringfied addition
56
+ *
57
+ * @returns the following shape:
58
+ *
59
+ * ```
60
+ * prefix name interfaces directives {
61
+ * field
62
+ * field
63
+ * ...
64
+ * }
65
+ * ```
66
+ */
67
+ export function shapeAddition ( options : SchemaAdditionOptions ) : string {
68
+ const typeName = ( ) : string => { return options . name ? ` ${ options . name } ` : '' ; } ;
69
+ const interfaces = generateInterfaces ( options . interfaceTypes ) ;
70
+ const directives = generateDirectives ( options . directives ) ;
71
+ return options . fields . reduce ( ( acc , field ) =>
72
+ `${ acc } ${ field } \n` , `${ options . prefix } ${ typeName ( ) } ${ interfaces } ${ directives } {\n` ) + '}' ;
73
+ }
74
+
15
75
/**
16
76
* Utility class to represent DynamoDB key conditions.
17
77
*/
@@ -118,4 +178,33 @@ export class Between extends BaseKeyCondition {
118
178
public args ( ) : string [ ] {
119
179
return [ this . arg1 , this . arg2 ] ;
120
180
}
181
+ }
182
+
183
+ function concatAndDedup < T > ( left : T [ ] , right : T [ ] ) : T [ ] {
184
+ return left . concat ( right ) . filter ( ( elem , index , self ) => {
185
+ return index === self . indexOf ( elem ) ;
186
+ } ) ;
187
+ }
188
+
189
+ /**
190
+ * Utility function to generate interfaces for object types
191
+ *
192
+ * @param interfaceTypes the interfaces this object type implements
193
+ */
194
+ function generateInterfaces ( interfaceTypes ?: InterfaceType [ ] ) : string {
195
+ if ( ! interfaceTypes || interfaceTypes . length === 0 ) return '' ;
196
+ return interfaceTypes . reduce ( ( acc , interfaceType ) =>
197
+ `${ acc } ${ interfaceType . name } ,` , ' implements' ) . slice ( 0 , - 1 ) ;
198
+ }
199
+
200
+ /**
201
+ * Utility function to generate directives
202
+ *
203
+ * @param directives the directives of a given type
204
+ * @param delimiter the separator betweeen directives (by default we will add a space)
205
+ */
206
+ function generateDirectives ( directives ?: Directive [ ] , delimiter ?: string ) : string {
207
+ if ( ! directives || directives . length === 0 ) return '' ;
208
+ return directives . reduce ( ( acc , directive ) =>
209
+ `${ acc } ${ directive . statement } ${ delimiter ?? ' ' } ` , ' ' ) . slice ( 0 , - 1 ) ;
121
210
}
0 commit comments