Skip to content

Commit

Permalink
client の openapi 生成に preprocess 処理を嚙ませた
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Jan 10, 2025
1 parent acfba7c commit b7dc579
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 39 deletions.
Binary file modified client/bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint:ci": "eslint .",
"format": "prettier --write src/",
"format:ci": "prettier --check src/",
"openapi": "openapi-typescript ../openapi/openapi.yml -o src/api/openapi.ts"
"openapi": "bun run scripts/openapi-gen.ts && openapi-typescript /tmp/openapi.yml --output src/api/openapi.ts"
},
"dependencies": {
"@tanstack/vue-query": "^5.62.16",
Expand All @@ -39,11 +39,12 @@
"typescript": "~5.6.3",
"vite": "^6.0.1",
"vite-plugin-vue-devtools": "^7.6.5",
"vue-tsc": "^2.1.10"
"vue-tsc": "^2.1.10",
"yaml": "^2.7.0"
},
"msw": {
"workerDirectory": [
"public"
]
}
}
}
20 changes: 20 additions & 0 deletions client/scripts/openapi-gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Bun from 'bun'
import * as yaml from 'yaml'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const stripDiscriminator = (schema: any) => {
Object.entries(schema).forEach(([key, value]) => {
if (!(typeof value === 'object')) return
if (key === 'discriminator') {
delete schema[key]
} else {
stripDiscriminator(value)
}
})
}

const file = await Bun.file('../openapi/openapi.yml').text()
const parsed = yaml.parse(file)
stripDiscriminator(parsed)
const temp = Bun.env['FILE'] ?? '/tmp/openapi.yml'
await Bun.write(temp, yaml.stringify(parsed))
43 changes: 14 additions & 29 deletions client/src/api/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,29 +549,23 @@ export interface components {
| components['schemas']['RunningBenchmark']
| components['schemas']['FinishedBenchmark']
/** @description ベンチマーク結果 */
Benchmark: {
status: 'Benchmark'
} & (Omit<
Benchmark: (
| components['schemas']['WaitingBenchmark']
| components['schemas']['RunningBenchmark']
| components['schemas']['FinishedBenchmark'],
'status'
> & {
| components['schemas']['FinishedBenchmark']
) & {
/**
* @description ベンチマークの競技者用ログ(標準出力)
* @example log
*/
log: string
})
}
/** @description Adminが見ることができるベンチマーク結果 */
BenchmarkAdminResult: {
status: 'BenchmarkAdminResult'
} & (Omit<
BenchmarkAdminResult: (
| components['schemas']['WaitingBenchmark']
| components['schemas']['RunningBenchmark']
| components['schemas']['FinishedBenchmark'],
'status'
> & {
| components['schemas']['FinishedBenchmark']
) & {
/**
* @description ベンチマークの競技者用ログ(標準出力)
* @example log
Expand All @@ -582,18 +576,15 @@ export interface components {
* @example admin log
*/
adminLog: string
})
}
/** @description status=waiting のベンチマーク結果 */
WaitingBenchmark: {
id: components['schemas']['BenchmarkId']
instanceId: components['schemas']['InstanceId']
teamId: components['schemas']['TeamId']
userId: components['schemas']['UserId']
/**
* @description discriminator enum property added by openapi-typescript
* @enum {string}
*/
status: 'WaitingBenchmark'
/** @enum {string} */
status: 'waiting'
createdAt: components['schemas']['CreatedAt']
}
/** @description status=running のベンチマーク結果 */
Expand All @@ -602,11 +593,8 @@ export interface components {
instanceId: components['schemas']['InstanceId']
teamId: components['schemas']['TeamId']
userId: components['schemas']['UserId']
/**
* @description discriminator enum property added by openapi-typescript
* @enum {string}
*/
status: 'RunningBenchmark'
/** @enum {string} */
status: 'running'
score: components['schemas']['Score']
createdAt: components['schemas']['CreatedAt']
startedAt: components['schemas']['StartedAt']
Expand All @@ -617,11 +605,8 @@ export interface components {
instanceId: components['schemas']['InstanceId']
teamId: components['schemas']['TeamId']
userId: components['schemas']['UserId']
/**
* @description discriminator enum property added by openapi-typescript
* @enum {string}
*/
status: 'FinishedBenchmark'
/** @enum {string} */
status: 'finished'
score: components['schemas']['Score']
/** @enum {string} */
result: 'passed' | 'failed' | 'error'
Expand Down
2 changes: 1 addition & 1 deletion client/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@vue/tsconfig/tsconfig.dom.json",
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
"include": ["env.d.ts", "src/**/*", "src/**/*.vue", "tsconfig.script.json"],
"exclude": ["src/**/__tests__/*"],
"compilerOptions": {
"composite": true,
Expand Down
9 changes: 3 additions & 6 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"files": [],
"references": [
{
"path": "./tsconfig.node.json"
},
{
"path": "./tsconfig.app.json"
}
{ "path": "./tsconfig.node.json" },
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.script.json" }
]
}
28 changes: 28 additions & 0 deletions client/tsconfig.script.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"include": ["scripts/**/*"],
"compilerOptions": {
// Enable latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags
"noUnusedLocals": true,
"noUnusedParameters": true,
"noPropertyAccessFromIndexSignature": true
}
}

0 comments on commit b7dc579

Please sign in to comment.