scipro/README.md
Andreas Svanberg 1554d4bc27
All checks were successful
Build and test / build-and-test (push) Successful in 11m55s
Enforce code formatting via Prettier (#44)
Fixes #43 by introducing [Prettier](https://prettier.io/).

Prettier is an extremely opinionated formatter. It will reformat every single line according to its style. There are virtually no configuration options so there can be no discussion about formatting rules.

There are two parameters that are configurable; indent length and line length. Indent length has been set to 4 because that's the Java standard.

Line length defaults to 80 but has been increased to 100. The rational for this is that Prettier was created for JavaScript which is much less verbose than Java. Not only does every Java line start with 8 spaces of indentation vs. JavaScripts 0 or 2, it also has types wile JavaScript does not and uses `const` for variable declarations. Compare the two below examples as well as an actual example from the source code that is too long for the default 80 characters. I have no problem dropping down to the default 80 if that is preferred I just felt that with the average length of a line of Java code being pretty long, excessive wrapping would reduce readability.

```javascript
  const attributes = {
    ...
  };
```
```java
        Map<String, String> attributes = Map.of(
            ...
        );
```

Or the following real code which is 97 characters long.
```java
        Set<ProjectParticipant> contributors = daisyAPI.getContributors(project.getIdentifier());
```

Reviewed-on: #44
Reviewed-by: Tom Zhao <tom.zhao@dsv.su.se>
Co-authored-by: Andreas Svanberg <andreass@dsv.su.se>
Co-committed-by: Andreas Svanberg <andreass@dsv.su.se>
2024-12-02 14:17:59 +01:00

28 lines
1.7 KiB
Markdown

## Working with the API
The API is protected by OAuth 2 acting as a [resource server](https://www.oauth.com/oauth2-servers/the-resource-server/)
verifying tokens using [token introspection](https://datatracker.ietf.org/doc/html/rfc7662).
When developing it uses a locally running instance of an
[authorization server](https://datatracker.ietf.org/doc/html/rfc6749#section-1.1)
that is run inside [Docker](https://www.docker.com). It can be started with `docker compose -f docker-compose.yml up`.
Since there is no frontend to interact with the authorization server there's a helper script in
[GetToken.java](GetToken.java) that can be run directly with `java GetToken.java` to run through the authorization flow
and get an access token.
Once the token has been obtained go to the [Swagger UI](http://localhost:8080/api/swagger) to interact with the API.
Click the "Authorize" button in the top right and paste the access token to log in.
## Code formatting
This project uses [prettier-java](https://github.com/jhipster/prettier-java)
to format all Java code. To reformat the code run
`./mvnw validate frontend:npm@reformat -pl .`.
Yes it's a mouthful but unfortunately the [prettier-maven-plugin](https://github.com/HubSpot/prettier-maven-plugin)
does not work due to an [outstanding issue](https://github.com/HubSpot/prettier-maven-plugin/issues/79).
An easier way to reformat code is to set IntelliJ to do it on save. Go to
`Settings -> Language & Frameworks -> JavaScript -> Prettier` and then check
`Automatic Prettier Configuration`, set `Run for files` to `**/*.{java}`,
and finally check `Run on save`.
The formatting is validated by CI, but you should do it beforehand with a simple `./mvnw verify -pl .`.