Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate openapi generator #38

Merged
merged 24 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
060f8c6
feat: add Orval config and generate example swagger
tienan92it Aug 28, 2023
f9b785b
chore(all): update validate spectral
chinhld12 Aug 29, 2023
cab162c
feat: init mocks service
tienan92it Aug 29, 2023
d5d2a3f
chore: generate api with backend boilerplate
tienan92it Sep 1, 2023
a17d8c8
chore: remove swagger-typescript-api
tienan92it Sep 1, 2023
cf4d9f9
chore: empty 'export {}' statement to make mocks a module
tienan92it Sep 1, 2023
c6f261d
chore: update generator docs
tienan92it Sep 2, 2023
1e53d9b
chore: demo account bypass
tienan92it Sep 2, 2023
0f24918
chore: update base url env
tienan92it Sep 2, 2023
7b44936
chore: update useless comment
tienan92it Sep 6, 2023
45e72c0
Update docs/GETTING_STARTED.md
tienan92it Sep 6, 2023
a3c8b43
chore: update cancel request message in requester
tienan92it Sep 6, 2023
e5ebf95
chore: remove unused dependencies
tienan92it Sep 6, 2023
d9079ec
chore: update base url and demo account
tienan92it Sep 12, 2023
816b7f1
chore: show loading on submitting
tienan92it Sep 13, 2023
38418ab
fix: change loading ui
zlatanpham Sep 14, 2023
abfafcb
fix: add UI for error handle
zlatanpham Sep 14, 2023
5ef4a40
chore: re-generate api client
tienan92it Sep 26, 2023
d373590
feat: use getMe for user profile
tienan92it Sep 26, 2023
cbea9c4
feat: handle unauthorized
tienan92it Sep 27, 2023
ce2248e
chore: refactor bootstrapAsync
tienan92it Sep 27, 2023
b64eaa3
chore: use full name instead of first name and last name
tienan92it Sep 27, 2023
e02bb16
chore: update demo user in auth cypress
tienan92it Sep 27, 2023
7928a0a
chore: set test env for github actions
tienan92it Sep 27, 2023
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
3 changes: 2 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NEXT_PUBLIC_BASE_URL='/api'
NEXT_PUBLIC_BASE_URL='https://hp-api.fly.dev/api/v1'
tienan92it marked this conversation as resolved.
Show resolved Hide resolved
NEXT_PUBLIC_API_MOCKING=disabled
102 changes: 102 additions & 0 deletions .spectral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const ibmCloudValidationRules = require('@ibm-cloud/openapi-ruleset') // Note 1
tienan92it marked this conversation as resolved.
Show resolved Hide resolved
const {
pathSegmentCaseConvention,
propertyCaseConvention,
enumCaseConvention,
operationIdCaseConvention,
parameterCaseConvention,
} = require('@ibm-cloud/openapi-ruleset/src/functions')
const {
schemas,
paths,
operations,
parameters,
} = require('@ibm-cloud/openapi-ruleset-utilities/src/collections')

module.exports = {
extends: ibmCloudValidationRules,
rules: {
'response-example-provided': 'off',
'operation-description': 'off',
'property-description': 'off',
'schema-description': 'off',
'response-error-response-schema': 'off',
'array-boundary': 'off',
'string-boundary': 'off',
'pagination-style': 'off',
'property-case-convention': {
description: 'Property names must follow camel case convention',
message: '{{error}}',
given: schemas,
resolved: true,
severity: 'error',
then: {
function: propertyCaseConvention,
functionOptions: {
type: 'camel',
},
},
},
'path-segment-case-convention': {
description: 'Path segments must follow camel case convention',
message: '{{error}}',
given: paths,
severity: 'error',
then: {
function: pathSegmentCaseConvention,
functionOptions: {
type: 'camel',
},
},
},
'enum-case-convention': {
description: 'Enum values must follow a specified case convention',
message: '{{error}}',
given: schemas,
severity: 'error',
then: {
function: enumCaseConvention,
functionOptions: {
type: 'camel',
},
},
},
'operation-id-case-convention': {
description: 'Operation ids must follow a specified case convention',
message: '{{error}}',
given: operations,
severity: 'error',
then: {
function: operationIdCaseConvention,
functionOptions: {
type: 'camel',
},
},
},
'parameter-case-convention': {
description: 'Parameter names must follow case conventions',
message: '{{error}}',
given: parameters,
severity: 'error',
resolved: true,
then: {
function: parameterCaseConvention,
functionOptions: {
query: {
type: 'camel',
},

path: {
type: 'camel',
},
header: {
type: 'pascal',
separator: {
char: '-',
},
},
},
},
},
},
}
26 changes: 16 additions & 10 deletions docs/GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ to see your project.
├── public # Public folder
│ └── img # Images used by the app
├── types # Shared TypeScript interfaces
├── api # Generated API interfaces from Swagger
├── components # Shared components
│ └── X
│ └── X.tsx
Expand All @@ -41,7 +42,9 @@ to see your project.
│ └── tests
│── cypress # Cypress configuration and tests
├── tailwind.config.js # Tailwind CSS configuration
└── tsconfig.json # TypeScript configuration
├── tsconfig.json # TypeScript configuration
├── .spectral.js # Orval validator rules for Swagger
tienan92it marked this conversation as resolved.
Show resolved Hide resolved
└── orval.config.js # Orval api generator configuration
```

### Develop UI components
Expand All @@ -60,23 +63,26 @@ The UI document then should be live at

If your team use Swagger to document APIs, we support a node script to generate
TypeScript interfaces via your Swagger scheme. In
[package.json](../package.json), change the default path to the location of your
Swagger JSON doc.

```
- "fetch-definitions": "swagger-typescript-api --no-client -p ./libs/swagger.json -o ./types -n schema.ts",
+ "fetch-definitions": "swagger-typescript-api --no-client -p your-api-swagger-json -o ./types -n schema.ts",
[orval.config.js](../orval.config.js), change the default path to the location
of your Swagger JSON doc.

