This repository was archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrule.ts
78 lines (63 loc) · 1.73 KB
/
rule.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
import Context from './context';
import { YesNoError } from './errors';
import { PartialResponseForRequest } from './filtering/collection';
import { Matcher } from './filtering/matcher';
export enum RuleType {
Live = 'LIVE',
Record = 'RECORD',
Respond = 'RESPOND',
}
export interface IRule {
matcher: Matcher;
mock?: PartialResponseForRequest;
ruleType: RuleType;
}
export interface IRuleParams {
context: Context;
matcher: Matcher;
}
export default class Rule implements IRule {
public matcher: Matcher;
public mock?: PartialResponseForRequest;
public ruleType: RuleType;
private readonly ctx: Context;
constructor({ context, matcher = {} }: IRuleParams) {
this.ctx = context;
this.matcher = matcher;
this.ruleType = RuleType.Record;
}
/**
* Set the rule type to 'record'
*/
public record(): IRule {
const index = this.ctx.rules.length - 1;
if (index < 0) {
throw new YesNoError('No rules have been defined yet');
}
this.ctx.rules[index].ruleType = RuleType.Record;
return this.ctx.rules[index];
}
/**
* Set the rule type to 'live'
*/
public live(): IRule {
const index = this.ctx.rules.length - 1;
if (index < 0) {
throw new YesNoError('No rules have been defined yet');
}
this.ctx.rules[index].ruleType = RuleType.Live;
return this.ctx.rules[index];
}
/**
* Set the rule type to 'respond'
*/
public respond(response: PartialResponseForRequest): IRule {
const index = this.ctx.rules.length - 1;
if (index < 0) {
throw new YesNoError('No rules have been defined yet');
}
this.ctx.rules[index].ruleType = RuleType.Respond;
this.ctx.rules[index].mock = response;
return this.ctx.rules[index];
}
}