-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-your-middleware.ts
47 lines (40 loc) · 1.3 KB
/
create-your-middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Middleware } from 'hyperlink-middleware';
export interface RandomParameters {
hello: string;
example: string;
}
/** This is a dummy example. We used a factory in case the middleware can be generic.
*
* You can instantiate the middleware with your own parameters:
* `const middleware = MyDummyMiddlewareFactory({ hello: '1', example: 'morning' })`
*/
export function MyDummyMiddlewareFactory(
parameters: RandomParameters
): Middleware {
return (properties, element, next) => {
// Do your own logic here
const url = new URL(properties.href);
url.hostname = 'test.com';
url.hash = parameters.example;
properties.href = url.toString();
// Must be called so following middlewares are chained
next();
};
}
/** You could also directly declare a middleware if you have no logic that depends on the context (like parameters...)
*
* You can instantiate the middleware with your own parameters:
* `const middleware = MyDummyMiddlewareFactory({ hello: '1', example: 'morning' })`
*/
export const MySecondDummyMiddleware: Middleware = (
properties,
element,
next
) => {
// Do your own logic here
const url = new URL(properties.href);
url.hash = 'fixed-hash-forever';
properties.href = url.toString();
// Must be called so following middlewares are chained
next();
};