Skip to content

Commit 7ce5835

Browse files
authored
Merge pull request #350 from seamapi/always-generate-options-param
Always generate option argument
2 parents c26f6a2 + 0ec587b commit 7ce5835

File tree

62 files changed

+6296
-1433
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+6296
-1433
lines changed

codegen/layouts/endpoints.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class SeamHttpEndpoints {
1818
{{> route-class-methods }}
1919

2020
{{#each endpoints}}
21-
get['{{path}}'](): {{className}}['{{methodName}}']
21+
get['{{path}}'](): {{> endpont-method-signature isFnType=true }}
2222
{
2323
const { client, defaults } = this
2424
{{#if isUndocumented}}
@@ -35,3 +35,5 @@ export class SeamHttpEndpoints {
3535

3636
{{/each}}
3737
}
38+
39+
export type SeamHttpEndpointPaths = {{#each endpoints}}'{{path}}' {{#unless @last}} | {{/unless}}{{/each}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(
2+
parameters{{#if isOptionalParamsOk}}?{{/if}}: {{parametersTypeName}},
3+
options{{#if isFnType}}?{{/if}}: {{optionsTypeName}}{{#unless isFnType}} = {}{{/unless}},
4+
){{#if isFnType}} => {{else}}: {{/if}}{{requestTypeName}}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
export type {{requestTypeName}} = RouteRequest{{requestFormatSuffix}}<'{{path}}'>
1+
export type {{parametersTypeName}} = RouteRequest{{requestFormatSuffix}}<'{{path}}'>
22

3+
/**
4+
* @deprecated Use {{parametersTypeName}} instead.
5+
*/
6+
export type {{legacyRequestTypeName}} = {{parametersTypeName}}
7+
8+
/**
9+
* @deprecated Use {{requestTypeName}} instead.
10+
*/
311
export type {{responseTypeName}} = SetNonNullable<
412
Required<RouteResponse<'{{path}}'>>
513
>
614

7-
export type {{optionsTypeName}} = {{#if returnsActionAttempt}}Pick<SeamHttpRequestOptions, 'waitForActionAttempt'>{{else}}never{{/if}}
15+
export type {{requestTypeName}} = SeamHttpRequest<{{#if returnsVoid}}void, undefined{{else}}{{responseTypeName}}, '{{responseKey}}'{{/if}}>
16+
17+
export type {{optionsTypeName}} = {{#if returnsActionAttempt}}Pick<SeamHttpRequestOptions, 'waitForActionAttempt'>{{else}}Record<string, never>{{/if}}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
{{methodName}}(
2-
{{methodParamName}}{{#if isOptionalParamsOk}}?{{/if}}: {{requestTypeName}},
3-
{{#if hasOptions}}options: {{optionsTypeName}} = {},{{/if}}
4-
): SeamHttpRequest<{{#if returnsVoid}}void, undefined{{else}}{{responseTypeName}}, '{{responseKey}}'{{/if}}>
1+
{{methodName}}
2+
{{> endpont-method-signature }}
53
{
64
{{#if isUndocumented}}
75
if (!this.defaults.isUndocumentedApiEnabled) {
@@ -11,8 +9,8 @@
119
return new SeamHttpRequest(this, {
1210
pathname: '{{path}}',
1311
method: '{{method}}',
14-
{{requestFormat}}: {{methodParamName}},
12+
{{requestFormat}}: parameters,
1513
responseKey: {{#if returnsVoid}}undefined{{else}}'{{responseKey}}'{{/if}},
16-
{{#if hasOptions}}options,{{/if}}
14+
options,
1715
})
1816
}

codegen/lib/layouts/endpoints.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ import {
44
type EndpointLayoutContext,
55
getClassName,
66
getEndpointLayoutContext,
7-
type SubrouteLayoutContext,
87
toFilePath,
98
} from './route.js'
109

1110
export interface EndpointsLayoutContext {
1211
className: string
1312
endpoints: EndpointLayoutContext[]
14-
routeImports: Array<Pick<SubrouteLayoutContext, 'className' | 'fileName'>>
13+
routeImports: RouteImportLayoutContext[]
1514
skipClientSessionImport: boolean
1615
}
1716

17+
interface RouteImportLayoutContext {
18+
className: string
19+
fileName: string
20+
typeNames: string[]
21+
}
22+
1823
export const setEndpointsLayoutContext = (
1924
file: Partial<EndpointsLayoutContext>,
2025
routes: Route[],
@@ -27,9 +32,17 @@ export const setEndpointsLayoutContext = (
2732
),
2833
)
2934
file.routeImports = routes.map((route) => {
35+
const endpoints = route.endpoints.map((endpoint) =>
36+
getEndpointLayoutContext(endpoint, route),
37+
)
3038
return {
3139
className: getClassName(route.path),
3240
fileName: `${toFilePath(route.path)}/index.js`,
41+
typeNames: endpoints.flatMap((endpoint) => [
42+
endpoint.parametersTypeName,
43+
endpoint.optionsTypeName,
44+
endpoint.requestTypeName,
45+
]),
3346
}
3447
})
3548
}

codegen/lib/layouts/route.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ export interface EndpointLayoutContext {
2020
functionName: string
2121
className: string
2222
method: Method
23-
hasOptions: boolean
2423
responseKey: string
25-
methodParamName: 'params' | 'body'
2624
requestFormat: 'params' | 'body'
27-
requestTypeName: string
25+
parametersTypeName: string
26+
legacyRequestTypeName: string
2827
responseTypeName: string
2928
requestFormatSuffix: string
3029
optionsTypeName: string
30+
requestTypeName: string
3131
returnsActionAttempt: boolean
3232
returnsVoid: boolean
3333
isOptionalParamsOk: boolean
@@ -79,7 +79,7 @@ export const getEndpointLayoutContext = (
7979
): EndpointLayoutContext => {
8080
const prefix = pascalCase([route.path.split('/'), endpoint.name].join('_'))
8181

82-
const methodParamName = ['GET', 'DELETE'].includes(
82+
const legacyMethodParamName = ['GET', 'DELETE'].includes(
8383
endpoint.request.semanticMethod,
8484
)
8585
? 'params'
@@ -104,15 +104,15 @@ export const getEndpointLayoutContext = (
104104
methodName,
105105
functionName: camelCase(prefix),
106106
method: endpoint.request.preferredMethod,
107-
hasOptions: returnsActionAttempt,
108107
className: getClassName(route.path),
109-
methodParamName,
110108
requestFormat,
111109
requestFormatSuffix,
112110
returnsActionAttempt,
113-
requestTypeName: `${prefix}${pascalCase(methodParamName)}`,
111+
parametersTypeName: `${prefix}Parameters`,
112+
legacyRequestTypeName: `${prefix}${pascalCase(legacyMethodParamName)}`,
114113
responseTypeName: `${prefix}Response`,
115114
optionsTypeName: `${prefix}Options`,
115+
requestTypeName: `${prefix}Request`,
116116
// UPSTREAM: Needs support in blueprint, fallback to true for now.
117117
// https://github.com/seamapi/blueprint/issues/205
118118
isOptionalParamsOk: true,

0 commit comments

Comments
 (0)