From ab29b28125cb864ac70f05c8de6eefacb481c7cf Mon Sep 17 00:00:00 2001 From: qpismont Date: Wed, 10 Apr 2024 19:46:07 +0200 Subject: [PATCH 1/2] add srv params in handler + upgrade deps --- .woodpecker/.build.yml | 8 ++----- .woodpecker/.lint.yml | 2 +- .woodpecker/.test.yml | 5 ++++- bun.lockb | Bin 8404 -> 8413 bytes package.json | 48 ++++++++++++++++++++--------------------- src/service.ts | 4 ++-- src/types.ts | 2 ++ tests/service.test.ts | 15 +++++-------- 8 files changed, 40 insertions(+), 44 deletions(-) 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 9b936975b504a3be4583980d9bc8364c7c3ba7f5..79ba9a04a38f0a7a78d179611a5467a4385183cb 100755 GIT binary patch delta 1977 zcmZWq3p`X=9KLr9F-%4tGZ%v~mW0b-Jn|T-t@I%4(V~Q6Rx+Nog*CKGDtUBRRPvZ* z*;T)Yina%-^bjhF@>o%pB=Y!WJ@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Rg?Uq)x_@RYb1I)1$lmtpJI_d&yF>CMw%fQ3u z0a8rZ02m)a?cvA@h?##9_*^~@%rUu++SiPkn4pk}Wz7M_rbH0PGMB`L i2-ueDk&CG=$YImek-n)m=wh3Ej!1xGMum~PnfzZbyM5pQ 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 }; }); -- 2.45.2 From fb7cd0bf341e1c5fe98f6f77043d8a88fe5134a7 Mon Sep 17 00:00:00 2001 From: qpismont Date: Wed, 10 Apr 2024 19:48:26 +0200 Subject: [PATCH 2/2] fix package.json --- package.json | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index c8adcaa..8dde485 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.4", - "@types/bun": "latest", - "typescript": "^5.0.0" - }, - "peerDependencies": { - "typescript": "^5.0.0" - }, - "dependencies": { - "nats": "^2.22.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" + } } -- 2.45.2