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

feat: slack UX improvements #83

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 22 additions & 40 deletions packages/bot-utils/src/measurements-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function buildLocations (from: string): Locations[] {
.map((l): Locations => ({ magic: l }));
}

export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
export const buildPostMeasurements = (args: Flags): PostMeasurement => {
const {
cmd,
target,
Expand All @@ -44,15 +44,17 @@ export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
} = args;
const locations = buildLocations(from);

if (locations.length > 10) { throw new Error('You can only query up to 10 different locations at once!'); }

const postArray: PostMeasurement[] = [];
if (locations.length > 10) {
throw new Error('You can only query up to 10 different locations at once!');
}

if (locations.length === 0) { throw new Error('Empty location! Run "/globalping help" for more information.'); }
if (locations.length === 0) {
throw new Error('Empty location! Run "/globalping help" for more information.');
}

switch (cmd) {
case 'ping': {
postArray.push({
case 'ping':
return {
type: 'ping',
target,
inProgressUpdates: false,
Expand All @@ -61,13 +63,9 @@ export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
measurementOptions: {
...packets && { packets },
},
});

break;
}

case 'traceroute': {
postArray.push({
};
case 'traceroute':
return {
type: 'traceroute',
target,
inProgressUpdates: false,
Expand All @@ -85,13 +83,9 @@ export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
},
...port && { port },
},
});

break;
}

case 'dns': {
postArray.push({
};
case 'dns':
return {
type: 'dns',
target,
inProgressUpdates: false,
Expand Down Expand Up @@ -122,13 +116,10 @@ export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
...resolver && { resolver },
...trace && { trace },
},
});
};

break;
}

case 'mtr': {
postArray.push({
case 'mtr':
return {
type: 'mtr',
target,
inProgressUpdates: false,
Expand All @@ -147,13 +138,9 @@ export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
...port && { port },
...packets && { packets },
},
});

break;
}

case 'http': {
postArray.push({
};
case 'http':
return {
type: 'http',
target,
inProgressUpdates: false,
Expand Down Expand Up @@ -187,10 +174,7 @@ export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
headers,
},
},
});

break;
}
};

default: {
throwArgError(
Expand All @@ -202,6 +186,4 @@ export const buildPostMeasurements = (args: Flags): PostMeasurement[] => {
throw new Error('Unknown error.');
}
}

return postArray;
};
34 changes: 13 additions & 21 deletions packages/bot-utils/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import type { PostMeasurement, PostMeasurementResponse } from './types.js';
import { userAgent } from './user-agent.js';

export const postMeasurement = async (
optsArr: PostMeasurement[],
opts: PostMeasurement,
token?: string,
): Promise<PostMeasurementResponse[]> => {
let index = 0;

): Promise<PostMeasurementResponse> => {
try {
const measurementArr: PostMeasurementResponse[] = [];
const headers: { [key: string]: string } = {
'Content-Type': 'application/json',
'User-Agent': userAgent(),
Expand All @@ -22,28 +19,23 @@ export const postMeasurement = async (
headers.Authorization = `Bearer ${token}`;
}

for (const opts of optsArr) {
// eslint-disable-next-line no-await-in-loop
const res = await got.post('https://api.globalping.io/v1/measurements', {
headers,
json: opts,
});
// eslint-disable-next-line no-await-in-loop
const res = await got.post('https://api.globalping.io/v1/measurements', {
headers,
json: opts,
});

if (res.statusCode === 202) {
measurementArr.push(JSON.parse(res.body));
index += 1;
} else {
const body = JSON.parse(res.body);
body.location = opts.locations[0].magic;
throw new Error(body);
}
if (res.statusCode !== 202) {
const body = JSON.parse(res.body);
body.location = opts.locations[0].magic;
throw new Error(body);
}

return measurementArr;
return JSON.parse(res.body);
} catch (error) {
if (error instanceof HTTPError) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const location = optsArr[index].locations[0].magic!;
const location = opts.locations[0].magic!;
const newError = new PostError(error.response, location, error.message);

throw newError;
Expand Down
Loading