diff --git a/.woodpecker/.build.yml b/.woodpecker/.build.yml index dbe75f1..5f30941 100644 --- a/.woodpecker/.build.yml +++ b/.woodpecker/.build.yml @@ -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 \ No newline at end of file + - bun run build \ No newline at end of file diff --git a/.woodpecker/.lint.yml b/.woodpecker/.lint.yml index 5e06448..8005326 100644 --- a/.woodpecker/.lint.yml +++ b/.woodpecker/.lint.yml @@ -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 diff --git a/.woodpecker/.test.yml b/.woodpecker/.test.yml index cc674e9..4f52a10 100644 --- a/.woodpecker/.test.yml +++ b/.woodpecker/.test.yml @@ -13,7 +13,10 @@ steps: matrix: BUN_VERSION: - 1.1.0 + - 1.1.1 + - 1.1.2 + - 1.1.3 services: nats: - image: nats \ No newline at end of file + image: nats:2-alpine \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index 9b93697..79ba9a0 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 2a2d711..c8adcaa 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,26 @@ { - "name": "@qpismont/nano-service", - "version": "0.0.2", - "module": "index.ts", - "type": "module", - "main": "./dist/index.js", - "types": "./dist/index.d.ts", - "files": ["/dist"], - "scripts": { - "ci": "biome ci . --error-on-warnings", - "test": "bun test", - "build": "tsc --project tsconfig.build.json" - }, - "devDependencies": { - "@biomejs/biome": "1.6.3", - "@types/bun": "latest", - "typescript": "^5.0.0" - }, - "peerDependencies": { - "typescript": "^5.0.0" - }, - "dependencies": { - "nats": "^2.19.0", - "zod": "^3.22.4" - } + "name": "@qpismont/nano-service", + "version": "0.0.2", + "module": "index.ts", + "type": "module", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "files": ["/dist"], + "scripts": { + "ci": "biome ci . --error-on-warnings", + "test": "bun test", + "build": "tsc --project tsconfig.build.json" + }, + "devDependencies": { + "@biomejs/biome": "1.6.4", + "@types/bun": "latest", + "typescript": "^5.0.0" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "nats": "^2.22.0", + "zod": "^3.22.4" + } } diff --git a/src/service.ts b/src/service.ts index e57d05f..b754d69 100644 --- a/src/service.ts +++ b/src/service.ts @@ -29,7 +29,7 @@ export default class Service { adaptor: string, subject: string, fn: RouteSubscribeTypeFn, 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, diff --git a/src/types.ts b/src/types.ts index 711a9f3..bb7e21e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,8 @@ import type { Request, Response } from "./messages.ts"; +import type Service from "./service.ts"; export type AdaptorSubscribeTypeFn = (msg: string) => Promise; export type RouteSubscribeTypeFn = ( + srv: Service, msg: Request, ) => Promise>; diff --git a/tests/service.test.ts b/tests/service.test.ts index d65524b..bc1101f 100644 --- a/tests/service.test.ts +++ b/tests/service.test.ts @@ -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) => { - throw new RequestError("request error", 500); - }, - z.string(), - ); + srv.subscribe(adaptorName, subject, async (srv, msg) => { + throw new RequestError("request error", 500); + }); 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 }; });