From d587de6b193713501de181183cbf4d238f5967a0 Mon Sep 17 00:00:00 2001 From: Convly Date: Wed, 6 Jul 2022 15:58:46 +0200 Subject: [PATCH] Handle UID attributes when generating attributes types definition --- .../strapi/lib/types/core/attributes/uid.d.ts | 16 +++++----- .../lib/generators/schemas/attributes.js | 31 +++++++++++++++++-- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/packages/core/strapi/lib/types/core/attributes/uid.d.ts b/packages/core/strapi/lib/types/core/attributes/uid.d.ts index 7e1eb64aff..f620940290 100644 --- a/packages/core/strapi/lib/types/core/attributes/uid.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/uid.d.ts @@ -18,31 +18,31 @@ export interface UIDAttributeOptions { } export interface UIDAttributeProperties< - // UID options - S extends UIDAttributeOptions = UIDAttributeOptions, // Own Schema Reference T extends SchemaUID | undefined = undefined, // Target attribute U extends T extends SchemaUID ? GetAttributesKeysByType - : undefined = undefined + : undefined = undefined, + // UID options + S extends UIDAttributeOptions = UIDAttributeOptions > { targetField?: U; - options?: S; + options?: UIDAttributeOptions & S; } export type UIDAttribute< - // UID options - S extends UIDAttributeOptions = UIDAttributeOptions, // Own Schema Reference T extends SchemaUID | undefined = undefined, // Target attribute U extends T extends SchemaUID ? GetAttributesKeysByType - : undefined = undefined + : undefined = undefined, + // UID options + S extends UIDAttributeOptions = UIDAttributeOptions > = Attribute<'uid'> & // Properties - UIDAttributeProperties & + UIDAttributeProperties & // Options ConfigurableOption & DefaultOption & diff --git a/packages/utils/typescript/lib/generators/schemas/attributes.js b/packages/utils/typescript/lib/generators/schemas/attributes.js index 71bc05929a..ac46abc586 100644 --- a/packages/utils/typescript/lib/generators/schemas/attributes.js +++ b/packages/utils/typescript/lib/generators/schemas/attributes.js @@ -1,5 +1,6 @@ 'use strict'; +const ts = require('typescript'); const { factory } = require('typescript'); const _ = require('lodash/fp'); @@ -185,8 +186,34 @@ const mappers = { decimal() { return ['DecimalAttribute']; }, - uid() { - return ['UIDAttribute']; + uid({ attribute, uid }) { + const { targetField, options } = attribute; + + // If there are no params to compute, then return the attribute type alone + if (targetField === undefined && options === undefined) { + return ['UIDAttribute']; + } + + const params = []; + + // If the targetField property is defined, then reference it, + // otherwise, put `undefined` keyword type nodes as placeholders + const targetFieldParams = _.isUndefined(targetField) + ? [ + factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword), + factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword), + ] + : [factory.createStringLiteral(uid), factory.createStringLiteral(targetField)]; + + params.push(...targetFieldParams); + + // If the options property is defined, transform it to + // a type literral node and add it to the params list + if (_.isObject(options)) { + params.push(toTypeLitteral(options)); + } + + return ['UIDAttribute', params]; }, enumeration() { return ['EnumerationAttribute'];