|
1 | 1 | import type { Blueprint, Endpoint, Namespace, Route } from '@seamapi/blueprint' |
| 2 | +import type { Method } from 'axios' |
2 | 3 | import { camelCase, kebabCase, pascalCase } from 'change-case' |
3 | 4 | import type Metalsmith from 'metalsmith' |
4 | 5 |
|
@@ -88,12 +89,18 @@ interface RouteLayoutContext { |
88 | 89 |
|
89 | 90 | interface EndpointLayoutContext { |
90 | 91 | path: string |
| 92 | + methodName: string |
| 93 | + method: Method |
| 94 | + hasOptions: boolean |
| 95 | + responseKey: string |
91 | 96 | requestFormat: 'params' | 'body' |
92 | 97 | requestTypeName: string |
93 | 98 | responseTypeName: string |
94 | 99 | requestFormatSuffix: string |
95 | 100 | optionsTypeName: string |
96 | 101 | returnsActionAttempt: boolean |
| 102 | + returnsVoid: boolean |
| 103 | + isOptionalParamsOk: boolean |
97 | 104 | } |
98 | 105 |
|
99 | 106 | interface SubrouteLayoutContext { |
@@ -152,15 +159,43 @@ const getEndpointLayoutContext = ( |
152 | 159 | : 'body' |
153 | 160 |
|
154 | 161 | const requestFormatSuffix = pascalCase(requestFormat) |
| 162 | + |
| 163 | + const returnsActionAttempt = |
| 164 | + endpoint.response.responseType === 'resource' && |
| 165 | + endpoint.response.resourceType === 'action_attempt' |
| 166 | + |
155 | 167 | return { |
156 | 168 | path: endpoint.path, |
| 169 | + methodName: camelCase(endpoint.name), |
| 170 | + method: endpoint.request.preferredMethod, |
| 171 | + hasOptions: returnsActionAttempt, |
157 | 172 | requestFormat, |
158 | 173 | requestFormatSuffix, |
159 | | - returnsActionAttempt: |
160 | | - endpoint.response.responseType === 'resource' && |
161 | | - endpoint.response.resourceType === 'action_attempt', |
| 174 | + returnsActionAttempt, |
162 | 175 | requestTypeName: `${prefix}${requestFormatSuffix}`, |
163 | 176 | responseTypeName: `${prefix}Response`, |
164 | 177 | optionsTypeName: `${prefix}Options`, |
| 178 | + // UPSTREAM: Needs support in blueprint, fallback to true for now. |
| 179 | + // https://github.com/seamapi/blueprint/issues/205 |
| 180 | + isOptionalParamsOk: true, |
| 181 | + ...getResponseContext(endpoint), |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +const getResponseContext = ( |
| 186 | + endpoint: Endpoint, |
| 187 | +): Pick< |
| 188 | + EndpointLayoutContext, |
| 189 | + 'returnsVoid' | 'responseKey' | 'resourceType' |
| 190 | +> => { |
| 191 | + if (endpoint.response.responseType === 'void') |
| 192 | + return { |
| 193 | + returnsVoid: true, |
| 194 | + responseKey: '', |
| 195 | + } |
| 196 | + const { responseKey } = endpoint.response |
| 197 | + return { |
| 198 | + returnsVoid: false, |
| 199 | + responseKey, |
165 | 200 | } |
166 | 201 | } |
0 commit comments