{"id":15287541,"url":"https://github.com/42bv/mad-spring-enum","last_synced_at":"2025-10-07T02:31:28.312Z","repository":{"id":143888442,"uuid":"100281307","full_name":"42BV/mad-spring-enum","owner":"42BV","description":"Sharing enums with a Java Spring Boot back-end.","archived":true,"fork":false,"pushed_at":"2020-09-11T12:41:47.000Z","size":397,"stargazers_count":0,"open_issues_count":6,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-17T20:08:02.636Z","etag":null,"topics":["enum","react","redux","spring","spring-mvc"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/42BV.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-14T15:16:33.000Z","updated_at":"2023-01-28T11:40:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"c652994c-2184-43da-93ca-dcd062fd5683","html_url":"https://github.com/42BV/mad-spring-enum","commit_stats":{"total_commits":9,"total_committers":3,"mean_commits":3.0,"dds":0.2222222222222222,"last_synced_commit":"1c44e145f8b7e2a114bd2a174a143a61e13d415f"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/42BV%2Fmad-spring-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/42BV%2Fmad-spring-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/42BV%2Fmad-spring-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/42BV%2Fmad-spring-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/42BV","download_url":"https://codeload.github.com/42BV/mad-spring-enum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235581533,"owners_count":19013089,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","react","redux","spring","spring-mvc"],"created_at":"2024-09-30T15:30:40.349Z","updated_at":"2025-10-07T02:31:23.055Z","avatar_url":"https://github.com/42BV.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About\n\n[![Build Status](https://travis-ci.org/42BV/mad-spring-enum.svg?branch=master)](https://travis-ci.org/42BV/mad-spring-enum)\n[![Codecov](https://codecov.io/gh/42BV/mad-spring-enum/branch/master/graph/badge.svg)](https://codecov.io/gh/42BV/mad-spring-enum)\n\nSharing enums with a Java Spring Boot back-end.\n\n# Installation\n\n`npm install mad-spring-enum --save`\n\n# Preparation\n\nFirst in your Java project make sure mad-spring-enum can read\nthe enums, via a GET request:\n\n```java\n// EnumController.java\n\n@RestController\n@RequestMapping(\"/enums\")\nclass EnumController {\n\n    private final Map\u003cString, Set\u003cString\u003e\u003e registry = new HashMap\u003c\u003e();\n\n    @Autowired\n    EnumController(EnumClassPathScanningCandidateComponentProvider enumProvider) {\n        enumProvider.findCandidateComponents(Application.class.getPackage().getName())\n            .forEach(component -\u003e {\n                Class\u003cEnum\u003c?\u003e\u003e componentClass = forName(component.getBeanClassName());\n                registry.put(componentClass.getSimpleName(), stream(componentClass.getEnumConstants())\n                    .map(Enum::name)\n                    .collect(toSet()));\n            });\n    }\n\n    @GetMapping\n    Map\u003cString, Set\u003cString\u003e\u003e findAll() {\n        return registry;\n    }\n\n}\n\n// EnumClassPathScanningCandidateComponentProvider.java\n\n/**\n * EnumClassPathScanningCandidateComponentProvider is a specialization of {@link ClassPathScanningCandidateComponentProvider}\n * that only takes enum values into account.\n *\n * Furthermore it overrides the default behaviour of the {@link ClassPathScanningCandidateComponentProvider}\n * that checks that the classes that are found on the classpath are non-abstract. By their definition, an\n * enum that contains abstract methods or implements an interface is abstract and ignored.\n * This does not serve our purpose, hence the specialisation.\n */\n@Component\nclass EnumClassPathScanningCandidateComponentProvider extends ClassPathScanningCandidateComponentProvider {\n\n    EnumClassPathScanningCandidateComponentProvider() {\n        super(false);\n        addIncludeFilter(new IsEnumFilter());\n    }\n\n    /**\n     * Determine whether the given bean definition qualifies as candidate.\n     *\n     * The default implementation checks whether the class is concrete\n     * but this is does not work for us because an enum is considered abstract\n     * when it implements an interface or has an abstract method.\n     *\n     * The JavaDoc of the default implementation also states that\n     * this behaviour can be overridden in subclasses.\n     *\n     * @param beanDefinition the bean definition to check\n     * @return whether the bean definition qualifies as a candidate component\n     */\n    @Override\n    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {\n        return beanDefinition.getMetadata().isIndependent();\n    }\n\n    private static class IsEnumFilter implements TypeFilter {\n\n        @Override\n        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {\n            String className = metadataReader.getClassMetadata().getClassName();\n            Class\u003cEnum\u003e clazz = Classes.forName(className);\n            return clazz.isEnum();\n        }\n    }\n}\n```\n\n# Getting started.\n\nWe assume you have a working Redux project, if you do not yet have\nRedux add Redux to your project by following the Redux's instructions.\n\nWe also assume that you have redux-form installed via the instructions\nprovided on the website.\n\nFirst install the following dependencies in the package.json:\n\n  1. \"react-redux\": \"5.0.3\",\n  2. \"redux\": \"3.6.0\",\n\nNow add the constraints-reducer to your rootReducer for example:\n\n```js\nimport { combineReducers } from 'redux';\n\nimport { EnumsStore, enums } from 'mad-spring-enum';\n\nexport interface Store {\n  enums: EnumsStore\n};\n\n// Use ES6 object literal shorthand syntax to define the object shape\nconst rootReducer: Store = combineReducers({\n  enums\n});\n\nexport default rootReducer;\n```\n\nThis should add the EnumsStore to Redux, which will store\nthe enums from the Spring back-end.\n\nNext you have to configure the enums module:\n\n```js\nimport { createStore } from 'redux';\nimport { configureEnums } from 'mad-spring-enum';\n\nexport const store = createStore(\n  rootReducer,\n);\n\nconfigureEnums({\n   // The URL which will provide the enums over a GET request.\n  enumsUrl: '/api/enums',\n\n  // Whether or not the 'enumsUrl' should be called with authentication.\n  needsAuthentication: true,\n\n  // The dispatch function for the Redux store.\n  dispatch: store.dispatch,\n\n  // A function which returns the latests EnumsStore from Redux.\n  enumsStore: () =\u003e store.getState().enums\n});\n```\n\nThe enums module must be configured before the application\nis rendered.\n\nFinally you will have load the enums from the back-end using\nthe `loadEnums` function. If in order for the constraints\nto be loaded you need to be logged in, you should load the enums\nas soon as you know that you are logged in:\n\n```js\nimport { loadEnums } from 'mad-spring-enum';\nimport { login } from 'somewhere';\n\nclass Login extends Component {\n  doLogin(username, password) {\n    login({ username, password })\n      .then(loadEnums); // Load enums ASAP\n  }\n\n  render() {\n    // Render here which calls doLogin\n  }\n}\n```\n\nIf you do not need a login before you can fetch the enums\nsimply fetch them using `loadEnums` as soon as possible.\n\n# Usage\n\nNow assuming you have an '' from the back-end and you want to use\nit in the front-end, you can retrieve the values of the enum by calling:\n\n```js\nimport { getEnum, EnumValues } from 'mad-spring-enum';\nimport type { EnumValues } from 'mad-spring-enum';\n\nconst userRoles: EnumValues = getEnum('UserRole'));\n\n// now use userRoles as you please, it should be an array of strings.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F42bv%2Fmad-spring-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F42bv%2Fmad-spring-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F42bv%2Fmad-spring-enum/lists"}