|
| 1 | +import { OpenAPIV3 } from "openapi-types"; |
| 2 | +import { AclConditionsPropertyType, Endpoint, EndpointAclInfo } from "src/generators/types/endpoint"; |
| 3 | +import { OperationAclInfo, OperationObject } from "src/generators/types/openapi"; |
| 4 | +import { isParamMediaTypeAllowed, isReferenceObject } from "src/generators/utils/openapi.utils"; |
| 5 | +import { isQuery } from "src/generators/utils/queries.utils"; |
| 6 | +import { decapitalize } from "src/generators/utils/string.utils"; |
| 7 | +import { getMissingAclConditionPropertyError } from "src/generators/utils/validation.utils"; |
| 8 | +import { SchemaResolver } from "../SchemaResolver.class"; |
| 9 | + |
| 10 | +export function getEndpointAcl({ |
| 11 | + resolver, |
| 12 | + endpoint, |
| 13 | + operation, |
| 14 | +}: { |
| 15 | + resolver: SchemaResolver; |
| 16 | + endpoint: Endpoint; |
| 17 | + operation: OperationObject; |
| 18 | +}): EndpointAclInfo[] | undefined { |
| 19 | + const acl = operation["x-acl"]; |
| 20 | + |
| 21 | + return acl?.map((item) => { |
| 22 | + const conditionsTypes = Object.keys(item.conditions ?? {}).reduce((acc, name) => { |
| 23 | + const propertyType = getEndpointAclConditionPropertyType({ resolver, endpoint, acl, name }); |
| 24 | + if (!propertyType) { |
| 25 | + resolver.validationErrors.push( |
| 26 | + getMissingAclConditionPropertyError(name, operation.operationId ?? `${endpoint.method} ${endpoint.path}`), |
| 27 | + ); |
| 28 | + return acc; |
| 29 | + } |
| 30 | + return [...acc, propertyType]; |
| 31 | + }, [] as AclConditionsPropertyType[]); |
| 32 | + |
| 33 | + return { ...item, conditionsTypes }; |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +function getEndpointAclConditionPropertyType({ |
| 38 | + resolver, |
| 39 | + endpoint, |
| 40 | + acl, |
| 41 | + name, |
| 42 | +}: { |
| 43 | + resolver: SchemaResolver; |
| 44 | + endpoint: Endpoint; |
| 45 | + acl: OperationAclInfo[]; |
| 46 | + name: string; |
| 47 | +}): AclConditionsPropertyType | undefined { |
| 48 | + const conditionPropertyPath = acl[0]?.conditions?.[name]; |
| 49 | + if (!conditionPropertyPath) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const pathSplits = conditionPropertyPath.replace(/^\$[^.]*\./, "").split("."); |
| 54 | + |
| 55 | + let schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined = undefined; |
| 56 | + let required: boolean | undefined = undefined; |
| 57 | + let info: string | undefined = undefined; |
| 58 | + let index = 0; |
| 59 | + |
| 60 | + const parameter = endpoint.parameters.find(({ name }) => name === pathSplits[index]); |
| 61 | + if (parameter) { |
| 62 | + required = parameter.parameterObject?.required; |
| 63 | + schema = parameter.parameterObject?.schema; |
| 64 | + info = `${parameter.name} ${decapitalize(parameter.type)} parameter`; |
| 65 | + index++; |
| 66 | + } else { |
| 67 | + const bodyParameter = endpoint.parameters.find(({ bodyObject }) => !!bodyObject); |
| 68 | + const mediaTypes = Object.keys(bodyParameter?.bodyObject?.content ?? {}); |
| 69 | + const matchingMediaType = mediaTypes.find(isParamMediaTypeAllowed); |
| 70 | + if (matchingMediaType) { |
| 71 | + schema = bodyParameter?.bodyObject?.content?.[matchingMediaType]?.schema; |
| 72 | + info = `${isQuery(endpoint) ? "query" : "mutation"} data`; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + while (schema && index < pathSplits.length) { |
| 77 | + const resolvedSchema = resolver.resolveObject(schema); |
| 78 | + const propertySchema = Object.entries(resolvedSchema.properties ?? {}).find( |
| 79 | + ([key]) => key === pathSplits[index], |
| 80 | + )?.[1] as OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject; |
| 81 | + schema = propertySchema; |
| 82 | + required = resolvedSchema.required?.includes(pathSplits[index]) ?? false; |
| 83 | + index++; |
| 84 | + } |
| 85 | + |
| 86 | + if (!schema) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + name, |
| 92 | + type: isReferenceObject(schema) ? undefined : schema.type, |
| 93 | + zodSchemaName: isReferenceObject(schema) ? resolver.getZodSchemaNameByRef(schema.$ref) : undefined, |
| 94 | + required, |
| 95 | + info, |
| 96 | + }; |
| 97 | +} |
0 commit comments