|
| 1 | +// Builds the template context for route files (src/Routes/{Name}Client.php): |
| 2 | +// one resource client class per file, with the use statements for everything |
| 3 | +// its methods reference. |
| 4 | + |
| 5 | +import { |
| 6 | + type PhpClient, |
| 7 | + type PhpClientMethod, |
| 8 | + sortPhpClientMethodParameters, |
| 9 | +} from '../class-model.js' |
| 10 | + |
| 11 | +const seamClientClass = 'Seam\\SeamClient' |
| 12 | +const resourcesNamespace = 'Seam\\Resources' |
| 13 | +const actionAttemptErrorClasses = [ |
| 14 | + 'Seam\\ActionAttemptFailedError', |
| 15 | + 'Seam\\ActionAttemptTimeoutError', |
| 16 | +] |
| 17 | + |
| 18 | +export interface MethodLayoutContext { |
| 19 | + methodName: string |
| 20 | + description: string |
| 21 | + responseDescription: string |
| 22 | + isDeprecated: boolean |
| 23 | + deprecationMessage: string |
| 24 | + parameters: Array<{ name: string; type: string; description: string }> |
| 25 | + path: string |
| 26 | + returnType: string |
| 27 | + hasParams: boolean |
| 28 | + signatureParams: string |
| 29 | + paramNames: string[] |
| 30 | + usesActionAttempt: boolean |
| 31 | + usesOnResponse: boolean |
| 32 | + returnsVoid: boolean |
| 33 | + isArrayResponse: boolean |
| 34 | + returnResource: string |
| 35 | + returnPath: string |
| 36 | +} |
| 37 | + |
| 38 | +export interface ClientLayoutContext { |
| 39 | + clientName: string |
| 40 | + hasChildClients: boolean |
| 41 | + childClients: Array<{ clientName: string; namespace: string }> |
| 42 | + methods: MethodLayoutContext[] |
| 43 | + isActionAttempts: boolean |
| 44 | +} |
| 45 | + |
| 46 | +export interface RouteLayoutContext extends ClientLayoutContext { |
| 47 | + useStatements: string[] |
| 48 | +} |
| 49 | + |
| 50 | +const getMethodLayoutContext = ( |
| 51 | + method: PhpClientMethod, |
| 52 | + clientName: string, |
| 53 | +): MethodLayoutContext => { |
| 54 | + const { methodName, path, parameters, returnResource, returnPath } = method |
| 55 | + |
| 56 | + const usesActionAttempt = |
| 57 | + returnResource === 'ActionAttempt' && clientName !== 'ActionAttempts' |
| 58 | + const usesOnResponse = |
| 59 | + parameters.some((p) => p.name === 'page_cursor') && methodName === 'list' |
| 60 | + const returnsVoid = returnResource === '' |
| 61 | + const returnType = method.isArrayResponse |
| 62 | + ? 'array' |
| 63 | + : returnResource !== '' |
| 64 | + ? returnResource |
| 65 | + : 'void' |
| 66 | + |
| 67 | + const sortedParameters = sortPhpClientMethodParameters(parameters) |
| 68 | + |
| 69 | + const signatureParams = sortedParameters |
| 70 | + .map( |
| 71 | + (p) => |
| 72 | + `${!(p.required ?? false) && p.type !== 'mixed' ? '?' : ''}${p.type} $${p.name}${(p.required ?? false) ? '' : ' = null'}`, |
| 73 | + ) |
| 74 | + .concat(usesActionAttempt ? ['bool $wait_for_action_attempt = true'] : []) |
| 75 | + .concat(usesOnResponse ? ['?callable $on_response = null'] : []) |
| 76 | + .join(', ') |
| 77 | + |
| 78 | + return { |
| 79 | + methodName, |
| 80 | + description: method.description, |
| 81 | + responseDescription: method.responseDescription, |
| 82 | + isDeprecated: method.isDeprecated, |
| 83 | + deprecationMessage: method.deprecationMessage, |
| 84 | + parameters: sortedParameters.map(({ name, type, description }) => ({ |
| 85 | + name, |
| 86 | + type, |
| 87 | + description, |
| 88 | + })), |
| 89 | + path, |
| 90 | + returnType, |
| 91 | + hasParams: parameters.length > 0, |
| 92 | + signatureParams, |
| 93 | + paramNames: sortedParameters.map((p) => p.name), |
| 94 | + usesActionAttempt, |
| 95 | + usesOnResponse, |
| 96 | + returnsVoid, |
| 97 | + isArrayResponse: method.isArrayResponse, |
| 98 | + returnResource, |
| 99 | + returnPath, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Child clients live in the same namespace as their parent, so only the |
| 104 | +// SeamClient, the resource classes returned by the methods, and the action |
| 105 | +// attempt errors thrown by poll_until_ready need importing. |
| 106 | +const getUseStatements = ( |
| 107 | + client: PhpClient, |
| 108 | + isActionAttempts: boolean, |
| 109 | +): string[] => { |
| 110 | + const resourceNames = new Set( |
| 111 | + client.methods |
| 112 | + .map((m) => m.returnResource) |
| 113 | + .filter((resourceName) => resourceName !== ''), |
| 114 | + ) |
| 115 | + |
| 116 | + if (isActionAttempts) resourceNames.add('ActionAttempt') |
| 117 | + |
| 118 | + return [ |
| 119 | + seamClientClass, |
| 120 | + ...[...resourceNames].map((name) => `${resourcesNamespace}\\${name}`), |
| 121 | + ...(isActionAttempts ? actionAttemptErrorClasses : []), |
| 122 | + ].sort((a, b) => a.localeCompare(b)) |
| 123 | +} |
| 124 | + |
| 125 | +export const setRouteLayoutContext = ( |
| 126 | + client: PhpClient, |
| 127 | +): RouteLayoutContext => { |
| 128 | + const isActionAttempts = client.clientName === 'ActionAttempts' |
| 129 | + |
| 130 | + return { |
| 131 | + useStatements: getUseStatements(client, isActionAttempts), |
| 132 | + clientName: client.clientName, |
| 133 | + hasChildClients: client.childClientIdentifiers.length > 0, |
| 134 | + childClients: client.childClientIdentifiers.map((i) => ({ |
| 135 | + clientName: i.clientName, |
| 136 | + namespace: i.namespace, |
| 137 | + })), |
| 138 | + methods: client.methods.map((m) => |
| 139 | + getMethodLayoutContext(m, client.clientName), |
| 140 | + ), |
| 141 | + isActionAttempts, |
| 142 | + } |
| 143 | +} |
0 commit comments