Skip to content

Commit

Permalink
fix: fix action payload
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Apr 3, 2024
1 parent 7131bc6 commit 832c925
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
15 changes: 14 additions & 1 deletion gen/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,19 @@ const generateResource = (type: string, name: string, resource: Resource): strin
}


const payloadDataType = (properties: any): any => {
const fields = Object.entries(properties).filter(([p, v]) => (p !== 'attributes')).map(([p, v]: [string, any]) => {
let type = v.type
if ((type === 'string') && v.enum && (v.enum.length > 0)) type = v.enum.map(t => { return `'${t}'`}).join(' | ')
return `${p}: ${type}`
}).join(', ')
const attributes = Object.entries(properties.attributes.properties).map(([k, v]: [string, any]) => `${k}${v.nullable? '?' : ''}: ${v.type}`).join(', ')

return `{ ${fields}, ${attributes} }`

}


const templatedOperation = (res: string, name: string, op: Operation, tpl: string, placeholders?: Record<string, string>): { operation: string, types: string[], typesDef: string[] } => {

let operation = tpl
Expand All @@ -677,7 +690,7 @@ const templatedOperation = (res: string, name: string, op: Operation, tpl: strin
const requestType = op.requestType
operation = operation.replace(/##__RESOURCE_REQUEST_CLASS__##/g, requestType)
if (isObjectType(requestType)) {
const typeDef = `export type ${Inflector.camelize(op.name)}DataType = { ${Object.entries(op.requestTypeDef).map(([k, v]: [string, any]) => `${k}${v.nullable? '?' : ''}: ${v.type}`).join(', ')} }`
const typeDef = `export type ${Inflector.camelize(op.name)}DataType = ${payloadDataType(op.requestTypeDef)}`
typesDef.push(typeDef)
}
else if (!types.includes(requestType)) types.push(requestType)
Expand Down
2 changes: 1 addition & 1 deletion gen/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const parsePaths = (schemaPaths: any[]): PathMap => {
if (id) op.id = id
if (oValue.requestBody) {
op.requestType = contentType(oValue.requestBody.content)
if (isObjectType(op.requestType)) op.requestTypeDef = contentSchema(oValue.requestBody.content).properties.data.properties.attributes.properties
if (isObjectType(op.requestType)) op.requestTypeDef = contentSchema(oValue.requestBody.content).properties.data.properties
}
if (oValue.responses) {
const responses = Object.values(oValue.responses) as any[]
Expand Down
3 changes: 3 additions & 0 deletions src/commercelayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class CommerceLayerProvisioningClient {

}

// ##__CL_RESOURCES_LEAZY_LOADING_START__##
// ##__CL_RESOURCES_LEAZY_LOADING_TEMPLATE:: ##__TAB__##get ##__RESOURCE_TYPE__##(): api.##__RESOURCE_CLASS__## { return this.###__RESOURCE_TYPE__## || (this.###__RESOURCE_TYPE__## = new api.##__RESOURCE_CLASS__##(this.#adapter)) }
// ##__CL_RESOURCES_LEAZY_LOADING_STOP__##
// get environment(): ApiMode { return this.#environment }


Expand Down
5 changes: 4 additions & 1 deletion src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ErrorType, SdkError } from './error'
import { CommerceLayerProvisioningStatic } from './static'

import Debug from './debug'
import { isResourceId } from './common'
const debug = Debug('resource')


Expand Down Expand Up @@ -256,7 +257,9 @@ class ResourceAdapter {
const queryParams = {}
if (options?.params) Object.assign(queryParams, options?.params)

await this.#client.request(cmd, path, payload, { ...options, params: queryParams })
const data = (payload && isResourceId(payload))? normalize(payload) : payload

await this.#client.request(cmd, path, data, { ...options, params: queryParams })

}

Expand Down
2 changes: 1 addition & 1 deletion src/resources/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ class Organizations extends ApiResource<Organization> {
export default Organizations

export type { Organization, OrganizationCreate, OrganizationUpdate, OrganizationType }
export type TransferOwnershipDataType = { new_owner_email: string }
export type TransferOwnershipDataType = { type: 'organizations', id: string, new_owner_email: string }

0 comments on commit 832c925

Please sign in to comment.