Skip to content

Commit ef0bbfd

Browse files
jellydngithub-actions[bot]
authored andcommitted
Apply automatic changes
1 parent b457eb8 commit ef0bbfd

File tree

11 files changed

+326
-314
lines changed

11 files changed

+326
-314
lines changed

moleculer.config.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,23 @@ const config: BrokerOptions = {
3939

4040
// Enable/disable logging or use custom logger. More info: https://moleculer.services/docs/0.14/logging.html
4141
// Available logger types: "Console", "File", "Pino", "Winston", "Bunyan", "debug", "Log4js", "Datadog"
42-
logger: [{
43-
// Note: Change to Console if you want to see the logger output
44-
type: "Pino",
45-
options: {
46-
pino: {
47-
// More info: http://getpino.io/#/docs/api?id=options-object
48-
options: {
49-
timestamp: stdTimeFunctions.isoTime,
42+
logger: [
43+
{
44+
// Note: Change to Console if you want to see the logger output
45+
type: "Pino",
46+
options: {
47+
pino: {
48+
// More info: http://getpino.io/#/docs/api?id=options-object
49+
options: {
50+
timestamp: stdTimeFunctions.isoTime,
51+
},
5052
},
5153
},
5254
},
53-
}, {
54-
type: "Console",
55-
}],
56-
55+
{
56+
type: "Console",
57+
},
58+
],
5759

5860
// Define transporter.
5961
// More info: https://moleculer.services/docs/0.14/networking.html

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
"name": "moleculer-typescript-template",
33
"version": "0.3.0",
44
"description": "My Moleculer-based microservices project",
5-
"keywords": [
6-
"microservices",
7-
"moleculer",
8-
"moleculer typescript template"
9-
],
5+
"keywords": ["microservices", "moleculer", "moleculer typescript template"],
106
"license": "MIT",
117
"author": "Huynh Duc Dung",
128
"scripts": {
@@ -91,4 +87,4 @@
9187
"engines": {
9288
"node": ">= 18.17.x"
9389
}
94-
}
90+
}

playwright.config.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import { defineConfig, devices } from "@playwright/test";
22

33
export default defineConfig({
4-
testDir: "./tests",
5-
fullyParallel: true,
6-
forbidOnly: !!process.env.CI,
7-
retries: process.env.CI ? 2 : 0,
8-
workers: process.env.CI ? 1 : undefined,
9-
reporter: "html",
10-
use: {
11-
baseURL: "http://localhost:4567",
12-
trace: "on-first-retry",
13-
},
4+
testDir: "./tests",
5+
fullyParallel: true,
6+
forbidOnly: !!process.env.CI,
7+
retries: process.env.CI ? 2 : 0,
8+
workers: process.env.CI ? 1 : undefined,
9+
reporter: "html",
10+
use: {
11+
baseURL: "http://localhost:4567",
12+
trace: "on-first-retry",
13+
},
1414

15-
projects: [
16-
{
17-
name: "chromium",
18-
use: { ...devices["Desktop Chrome"] },
19-
},
20-
{
21-
name: "firefox",
22-
use: { ...devices["Desktop Firefox"] },
23-
},
24-
{
25-
name: "webkit",
26-
use: { ...devices["Desktop Safari"] },
27-
},
28-
],
15+
projects: [
16+
{
17+
name: "chromium",
18+
use: { ...devices["Desktop Chrome"] },
19+
},
20+
{
21+
name: "firefox",
22+
use: { ...devices["Desktop Firefox"] },
23+
},
24+
{
25+
name: "webkit",
26+
use: { ...devices["Desktop Safari"] },
27+
},
28+
],
2929

30-
webServer: {
31-
command: "npm run dev",
32-
url: "http://localhost:4567",
33-
reuseExistingServer: !process.env.CI,
34-
},
30+
webServer: {
31+
command: "npm run dev",
32+
url: "http://localhost:4567",
33+
reuseExistingServer: !process.env.CI,
34+
},
3535
});

services/weather.service.ts

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,79 @@
11
import type { Context, Service, ServiceSchema } from "moleculer";
22

33
export type WeatherData = {
4-
current: {
5-
temperature_2m: number;
6-
relative_humidity_2m: number;
7-
rain: number;
8-
weather_code: number;
9-
};
4+
current: {
5+
temperature_2m: number;
6+
relative_humidity_2m: number;
7+
rain: number;
8+
weather_code: number;
9+
};
1010
};
1111

1212
export type GetWeatherParams = {
13-
latitude: string;
14-
longitude: string;
13+
latitude: string;
14+
longitude: string;
1515
};
1616

1717
type ServiceSettings = {
18-
weatherApiUrl: string;
18+
weatherApiUrl: string;
1919
};
2020

2121
type ServiceMethods = {
22-
fetchWeatherData(params: GetWeatherParams): Promise<WeatherData>;
22+
fetchWeatherData(params: GetWeatherParams): Promise<WeatherData>;
2323
};
2424

2525
type ServiceThis = Service<ServiceSettings> & ServiceMethods;
2626

2727
const weatherService: ServiceSchema<ServiceSettings, ServiceThis> = {
28-
name: "weather",
29-
30-
settings: {
31-
weatherApiUrl: "https://api.open-meteo.com/v1/forecast",
32-
},
33-
34-
dependencies: [],
35-
36-
actions: {
37-
getCurrentWeather: {
38-
rest: "GET /",
39-
params: {
40-
latitude: { type: "string", min: 6, max: 25 },
41-
longitude: { type: "string", min: 6, max: 25 },
42-
},
43-
async handler(
44-
this: ServiceThis,
45-
ctx: Context<GetWeatherParams>,
46-
): Promise<WeatherData> {
47-
return this.fetchWeatherData(ctx.params);
48-
},
49-
},
50-
},
51-
52-
events: {},
53-
54-
methods: {
55-
async fetchWeatherData(params: GetWeatherParams): Promise<WeatherData> {
56-
const queryParams = new URLSearchParams({
57-
latitude: params.latitude.toString(),
58-
longitude: params.longitude.toString(),
59-
current: "temperature_2m,relative_humidity_2m,rain,weather_code",
60-
});
61-
const response = await fetch(`${this.settings.weatherApiUrl}?${queryParams}`);
62-
if (!response.ok) {
63-
throw new Error(`Weather API error: ${response.statusText}`);
64-
}
65-
return response.json();
66-
},
67-
},
68-
69-
created() {
70-
this.logger.info(`The ${this.name} service created.`);
71-
},
72-
73-
async started() {
74-
this.logger.info(`The ${this.name} service started.`);
75-
},
76-
77-
async stopped() {
78-
this.logger.info(`The ${this.name} service stopped.`);
79-
},
28+
name: "weather",
29+
30+
settings: {
31+
weatherApiUrl: "https://api.open-meteo.com/v1/forecast",
32+
},
33+
34+
dependencies: [],
35+
36+
actions: {
37+
getCurrentWeather: {
38+
rest: "GET /",
39+
params: {
40+
latitude: { type: "string", min: 6, max: 25 },
41+
longitude: { type: "string", min: 6, max: 25 },
42+
},
43+
async handler(this: ServiceThis, ctx: Context<GetWeatherParams>): Promise<WeatherData> {
44+
return this.fetchWeatherData(ctx.params);
45+
},
46+
},
47+
},
48+
49+
events: {},
50+
51+
methods: {
52+
async fetchWeatherData(params: GetWeatherParams): Promise<WeatherData> {
53+
const queryParams = new URLSearchParams({
54+
latitude: params.latitude.toString(),
55+
longitude: params.longitude.toString(),
56+
current: "temperature_2m,relative_humidity_2m,rain,weather_code",
57+
});
58+
const response = await fetch(`${this.settings.weatherApiUrl}?${queryParams}`);
59+
if (!response.ok) {
60+
throw new Error(`Weather API error: ${response.statusText}`);
61+
}
62+
return response.json();
63+
},
64+
},
65+
66+
created() {
67+
this.logger.info(`The ${this.name} service created.`);
68+
},
69+
70+
async started() {
71+
this.logger.info(`The ${this.name} service started.`);
72+
},
73+
74+
async stopped() {
75+
this.logger.info(`The ${this.name} service stopped.`);
76+
},
8077
};
8178

8279
export default weatherService;
83-

test/unit/services/weather.spec.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
import { Errors, ServiceBroker, type ServiceSchema } from "moleculer";
2+
import TestService, {
3+
type GetWeatherParams,
4+
type WeatherData,
5+
} from "../../../services/weather.service";
26
import "../../../tests/mocks/server";
3-
import TestService, { type GetWeatherParams, type WeatherData } from "../../../services/weather.service";
47

58
const { ValidationError } = Errors;
69

710
describe("Test 'weather' service", () => {
8-
const broker = new ServiceBroker({ logger: false });
9-
broker.createService(TestService as unknown as ServiceSchema);
11+
const broker = new ServiceBroker({ logger: false });
12+
broker.createService(TestService as unknown as ServiceSchema);
1013

11-
beforeAll(async () => broker.start());
12-
afterAll(async () => broker.stop());
14+
beforeAll(async () => broker.start());
15+
afterAll(async () => broker.stop());
1316

14-
describe("Test 'weather.getCurrentWeather' action", () => {
15-
it("should return weather data for valid coordinates", async () => {
16-
const response = await broker.call<WeatherData, GetWeatherParams>("weather.getCurrentWeather", {
17-
latitude: "46.9481",
18-
longitude: "7.4474",
19-
});
20-
expect(response.current.temperature_2m).toBe(20.5);
21-
expect(response.current.relative_humidity_2m).toBe(65);
22-
expect(response.current.rain).toBe(0);
23-
expect(response.current.weather_code).toBe(1);
24-
});
17+
describe("Test 'weather.getCurrentWeather' action", () => {
18+
it("should return weather data for valid coordinates", async () => {
19+
const response = await broker.call<WeatherData, GetWeatherParams>(
20+
"weather.getCurrentWeather",
21+
{
22+
latitude: "46.9481",
23+
longitude: "7.4474",
24+
},
25+
);
26+
expect(response.current.temperature_2m).toBe(20.5);
27+
expect(response.current.relative_humidity_2m).toBe(65);
28+
expect(response.current.rain).toBe(0);
29+
expect(response.current.weather_code).toBe(1);
30+
});
2531

26-
it("should reject with ValidationError when params are missing", async () => {
27-
expect.assertions(1);
28-
try {
29-
await broker.call("weather.getCurrentWeather");
30-
} catch (error: unknown) {
31-
expect(error).toBeInstanceOf(ValidationError);
32-
}
33-
});
34-
});
32+
it("should reject with ValidationError when params are missing", async () => {
33+
expect.assertions(1);
34+
try {
35+
await broker.call("weather.getCurrentWeather");
36+
} catch (error: unknown) {
37+
expect(error).toBeInstanceOf(ValidationError);
38+
}
39+
});
40+
});
3541
});

0 commit comments

Comments
 (0)