-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.mock.ts
111 lines (106 loc) · 3.04 KB
/
request.mock.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
/*
* @forgerock/javascript-sdk
*
* middleware.mock.data.ts
*
* Copyright (c) 2020 ForgeRock. All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
import type {
Action,
ActionTypes,
ModifiedFetchArgs,
RequestMiddleware,
} from '@forgerock/shared-types';
type NextFn = () => ModifiedFetchArgs;
const a = 'a' as ActionTypes;
const b = 'b' as ActionTypes;
const one = '1' as ActionTypes;
const two = '2' as ActionTypes;
const add = 'ADD' as ActionTypes;
const reassignment = 'REASSIGNMENT' as ActionTypes;
const mutateAction = 'MUTATE-ACTION' as ActionTypes;
const middleware: RequestMiddleware<ActionTypes>[] = [
(req: ModifiedFetchArgs, action: Action, next: NextFn): void => {
switch (action.type) {
case a:
case b:
req.url.searchParams.set('letter', 'true');
if (req.headers) req.headers.set('x-letter', 'true');
break;
case one:
case two:
req.url.searchParams.set('letter', 'false');
if (req.headers) req.headers.set('x-letter', 'false');
break;
}
next();
},
(req: ModifiedFetchArgs, action: Action, next: NextFn): void => {
switch (action.type) {
case a:
req.url.searchParams.set('char', 'a');
if (req.headers) req.headers.set('x-char', 'a');
break;
case b:
req.url.searchParams.set('char', 'b');
if (req.headers) req.headers.set('x-char', 'b');
break;
}
next();
},
(req: ModifiedFetchArgs, action: Action, next: NextFn): void => {
switch (action.type) {
case one:
req.url.searchParams.set('char', '1');
if (req.headers) req.headers.set('x-char', '1');
break;
case two:
req.url.searchParams.set('char', '2');
if (req.headers) req.headers.set('x-char', '2');
break;
}
next();
},
(req: ModifiedFetchArgs, action: Action, next: NextFn): void => {
switch (action.type) {
case add:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
req.headers?.set('x-char', 'a,' + action.payload);
break;
}
next();
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(req: ModifiedFetchArgs, action: Action, next: NextFn): void => {
switch (action.type) {
case reassignment:
req = {
url: new URL('https://bad.com'),
headers: new Headers({ 'x-bad': 'true' }),
};
break;
}
next();
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(_req: ModifiedFetchArgs, action: Action, next: NextFn): void => {
switch (action.type) {
case mutateAction:
action.type = 'hello' as ActionTypes;
break;
}
next();
},
];
export default middleware;