Skip to content

Commit 054cbbb

Browse files
authored
Merge pull request #126 from zxbodya/enable-type-publish
More type improvements
2 parents ce9f0e3 + ae71aa3 commit 054cbbb

File tree

11 files changed

+258
-161
lines changed

11 files changed

+258
-161
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default defineConfig([
2727
'@typescript-eslint/no-require-imports': 'off',
2828
'@typescript-eslint/no-this-alias': 'off',
2929
'@typescript-eslint/no-explicit-any': 'off',
30+
'@typescript-eslint/no-namespace': 'off',
3031
},
3132
},
3233
]);

src/alert/condition.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import type { GrafanaCondition } from '../grafana';
1+
import type {
2+
GrafanaCondition,
3+
GrafanaEvaluatorType,
4+
GrafanaOperatorType,
5+
GrafanaReducerType,
6+
} from '../grafana';
27

38
class Condition {
4-
private state: GrafanaCondition;
5-
private _evaluator: { params: any[]; type: string };
6-
private _operator: { type: string };
7-
private _query: { params: string[] };
8-
private _reducer: { params: any[]; type: string };
9+
private state: Partial<GrafanaCondition>;
10+
private _evaluator: GrafanaCondition['evaluator'];
11+
private _operator: GrafanaCondition['operator'];
12+
private _query: GrafanaCondition['query'];
13+
private _reducer: GrafanaCondition['reducer'];
914
constructor(opts: Partial<GrafanaCondition> = {}) {
10-
// @ts-expect-error todo: should fields be optional?
1115
this.state = {};
1216

1317
this._evaluator = {
@@ -32,7 +36,12 @@ class Condition {
3236
this.state[opt] = opts[opt];
3337
});
3438
}
35-
withEvaluator(value, type) {
39+
withEvaluator(value: (string | number)[], type: 'within_range'): Condition;
40+
withEvaluator(value: string | number, type: 'gt' | 'lt'): Condition;
41+
withEvaluator(
42+
value: (string | number) | (string | number)[],
43+
type: GrafanaEvaluatorType
44+
) {
3645
const types = ['gt', 'lt', 'within_range'];
3746

3847
if (!types.includes(type)) {
@@ -42,15 +51,15 @@ class Condition {
4251
this._evaluator.type = type;
4352

4453
if (['gt', 'lt'].includes(type)) {
45-
this._evaluator.params = [value];
54+
this._evaluator.params = [value as string];
4655
} else if (Array.isArray(value)) {
4756
this._evaluator.params = value;
4857
}
4958

5059
return this;
5160
}
52-
withOperator(operator) {
53-
const types = ['and', 'or'];
61+
withOperator(operator: GrafanaOperatorType) {
62+
const types = ['and', 'or'] as const;
5463

5564
if (!types.includes(operator)) {
5665
throw Error(`Operator must be one of [${types.toString}]`);
@@ -67,7 +76,7 @@ class Condition {
6776
this._operator.type = 'and';
6877
return this;
6978
}
70-
onQuery(query, duration, from) {
79+
onQuery(query: string, duration?: string, from?: string) {
7180
if (typeof query !== 'string') {
7281
throw Error(
7382
'Query identifier must be a string. eg. "A" or "B", etc...'
@@ -79,7 +88,7 @@ class Condition {
7988

8089
return this;
8190
}
82-
withReducer(type) {
91+
withReducer(type: GrafanaReducerType) {
8392
const types = [
8493
'min',
8594
'max',
@@ -89,7 +98,7 @@ class Condition {
8998
'last',
9099
'median',
91100
'diff',
92-
];
101+
] satisfies GrafanaReducerType[];
93102

94103
if (!types.includes(type)) {
95104
throw Error(`Reducer has to be one of [${types.toString()}]`);
@@ -98,7 +107,7 @@ class Condition {
98107
this._reducer.type = type;
99108
return this;
100109
}
101-
generate() {
110+
generate(): GrafanaCondition {
102111
return Object.assign(
103112
{},
104113
{

src/alert/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import Alert = require('./alert');
2-
import Condition = require('./condition');
1+
import _Alert = require('./alert');
2+
import _Condition = require('./condition');
33

4-
export = {
5-
Alert: Alert,
6-
Condition: Condition,
7-
};
4+
namespace alert {
5+
export type Alert = _Alert;
6+
export const Alert = _Alert;
7+
export type Condition = _Condition;
8+
export const Condition = _Condition;
9+
}
10+
11+
export = alert;

src/annotations/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
import Graphite = require('./graphite');
21+
import _Graphite = require('./graphite');
2222

23-
export = {
24-
Graphite: Graphite,
25-
};
23+
namespace annotations {
24+
export type Graphite = _Graphite;
25+
export const Graphite = _Graphite;
26+
}
27+
28+
export = annotations;

0 commit comments

Comments
 (0)