Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature/typescript] Moved Api,Domain,helpers,logic from flow to ts #330

Merged
merged 10 commits into from
Aug 20, 2020
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react/require-default-props": "off",
"react/prop-types": "off",
"react-hooks/rules-of-hooks": "error", // Checks rules of Hooks
"react-hooks/exhaustive-deps": "warn" // Checks effect dependencies
"react-hooks/exhaustive-deps": "warn", // Checks effect dependencies
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "args": "all"}]
}
}
4 changes: 2 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"parser": "babel-flow",
"parser": "typescript",
"printWidth": 100,
"tabWidth": 4,
"trailingComma": "es5",
"endOfLine": "lf"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@skbkontur/react-icons": "4.1.0",
"@skbkontur/react-stack-layout": "1.0.3",
"@skbkontur/react-stack-layout": "^1.0.3",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Версии старайся ставить --save-exact

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему? Таким способом не будет автоматического обновления, которое притянет мажорные изменения, зато все мелкие багфиксы будут актуальны в проекте

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потому что мажорные изменения, увы, бывают breaking-change

Copy link

@sorovlad sorovlad Aug 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вы скорее про patch изменения говорите, мажорные изменения это первая цифра из трех, и если она меняется, то это 99.99% breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sashasushko, @sorovlad Это понятно) но при такой записи "@skbkontur/react-stack-layout": "^1.0.3" не притянется версия с первой цифрой 2. Ее все равно нужно будет вручную править. А вот вторую и третью будет обновлять сам. Насколько я понимаю, это багфиксы и они не могут быть breaking change?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В идеально мире да, не могут. Но бывало, что ломают.
В yarn при вызове "install" обновляются пакеты. Здесь мне нравится подход npm, версии обновляются только при установке или редактировании версии пакетов. И есть команда "ci", которая ставит только что нужно.

"@skbkontur/react-ui": "2.4.0",
"@skbkontur/react-ui-validations": "1.1.3",
"bundle-loader": "0.5.6",
Expand Down Expand Up @@ -60,6 +60,7 @@
"@storybook/addons": "^5.3.17",
"@storybook/react": "^5.3.17",
"@types/classnames": "^2.2.10",
"@types/jest": "^26.0.9",
"@types/react": "^16.9.43",
"@types/react-dom": "^16.9.8",
"@types/react-router-dom": "^5.1.5",
Expand Down
156 changes: 92 additions & 64 deletions src/Api/MoiraApi.js → src/Api/MoiraApi.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
// @flow
import queryString from "query-string";
import type { Config } from "../Domain/Config";
import type { EventList } from "../Domain/Event";
import type { Trigger, TriggerList, TriggerState } from "../Domain/Trigger";
import type { Settings } from "../Domain/Settings";
import type { TagStat } from "../Domain/Tag";
import type { PatternList } from "../Domain/Pattern";
import type { NotificationList } from "../Domain/Notification";
import type { Contact, ContactList } from "../Domain/Contact";
import type { ContactCreateInfo } from "../Domain/ContactCreateInfo";
import type { Subscription } from "../Domain/Subscription";
import type { Schedule } from "../Domain/Schedule";
import type { NotifierState } from "../Domain/MoiraServiceStates";

export type SubscriptionCreateInfo = {|
sched: Schedule,
tags: Array<string>,
throttling: boolean,
contacts: Array<string>,
enabled: boolean,
any_tags: boolean,
user: string,
id?: string,
ignore_recoverings: boolean,
ignore_warnings: boolean,
import * as queryString from "query-string";
import { Config } from "../Domain/Config";
import { EventList } from "../Domain/Event";
import { Trigger, TriggerList, TriggerState } from "../Domain/Trigger";
import { Settings } from "../Domain/Settings";
import { TagStat } from "../Domain/Tag";
import { PatternList } from "../Domain/Pattern";
import { NotificationList } from "../Domain/Notification";
import { Contact, ContactList } from "../Domain/Contact";
import { ContactCreateInfo } from "../Domain/ContactCreateInfo";
import { Subscription } from "../Domain/Subscription";
import { Schedule } from "../Domain/Schedule";
import { NotifierState } from "../Domain/MoiraServiceStates";

export type SubscriptionCreateInfo = {
sched: Schedule;
tags: Array<string>;
throttling: boolean;
contacts: Array<string>;
enabled: boolean;
any_tags: boolean;
user: string;
id?: string;
ignore_recoverings: boolean;
ignore_warnings: boolean;
plotting?: {
enabled: boolean,
theme: "light" | "dark",
},
|};
enabled: boolean;
theme: "light" | "dark";
};
};

