Skip to content

Commit 93befa3

Browse files
committed
refactor documentation to include routeEnum support and examples
1 parent 6a0b728 commit 93befa3

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

doc/TUTORIAL.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,26 @@ Description of the code:
108108
4. `route` declares the URL path at which the service operates. It also maps components of the URL path to handler parameters.
109109
- `base` defines the first part of the URL that doesn't change, e.g. `/example/`.
110110
- `pattern` defines the variable part of the route, everything that comes after `/example/`. It can include any number of named parameters. These are converted into regular expressions by [`path-to-regexp`][path-to-regexp]. Because a service instance won't be created until it's time to handle a request, the route and other metadata must be obtained by examining the classes themselves. [That's why they're marked `static`.][static]
111-
- There is additional documentation on conventions for [designing badge URLs](./badge-urls.md)
111+
112+
- `routeEnum` (optional): an array of strings that defines a finite set of allowed values for an enum-like parameter in the route. When `routeEnum` is present, the first named parameter in the route `pattern` is treated as the enum value to validate against `routeEnum`.
113+
- Example: for a route with pattern `:type/:user/:repo` you would set `static routeEnum = ['dt', 'dm', 'dd']` and the incoming `type` value will be validated against that array.
114+
- If the first named parameter is not present in `routeEnum`, the request will be rejected before `handle()` is invoked.
115+
116+
```js
117+
// Example service that uses routeEnum
118+
export default class Downloads extends BaseService {
119+
static route = { base: 'downloads', pattern: ':type/:user/:repo' }
120+
static routeEnum = ['dt', 'dm', 'dd']
121+
122+
async handle({ type, user, repo }) {
123+
// `type` is guaranteed to be one of 'dt', 'dm', or 'dd'
124+
// implement fetch/render logic here
125+
}
126+
}
127+
```
128+
129+
- There is additional documentation on conventions for [designing badge URLs](./badge-urls.md)
130+
112131
5. All badges must implement the `async handle()` function that receives parameters to render the badge. Parameters of `handle()` will match the name defined in `route` Because we're capturing a single variable called `text` our function signature is `async handle({ text })`. `async` is needed to let JavaScript do other things while we are waiting for result from external API. Although in this simple case, we don't make any external calls. Our `handle()` function should return an object with 3 properties:
113132
- `label`: the text on the left side of the badge
114133
- `message`: the text on the right side of the badge - here we are passing through the parameter we captured in the route regex

doc/badge-redirectors.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ export default redirector({
6060
category: 'analysis',
6161
route: {
6262
base: 'scrutinizer',
63-
pattern: ':vcs(g|b)/:user/:repo/:branch*',
63+
pattern: ':vcs/:user/:repo/:branch*',
6464
},
65+
routeEnum: ['g', 'b'],
6566
transformPath: ({ vcs, user, repo, branch }) =>
6667
`/scrutinizer/quality/${vcs}/${user}/${repo}${branch ? `/${branch}` : ''}`,
6768
dateAdded: new Date('2025-02-02'),
@@ -81,8 +82,9 @@ export default redirector({
8182
category: 'monitoring',
8283
route: {
8384
base: 'website',
84-
pattern: ':protocol(https|http)/:hostAndPath+',
85+
pattern: ':protocol/:hostAndPath+',
8586
},
87+
routeEnum: ['https', 'http'],
8688
transformPath: () => '/website',
8789
transformQueryParams: ({ protocol, hostAndPath }) => ({
8890
url: `${protocol}://${hostAndPath}`,

doc/badge-urls.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- singular if the badge message represents a single entity, such as the current status of a build (e.g: `/build`), or a more abstract or aggregate representation of the thing (e.g.: `/coverage`, `/quality`)
66
- plural if there are (or may) be many of the thing (e.g: `/dependencies`, `/stars`)
77
- Parameters should always be part of the route if they are required to display a badge e.g: `:packageName` and should be lower camelCase.
8+
- Parameters should always be part of the route if they are required to display a badge e.g: `:packageName` and should be lower camelCase.
9+
- `routeEnum` support: when a service defines a finite set of allowed values for a parameter, the service can declare `static routeEnum = [...]`. In that case the first named parameter in the route `pattern` is treated as the enum to validate against `routeEnum`.
810
- Common optional params like, `:branch` or `:tag` should also be passed as part of the route.
911
- Query string parameters should be used when:
1012
- The parameter is related to formatting. e.g: `/appveyor/tests/:user/:repo?compact_message`.

0 commit comments

Comments
 (0)