Skip to content

Commit 49b2106

Browse files
committed
Generate single route
1 parent f1752ba commit 49b2106

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed

.github/workflows/generate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
install_dependencies: 'false'
3434
- name: Normalize package-lock.json
3535
run: npm install
36+
- name: Generate code
37+
run: npm run generate
3638
- name: Commit
3739
uses: stefanzweifel/git-auto-commit-action@v4
3840
with:

generate-routes.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { writeFile } from 'node:fs/promises'
2+
import { resolve } from 'node:path'
3+
14
import { camelCase, pascalCase, snakeCase } from 'change-case'
2-
import { format } from 'prettier'
5+
import { format, resolveConfig } from 'prettier'
36

47
interface Route {
58
namespace: string
@@ -95,7 +98,7 @@ type ${renderRequestType({
9598
namespace,
9699
requestFormat,
97100
})} = SetNonNullable<
98-
Required<RouteRequest${requestFormat}<'${path}'>>
101+
Required<RouteRequest${pascalCase(requestFormat)}<'${path}'>>
99102
>
100103
101104
type ${renderResponseType({ name, namespace })}= SetNonNullable<
@@ -132,15 +135,19 @@ const exampleRoute: Route = {
132135
resource: 'workspace',
133136
requestFormat: getRequestFormat('GET'),
134137
},
135-
{
136-
name: 'create',
137-
namespace: 'workspaces',
138-
path: '/workspaces/create',
139-
method: 'POST',
140-
resource: 'workspace',
141-
requestFormat: getRequestFormat('POST'),
142-
},
143138
],
144139
}
145140

146-
console.log(await format(renderRoute(exampleRoute), { parser: 'typescript' }))
141+
const write = async (data: string, ...path: string[]): Promise<void> => {
142+
const filepath = resolve(...path)
143+
const prettierConfig = await resolveConfig(filepath)
144+
if (prettierConfig == null) {
145+
throw new Error('Failed to resolve Prettier config')
146+
}
147+
const output = await format(data, { ...prettierConfig, filepath })
148+
await writeFile(filepath, output)
149+
}
150+
151+
const routeRootPath = resolve('src', 'lib', 'seam', 'connect', 'routes')
152+
153+
await write(renderRoute(exampleRoute), routeRootPath, 'workspaces.ts')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"postversion": "git push --follow-tags",
6363
"example": "tsx examples",
6464
"example:inspect": "tsx --inspect examples",
65+
"generate": "tsx generate-routes.ts",
6566
"format": "eslint --ignore-path .gitignore --fix .",
6667
"preformat": "prettier --write --ignore-path .gitignore .",
6768
"report": "c8 report"

src/lib/seam/connect/routes/workspaces.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,19 @@ export class SeamHttpWorkspaces {
2222
async get(
2323
params: WorkspacesGetParams = {},
2424
): Promise<WorkspacesGetResponse['workspace']> {
25-
const { data } = await this.client.get<WorkspacesGetResponse>(
26-
'/workspaces/get',
27-
{
28-
params,
29-
},
30-
)
25+
const { data } = await this.client.request<WorkspacesGetResponse>({
26+
url: '/workspaces/get',
27+
method: 'get',
28+
params,
29+
})
3130
return data.workspace
3231
}
3332
}
3433

35-
export type WorkspacesGetParams = SetNonNullable<
34+
type WorkspacesGetParams = SetNonNullable<
3635
Required<RouteRequestParams<'/workspaces/get'>>
3736
>
3837

39-
export type WorkspacesGetResponse = SetNonNullable<
38+
type WorkspacesGetResponse = SetNonNullable<
4039
Required<RouteResponse<'/workspaces/get'>>
4140
>

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
},
1010
"files": ["src/index.ts", "src/connect.ts"],
1111
"include": ["src/**/*"],
12-
"exclude": ["**/*.test.ts", "tsup.config.ts"]
12+
"exclude": ["**/*.test.ts", "tsup.config.ts", "generate-routes.ts"]
1313
}

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@
3030
}
3131
},
3232
"files": ["src/index.ts"],
33-
"include": ["src/**/*", "test/**/*", "examples/**/*", "tsup.config.ts"]
33+
"include": [
34+
"src/**/*",
35+
"test/**/*",
36+
"examples/**/*",
37+
"tsup.config.ts",
38+
"generate-routes.ts"
39+
]
3440
}

0 commit comments

Comments
 (0)