```diff
input: {
- target: 'https://hp-api.fly.dev/swagger/doc.json',
+ target: 'your-api-swagger-json',
...
}
```

Then, you can run the script locally to generate TypeScript definitions for the
APIs:

```bash
pnpm fetch-definitions
pnpm generate:api
```

The generated interfaces will be located at
[types/schema.ts](../types/schema.ts).
The generated models and api clients will be located at [api](../src/api)
folder.

## Read on:

Expand Down
39 changes: 39 additions & 0 deletions orval.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { faker } from '@faker-js/faker'
import { defineConfig } from 'orval'

export default defineConfig({
app: {
output: {
mode: 'tags-split',
workspace: './src/api',
target: './app.ts',
schemas: './model',
client: 'swr',
mock: true,
prettier: true,
override: {
mutator: {
path: './mutator/requester.ts',
name: 'requester',
},
mock: {
properties: {
email: () => faker.internet.email(),
'data.email': () => faker.internet.email(),
'data.[].email': () => faker.internet.email(),
avatar: () => faker.image.avatar(),
'data.avatar': () => faker.image.avatar(),
'data.[].avatar': () => faker.image.avatar(),
},
},
},
},
input: {
target: 'https://hp-api.fly.dev/swagger/doc.json',
validation: false,
},
hooks: {
afterAllFilesWrite: 'eslint ./src/api --ext .ts,.tsx,.js --fix', // run lint fix after all files are written
},
},
})
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"generate-icons": "node ./scripts/generate-icon-components.js",
"storybook": "storybook dev -p 6006 -s ./public",
"build-storybook": "storybook build -s ./public",
"fetch-definitions": "swagger-typescript-api --no-client -p ./libs/swagger.json -o ./types -n schema.ts",
"cypress:open": "cypress open",
"release": "standard-version -a",
"preinstall": "npx only-allow pnpm"
"preinstall": "npx only-allow pnpm",
"generate:api": "orval --config ./orval.config.js"
},
"dependencies": {
"@babel/eslint-parser": "^7.22.10",
Expand Down Expand Up @@ -48,6 +48,12 @@
"@commitlint/config-conventional": "^17.3.0",
"@dwarvesf/react-eslint-config": "^1.0.1",
"@prettier/plugin-xml": "^3.2.0",
"@babel/core": "^7.22.5",
"@babel/helper-plugin-utils": "^7.20.2",
"@babel/preset-react": "^7.22.5",
tienan92it marked this conversation as resolved.
Show resolved Hide resolved
"@faker-js/faker": "^8.0.2",
"@ibm-cloud/openapi-ruleset": "0.45.5",
"@ibm-cloud/openapi-ruleset-utilities": "0.0.1",
"@storybook/addon-actions": "^7.0.12",
"@storybook/addon-essentials": "^7.0.12",
"@storybook/addon-links": "^7.0.12",
Expand Down Expand Up @@ -90,6 +96,8 @@
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"lint-staged": "^14.0.0",
"msw": "^1.2.3",
"orval": "^6.17.0",
"postcss": "^8.4.20",
"prettier": "^2.8.1",
"rimraf": "^3.0.2",
Expand All @@ -98,7 +106,6 @@
"stylelint": "^15.10.2",
"stylelint-config-prettier": "^9.0.4",
"stylelint-config-standard": "^34.0.0",
"swagger-typescript-api": "^13.0.3",
"tailwindcss": "3.2.4",
"tailwindcss-animate": "^1.0.5",
"ts-jest": "^29.0.3",
Expand All @@ -125,5 +132,8 @@
"packageManager": "[email protected]",
"engines": {
"pnpm": ">=8.2.0"
},
"msw": {
"workerDirectory": "public"
}
}
}
Loading