Skip to content

Commit

Permalink
feat: generate openapi type
Browse files Browse the repository at this point in the history
  • Loading branch information
tosaken1116 committed Oct 14, 2023
1 parent ff2f7bd commit 435a0ee
Show file tree
Hide file tree
Showing 32 changed files with 948 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ build/

_templates/
setup.js
*.config.*
*.config.*
src/apis
5 changes: 5 additions & 0 deletions aspida.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
input: 'src/apis/', // "input" of aspida is "output" for openapi2aspida
outputEachDir: true, // Generate $api.ts in each endpoint directory
openapi: { inputFile: './schemas/build/openapi.yml' },
};
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion cspell.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"language": "en",
// words - list of words to be always considered correct
"words": ["autodocs"],
"ignorePaths": []
"ignorePaths": ["./src/apis"]
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// For example "hte" should be "the"
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"new:model": "hygen generator model",
"new:page": "hygen generator page",
"prepare": "husky install",
"generate:type": "rm -r src/apis && openapi2aspida -c ./aspida.config.js",
"commitmsg": "commitlint -e $GIT_PARAMS",
"fmt": "prettier --write 'src/**/*.{js,jsx,ts,tsx,json,css,scss}'",
"check:lint": "eslint 'src/**/*.{ts,tsx}' --report-unused-disable-directives --max-warnings 0",
Expand Down Expand Up @@ -43,6 +44,10 @@
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@fortawesome/fontawesome-free": "^6.4.2",
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@markuplint/jsx-parser": "^3.9.1",
"@markuplint/react-spec": "^3.10.1",
"@storybook/addon-essentials": "^7.2.2",
Expand All @@ -59,8 +64,8 @@
"@tsconfig/strictest": "^2.0.1",
"@types/jest": "^29.5.4",
"@types/node": "20.8.4",
"@types/react-dom": "18.2.13",
"@types/react": "18.2.28",
"@types/react-dom": "18.2.13",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"autoprefixer": "^10.4.16",
Expand All @@ -80,16 +85,13 @@
"lint-staged": "^14.0.1",
"markuplint": "^3.12.1",
"npm-run-all": "^4.1.5",
"openapi2aspida": "^0.23.2",
"postcss": "^8.4.31",
"prettier": "^3.0.1",
"storybook": "^7.2.2",
"tailwindcss": "^3.3.3",
"ts-jest": "^29.1.1",
"typescript": "5.2.2",
"@fortawesome/fontawesome-free": "^6.4.2",
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0"
"typescript": "5.2.2"
},
"volta": {
"node": "20.6.1",
Expand Down
215 changes: 215 additions & 0 deletions src/apis/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import type { AspidaClient, BasicHeaders } from 'aspida';
import type { Methods as Methods_1vmdska } from './accounts/_id@string';
import type { Methods as Methods_17l7lnu } from './accounts/register';
import type { Methods as Methods_66bvhy } from './accounts/update';
import type { Methods as Methods_12twjkf } from './follows/create/_id@string';
import type { Methods as Methods_1ot5dgu } from './follows/delete/_id@string';
import type { Methods as Methods_hm9c1h } from './likes/create/_id@string';
import type { Methods as Methods_oom3sk } from './likes/delete/_id@string';
import type { Methods as Methods_ubgeda } from './tweets/delete/_id@string';
import type { Methods as Methods_um4wq7 } from './tweets/get/timeline';
import type { Methods as Methods_hsfp97 } from './tweets/post';

const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? 'http://localhost:8000' : baseURL).replace(/\/$/, '');
const PATH0 = '/accounts';
const PATH1 = '/accounts/register';
const PATH2 = '/accounts/update';
const PATH3 = '/follows/create';
const PATH4 = '/follows/delete';
const PATH5 = '/likes/create';
const PATH6 = '/likes/delete';
const PATH7 = '/tweets/delete';
const PATH8 = '/tweets/get/timeline';
const PATH9 = '/tweets/post';
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';

