add srv params in handler + upgrade deps
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint Pipeline failed
ci/woodpecker/push/test/1 Pipeline was successful
ci/woodpecker/push/test/2 Pipeline was successful
ci/woodpecker/push/test/3 Pipeline was successful
ci/woodpecker/push/test/4 Pipeline was successful

This commit is contained in:
qpismont 2024-04-10 19:46:07 +02:00
parent bf7037eb15
commit ab29b28125
8 changed files with 40 additions and 44 deletions

View file

@ -3,11 +3,7 @@ when:
steps:
build:
image: oven/bun:${BUN_VERSION}-slim
image: oven/bun:1.1.3-slim
commands:
- bun install
- bun run build
matrix:
BUN_VERSION:
- 1.1.0

View file

@ -3,7 +3,7 @@ when:
steps:
lint:
image: oven/bun:1.0.33-slim
image: oven/bun:1.1.3-slim
commands:
- bun install
- bun run ci

View file

@ -13,7 +13,10 @@ steps:
matrix:
BUN_VERSION:
- 1.1.0
- 1.1.1
- 1.1.2
- 1.1.3
services:
nats:
image: nats
image: nats:2-alpine

BIN
bun.lockb

Binary file not shown.

View file

@ -12,7 +12,7 @@
"build": "tsc --project tsconfig.build.json"
},
"devDependencies": {
"@biomejs/biome": "1.6.3",
"@biomejs/biome": "1.6.4",
"@types/bun": "latest",
"typescript": "^5.0.0"
},
@ -20,7 +20,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"nats": "^2.19.0",
"nats": "^2.22.0",
"zod": "^3.22.4"
}
}

View file

@ -29,7 +29,7 @@ export default class Service {
adaptor: string,
subject: string,
fn: RouteSubscribeTypeFn<z.infer<T>, U>,
schema: T | undefined = undefined,
schema?: T,
) {
this.adaptors[adaptor].subscribe(
`${this.name}.${subject}`,
@ -64,7 +64,7 @@ export default class Service {
}
try {
const res = await fn(req);
const res = await fn(this, req);
const internalResponse = {
statusCode: res.statusCode,
data: res.data,

View file

@ -1,6 +1,8 @@
import type { Request, Response } from "./messages.ts";
import type Service from "./service.ts";
export type AdaptorSubscribeTypeFn = (msg: string) => Promise<string>;
export type RouteSubscribeTypeFn<T, U> = (
srv: Service,
msg: Request<T>,
) => Promise<Response<U>>;

View file

@ -27,7 +27,7 @@ test("request success", async () => {
srv.subscribe(
adaptorName,
subject,
async (msg) => {
async (srv, msg) => {
return { data: msg.data, statusCode: statusCodeExpected };
},
z.string(),
@ -54,14 +54,9 @@ test("request error", async () => {
const statusCodeExpected = 500;
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
srv.subscribe(
adaptorName,
subject,
async (msg) => {
srv.subscribe(adaptorName, subject, async (srv, msg) => {
throw new RequestError("request error", 500);
},
z.string(),
);
});
await srv.listen();
expect(() => {
@ -82,7 +77,7 @@ test("request adaptor not found", async () => {
const statusCodeExpected = 200;
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
srv.subscribe(adaptorName, subject, async (msg) => {
srv.subscribe(adaptorName, subject, async (srv, msg) => {
return { data: msg.data, statusCode: statusCodeExpected };
});