Skip to content

Commit

Permalink
refactor: refactor types (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
cokemine authored Oct 22, 2022
1 parent 69b3677 commit c850dd9
Show file tree
Hide file tree
Showing 25 changed files with 1,044 additions and 354 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
!packages/design/.storybook
!packages/design/.storybook
packages/types/types
30 changes: 30 additions & 0 deletions .github/workflows/update-types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Update Type Definition
on:
repository_dispatch:
types: [update-types]
jobs:
update-types:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: pnpm/[email protected]
with:
version: "6"

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: "16.x"
cache: "pnpm"

- name: Install Dependencies
run: pnpm install

- name: Build Type Definition
run: pnpm build:types

- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
title: Update Type Definition from bangumi/dev-docs
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"prepare": "husky install",
"preinstall": "npx only-allow pnpm",
"build": "pnpm website build",
"build:types": "pnpm --filter @bangumi/types build",
"build:test": "pnpm website build:test",
"design": "pnpm --filter=@bangumi/design",
"design:doc": "pnpm design storybook",
Expand Down
9 changes: 9 additions & 0 deletions packages/types/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { components } from './types'

export type Avatar = components['schemas']['Avatar']
export type Topic = components['schemas']['Topic']
export type Pagination = Omit<components['schemas']['Paged'], 'data'>

export interface ResponseWithPagination<T = unknown> extends Pagination {
data: T
}
7 changes: 7 additions & 0 deletions packages/types/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { components } from './types'

export * from './common'

export type Group = components['schemas']['Group']
export type GroupProfile = components['schemas']['GroupProfile']
export type GroupMember = components['schemas']['GroupMember']
14 changes: 14 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@bangumi/types",
"version": "0.0.0",
"type": "module",
"private": true,
"scripts": {
"build": "node scripts/build.js"
},
"devDependencies": {
"js-yaml": "^4.1.0",
"node-fetch": "^3.2.10",
"openapi-typescript": "^5.4.1"
}
}
21 changes: 21 additions & 0 deletions packages/types/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'path'
import { fileURLToPath } from 'url'
import { promises as fs } from 'fs'
import openapiTS from 'openapi-typescript'
import fetch from 'node-fetch'
import yaml from 'js-yaml'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

const openapiURL = process.env.OPENAPI_URL || 'https://bangumi.github.io/dev-docs/api.yaml'

async function fetchSchema (url) {
const res = await fetch(url)
const text = await res.text()
return yaml.load(text)
}
const schema = await fetchSchema(openapiURL)

const data = await openapiTS(schema)

await fs.writeFile(path.resolve(__dirname, '../types/index.ts'), data)
Loading

0 comments on commit c850dd9

Please sign in to comment.