-
Notifications
You must be signed in to change notification settings - Fork 292
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
generate types, create http client and add exemplary usage #3384
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
786d459
generate types, create http client and add exemplary usage
brojd 9ae3f9e
update
brojd 7f25d69
openapi-codegen
brojd f8918b1
update
brojd 7c5b631
update
brojd c511baf
update
brojd f23be02
update
brojd ee72689
Merge branch 'dev' into brojd/generate-ts-types-from-open-api-schemas
brojd 28ac43e
test customFetch
brojd d8abf1c
Remove dead code and add exemplary usage of new http client
brojd 765d43e
update
brojd 0b3a629
prettify autogenerated types
brojd a4cf0a9
add READMEs
brojd 04d0028
cleanup
brojd a77204e
update
brojd 18e8ed7
exclude autogenerated files from eslint warning
brojd 4d32040
improve readme
brojd ca33d24
fix tests
brojd a6ed4fd
cleanup
brojd 9fa960c
fix typo
brojd 62aa3db
Merge branch 'dev' into brojd/generate-ts-types-from-open-api-schemas
brojd 2ed922e
fix typos in tests
brojd a9bc923
use double quotes in yaml
brojd ae439f3
set DRF_SPECTACULAR_ENABLED to True by default
brojd 8f8192c
Merge branch 'dev' into brojd/generate-ts-types-from-open-api-schemas
brojd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ RABBITMQ_PORT=5672 | |
RABBITMQ_DEFAULT_VHOST="/" | ||
|
||
REDIS_URI=redis://redis:6379/0 | ||
|
||
DRF_SPECTACULAR_ENABLED=True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/src/assets | ||
/src/assets | ||
/src/network/oncall-api/autogenerated-api.types.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Grafana OnCall | ||
|
||
Developer-Friendly | ||
Alert Management | ||
with Brilliant Slack Integration | ||
|
||
- Connect monitoring systems | ||
- Collect and analyze data | ||
- On-call rotation | ||
- Automatic escalation | ||
- Never miss alerts with calls and SMS | ||
|
||
## Documentation | ||
|
||
- [On Github](http://github.com/grafana/oncall) | ||
- [Grafana OnCall](https://grafana.com/docs/oncall/latest/) | ||
|
||
## Development | ||
|
||
### Autogenerating TS types based on OpenAPI schema | ||
|
||
| :warning: WARNING | | ||
| :------------------------------------------------------------------------------------------ | | ||
| Transition to this approach is [in progress](https://github.com/grafana/oncall/issues/3338) | | ||
|
||
#### Overview | ||
|
||
In order to automate types creation and prevent API usage pitfalls, OnCall project is using the following approach: | ||
|
||
1. OnCall Engine (backend) exposes OpenAPI schema | ||
2. OnCall Grafana Plugin (frontend) autogenerates TS type definitions based on it | ||
3. OnCall Grafana Plugin (frontend) uses autogenerated types as a single source of truth for | ||
any backend-related interactions (url paths, request bodies, params, response payloads) | ||
|
||
#### Instruction | ||
|
||
1. Whenever API contract changes, run `yarn generate-types` from `grafana-plugin` directory | ||
2. Then you can start consuming types and you can use fully typed http client: | ||
|
||
```ts | ||
import { ApiSchemas } from 'network/oncall-api/api.types'; | ||
import onCallApi from 'network/oncall-api/http-client'; | ||
|
||
const { | ||
data: { results }, | ||
} = await onCallApi.GET('/alertgroups/'); | ||
const alertGroups: Array<ApiSchemas['AlertGroup']> = results; | ||
``` | ||
|
||
3. [Optional] If there is any property that is not yet exposed in OpenAPI schema and you already want to use it, | ||
you can append missing properties to particular schemas by editing | ||
`grafana-plugin/src/network/oncall-api/types-generator/custom-schemas.ts` file: | ||
|
||
```ts | ||
export type CustomApiSchemas = { | ||
Alert: { | ||
propertyMissingInOpenAPI: string; | ||
}; | ||
AlertGroup: { | ||
anotherPropertyMissingInOpenAPI: number[]; | ||
}; | ||
}; | ||
``` | ||
|
||
Then add their names to `CUSTOMIZED_SCHEMAS` array in `grafana-plugin/src/network/oncall-api/types-generator/generate-types.ts`: | ||
|
||
```ts | ||
const CUSTOMIZED_SCHEMAS = ['Alert', 'AlertGroup']; | ||
``` | ||
|
||
The outcome is that autogenerated schemas will be modified as follows: | ||
|
||
```ts | ||
import type { CustomApiSchemas } from './types-generator/custom-schemas'; | ||
|
||
export interface components { | ||
schemas: { | ||
Alert: CustomApiSchemas['Alert'] & { | ||
readonly id: string; | ||
... | ||
}; | ||
AlertGroup: CustomApiSchemas['AlertGroup'] & { | ||
readonly pk: string; | ||
... | ||
}, | ||
... | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => ({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3