nano-service/src/service.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

import Adaptor from "./adaptors/adaptor";
import { Request, Response } from "./messages";
import { RouteSubscribeTypeFn } from "./types";
export default class Service {
private name: string;
private adaptors: Record<string, Adaptor>;
constructor(name: string) {
this.name = name;
this.adaptors = {};
}
public addAdaptor(name: string, adaptor: Adaptor) {
this.adaptors[name] = adaptor;
}
public async subscribe<T, U>(adaptor: string, subject: string, fn: RouteSubscribeTypeFn<T, U>) {
this.adaptors[adaptor].subscribe(`${this.name}.${subject}`, async (rawReq) => {
const msg: Request<T> = JSON.parse(rawReq);
const res = await fn({ data: msg.data, from: this.name });
return JSON.stringify(res);
});
}
public async request<T, U>(adaptor: string, req: Request<T>): Promise<Response<U>> {
if(!this.adaptors[adaptor]) {
throw new Error(`${adaptor} adaptor not exist`);
}
const rawReq = JSON.stringify(req);
try {
const rawRes = await this.adaptors[adaptor].request(`${req.service}.${req.subject}`, rawReq);
return JSON.parse(rawRes);
} catch(err) {
return { statusCode: 500 }
}
}
public async listen() {
for(const index in this.adaptors) {
await this.adaptors[index].listen(this.name);
}
}
public async stop() {
for(const index in this.adaptors) {
await this.adaptors[index].stop();
}
}
}