Skip to content

Commit

Permalink
native service example
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 25, 2023
1 parent 83c6d21 commit b95b2a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
36 changes: 21 additions & 15 deletions dev/native-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ module.exports = NativeService;

class MyService extends NativeService {
// Service name
static name = "my-service";
name = "my-service";
// Service version
static version = 1;
version = 1;
// Service dependencies
static dependencies = ["posts", "users"];
dependencies = ["posts", "users"];
// Service settings
static settings = {
settings = {
foo: "bar"
};
// Service metadata
static metadata = {
metadata = {
a: 5
};
// Action Hook
static hooks = {
hooks = {
before: {
create: ["validate"]
},
Expand All @@ -34,38 +34,44 @@ class MyService extends NativeService {
// --- ACTIONS ---

// Action definition
static actionCreateUser = {
actionCreateUser = {
name: "createUser",
visibility: "published",
params: {
username: "string",
password: "string"
},
handler: "createUser" // The name of action handler method, can be omitted if the same as `name`
handler: "createUser", // The name of action handler method, can be omitted if the same as `name`
asyncCtx: true // The handler method will be called with `ctx.params` as first argument. If you need the `ctx` you can get is from AsyncStorage
};

// Action handler
async createUser(ctx) {
await this.#myMethod(ctx.params);
return ctx.params.user;
async createUser(params) {
await this.myMethod(params);
return params.user;
}

// Minimal Action definition, the `name` and handler function name are came from property name without "action" prefix.
actionListUsers = {};
async listUsers(ctx) {
return this.adapter.find();
}

// --- EVENTS ---

// Event definition
static eventUserCreated = {
eventUserCreated = {
name: "user.created",
group: "user",
handler: "userCreated" // The name of event handler method, can be omitted if the same as `name`
};

// Event handler
userCreated(ctx) {
this.#myMethod();
this.myMethod();
}

// Methods
#myMethod(params) {
myMethod(params) {
this.broker.info("Params:", params);
}

Expand Down
6 changes: 2 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
"target": "ES2022",
"moduleResolution": "node",
"noEmit": true,
"lib": ["ES2022"],
"typeRoots": ["types/**/*.d.ts"],
"types": ["node"]
"lib": ["ES2022"]
},
"include": ["**.js"],
"include": ["types/extends.d.ts", "**.js"],
"exclude": ["node_modules", ".eslintrc.js", "prettier.config.js"]
}
3 changes: 3 additions & 0 deletions types/extends.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ExtendableError } from "../src/errors";
import type Context = require("../src/context");

interface Promise<T> {
delay<T>(ms: number): Promise<T>;
method(fn: Function): Function;
Expand Down

0 comments on commit b95b2a8

Please sign in to comment.