return {
accounts: {
_id: (val1: string) => {
const prefix1 = `${PATH0}/${val1}`;

return {
/**
* @returns success
*/
get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_1vmdska['get']['resBody'], BasicHeaders, Methods_1vmdska['get']['status']>(prefix, prefix1, GET, option).json(),
/**
* @returns success
*/
$get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_1vmdska['get']['resBody'], BasicHeaders, Methods_1vmdska['get']['status']>(prefix, prefix1, GET, option).json().then(r => r.body),
$path: () => `${prefix}${prefix1}`,
};
},
register: {
/**
* @param option.body - account to create
* @returns success
*/
post: (option: { body: Methods_17l7lnu['post']['reqBody'], config?: T | undefined }) =>
fetch<Methods_17l7lnu['post']['resBody'], BasicHeaders, Methods_17l7lnu['post']['status']>(prefix, PATH1, POST, option).json(),
/**
* @param option.body - account to create
* @returns success
*/
$post: (option: { body: Methods_17l7lnu['post']['reqBody'], config?: T | undefined }) =>
fetch<Methods_17l7lnu['post']['resBody'], BasicHeaders, Methods_17l7lnu['post']['status']>(prefix, PATH1, POST, option).json().then(r => r.body),
$path: () => `${prefix}${PATH1}`,
},
update: {
/**
* @param option.body - account to update
* @returns success
*/
put: (option: { body: Methods_66bvhy['put']['reqBody'], config?: T | undefined }) =>
fetch<Methods_66bvhy['put']['resBody'], BasicHeaders, Methods_66bvhy['put']['status']>(prefix, PATH2, PUT, option).json(),
/**
* @param option.body - account to update
* @returns success
*/
$put: (option: { body: Methods_66bvhy['put']['reqBody'], config?: T | undefined }) =>
fetch<Methods_66bvhy['put']['resBody'], BasicHeaders, Methods_66bvhy['put']['status']>(prefix, PATH2, PUT, option).json().then(r => r.body),
$path: () => `${prefix}${PATH2}`,
},
},
follows: {
create: {
_id: (val2: string) => {
const prefix2 = `${PATH3}/${val2}`;

return {
/**
* @returns success
*/
post: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_12twjkf['post']['resBody'], BasicHeaders, Methods_12twjkf['post']['status']>(prefix, prefix2, POST, option).json(),
/**
* @returns success
*/
$post: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_12twjkf['post']['resBody'], BasicHeaders, Methods_12twjkf['post']['status']>(prefix, prefix2, POST, option).json().then(r => r.body),
$path: () => `${prefix}${prefix2}`,
};
},
},
delete: {
_id: (val2: string) => {
const prefix2 = `${PATH4}/${val2}`;

return {
/**
* @returns success
*/
delete: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_1ot5dgu['delete']['resBody'], BasicHeaders, Methods_1ot5dgu['delete']['status']>(prefix, prefix2, DELETE, option).json(),
/**
* @returns success
*/
$delete: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_1ot5dgu['delete']['resBody'], BasicHeaders, Methods_1ot5dgu['delete']['status']>(prefix, prefix2, DELETE, option).json().then(r => r.body),
$path: () => `${prefix}${prefix2}`,
};
},
},
},
likes: {
create: {
_id: (val2: string) => {
const prefix2 = `${PATH5}/${val2}`;

return {
/**
* @returns success
*/
post: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_hm9c1h['post']['resBody'], BasicHeaders, Methods_hm9c1h['post']['status']>(prefix, prefix2, POST, option).json(),
/**
* @returns success
*/
$post: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_hm9c1h['post']['resBody'], BasicHeaders, Methods_hm9c1h['post']['status']>(prefix, prefix2, POST, option).json().then(r => r.body),
$path: () => `${prefix}${prefix2}`,
};
},
},
delete: {
_id: (val2: string) => {
const prefix2 = `${PATH6}/${val2}`;

return {
/**
* @returns success
*/
delete: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_oom3sk['delete']['resBody'], BasicHeaders, Methods_oom3sk['delete']['status']>(prefix, prefix2, DELETE, option).json(),
/**
* @returns success
*/
$delete: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_oom3sk['delete']['resBody'], BasicHeaders, Methods_oom3sk['delete']['status']>(prefix, prefix2, DELETE, option).json().then(r => r.body),
$path: () => `${prefix}${prefix2}`,
};
},
},
},
tweets: {
delete: {
_id: (val2: string) => {
const prefix2 = `${PATH7}/${val2}`;

return {
/**
* @returns success
*/
delete: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_ubgeda['delete']['resBody'], BasicHeaders, Methods_ubgeda['delete']['status']>(prefix, prefix2, DELETE, option).json(),
/**
* @returns success
*/
$delete: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_ubgeda['delete']['resBody'], BasicHeaders, Methods_ubgeda['delete']['status']>(prefix, prefix2, DELETE, option).json().then(r => r.body),
$path: () => `${prefix}${prefix2}`,
};
},
},
get: {
timeline: {
/**
* @returns success
*/
get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_um4wq7['get']['resBody'], BasicHeaders, Methods_um4wq7['get']['status']>(prefix, PATH8, GET, option).json(),
/**
* @returns success
*/
$get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods_um4wq7['get']['resBody'], BasicHeaders, Methods_um4wq7['get']['status']>(prefix, PATH8, GET, option).json().then(r => r.body),
$path: () => `${prefix}${PATH8}`,
},
},
post: {
/**
* @param option.body - tweet to delete
* @returns success
*/
post: (option: { body: Methods_hsfp97['post']['reqBody'], config?: T | undefined }) =>
fetch<Methods_hsfp97['post']['resBody'], BasicHeaders, Methods_hsfp97['post']['status']>(prefix, PATH9, POST, option).json(),
/**
* @param option.body - tweet to delete
* @returns success
*/
$post: (option: { body: Methods_hsfp97['post']['reqBody'], config?: T | undefined }) =>
fetch<Methods_hsfp97['post']['resBody'], BasicHeaders, Methods_hsfp97['post']['status']>(prefix, PATH9, POST, option).json().then(r => r.body),
$path: () => `${prefix}${PATH9}`,
},
},
};
};

