@@ -89,7 +89,7 @@ const Generator: CollectionConfig = {
89
89
if ( ! context . internal ) context . internal = [ ]
90
90
; ( context . internal as string [ ] ) ?. push ( 'schemas.afterHook' )
91
91
// #region Retrieve data
92
- const schemas : any = { }
92
+ const schemas : Record < string , Schema > = { }
93
93
94
94
const [ graphSchemas , nouns , constraintSpans , examples , jsons ] = ( await Promise . all ( [
95
95
payload
@@ -198,22 +198,21 @@ const Generator: CollectionConfig = {
198
198
processUnarySchemas ( graphSchemas , nouns , nounRegex , schemas , jsonExamples , examples )
199
199
// #region Schema processor
200
200
// Flatten allOf chains in order to help parsing engines that don't support them
201
- const componentSchemas : [ string , JSONSchema ] [ ] = Object . entries ( schemas )
201
+ const componentSchemas : [ string , Schema ] [ ] = Object . entries ( schemas )
202
202
for ( const [ key , schema ] of componentSchemas ) {
203
203
while ( schema . allOf ) {
204
204
const mergedRequired : string [ ] = [ ...( schema . required || [ ] ) ]
205
205
let mergedProperties = schema . properties || { }
206
- const mergedAllOf : JSONSchema [ ] = [ ]
207
- schema . allOf . forEach ( ( s : any ) => {
208
- const dependency = schemas [ s . $ref ?. split ( '/' ) . pop ( ) ]
206
+ const mergedAllOf : JSONSchemaDefinition [ ] = [ ]
207
+ schema . allOf . forEach ( ( s ) => {
208
+ const dependency = schemas [ ( s as JSONSchema ) . $ref ?. split ( '/' ) . pop ( ) || '' ]
209
209
if ( dependency . required ?. length )
210
210
mergedRequired . push (
211
211
...dependency . required . filter ( ( f : string ) => ! mergedRequired . includes ( f ) ) ,
212
212
)
213
213
if ( Object . keys ( dependency . properties || { } ) . length )
214
214
mergedProperties = { ...dependency . properties , ...mergedProperties }
215
- if ( dependency . allOf ?. length )
216
- mergedAllOf . push ( ...dependency . allOf . map ( ( a : JSONSchema ) => a ) )
215
+ if ( dependency . allOf ?. length ) mergedAllOf . push ( ...dependency . allOf . map ( ( a ) => a ) )
217
216
if ( ! schema . title && dependency . title ) schema . title = dependency . title
218
217
if ( ! schema . description && dependency . description )
219
218
schema . description = dependency . description
@@ -318,9 +317,12 @@ const Generator: CollectionConfig = {
318
317
paths : Object . fromEntries (
319
318
Object . entries ( schemas )
320
319
. filter ( ( [ key , _schema ] ) => schemas [ 'Update' + key ] || schemas [ 'New' + key ] )
321
- . flatMap ( ( [ key , schema ] : [ string , any ] ) => {
320
+ . flatMap ( ( [ key , schema ] : [ string , Schema ] ) => {
322
321
const baseSchema = schemas [ 'Update' + key ] || schemas [ 'New' + key ] || schema
323
- const idScheme : [ PropertyKey , any ] [ ] | undefined = getIdScheme ( baseSchema , schemas )
322
+ const idScheme : [ PropertyKey , JSONSchemaDefinition ] [ ] | undefined = getIdScheme (
323
+ baseSchema ,
324
+ schemas ,
325
+ )
324
326
const subject = nouns . find ( ( n ) => nameToKey ( n . name || '' ) === key )
325
327
const permissions = (
326
328
subject ?. permissions || [ 'create' , 'read' , 'update' , 'list' , 'login' , 'rateLimit' ]
@@ -352,9 +354,10 @@ const Generator: CollectionConfig = {
352
354
// #region Add paths for CRUD operations based on permissions
353
355
const basePath = `/${ nameToKey ( plural ) . toLowerCase ( ) } `
354
356
if ( permissions . includes ( 'list' ) ) {
355
- const wrapperTemplate = _ . cloneDeep ( globalWrapperTemplate ) as {
356
- [ key : string ] : any
357
- }
357
+ const wrapperTemplate = _ . cloneDeep ( globalWrapperTemplate ) as Record <
358
+ string ,
359
+ Schema
360
+ >
358
361
const pathParameters = [ ]
359
362
const operationParameters = [ ]
360
363
if ( databaseEngine === 'Payload' ) {
@@ -559,7 +562,7 @@ const Generator: CollectionConfig = {
559
562
if ( permissions . includes ( 'create' ) ) {
560
563
const createSchema : JSONSchema | undefined = fillSchemaTemplate ( {
561
564
schema : { $ref : `#/components/schemas/${ schema . $id } ` } ,
562
- wrapperTemplate : globalWrapperTemplate ,
565
+ wrapperTemplate : globalWrapperTemplate as Record < string , Schema > ,
563
566
replacementFieldPath,
564
567
} )
565
568
postUsed = true
@@ -574,7 +577,7 @@ const Generator: CollectionConfig = {
574
577
_ . set ( createError , errorCodePath || 'error.code' , 400 )
575
578
}
576
579
577
- const create : any = {
580
+ const create = {
578
581
summary : `${ isId ? 'Create' : 'Add' } ${ nounIsPlural ? '' : 'a ' } new ${ title } ` ,
579
582
operationId : `post-${ key . toLowerCase ( ) } ` ,
580
583
responses : {
@@ -667,11 +670,11 @@ const Generator: CollectionConfig = {
667
670
const parameters =
668
671
( idScheme ?. length &&
669
672
idScheme . map ( ( id ) => ( {
670
- schema : { type : id [ 1 ] . type } ,
673
+ schema : { type : ( id [ 1 ] as JSONSchema ) . type } ,
671
674
name : id [ 0 ] ,
672
675
in : 'path' ,
673
676
required : true ,
674
- description : id [ 1 ] . description ,
677
+ description : ( id [ 1 ] as JSONSchema ) . description ,
675
678
} ) ) ) ||
676
679
[ ]
677
680
if ( databaseEngine === 'Payload' ) {
@@ -693,7 +696,7 @@ const Generator: CollectionConfig = {
693
696
if ( permissions . includes ( 'read' ) ) {
694
697
const readSchema : JSONSchema | undefined = fillSchemaTemplate ( {
695
698
schema : { $ref : `#/components/schemas/${ schema . $id } ` } ,
696
- wrapperTemplate : globalWrapperTemplate ,
699
+ wrapperTemplate : globalWrapperTemplate as Record < string , Schema > ,
697
700
replacementFieldPath,
698
701
} )
699
702
retval [ retval . length - 1 ] [ 1 ] . get = {
@@ -755,7 +758,7 @@ const Generator: CollectionConfig = {
755
758
if ( permissions . includes ( 'update' ) ) {
756
759
const updateSchema : JSONSchema | undefined = fillSchemaTemplate ( {
757
760
schema : { $ref : `#/components/schemas/${ schema . $id } ` } ,
758
- wrapperTemplate : globalWrapperTemplate ,
761
+ wrapperTemplate : globalWrapperTemplate as Record < string , Schema > ,
759
762
replacementFieldPath,
760
763
} )
761
764
patchUsed = true
@@ -826,7 +829,7 @@ const Generator: CollectionConfig = {
826
829
}
827
830
if ( permissions . includes ( 'delete' ) ) {
828
831
const deleteSchema : JSONSchema | undefined = fillSchemaTemplate ( {
829
- wrapperTemplate : globalWrapperTemplate ,
832
+ wrapperTemplate : globalWrapperTemplate as Record < string , Schema > ,
830
833
} )
831
834
retval [ retval . length - 1 ] [ 1 ] . delete = {
832
835
responses : {
@@ -921,7 +924,7 @@ function processArraySchemas(
921
924
arrayTypes : { gs : gdl . GraphSchema ; cs : Omit < gdl . ConstraintSpan , 'updatedAt' > } [ ] ,
922
925
nouns : Omit < gdl . GraphSchema | gdl . Noun , 'updatedAt' > [ ] ,
923
926
nounRegex : RegExp ,
924
- schemas : any ,
927
+ schemas : Record < string , Schema > ,
925
928
jsonExamples : { [ k : string ] : JSONSchemaType } ,
926
929
) {
927
930
for ( const { gs : schema } of arrayTypes ) {
@@ -955,7 +958,7 @@ function processArraySchemas(
955
958
956
959
function processBinarySchemas (
957
960
constraintSpans : Omit < gdl . ConstraintSpan , 'updatedAt' > [ ] ,
958
- schemas : any ,
961
+ schemas : Record < string , Schema > ,
959
962
nouns : Omit < gdl . GraphSchema | gdl . Noun , 'updatedAt' > [ ] ,
960
963
jsonExamples : { [ k : string ] : JSONSchemaType } ,
961
964
nounRegex : RegExp ,
@@ -991,7 +994,7 @@ function processBinarySchemas(
991
994
. map (
992
995
( c ) =>
993
996
propertySchema . roles ?. find (
994
- ( r : any ) => r . id !== ( ( c . value as gdl . ConstraintSpan ) . roles [ 0 ] as gdl . Role ) . id ,
997
+ ( r ) => ( r as gdl . Role ) . id !== ( ( c . value as gdl . ConstraintSpan ) . roles [ 0 ] as gdl . Role ) . id ,
995
998
) as gdl . Role ,
996
999
)
997
1000
@@ -1030,11 +1033,11 @@ function processUnarySchemas(
1030
1033
graphSchemas : Omit < gdl . GraphSchema , 'updatedAt' > [ ] ,
1031
1034
nouns : Omit < gdl . Noun | gdl . GraphSchema , 'updatedAt' > [ ] ,
1032
1035
nounRegex : RegExp ,
1033
- schemas : any ,
1036
+ schemas : Record < string , Schema > ,
1034
1037
jsonExamples : { [ k : string ] : JSONSchemaType } ,
1035
1038
examples : Omit < gdl . Graph , 'updatedAt' > [ ] ,
1036
1039
) {
1037
- for ( const unarySchema of graphSchemas . filter ( ( s : any ) => s . roles ?. length === 1 ) ) {
1040
+ for ( const unarySchema of graphSchemas . filter ( ( s ) => s . roles ?. length === 1 ) ) {
1038
1041
const unaryRole = unarySchema . roles ?. [ 0 ] as gdl . Role
1039
1042
const subject = unaryRole ?. noun ?. value as gdl . Noun | gdl . GraphSchema
1040
1043
const reading = ( unarySchema . readings as gdl . Reading [ ] ) ?. [ 0 ]
@@ -1060,7 +1063,7 @@ function processUnarySchemas(
1060
1063
. map (
1061
1064
( c ) =>
1062
1065
unarySchema . roles ?. find (
1063
- ( r : any ) => r . id !== ( ( c . value as gdl . ConstraintSpan ) . roles [ 0 ] as gdl . Role ) . id ,
1066
+ ( r ) => ( r as gdl . Role ) . id !== ( ( c . value as gdl . ConstraintSpan ) . roles [ 0 ] as gdl . Role ) . id ,
1064
1067
) as gdl . Role ,
1065
1068
)
1066
1069
@@ -1082,7 +1085,7 @@ function processUnarySchemas(
1082
1085
// #region Helper functions
1083
1086
function removeDuplicateSchemas (
1084
1087
patchUsed : boolean ,
1085
- schemas : any ,
1088
+ schemas : Record < string , Schema > ,
1086
1089
key : string ,
1087
1090
replacements : string [ ] [ ] ,
1088
1091
postUsed : boolean ,
@@ -1115,15 +1118,15 @@ function removeDuplicateSchemas(
1115
1118
Object . keys ( schemas [ key ] . properties || { } ) . length ) )
1116
1119
) {
1117
1120
postUsed = false
1118
- const allOf : any = schemas [ key ] . allOf
1121
+ const allOf = schemas [ key ] . allOf
1119
1122
if ( ! schemas [ 'New' + key ] ?. allOf ) delete schemas [ key ] . allOf
1120
1123
else if (
1121
- allOf [ 1 ] &&
1124
+ allOf ?. [ 1 ] &&
1122
1125
( schemas [ 'New' + key ] . allOf ?. [ 0 ] as JSONSchema ) . $ref ?. replace ( / U p d a t e | N e w / , '' ) ===
1123
- allOf [ 1 ] . $ref
1126
+ ( allOf [ 1 ] as Schema ) . $ref
1124
1127
)
1125
1128
allOf . splice ( 0 , 1 )
1126
- else allOf [ 0 ] = schemas [ 'New' + key ] . allOf ?. [ 0 ]
1129
+ else if ( allOf ) allOf [ 0 ] = schemas [ 'New' + key ] . allOf ?. [ 0 ] as Schema
1127
1130
1128
1131
const required = schemas [ key ] . required
1129
1132
schemas [ key ] = {
@@ -1157,7 +1160,7 @@ function createErrorTemplates(
1157
1160
) [ ] ,
1158
1161
errorMessagePath : string | null | undefined ,
1159
1162
errorCodePath : string | null | undefined ,
1160
- title : any ,
1163
+ title : string ,
1161
1164
) {
1162
1165
let unauthorizedError : object | undefined = undefined ,
1163
1166
rateError : object | undefined = undefined ,
@@ -1181,40 +1184,40 @@ function createErrorTemplates(
1181
1184
return { unauthorizedError, rateError, notFoundError }
1182
1185
}
1183
1186
1184
- function getIdScheme ( baseSchema : any , schemas : any ) {
1187
+ function getIdScheme ( baseSchema : Schema , schemas : Record < string , Schema > ) {
1185
1188
let idSchema = baseSchema
1186
- let idScheme : [ PropertyKey , any ] [ ] | undefined =
1187
- idSchema ?. properties &&
1188
- Object . entries ( idSchema . properties ) ?. filter ( ( p : any ) =>
1189
- p [ 1 ] ?. description ?. includes ( 'is uniquely identified by' ) ,
1190
- )
1189
+ let idScheme : [ PropertyKey , JSONSchemaDefinition ] [ ] | undefined = idSchema ?. properties
1190
+ ? Object . entries ( idSchema . properties ) ?. filter ( ( p ) =>
1191
+ ( p [ 1 ] as JSONSchema ) ?. description ?. includes ( 'is uniquely identified by' ) ,
1192
+ )
1193
+ : undefined
1191
1194
while ( ! idScheme ?. length && idSchema ?. allOf ) {
1192
1195
const key = ( idSchema . allOf [ 0 ] as JSONSchema ) ?. $ref ?. split ( '/' ) ?. pop ( ) as string
1193
1196
const bareKey = key . replace ( / ^ N e w | ^ U p d a t e / , '' )
1194
1197
idSchema = schemas [ 'Update' + bareKey ] || schemas [ 'New' + bareKey ] || schemas [ bareKey ]
1195
1198
idScheme =
1196
1199
idSchema ?. properties &&
1197
- Object . entries ( idSchema . properties ) ?. filter ( ( p : any ) =>
1198
- p [ 1 ] ?. description ?. includes ( 'is uniquely identified by' ) ,
1200
+ Object . entries ( idSchema . properties ) ?. filter ( ( p ) =>
1201
+ ( p [ 1 ] as JSONSchema ) ?. description ?. includes ( 'is uniquely identified by' ) ,
1199
1202
)
1200
1203
}
1201
1204
1202
1205
if ( idScheme ?. length )
1203
1206
for ( let i = 0 ; i < idScheme . length ; i ++ ) {
1204
- let value = idScheme [ i ] [ 1 ]
1207
+ let value = idScheme [ i ] [ 1 ] as Schema
1205
1208
while ( value . oneOf ) {
1206
1209
const description = value . description
1207
- value = value . oneOf [ 0 ]
1210
+ value = value . oneOf [ 0 ] as Schema
1208
1211
if ( value . properties ) {
1209
1212
idScheme . splice (
1210
1213
i ,
1211
1214
1 ,
1212
- ...( Object . entries ( value . properties ) . map ( ( p : [ PropertyKey , any ] ) => [
1215
+ ...( Object . entries ( value . properties ) . map ( ( p : [ PropertyKey , JSONSchemaDefinition ] ) => [
1213
1216
p [ 0 ] ,
1214
- { description, ...p [ 1 ] } ,
1215
- ] ) as [ PropertyKey , any ] [ ] ) ,
1217
+ { description, ...( p [ 1 ] as JSONSchema ) } ,
1218
+ ] ) as [ PropertyKey , JSONSchemaDefinition ] [ ] ) ,
1216
1219
)
1217
- value = idScheme [ i ] [ 1 ]
1220
+ value = idScheme [ i ] [ 1 ] as Schema
1218
1221
} else idScheme [ i ] [ 1 ] = { description, ...value }
1219
1222
}
1220
1223
}
@@ -1228,18 +1231,18 @@ function fillSchemaTemplate({
1228
1231
replacementFieldPath,
1229
1232
} : {
1230
1233
schema ?: JSONSchema
1231
- example ?: any
1232
- wrapperTemplate : any
1233
- replacementFieldPath ?: any
1234
+ example ?: unknown
1235
+ wrapperTemplate ?: Record < string , Schema >
1236
+ replacementFieldPath ?: string | null
1234
1237
} ) {
1235
1238
const jsonSchema = new Draft06 ( )
1236
- if ( Object . keys ( wrapperTemplate ) . length ) {
1239
+ if ( wrapperTemplate && Object . keys ( wrapperTemplate ) . length ) {
1237
1240
const wrapperSchema = jsonSchema . createSchemaOf ( wrapperTemplate )
1238
1241
if ( replacementFieldPath && _ . get ( wrapperSchema . properties , replacementFieldPath ) )
1239
1242
if ( schema ) _ . set ( wrapperSchema . properties , replacementFieldPath , schema )
1240
1243
else _ . unset ( wrapperSchema . properties , replacementFieldPath )
1241
1244
if ( example ) {
1242
- wrapperTemplate [ replacementFieldPath ] = example
1245
+ wrapperTemplate [ replacementFieldPath || '' ] = example
1243
1246
wrapperSchema . examples = [ wrapperTemplate ]
1244
1247
}
1245
1248
schema = wrapperSchema
@@ -1263,7 +1266,7 @@ function createProperty({
1263
1266
}
1264
1267
} ) {
1265
1268
object = nouns . find ( ( n ) => n . id === object . id ) || object
1266
- const property : JSONSchemaDefinition = { }
1269
+ const property : Schema = { }
1267
1270
let { referenceScheme, superType, valueType } = object as gdl . Noun
1268
1271
if ( ! referenceScheme ) {
1269
1272
referenceScheme =
@@ -1286,7 +1289,7 @@ function createProperty({
1286
1289
property . enum = noun . enum . split ( ',' ) . map ( ( e ) => {
1287
1290
const val = e . trim ( )
1288
1291
if ( val === 'null' ) {
1289
- ; ( property as any ) . nullable = true
1292
+ property . nullable = true
1290
1293
return null
1291
1294
}
1292
1295
return val
@@ -1586,3 +1589,12 @@ export function extractPropertyName(objectReading: string[]) {
1586
1589
return propertyName
1587
1590
}
1588
1591
// #endregion
1592
+
1593
+ type Schema = JSONSchema & {
1594
+ nullable ?: true | false
1595
+ properties ?:
1596
+ | {
1597
+ [ k : string ] : Schema
1598
+ }
1599
+ | undefined
1600
+ }
0 commit comments