@aws-amplify/data-schema 1.2.7 → 1.2.9

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.
@@ -117,6 +117,10 @@ type RDSModelSchemaFunctions =
117
117
  | 'renameModelFields'
118
118
  | 'renameModels';
119
119
 
120
+ type OmitFromEach<Models, Modifier extends string> = {
121
+ [ModelName in keyof Models]: Omit<Models[ModelName], Modifier>;
122
+ };
123
+
120
124
  export type RDSModelSchema<
121
125
  T extends RDSModelSchemaParamShape,
122
126
  UsedMethods extends RDSModelSchemaFunctions = never,
@@ -173,7 +177,7 @@ export type RDSModelSchema<
173
177
  >;
174
178
  setAuthorization: (
175
179
  callback: (
176
- models: BaseSchema<T, true>['models'],
180
+ models: OmitFromEach<BaseSchema<T, true>['models'], 'secondaryIndexes'>,
177
181
  schema: RDSModelSchema<T, UsedMethods | 'setAuthorization'>,
178
182
  ) => void,
179
183
  ) => RDSModelSchema<T>;
@@ -182,7 +186,12 @@ export type RDSModelSchema<
182
186
  Partial<Record<keyof T['types'], RelationshipTemplate>>
183
187
  >,
184
188
  >(
185
- callback: (models: BaseSchema<T, true>['models']) => Relationships,
189
+ callback: (
190
+ models: OmitFromEach<
191
+ BaseSchema<T, true>['models'],
192
+ 'authorization' | 'fields' | 'secondaryIndexes'
193
+ >,
194
+ ) => Relationships,
186
195
  ) => RDSModelSchema<
187
196
  UnionToIntersection<Relationships[number]> extends infer RelationshipsDefs
188
197
  ? RelationshipsDefs extends Record<string, RelationshipTemplate>
@@ -1245,13 +1245,29 @@ const schemaPreprocessor = (
1245
1245
  getRefType,
1246
1246
  );
1247
1247
 
1248
+ topLevelTypes.push(...implicitTypes);
1249
+
1248
1250
  mergeCustomTypeAuthRules(
1249
1251
  customTypeInheritedAuthRules,
1250
1252
  customTypeAuthRules,
1251
1253
  );
1252
- Object.assign(lambdaFunctions, lambdaFunctionDefinition);
1253
1254
 
1254
- topLevelTypes.push(...implicitTypes);
1255
+ if (customTypeAuthRules) {
1256
+ const nestedCustomTypeNames = extractNestedCustomTypeNames(
1257
+ customTypeAuthRules,
1258
+ topLevelTypes,
1259
+ getRefType,
1260
+ );
1261
+
1262
+ for (const nestedCustomType of nestedCustomTypeNames) {
1263
+ mergeCustomTypeAuthRules(customTypeInheritedAuthRules, {
1264
+ typeName: nestedCustomType,
1265
+ authRules: customTypeAuthRules.authRules, // apply the same auth rules as the top-level custom type
1266
+ });
1267
+ }
1268
+ }
1269
+
1270
+ Object.assign(lambdaFunctions, lambdaFunctionDefinition);
1255
1271
 
1256
1272
  if (jsFunctionForField) {
1257
1273
  jsFunctions.push(jsFunctionForField);
@@ -1666,6 +1682,55 @@ function generateCustomOperationTypes({
1666
1682
  return types;
1667
1683
  }
1668
1684
 
1685
+ function extractNestedCustomTypeNames(
1686
+ customTypeAuthRules: CustomTypeAuthRules,
1687
+ topLevelTypes: [string, any][],
1688
+ getRefType: ReturnType<typeof getRefTypeForSchema>,
1689
+ ): string[] {
1690
+ if (!customTypeAuthRules) {
1691
+ return [];
1692
+ }
1693
+
1694
+ const [_, customTypeDef] = topLevelTypes.find(
1695
+ ([topLevelTypeName]) => customTypeAuthRules.typeName === topLevelTypeName,
1696
+ )!;
1697
+
1698
+ // traverse the custom type's fields and extract any nested custom type names.
1699
+ // Those nested custom types also inherit the custom op's auth configuration.
1700
+ // Supports both inline custom types and refs to custom types
1701
+ const traverseCustomTypeFields = (
1702
+ name: string,
1703
+ typeDef: any,
1704
+ namesList: string[] = [],
1705
+ ) => {
1706
+ const fields = typeDef.data.fields as Record<string, any>;
1707
+
1708
+ for (const [fieldName, fieldDef] of Object.entries(fields)) {
1709
+ if (isCustomType(fieldDef)) {
1710
+ const customTypeName = `${capitalize(name)}${capitalize(fieldName)}`;
1711
+ namesList.push(customTypeName);
1712
+ traverseCustomTypeFields(customTypeName, fieldDef, namesList);
1713
+ } else if (isRefField(fieldDef)) {
1714
+ const refType = getRefType(fieldDef.data.link, name);
1715
+
1716
+ if (refType.type === 'CustomType') {
1717
+ namesList.push(fieldDef.data.link);
1718
+ traverseCustomTypeFields(fieldDef.data.link, refType.def, namesList);
1719
+ }
1720
+ }
1721
+ }
1722
+
1723
+ return namesList;
1724
+ };
1725
+
1726
+ const nestedCustomTypeNames = traverseCustomTypeFields(
1727
+ customTypeAuthRules.typeName,
1728
+ customTypeDef,
1729
+ );
1730
+
1731
+ return nestedCustomTypeNames;
1732
+ }
1733
+
1669
1734
  /**
1670
1735
  * Returns API definition from ModelSchema or string schema
1671
1736
  * @param arg - { schema }