-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathconfigSchema.ts
More file actions
207 lines (175 loc) · 6.03 KB
/
Copy pathconfigSchema.ts
File metadata and controls
207 lines (175 loc) · 6.03 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { z, ZodType } from "zod";
/**
* Define a default implementation of the identifier and overrideTypeNode methods for ZodType if it doesn't exist
* It is defined by @duplojs/zod-to-typescript and used to generate the type definitions, but
* it is not available in the zod library itself.
*/
ZodType.prototype.identifier ??= function () {
return this;
};
ZodType.prototype.overrideTypeNode ??= function () {
return this;
};
const NativeUrlSchema = z
.instanceof(URL)
.overrideTypeNode((ts) =>
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("URL"))
);
const RegexpSchema = z
.instanceof(RegExp)
.overrideTypeNode((ts) =>
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("RegExp"))
);
// ===== Process & URL Options =====
const ProcessInfoSchema = z
.object({
name: z.string(),
bundleId: z.string(),
path: z.string(),
windowTitle: z.string().optional(),
})
.identifier("ProcessInfo");
export type ProcessInfo = z.infer<typeof ProcessInfoSchema>;
const OpenUrlOptionsSchema = z
.object({
opener: ProcessInfoSchema.nullable(),
originalUrl: NativeUrlSchema.optional(),
})
.identifier("OpenUrlOptions");
export type OpenUrlOptions = z.infer<typeof OpenUrlOptionsSchema>;
// ===== URL Schemas =====
const UrlTransformerSchema = z
.function(z.tuple([NativeUrlSchema, OpenUrlOptionsSchema]))
.returns(z.union([z.string(), NativeUrlSchema]))
.identifier("UrlTransformer");
const UrlTransformSpecificationSchema = z
.union([z.string(), NativeUrlSchema, UrlTransformerSchema])
.identifier("UrlTransformSpecification");
// ===== Matcher Schemas =====
const UrlMatcherFunctionSchema = z
.function(z.tuple([NativeUrlSchema, OpenUrlOptionsSchema]))
.returns(z.boolean())
.identifier("UrlMatcherFunction");
const UrlMatcherSchema = z
.union([z.string(), RegexpSchema, UrlMatcherFunctionSchema])
.identifier("UrlMatcher");
const UrlMatcherPatternSchema = z.union([
UrlMatcherSchema,
z.array(UrlMatcherSchema),
]);
// ===== Browser Schemas =====
const appTypes = ["appName", "bundleId", "path", "none"] as const;
export type AppType = (typeof appTypes)[number];
const BrowserConfigSchema = z
.object({
name: z.string(),
appType: z.enum(appTypes).optional(),
openInBackground: z.boolean().optional(),
profile: z.string().optional(),
args: z.array(z.string()).optional(),
})
.identifier("BrowserConfig")
.describe("A browser or app to open for urls");
export const BrowserConfigStrictSchema = z.object({
name: z.string(),
appType: z.enum(appTypes),
openInBackground: z.boolean().optional(),
profile: z.string(),
args: z.array(z.string()),
url: z.string(),
});
const BrowserResolverSchema = z
.function(z.tuple([NativeUrlSchema, OpenUrlOptionsSchema]))
.returns(z.union([z.string(), BrowserConfigSchema]))
.identifier("BrowserResolver");
export const BrowserSpecificationSchema = z
.union([z.null(), z.string(), BrowserConfigSchema, BrowserResolverSchema])
.identifier("BrowserSpecification");
// ===== Rule Schemas =====
const RewriteRuleSchema = z
.object({
match: UrlMatcherPatternSchema,
url: UrlTransformSpecificationSchema,
})
.describe(
"A rewrite rule contains a matcher and a url. If the matcher matches when opening a url, the url will be rewritten to the returnedurl in the rewrite rule."
)
.identifier("UrlRewriteRule");
const HandlerRuleSchema = z
.object({
match: UrlMatcherPatternSchema,
browser: BrowserSpecificationSchema,
})
.describe(
"A handler contains a matcher and a browser. If the matcher matches when opening a url, the browser in the handler will be opened."
)
.identifier("BrowserHandler");
// ===== Configuration Schemas =====
const ConfigOptionsSchema = z
.object({
urlShorteners: z.array(z.string()).optional(),
logRequests: z.boolean().optional().describe("Log to file on disk"),
checkForUpdates: z.boolean().optional().describe("Check for updates"),
keepRunning: z.boolean().optional().describe("Keep the app running"),
hideIcon: z.boolean().optional().describe("Hide the app icon"),
hideWindowOnStart: z
.boolean()
.optional()
.describe("Don't open the window when Finicky starts")
})
.identifier("ConfigOptions");
/**
* @internal - don't export this schema as a type
*/
export const ConfigSchema = z
.object({
defaultBrowser: BrowserSpecificationSchema.describe(
"The default browser or app to open for urls where no other handler"
),
options: ConfigOptionsSchema.optional(),
rewrite: z
.array(RewriteRuleSchema)
.optional()
.describe(
"An array of rewriter rules that can change the url being opened"
),
handlers: z
.array(HandlerRuleSchema)
.optional()
.describe(
"An array of handlers to select which browser or app to open for urls"
),
})
.describe(
`
This represents the full \`~/.finicky.js\` configuration object
Example usage:
\`\`\`js
export default = {
defaultBrowser: "Google Chrome",
options: {
logRequests: false
},
handlers: [{
match: "example.com*",
browser: "Firefox"
}]
}
\`\`\`
`
);
export type UrlTransformSpecification = z.infer<
typeof UrlTransformSpecificationSchema
>;
export type UrlTransformer = z.infer<typeof UrlTransformerSchema>;
export type UrlMatcherFunction = z.infer<typeof UrlMatcherFunctionSchema>;
export type UrlMatcher = z.infer<typeof UrlMatcherSchema>;
export type UrlMatcherPattern = z.infer<typeof UrlMatcherPatternSchema>;
export type BrowserConfigStrict = z.infer<typeof BrowserConfigStrictSchema>;
export type BrowserConfig = z.infer<typeof BrowserConfigSchema>;
export type BrowserResolver = z.infer<typeof BrowserResolverSchema>;
export type BrowserSpecification = z.infer<typeof BrowserSpecificationSchema>;
export type RewriteRule = z.infer<typeof RewriteRuleSchema>;
export type HandlerRule = z.infer<typeof HandlerRuleSchema>;
export type ConfigOptions = z.infer<typeof ConfigOptionsSchema>;
export type Config = z.infer<typeof ConfigSchema>;