schemas.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {
  2. SchemasApi,
  3. Configuration,
  4. NewSchemaSubject,
  5. SchemaSubject,
  6. } from 'generated-sources';
  7. import {
  8. PromiseThunkResult,
  9. ClusterName,
  10. SchemaName,
  11. FailurePayload,
  12. } from 'redux/interfaces';
  13. import { BASE_PARAMS } from 'lib/constants';
  14. import * as actions from 'redux/actions';
  15. import { getResponse } from 'lib/errorHandling';
  16. const apiClientConf = new Configuration(BASE_PARAMS);
  17. export const schemasApiClient = new SchemasApi(apiClientConf);
  18. export const fetchSchemasByClusterName = (
  19. clusterName: ClusterName
  20. ): PromiseThunkResult<void> => async (dispatch) => {
  21. dispatch(actions.fetchSchemasByClusterNameAction.request());
  22. try {
  23. const schemas = await schemasApiClient.getSchemas({ clusterName });
  24. dispatch(actions.fetchSchemasByClusterNameAction.success(schemas));
  25. } catch (e) {
  26. dispatch(actions.fetchSchemasByClusterNameAction.failure());
  27. }
  28. };
  29. export const fetchSchemaVersions = (
  30. clusterName: ClusterName,
  31. subject: SchemaName
  32. ): PromiseThunkResult<void> => async (dispatch) => {
  33. if (!subject) return;
  34. dispatch(actions.fetchSchemaVersionsAction.request());
  35. try {
  36. const versions = await schemasApiClient.getAllVersionsBySubject({
  37. clusterName,
  38. subject,
  39. });
  40. dispatch(actions.fetchSchemaVersionsAction.success(versions));
  41. } catch (e) {
  42. dispatch(actions.fetchSchemaVersionsAction.failure());
  43. }
  44. };
  45. export const createSchema = (
  46. clusterName: ClusterName,
  47. newSchemaSubject: NewSchemaSubject
  48. ): PromiseThunkResult => async (dispatch) => {
  49. dispatch(actions.createSchemaAction.request());
  50. try {
  51. const schema: SchemaSubject = await schemasApiClient.createNewSchema({
  52. clusterName,
  53. newSchemaSubject,
  54. });
  55. dispatch(actions.createSchemaAction.success(schema));
  56. } catch (error) {
  57. const response = await getResponse(error);
  58. const alert: FailurePayload = {
  59. subject: ['schema', newSchemaSubject.subject].join('-'),
  60. title: `Schema ${newSchemaSubject.subject}`,
  61. response,
  62. };
  63. dispatch(actions.createSchemaAction.failure({ alert }));
  64. }
  65. };
  66. export const deleteSchema = (
  67. clusterName: ClusterName,
  68. subject: string
  69. ): PromiseThunkResult => async (dispatch) => {
  70. dispatch(actions.deleteSchemaAction.request());
  71. try {
  72. await schemasApiClient.deleteSchema({
  73. clusterName,
  74. subject,
  75. });
  76. dispatch(actions.deleteSchemaAction.success(subject));
  77. } catch (error) {
  78. const response = await getResponse(error);
  79. const alert: FailurePayload = {
  80. subject: ['schema', subject].join('-'),
  81. title: `Schema ${subject}`,
  82. response,
  83. };
  84. dispatch(actions.deleteSchemaAction.failure({ alert }));
  85. }
  86. };