export type TagList = {|
list: Array<string>,
|};
export type TagList = {
list: Array<string>;
};

export type TagStatList = {|
list: Array<TagStat>,
|};
export type TagStatList = {
list: Array<TagStat>;
};

export interface IMoiraApi {
getSettings(): Promise<Settings>;
Expand All @@ -61,13 +60,27 @@ export interface IMoiraApi {
tags: Array<string>,
searchText: string
): Promise<TriggerList>;
getTrigger(id: string, params: Object): Promise<Trigger>;
addTrigger(data: $Shape<Trigger>): Promise<{ [key: string]: string }>;
setTrigger(id: string, data: $Shape<Trigger>): Promise<{ [key: string]: string }>;
getTrigger(id: string, params?: { populated: boolean }): Promise<Trigger>;
addTrigger(
data: Partial<Trigger>
): Promise<{
[key: string]: string;
sorovlad marked this conversation as resolved.
Show resolved Hide resolved
}>;
setTrigger(
id: string,
data: Partial<Trigger>
): Promise<{
[key: string]: string;
}>;
delTrigger(id: string): Promise<void>;
setMaintenance(
triggerId: string,
data: { trigger?: number, metrics?: { [metric: string]: number } }
data: {
trigger?: number;
metrics?: {
[metric: string]: number;
};
}
): Promise<void>;
getTriggerState(id: string): Promise<TriggerState>;
getTriggerEvents(id: string, page: number): Promise<EventList>;
Expand All @@ -85,7 +98,7 @@ export interface IMoiraApi {
class ApiError extends Error {
status: number;

constructor({ message, status }) {
constructor({ message, status }: { message: string; status: number }) {
super(message);
this.name = "ApiError";
this.status = status;
Expand All @@ -101,19 +114,19 @@ export { statusCode };
export default class MoiraApi implements IMoiraApi {
apiUrl: string;

config: Config;
config?: Config;

triggerListPageSize: number = 20;
triggerListPageSize = 20;

eventHistoryPageSize: number = 100;
eventHistoryPageSize = 100;

constructor(apiUrl: string) {
this.apiUrl = apiUrl;
}

static async checkStatus(response: Response) {
static async checkStatus(response: Response): Promise<void> {
if (!(response.status >= 200 && response.status < 300)) {
let serverResponse: { status: string, error: string } | null = null;
let serverResponse: { status: string; error: string } | null = null;
const errorText = await response.text();
try {
serverResponse = JSON.parse(errorText);
Expand Down Expand Up @@ -174,7 +187,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async testContact(contactId: string) {
async testContact(contactId: string): Promise<void> {
const url = `${this.apiUrl}/contact/${encodeURI(contactId)}/test`;
const response = await fetch(url, {
method: "POST",
Expand Down Expand Up @@ -219,7 +232,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async testSubscription(subscriptionId: string) {
async testSubscription(subscriptionId: string): Promise<void> {
const url = `${this.apiUrl}/subscription/${encodeURI(subscriptionId)}/test`;
const response = await fetch(url, {
method: "PUT",
Expand All @@ -228,7 +241,7 @@ export default class MoiraApi implements IMoiraApi {
await MoiraApi.checkStatus(response);
}

async deleteContact(contactId: string) {
async deleteContact(contactId: string): Promise<void> {
const url = `${this.apiUrl}/contact/${encodeURI(contactId)}`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -247,7 +260,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async delPattern(pattern: string) {
async delPattern(pattern: string): Promise<void> {
const url = `${this.apiUrl}/pattern/${encodeURI(pattern)}`;
const response = await fetch(url, {
method: "DELETE",
Expand Down Expand Up @@ -276,7 +289,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async delTag(tag: string) {
async delTag(tag: string): Promise<void> {
const url = `${this.apiUrl}/tag/${encodeURI(tag)}`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -295,6 +308,7 @@ export default class MoiraApi implements IMoiraApi {
{
/* eslint-disable */
p: page,

/* eslint-enable */
size: this.triggerListPageSize,
tags,
Expand All @@ -311,7 +325,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async getTrigger(id: string, params: Object): Promise<Trigger> {
async getTrigger(id: string, params?: { populated: boolean }): Promise<Trigger> {
const url = `${this.apiUrl}/trigger/${encodeURI(id)}${
params ? `?${queryString.stringify(params)}` : ""
}`;
Expand All @@ -324,7 +338,11 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async addTrigger(data: $Shape<Trigger>): Promise<{ [key: string]: string }> {
async addTrigger(
data: Partial<Trigger>
): Promise<{
[key: string]: string;
}> {
const url = `${this.apiUrl}/trigger`;
const response = await fetch(url, {
method: "PUT",
Expand All @@ -335,7 +353,12 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async setTrigger(id: string, data: $Shape<Trigger>): Promise<{ [key: string]: string }> {
async setTrigger(
id: string,
data: Partial<Trigger>
): Promise<{
[key: string]: string;
}> {
const url = `${this.apiUrl}/trigger/${encodeURI(id)}`;
const response = await fetch(url, {
method: "PUT",
Expand All @@ -346,7 +369,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async delTrigger(id: string) {
async delTrigger(id: string): Promise<void> {
const url = `${this.apiUrl}/trigger/${encodeURI(id)}`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -357,8 +380,13 @@ export default class MoiraApi implements IMoiraApi {

async setMaintenance(
triggerId: string,
data: { trigger?: number, metrics?: { [metric: string]: number } }
) {
data: {
trigger?: number;
metrics?: {
[metric: string]: number;
};
}
): Promise<void> {
const url = `${this.apiUrl}/trigger/${encodeURI(triggerId)}/setMaintenance`;
const response = await fetch(url, {
method: "PUT",
Expand Down Expand Up @@ -390,7 +418,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async delThrottling(triggerId: string) {
async delThrottling(triggerId: string): Promise<void> {
const url = `${this.apiUrl}/trigger/${encodeURI(triggerId)}/throttling`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -399,7 +427,7 @@ export default class MoiraApi implements IMoiraApi {
await MoiraApi.checkStatus(response);
}

async delMetric(triggerId: string, metric: string) {
async delMetric(triggerId: string, metric: string): Promise<void> {
const url = `${this.apiUrl}/trigger/${encodeURI(triggerId)}/metrics?name=${encodeURI(
metric
)}`;
Expand All @@ -410,7 +438,7 @@ export default class MoiraApi implements IMoiraApi {
await MoiraApi.checkStatus(response);
}

async delNoDataMetric(triggerId: string) {
async delNoDataMetric(triggerId: string): Promise<void> {
const url = `${this.apiUrl}/trigger/${encodeURI(triggerId)}/metrics/nodata`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -429,7 +457,7 @@ export default class MoiraApi implements IMoiraApi {
return response.json();
}

async delNotification(id: string) {
async delNotification(id: string): Promise<void> {
const url = `${this.apiUrl}/notification?id=${encodeURI(id)}`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -438,7 +466,7 @@ export default class MoiraApi implements IMoiraApi {
await MoiraApi.checkStatus(response);
}

async delAllNotifications() {
async delAllNotifications(): Promise<void> {
const url = `${this.apiUrl}/notification/all`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -447,7 +475,7 @@ export default class MoiraApi implements IMoiraApi {
await MoiraApi.checkStatus(response);
}

async delAllNotificationEvents() {
async delAllNotificationEvents(): Promise<void> {
const url = `${this.apiUrl}/event/all`;
const response = await fetch(url, {
method: "DELETE",
Expand All @@ -456,7 +484,7 @@ export default class MoiraApi implements IMoiraApi {
await MoiraApi.checkStatus(response);
}

async delSubscription(subscriptionId: string) {
async delSubscription(subscriptionId: string): Promise<void> {
const url = `${this.apiUrl}/subscription/${encodeURI(subscriptionId)}`;
const response = await fetch(url, {
method: "DELETE",
Expand Down
23 changes: 0 additions & 23 deletions src/Api/MoiraApiInjection.jsx

This file was deleted.

Loading