Skip to content

Commit

Permalink
fix: fix singleton type
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Dec 20, 2024
1 parent b6c624c commit 55ecca4
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 307 deletions.
4 changes: 1 addition & 3 deletions gen/fixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,11 @@ const fixHeadingEmptyLines = (lines: string[]): string[] => {

const fixResourcesType = (schema: ApiSchema): ApiSchema => {

const skippedKeys = ['user']

const resources = {}

for (const key in schema.resources) {
const type = Inflector.pluralize(key)
if ((type === key) || skippedKeys.includes(key)) resources[key] = schema.resources[key]
if (type === key) resources[key] = schema.resources[key]
else resources[type] = schema.resources[key]
}

Expand Down
14 changes: 6 additions & 8 deletions gen/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const generate = async (localSchema?: boolean) => {

// Remove redundant components and force usage of global resource component
const fixedSchema = await Fixer.fixSchema(schema)

console.log(Object.keys(fixedSchema.resources))

loadTemplates()

Expand Down Expand Up @@ -234,7 +234,7 @@ const updateSdkInterfaces = (resources: { [key: string]: ApiRes }): void => {

const initializations: string[] = []
if (!CONFIG.LEAZY_LOADING) Object.entries(resources).forEach(([type, res]) => {
const fieldName = res.singleton ? Inflector.singularize(type) : type
const fieldName = res.singleton? Inflector.singularize(type) : type
let ini = iniTpl
ini = ini.replace(/##__TAB__##/g, '\t')
ini = ini.replace(/##__RESOURCE_TYPE__##/, fieldName)
Expand All @@ -254,7 +254,7 @@ const updateSdkInterfaces = (resources: { [key: string]: ApiRes }): void => {

const leazyLoaders: string[] = []
if (CONFIG.LEAZY_LOADING) Object.entries(resources).forEach(([type, res]) => {
const fieldName = res.singleton ? Inflector.singularize(type) : type
const fieldName = res.singleton? Inflector.singularize(type) : type
let ll = llTpl
ll = ll.replace(/##__TAB__##/g, '\t')
ll = ll.replace(/##__RESOURCE_TYPE__##/g, fieldName)
Expand Down Expand Up @@ -538,7 +538,7 @@ const generateSpec = (type: string, name: string, resource: Resource): string =>

spec = spec.replace(/##__RESOURCE_CLASS__##/g, name)
spec = spec.replace(/##__RESOURCE_TYPE__##/g, type)
spec = spec.replace(/##__RESOURCE_PATH__##/g, singleton ? Inflector.singularize(type) : type)
spec = spec.replace(/##__RESOURCE_PATH__##/g, singleton? Inflector.singularize(type) : type)
// Clear unused placeholders
spec = spec.replace(/##__RELATIONSHIP_SPECS__##/g, '')
spec = spec.replace(/##__TRIGGER_SPECS__##/g, '')
Expand Down Expand Up @@ -713,7 +713,7 @@ const generateResource = (type: string, name: string, resource: Resource): strin
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(', '))
res = res.replace(/##__MODEL_SORTABLE_INTERFACE__##/, singletonResource ? '' : `, ${resModelInterface}Sort`)
res = res.replace(/##__MODEL_SORTABLE_INTERFACE__##/, singletonResource? '' : `, ${resModelInterface}Sort`)

const importQueryModels = (qryMod.size > 0) ? `import type { ${Array.from(qryMod).sort().reverse().join(', ')} } from '../query'` : ''
res = res.replace(/##__IMPORT_QUERY_MODELS__##/, importQueryModels)
Expand Down Expand Up @@ -773,9 +773,7 @@ const generateResource = (type: string, name: string, resource: Resource): strin
const impResMod: string[] = Array.from(declaredImportsModels)
.filter(i => !typesArray.includes(i)) // excludes resource self reference
.map(i => {
// [TEMP] Fix singleton type problem in provisioning api
const singletonRel = Object.values(global.singletons).includes(i)
const fileRel = Inflector.underscore(singletonRel? i : Inflector.pluralize(i))
const fileRel = Inflector.underscore(Inflector.pluralize(i))
return `import type { ${i}${relationshipTypes.has(i) ? `, ${i}Type` : ''} } from './${fileRel}'`
})
const importStr = impResMod.join('\n') + (impResMod.length ? '\n' : '')
Expand Down
125 changes: 29 additions & 96 deletions gen/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.1",
"info": {
"title": "Commerce Layer Provisioning API",
"version": "1.0.6",
"version": "1.0.7",
"contact": {
"name": "API Support",
"url": "https://commercelayer.io",
Expand Down Expand Up @@ -639,7 +639,7 @@
"summary": "Related identity provider",
"description": "Related identity provider",
"tags": [
"user",
"users",
"has_one"
],
"parameters": [
Expand Down Expand Up @@ -1730,7 +1730,7 @@
},
"/user": {
"get": {
"operationId": "GET/user/userId",
"operationId": "GET/user",
"summary": "Retrieve an user",
"description": "Retrieve an user",
"parameters": [],
Expand All @@ -1752,7 +1752,7 @@
}
},
"patch": {
"operationId": "PATCH/user/userId",
"operationId": "PATCH/user",
"summary": "Updates an user",
"description": "Updates an user",
"tags": [
Expand Down Expand Up @@ -1784,33 +1784,6 @@
}
}
},
"/user/{userId}/identity_providers": {
"get": {
"operationId": "GET/userId/identity_providers",
"summary": "Related user",
"description": "Related user",
"tags": [
"identity_providers",
"has_many"
],
"parameters": [
{
"name": "userId",
"in": "path",
"schema": {
"type": "string"
},
"required": true,
"description": "The resource's id"
}
],
"responses": {
"200": {
"description": "The identity_providers associated to the user"
}
}
}
},
"/versions": {
"get": {
"operationId": "GET/versions",
Expand Down Expand Up @@ -3150,7 +3123,7 @@
"type": "string",
"description": "The resource's type",
"enum": [
"user"
"users"
]
},
"id": {
Expand Down Expand Up @@ -4363,6 +4336,12 @@
"example": false,
"readOnly": true
},
"api_max_regex_length": {
"type": "integer",
"description": "The maximum length for the regular expressions, default is 5000.",
"example": 5000,
"readOnly": true
},
"addresses_phone_required": {
"type": "boolean",
"description": "Indicates if the phone attribute is required for addresses, default is true.",
Expand Down Expand Up @@ -4453,6 +4432,12 @@
"example": true,
"readOnly": true
},
"imports_skip_errors": {
"type": "boolean",
"description": "Disables the interruption of the import in case its errors exceeds the 10% threshold, default is false.",
"example": false,
"readOnly": true
},
"promotions_max_concurrent_count": {
"type": "integer",
"description": "The maximum number of active concurrent promotions allowed for your organization, default is 10.",
Expand All @@ -4477,6 +4462,12 @@
"example": false,
"readOnly": true
},
"tags_max_name_length": {
"type": "integer",
"description": "The maximum length for the tag name, default is 25.",
"example": 25,
"readOnly": true
},
"tax_calculators_errors": {
"type": "boolean",
"description": "Enables raising of API errors in case of tax calculation failure, default is false.",
Expand Down Expand Up @@ -6325,7 +6316,7 @@
"type": "string",
"description": "The resource's type",
"enum": [
"user"
"users"
]
},
"attributes": {
Expand Down Expand Up @@ -6397,30 +6388,7 @@
},
"relationships": {
"type": "object",
"properties": {
"identity_providers": {
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "The resource's type",
"enum": [
"identity_providers"
]
},
"id": {
"type": "string",
"description": "The resource's id",
"example": "XGZwpOSrWL"
}
}
}
}
}
}
"properties": {}
}
}
}
Expand All @@ -6443,7 +6411,7 @@
"type": "string",
"description": "The resource's type",
"enum": [
"user"
"users"
]
},
"attributes": {
Expand Down Expand Up @@ -6518,7 +6486,7 @@
"type": "string",
"description": "The resource's type",
"enum": [
"user"
"users"
]
},
"links": {
Expand All @@ -6535,42 +6503,7 @@
},
"relationships": {
"type": "object",
"properties": {
"identity_providers": {
"type": "object",
"properties": {
"links": {
"type": "object",
"properties": {
"self": {
"type": "string",
"description": "URL"
},
"related": {
"type": "string",
"description": "URL"
}
}
},
"data": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "The resource's type",
"enum": [
"identity_providers"
]
},
"id": {
"type": "string",
"description": "The resource ID"
}
}
}
}
}
}
"properties": {}
}
}
}
Expand Down Expand Up @@ -6790,7 +6723,7 @@
"description": "resource type"
},
{
"name": "user",
"name": "users",
"description": "resource type"
},
{
Expand Down
47 changes: 34 additions & 13 deletions gen/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,17 @@
"sortable": false,
"example": "false"
},
"api_max_regex_length": {
"type": "integer",
"default": 5000,
"desc": "The maximum length for the regular expressions, default is {API_MAX_REGEX_LENGTH}.",
"creatable": false,
"updatable": false,
"fetchable": true,
"filterable": false,
"sortable": false,
"example": "5000"
},
"addresses_phone_required": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -1552,6 +1563,17 @@
"sortable": false,
"example": "true"
},
"imports_skip_errors": {
"type": "boolean",
"default": false,
"desc": "Disables the interruption of the import in case its errors exceeds the 10% threshold, default is {IMPORTS_SKIP_ERRORS}.",
"creatable": false,
"updatable": false,
"fetchable": true,
"filterable": false,
"sortable": false,
"example": "false"
},
"promotions_max_concurrent_count": {
"type": "integer",
"default": 10,
Expand Down Expand Up @@ -1597,6 +1619,17 @@
"sortable": false,
"example": "false"
},
"tags_max_name_length": {
"type": "integer",
"default": 25,
"desc": "The maximum length for the tag name, default is {TAGS_MAX_NAME_LENGTH}.",
"creatable": false,
"updatable": false,
"fetchable": true,
"filterable": false,
"sortable": false,
"example": "25"
},
"tax_calculators_errors": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -2662,18 +2695,7 @@
}
}
},
"relationships": {
"identity_providers": {
"type": "has_many",
"desc": "The identity providers associated to the user.",
"creatable": false,
"updatable": false,
"filterable": true,
"sortable": false,
"parent_resource": "V1::UserResource",
"class_name": "IdentityProvider"
}
},
"relationships": {},
"filters": [
"id",
"email",
Expand All @@ -2686,7 +2708,6 @@
"reference_origin",
"metadata",
"q",
"identity_providers_id",
"ids",
"created_at_from",
"created_at_to",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"clean": "rm -rf ./.nyc_output ./node_modules/.cache ./coverage",
"build": "tsup",
"generate": "tsx gen/generator.ts",
"generate-local": "tsx gen/generator.ts -- --local",
"generate-local": "pnpm generate --local",
"lint": "eslint ./src --ext .ts",
"lintspec": "eslint ./specs/resources/ --ext .spec.ts",
"semantic-release": "semantic-release",
Expand Down
Loading

0 comments on commit 55ecca4

Please sign in to comment.