Skip to content

Commit 31a69f7

Browse files
committed
better typings
1 parent 65b4a76 commit 31a69f7

File tree

6 files changed

+32
-31
lines changed

6 files changed

+32
-31
lines changed

template/mixins/db.mixin.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import type { Context, Service, ServiceSchema } from "moleculer";
1+
import type { Context, ServiceSettingSchema, ServiceSchema } from "moleculer";
22
import type { DatabaseMethods } from "../moleculer-types.js";
33
import { Service as DbService } from "@moleculer/database";
44

5-
export type DbServiceMethods = {
5+
export type DbServiceMethods = DatabaseMethods & {
66
seedDB?(): Promise<void>;
77
};
88

9-
type DbServiceSchema = Partial<ServiceSchema>;
10-
11-
export type DbServiceThis = Service & DbServiceMethods & DatabaseMethods;
9+
type DbServiceSchema = Partial<ServiceSchema<ServiceSettingSchema, DbServiceMethods>>;
1210

1311
export default function (collection: string): DbServiceSchema {
1412
const cacheCleanEventName = `cache.clean.${collection}`;

template/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@types/node": "^22.15.29",
2727
{{#lint}}
2828
"@eslint/js": "^9.28.0",
29+
"@vitest/coverage-v8": "^3.2.4",
2930
"eslint": "^9.28.0",
3031
"eslint-config-prettier": "^10.1.5",
3132
"eslint-plugin-prettier": "^5.4.1",

template/services/greeter.service.ts

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

33
export interface ActionHelloParams {
44
name: string;
@@ -16,9 +16,7 @@ interface GreeterLocalVars {
1616
myVar: string;
1717
}
1818

19-
type GreeterThis = Service<GreeterSettings> & GreeterMethods & GreeterLocalVars;
20-
21-
const GreeterService: ServiceSchema<GreeterSettings> = {
19+
const GreeterService: ServiceSchema<GreeterSettings, GreeterMethods, GreeterLocalVars> = {
2220
name: "greeter",
2321

2422
/**
@@ -50,7 +48,7 @@ const GreeterService: ServiceSchema<GreeterSettings> = {
5048
{{#apiGQL}}graphql: {
5149
query: "hello: String"
5250
},{{/apiGQL}}
53-
async handler(this: GreeterThis /* , ctx: Context */): Promise<string> {
51+
async handler(/* , ctx: Context */): Promise<string> {
5452
return "Hello Moleculer";
5553
}
5654
},
@@ -68,8 +66,8 @@ const GreeterService: ServiceSchema<GreeterSettings> = {
6866
{{#apiGQL}}graphql: {
6967
mutation: "welcome(name: String!): String"
7068
},{{/apiGQL}}
71-
async handler(this: GreeterThis, ctx: Context<ActionHelloParams>): Promise<string> {
72-
return `Welcome, ${ctx.params.name}`;
69+
async handler(ctx: Context<ActionHelloParams>): Promise<string> {
70+
return `Welcome, ${this.uppercase(ctx.params.name)}`;
7371
}
7472
}
7573
},
@@ -82,26 +80,27 @@ const GreeterService: ServiceSchema<GreeterSettings> = {
8280
/**
8381
* Methods. More info: https://moleculer.services/docs/0.15/services.html#Methods
8482
*/
85-
methods: {},
83+
methods: {
84+
uppercase(str: string): string {
85+
return str.toUpperCase();
86+
}
87+
},
8688

8789
/**
8890
* Service created lifecycle event handler
8991
* More info: https://moleculer.services/docs/0.15/lifecycle.html#created-event-handler
90-
* @this {import('moleculer').Service}
9192
*/
9293
created() {},
9394

9495
/**
9596
* Service started lifecycle event handler
9697
* More info: https://moleculer.services/docs/0.15/lifecycle.html#started-event-handler
97-
* @this {import('moleculer').Service}
9898
*/
9999
async started() {},
100100

101101
/**
102102
* Service stopped lifecycle event handler
103103
* More info: https://moleculer.services/docs/0.15/lifecycle.html#stopped-event-handler
104-
* @this {import('moleculer').Service}
105104
*/
106105
async stopped() {}
107106
};

template/services/inventory.service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Context, Service, ServiceSchema, ServiceSettingSchema } from "moleculer";
22

3+
interface ReserveParams {
4+
productId: string;
5+
quantity: number;
6+
}
7+
38
const InventoryService: ServiceSchema = {
49
name: "inventory",
510

@@ -9,12 +14,10 @@ const InventoryService: ServiceSchema = {
914
channels: {
1015
/**
1116
* Reserve a product in the inventory.
12-
*
13-
* @this {import('moleculer').Service}
1417
*/
1518
"inventory.reserve": {
1619
context: true,
17-
async handler(this: Service, ctx: Context<{ productId: string; quantity: number }>) {
20+
async handler(ctx: Context<ReserveParams>) {
1821
// Simulate external API call to reserve the quantity...
1922
await new Promise(resolve => setTimeout(resolve, 1000));
2023

@@ -46,7 +49,7 @@ const InventoryService: ServiceSchema = {
4649
productId: { type: "string" },
4750
quantity: { type: "number" }
4851
},
49-
async handler(ctx: Context<{ productId: string; quantity: number }>) {
52+
async handler(ctx: Context<ReserveParams>) {
5053
await this.broker.sendToChannel(
5154
"inventory.reserve",
5255
{

template/services/products.service.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { Context, Service, ServiceSchema } from "moleculer";
2-
import type { DatabaseMethods, DatabaseSettings, ApolloServiceSettings } from "../moleculer-types.js";
1+
import type { Context, ServiceSchema } from "moleculer";
2+
import type { DatabaseSettings, ApolloServiceSettings } from "../moleculer-types.js";
33
import DbMixin from "../mixins/db.mixin.js";
4+
import type { DbServiceMethods } from "../mixins/db.mixin.js";
45

56
export interface ProductEntity {
67
id: string;
@@ -18,16 +19,14 @@ export interface ActionQuantityParams {
1819

1920
interface ProductSettings extends DatabaseSettings, ApolloServiceSettings {}
2021

21-
interface ProductsThis extends Service<ProductSettings>, DatabaseMethods {};
22-
23-
const ProductsService: ServiceSchema<ProductSettings> = {
22+
const ProductsService: ServiceSchema<ProductSettings, DbServiceMethods> = {
2423
name: "products",
2524
// version: 1
2625

2726
/**
2827
* Mixins. More info: https://moleculer.services/docs/0.15/services.html#Mixins
2928
*/
30-
mixins: [DbMixin("products")],
29+
mixins: [DbMixin("products") as ServiceSchema],
3130

3231
/**
3332
* Settings. More info: https://moleculer.services/docs/0.15/services.html#Settings
@@ -161,7 +160,7 @@ const ProductsService: ServiceSchema<ProductSettings> = {
161160
{{#apiGQL}}graphql: {
162161
mutation: "increaseQuantity(id: String!, value: Int!): Product"
163162
},{{/apiGQL}}
164-
async handler(this: ProductsThis, ctx: Context<ActionQuantityParams>): Promise<ProductEntity> {
163+
async handler(ctx: Context<ActionQuantityParams>): Promise<ProductEntity> {
165164
// Get current quantity
166165
const adapter = await this.getAdapter(ctx);
167166
const dbEntry = await adapter.findById<ProductEntity>(ctx.params.id);
@@ -191,7 +190,7 @@ const ProductsService: ServiceSchema<ProductSettings> = {
191190
{{#apiGQL}}graphql: {
192191
mutation: "decreaseQuantity(id: String!, value: Int!): Product"
193192
},{{/apiGQL}}
194-
async handler(this: ProductsThis, ctx: Context<ActionQuantityParams>): Promise<ProductEntity> {
193+
async handler(ctx: Context<ActionQuantityParams>): Promise<ProductEntity> {
195194
// Get current quantity
196195
const adapter = await this.getAdapter(ctx);
197196
const dbEntry = await adapter.findById<ProductEntity>(ctx.params.id);
@@ -207,12 +206,13 @@ const ProductsService: ServiceSchema<ProductSettings> = {
207206
quantity: newQuantity
208207
});
209208

209+
{{#needChannels}}
210210
if (doc.quantity === 0) {
211211
this.logger.info(`Stock of ${doc.name} depleted... Ordering more`);
212212
// Emit a persistent event to order more products
213213
// inventory.service will handle this event
214214
this.broker.sendToChannel("order.more", doc);
215-
}
215+
}{{/needChannels}}
216216

217217
return doc;
218218
}
@@ -228,7 +228,7 @@ const ProductsService: ServiceSchema<ProductSettings> = {
228228
* It is called in the DB.mixin after the database
229229
* connection establishing & the collection is empty.
230230
*/
231-
async seedDB(this: ProductsThis) {
231+
async seedDB() {
232232
const adapter = await this.getAdapter();
233233
await adapter.insertMany([
234234
{ name: "Samsung Galaxy S10 Plus", quantity: 10, price: 704 },

template/test/unit/services/products.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe("Test 'products' service", () => {
104104
value: -5
105105
});
106106
} catch (err) {
107-
expect(err).toBeInstanceOf(ValidationError);
107+
expect(err).toBeInstanceOf(Errors.ValidationError);
108108
expect(err.data).toEqual([
109109
{
110110
action: "products.decreaseQuantity",

0 commit comments

Comments
 (0)