-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinvariant.ts
60 lines (53 loc) · 1.8 KB
/
invariant.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
const genericMessage = "Invariant Violation";
const {
setPrototypeOf = function (obj: any, proto: any) {
obj.__proto__ = proto;
return obj;
},
} = Object as any;
export class InvariantError extends Error {
framesToPop = 1;
name = genericMessage;
constructor(message: string | number = genericMessage) {
super(
typeof message === "number"
? `${genericMessage}: ${message} (see https://github.com/apollographql/invariant-packages)`
: message
);
setPrototypeOf(this, InvariantError.prototype);
}
}
export function invariant(
condition: any,
message?: string | number,
): asserts condition {
if (!condition) {
throw new InvariantError(message);
}
}
const verbosityLevels = ["debug", "log", "warn", "error", "silent"] as const;
export type VerbosityLevel = (typeof verbosityLevels)[number];
export type ConsoleMethodName = Exclude<VerbosityLevel, "silent">;
let verbosityLevel = verbosityLevels.indexOf("log");
function wrapConsoleMethod<M extends ConsoleMethodName>(name: M) {
return function () {
if (verbosityLevels.indexOf(name) >= verbosityLevel) {
// Default to console.log if this host environment happens not to provide
// all the console.* methods we need.
const method = console[name] || console.log;
return method.apply(console, arguments as any);
}
} as (typeof console)[M];
}
export namespace invariant {
export const debug = wrapConsoleMethod("debug");
export const log = wrapConsoleMethod("log");
export const warn = wrapConsoleMethod("warn");
export const error = wrapConsoleMethod("error");
}
export function setVerbosity(level: VerbosityLevel): VerbosityLevel {
const old = verbosityLevels[verbosityLevel];
verbosityLevel = Math.max(0, verbosityLevels.indexOf(level));
return old;
}
export default invariant;