-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
146 lines (118 loc) · 3.17 KB
/
client.ts
File metadata and controls
146 lines (118 loc) · 3.17 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as BS from '@bugsnag/js';
import * as E from 'fp-ts/Either';
import * as IOE from 'fp-ts/IOEither';
import {constVoid as undef} from 'fp-ts/function';
import {pipe} from 'fp-ts/function';
import {Config, validate} from './validate';
// --- Constants
const DEFAULT_CONFIG: Partial<Config> = {
enabledBreadcrumbTypes: [
'error',
'manual',
'navigation',
'process',
'request',
'state',
'user'
]
};
const NOT_STARTED_ERR_MSG = 'Client not yet started';
// --- States
type ActualClient = ConfigError | Still | Started;
interface ConfigError {
readonly type: 'ConfigError';
readonly error: Error;
}
const ConfigError = (error: Error): ConfigError => ({
type: 'ConfigError',
error
});
interface Still {
readonly type: 'Still';
readonly config: Config;
}
const Still = (config: Config): Still => ({type: 'Still', config});
interface Started {
readonly type: 'Started';
readonly bugsnag: BS.Client;
}
const Started = (bugsnag: BS.Client): Started => ({
type: 'Started',
bugsnag
});
// --- Client
export interface Client {
readonly client: () => ActualClient;
readonly start: () => void;
readonly notify: (
error: BS.NotifiableError,
onError?: BS.OnErrorCallback
) => IOE.IOEither<Error, void>;
readonly setUser: (user: BS.User) => IOE.IOEither<Error, void>;
}
export const create =
(creator: BS.BugsnagStatic) =>
(config: Config): Client => {
let actualClient = pipe(
validate(config),
E.map(withDefaults),
E.fold<Error, Config, ActualClient>(ConfigError, Still)
);
const c: Client = {
client: () => actualClient,
start: () =>
match(
actualClient,
undef,
s => (actualClient = Started(creator.start(s.config))),
undef
),
notify: (error, onError) =>
match(
actualClient,
configErrorThrows,
stillThrows,
notify(error, onError)
),
setUser: user =>
match(actualClient, configErrorThrows, stillThrows, setUser(user))
};
return c;
};
// --- Helpers
function match<R>(
value: ActualClient,
whenConfigError: (v: ConfigError) => R,
whenStill: (v: Still) => R,
whenStarted: (v: Started) => R
): R {
switch (value.type) {
case 'ConfigError':
return whenConfigError(value);
case 'Still':
return whenStill(value);
case 'Started':
return whenStarted(value);
}
}
function withDefaults(c: Config): Config {
return Object.assign({}, DEFAULT_CONFIG, c);
}
function configErrorThrows(client: ConfigError): IOE.IOEither<Error, void> {
return IOE.MonadThrow.throwError(client.error);
}
function stillThrows(_: Still): IOE.IOEither<Error, void> {
return IOE.MonadThrow.throwError(new Error(NOT_STARTED_ERR_MSG));
}
type ExecWhenIsStarted = (client: Started) => IOE.IOEither<Error, void>;
function notify(
error: BS.NotifiableError,
onError?: BS.OnErrorCallback
): ExecWhenIsStarted {
return ({bugsnag}) =>
IOE.tryCatch(() => bugsnag.notify(error, onError), E.toError);
}
function setUser(user: BS.User): ExecWhenIsStarted {
return ({bugsnag}) =>
IOE.rightIO(() => bugsnag.setUser(user.id, user.email, user.name));
}