export type ApiInstance = ReturnType<typeof api>;
export default api;
67 changes: 67 additions & 0 deletions src/apis/@types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable */
export type OkResponse = {
message?: string | undefined
}

export type ErrorResponse = {
message?: string | undefined
}

export type RegisterAccountPayload = {
id: string
name: string
}

export type UpdateAccountPayload = {
name?: string | undefined
description?: string | undefined
image_url?: string | undefined
birth_day?: string | undefined
website_url?: string | undefined
}

export type Account = {
id?: string | undefined
name?: string | undefined
description?: string | undefined
image_url?: string | undefined
birth_day?: string | undefined
website_url?: string | undefined
}

export type CreateTweetPayload = {
content?: string | undefined
image_url_list?: string[] | undefined
account_id?: string | undefined
reply_to?: string | undefined
root?: string | undefined
}

export type CreateTweetResponse = {
id?: string | undefined
content?: string | undefined
image_url_list?: string[] | undefined
created_at?: string | undefined
account_id?: string | undefined
reply_to?: string | undefined
root?: string | undefined
}

export type TweetItem = {
id?: string | undefined
content?: string | undefined
image_url_list?: string[] | undefined
created_at?: string | undefined
account_id?: string | undefined
reply_to?: string | undefined
root?: string | undefined
}

export type GetTweetResponse = {
timeline?: TweetItem[] | undefined
}

export type Like = {
account_id?: string | undefined
tweet_id?: string | undefined
}
Loading

0 comments on commit 435a0ee

Please sign in to comment.