-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.ts
175 lines (150 loc) · 5.44 KB
/
types.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as Schema from '@effect/schema/Schema';
import {Brand, Data, Match, type Option, pipe} from 'effect';
import {dual} from 'effect/Function';
export type JiraApiUrl = string & Brand.Brand<'JiraApiUrl'>;
export const JiraApiUrl = Brand.refined<JiraApiUrl>(
(s: string) => s.startsWith('https://') || s.startsWith('http://'),
(s: string) => Brand.error(`'${s}' is not a valid Jira API URL`),
);
export type JiraPat = string & Brand.Brand<'JiraPat'>;
export const JiraPat = Brand.refined<JiraPat>(
(s: string) => s.length > 0,
() => Brand.error('Provided value is not a valid Jira PAT'),
);
export type JiraApiToken = string & Brand.Brand<'JiraApiToken'>;
export const JiraApiToken = Brand.refined<JiraApiToken>(
(s: string) => s.length > 0,
() => Brand.error('Provided value is not a valid Jira API token'),
);
export type JiraUserEmail = string & Brand.Brand<'JiraUserEmail'>;
export const JiraUserEmail = Brand.refined<JiraUserEmail>(
// NOTE: This validation does not strictly adhere to the email RFC 2822. It is
// however sufficient to reject inputs that are obviously not valid email
// addresses while allowing potentially valid ones.
(s: string) => /^.+@.+\..+$/.test(s),
(s: string) => Brand.error(`'${s}' is not a valid email address`),
);
export type JiraKeyPrefix = string & Brand.Brand<'JiraKeyPrefix'>;
export const JiraKeyPrefix = Brand.refined<JiraKeyPrefix>(
(s: string) => s.length > 0,
(s: string) => Brand.error(`'${s}' is not a valid Jira key prefix`),
);
export type JiraAuth = Data.TaggedEnum<{
JiraDataCenterAuth: {jiraPat: JiraPat};
JiraCloudAuth: {jiraUserEmail: JiraUserEmail; jiraApiToken: JiraApiToken};
}>;
export const {JiraDataCenterAuth, JiraCloudAuth} = Data.taggedEnum<JiraAuth>();
export type AppConfig = {
jiraApiUrl: JiraApiUrl;
defaultJiraKeyPrefix: Option.Option<JiraKeyPrefix>;
jiraAuth: JiraAuth;
};
export interface GitBranch {
name: string;
isCurrent: boolean;
}
export const GitBranch = Data.case<GitBranch>();
export const JiraIssuetypeSchema = Schema.Struct({
name: Schema.String,
});
export type JiraIssuetype = Schema.Schema.Type<typeof JiraIssuetypeSchema>;
// TODO: transform nullable values into optionals
export const JiraIssueSchema = Schema.Struct({
key: Schema.String,
fields: Schema.Struct({
summary: Schema.String,
issuetype: JiraIssuetypeSchema,
status: Schema.Struct({
name: Schema.String,
}),
description: Schema.NullOr(Schema.String),
assignee: Schema.NullOr(
Schema.Struct({
displayName: Schema.String,
}),
),
creator: Schema.Struct({
displayName: Schema.String,
}),
}),
});
export type JiraIssue = Schema.Schema.Type<typeof JiraIssueSchema>;
export type GitCreateJiraBranchResult = Data.TaggedEnum<{
CreatedBranch: {branch: string};
SwitchedBranch: {branch: string};
ResetBranch: {branch: string};
}>;
export const {CreatedBranch, SwitchedBranch, ResetBranch} =
Data.taggedEnum<GitCreateJiraBranchResult>();
export type SwitchedBranch = ReturnType<typeof SwitchedBranch>;
export type CreatedBranch = ReturnType<typeof CreatedBranch>;
export type ResetBranch = ReturnType<typeof ResetBranch>;
export const matchGitCreateJiraBranchResult: {
<A>(
result: GitCreateJiraBranchResult,
matcher: {
onCreatedBranch: (branch: CreatedBranch) => A;
onSwitchedBranch: (branch: SwitchedBranch) => A;
onResetBranch: (branch: ResetBranch) => A;
},
): A;
<A>(matcher: {
onCreatedBranch: (branch: CreatedBranch) => A;
onSwitchedBranch: (branch: SwitchedBranch) => A;
onResetBranch: (branch: ResetBranch) => A;
}): (result: GitCreateJiraBranchResult) => A;
} = dual(
2,
<A>(
result: GitCreateJiraBranchResult,
matcher: {
onCreatedBranch: (branch: CreatedBranch) => A;
onSwitchedBranch: (branch: SwitchedBranch) => A;
onResetBranch: (branch: ResetBranch) => A;
},
): A =>
pipe(
Match.type<GitCreateJiraBranchResult>(),
Match.tag('CreatedBranch', (res) => matcher.onCreatedBranch(res)),
Match.tag('SwitchedBranch', (res) => matcher.onSwitchedBranch(res)),
Match.tag('ResetBranch', (res) => matcher.onResetBranch(res)),
Match.exhaustive,
)(result) as A,
);
export type GitJiraBranchError =
| AppConfigError
| UsageError
| GitExecError
| JiraApiError;
export declare namespace GitJiraBranchError {
export interface Proto {
readonly _tag: string;
readonly message: string;
}
export type ProvidedFields = '_tag';
}
const makeError =
<E extends GitJiraBranchError.Proto>(tag: E['_tag']) =>
(args: Omit<E, GitJiraBranchError.ProvidedFields>): E =>
Data.struct({
_tag: tag,
...args,
} as E);
export interface GitExecError extends GitJiraBranchError.Proto {
readonly _tag: 'GitExecError';
}
export const GitExecError = makeError<GitExecError>('GitExecError');
export interface UsageError extends GitJiraBranchError.Proto {
readonly _tag: 'UsageError';
}
export const UsageError = makeError<UsageError>('UsageError');
export interface AppConfigError extends GitJiraBranchError.Proto {
readonly _tag: 'AppConfigError';
}
export const AppConfigError = makeError<AppConfigError>('AppConfigError');
export const isAppConfigError = (e: unknown): e is AppConfigError =>
e instanceof Object && '_tag' in e && e._tag === 'AppConfigError';
export interface JiraApiError extends GitJiraBranchError.Proto {
readonly _tag: 'JiraApiError';
}
export const JiraApiError = makeError<JiraApiError>('JiraApiError');