Skip to content

Commit

Permalink
feat: update resources to schema v6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Jul 15, 2024
1 parent ce96904 commit 6689a88
Show file tree
Hide file tree
Showing 52 changed files with 4,318 additions and 2,465 deletions.
23 changes: 15 additions & 8 deletions gen/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,9 @@ const generateResource = (type: string, name: string, resource: Resource): strin


// Operations
const qryMod = new Set<string>()
const resMod = new Set<string>()
const qryMod = new Set<string>() // Query models (Retrieve/List)
const resMod = new Set<string>() // Resource generic models (Es. ResponseList)
// const relMod = new Set<string>() // Relationships models
Object.entries(resource.operations).forEach(([opName, op]) => {
const tpl = op.singleton ? templates['singleton'] : templates[opName]
if (op.singleton) resModelType = 'ApiSingleton'
Expand All @@ -558,7 +559,7 @@ const generateResource = (type: string, name: string, resource: Resource): strin
else {
const tplOp = templatedOperation(resName, opName, op, tpl)
operations.push(tplOp.operation)
tplOp.types.forEach(t => { declaredTypes.add(t) })
tplOp.types.forEach(t => declaredTypes.add(t))
}
}
else {
Expand All @@ -572,6 +573,10 @@ const generateResource = (type: string, name: string, resource: Resource): strin
resMod.add('ListResponse')
}
operations.push(tplrOp.operation)
tplrOp.types.forEach(t => { // Fix tax_calculators issue
// relMod.add(t) // Add releationship type
declaredImportsModels.add(t) // Add import type
})
} else console.log('Unknown operation: ' + opName)
}
})
Expand All @@ -580,23 +585,23 @@ const generateResource = (type: string, name: string, resource: Resource): strin
// Trigger functions (only boolean)
if (CONFIG.TRIGGER_FUNCTIONS) triggerFunctions(type, resName, resource, operations)


if (operations && (operations.length > 0)) declaredImportsCommon.add('ResourcesConfig')

res = res.replace(/##__RESOURCE_MODEL_TYPE__##/g, resModelType)
res = res.replace(/##__RESPONSE_MODELS__##/g, (resMod.size > 0) ? `, ${Array.from(resMod).join(', ')}`: '')
res = res.replace(/##__MODEL_RESOURCE_INTERFACE__##/g, resModelInterface)
res = res.replace(/##__IMPORT_RESOURCE_COMMON__##/, Array.from(declaredImportsCommon).join(', '))

const importQueryModels = (qryMod.size > 0)? `import type { ${Array.from(qryMod).sort().reverse().join(', ')} } from '../query'` : ''
const importQueryModels = (qryMod.size > 0) ? `import type { ${Array.from(qryMod).sort().reverse().join(', ')} } from '../query'` : ''
res = res.replace(/##__IMPORT_QUERY_MODELS__##/, importQueryModels)


// Resource definition
res = res.replace(/##__RESOURCE_TYPE__##/g, type)
res = res.replace(/##__RESOURCE_CLASS__##/g, resName)

const resourceOperations = (operations && (operations.length > 0))? operations.join('\n\n\t') : ''
const resourceOperations = (operations && (operations.length > 0)) ? operations.join('\n\n\t') : ''
res = res.replace(/##__RESOURCE_OPERATIONS__##/, resourceOperations)


Expand All @@ -613,7 +618,9 @@ const generateResource = (type: string, name: string, resource: Resource): strin
const cudSuffix = getCUDSuffix(t)
resourceInterfaces.push(`Resource${cudSuffix}`)
const tplCmp = templatedComponent(resName, t, resource.components[t])
tplCmp.models.forEach(m => declaredImportsModels.add(m))
tplCmp.models.forEach(m => {
if (m !== 'Resource') declaredImportsModels.add(m) // Fix resource_errors issue
})
modelInterfaces.push(tplCmp.component)
if (cudSuffix) tplCmp.models.forEach(t => relationshipTypes.add(t))
})
Expand Down
Loading

0 comments on commit 6689a88

Please sign in to comment.