Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion codegen/layouts/endpoints.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class SeamHttpEndpoints {
{{> route-class-methods }}

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

{{/each}}
}

export type SeamHttpEndpointPaths = {{#each endpoints}}'{{path}}' {{#unless @last}} | {{/unless}}{{/each}}
4 changes: 4 additions & 0 deletions codegen/layouts/partials/endpont-method-signature.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(
parameters{{#if isOptionalParamsOk}}?{{/if}}: {{parametersTypeName}},
options{{#if isFnType}}?{{/if}}: {{optionsTypeName}}{{#unless isFnType}} = {}{{/unless}},
){{#if isFnType}} => {{else}}: {{/if}}{{requestTypeName}}
14 changes: 12 additions & 2 deletions codegen/layouts/partials/route-class-endpoint-export.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
export type {{requestTypeName}} = RouteRequest{{requestFormatSuffix}}<'{{path}}'>
export type {{parametersTypeName}} = RouteRequest{{requestFormatSuffix}}<'{{path}}'>

/**
* @deprecated Use {{parametersTypeName}} instead.
*/
export type {{legacyRequestTypeName}} = {{parametersTypeName}}

/**
* @deprecated Use {{requestTypeName}} instead.
*/
export type {{responseTypeName}} = SetNonNullable<
Required<RouteResponse<'{{path}}'>>
>

export type {{optionsTypeName}} = {{#if returnsActionAttempt}}Pick<SeamHttpRequestOptions, 'waitForActionAttempt'>{{else}}never{{/if}}
export type {{requestTypeName}} = SeamHttpRequest<{{#if returnsVoid}}void, undefined{{else}}{{responseTypeName}}, '{{responseKey}}'{{/if}}>

export type {{optionsTypeName}} = {{#if returnsActionAttempt}}Pick<SeamHttpRequestOptions, 'waitForActionAttempt'>{{else}}Record<string, never>{{/if}}
10 changes: 4 additions & 6 deletions codegen/layouts/partials/route-class-endpoint.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{{methodName}}(
{{methodParamName}}{{#if isOptionalParamsOk}}?{{/if}}: {{requestTypeName}},
{{#if hasOptions}}options: {{optionsTypeName}} = {},{{/if}}
): SeamHttpRequest<{{#if returnsVoid}}void, undefined{{else}}{{responseTypeName}}, '{{responseKey}}'{{/if}}>
{{methodName}}
{{> endpont-method-signature }}
{
{{#if isUndocumented}}
if (!this.defaults.isUndocumentedApiEnabled) {
Expand All @@ -11,8 +9,8 @@
return new SeamHttpRequest(this, {
pathname: '{{path}}',
method: '{{method}}',
{{requestFormat}}: {{methodParamName}},
{{requestFormat}}: parameters,
responseKey: {{#if returnsVoid}}undefined{{else}}'{{responseKey}}'{{/if}},
{{#if hasOptions}}options,{{/if}}
options,
})
}
17 changes: 15 additions & 2 deletions codegen/lib/layouts/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import {
type EndpointLayoutContext,
getClassName,
getEndpointLayoutContext,
type SubrouteLayoutContext,
toFilePath,
} from './route.js'

export interface EndpointsLayoutContext {
className: string
endpoints: EndpointLayoutContext[]
routeImports: Array<Pick<SubrouteLayoutContext, 'className' | 'fileName'>>
routeImports: RouteImportLayoutContext[]
skipClientSessionImport: boolean
}

interface RouteImportLayoutContext {
className: string
fileName: string
typeNames: string[]
}

export const setEndpointsLayoutContext = (
file: Partial<EndpointsLayoutContext>,
routes: Route[],
Expand All @@ -27,9 +32,17 @@ export const setEndpointsLayoutContext = (
),
)
file.routeImports = routes.map((route) => {
const endpoints = route.endpoints.map((endpoint) =>
getEndpointLayoutContext(endpoint, route),
)
return {
className: getClassName(route.path),
fileName: `${toFilePath(route.path)}/index.js`,
typeNames: endpoints.flatMap((endpoint) => [
endpoint.parametersTypeName,
endpoint.optionsTypeName,
endpoint.requestTypeName,
]),
}
})
}
14 changes: 7 additions & 7 deletions codegen/lib/layouts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export interface EndpointLayoutContext {
functionName: string
className: string
method: Method
hasOptions: boolean
responseKey: string
methodParamName: 'params' | 'body'
requestFormat: 'params' | 'body'
requestTypeName: string
parametersTypeName: string
legacyRequestTypeName: string
responseTypeName: string
requestFormatSuffix: string
optionsTypeName: string
requestTypeName: string
returnsActionAttempt: boolean
returnsVoid: boolean
isOptionalParamsOk: boolean
Expand Down Expand Up @@ -79,7 +79,7 @@ export const getEndpointLayoutContext = (
): EndpointLayoutContext => {
const prefix = pascalCase([route.path.split('/'), endpoint.name].join('_'))

const methodParamName = ['GET', 'DELETE'].includes(
const legacyMethodParamName = ['GET', 'DELETE'].includes(
endpoint.request.semanticMethod,
)
? 'params'
Expand All @@ -104,15 +104,15 @@ export const getEndpointLayoutContext = (
methodName,
functionName: camelCase(prefix),
method: endpoint.request.preferredMethod,
hasOptions: returnsActionAttempt,
className: getClassName(route.path),
methodParamName,
requestFormat,
requestFormatSuffix,
returnsActionAttempt,
requestTypeName: `${prefix}${pascalCase(methodParamName)}`,
parametersTypeName: `${prefix}Parameters`,
legacyRequestTypeName: `${prefix}${pascalCase(legacyMethodParamName)}`,
responseTypeName: `${prefix}Response`,
optionsTypeName: `${prefix}Options`,
requestTypeName: `${prefix}Request`,
// UPSTREAM: Needs support in blueprint, fallback to true for now.
// https://github.com/seamapi/blueprint/issues/205
isOptionalParamsOk: true,
Expand Down
Loading