fastify-effect-runtime 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kyrylo Savieliev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.mts CHANGED
@@ -6,7 +6,7 @@ declare const withEffect: <A, E, R extends FastifyRequest = FastifyRequest>(hand
6
6
  //#endregion
7
7
  //#region src/index.d.ts
8
8
  interface FastifyEffectOptions<R = any> {
9
- layer: Layer.Layer<R, never, any>;
9
+ layers: Layer.Layer<R, never, any>;
10
10
  }
11
11
  declare module "fastify" {
12
12
  interface FastifyInstance {
@@ -17,7 +17,7 @@ declare module "fastify" {
17
17
  effectScope?: Runtime.Runtime<unknown>;
18
18
  }
19
19
  }
20
- declare const fastifyEffectPlugin: FastifyPluginCallback<FastifyEffectOptions<any>>;
20
+ declare const fastifyEffectRuntime: FastifyPluginCallback<FastifyEffectOptions<any>>;
21
21
  //#endregion
22
- export { fastifyEffectPlugin as default, withEffect };
22
+ export { fastifyEffectRuntime as default, withEffect };
23
23
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":["fastifyEffectPlugin: FastifyPluginCallback<FastifyEffectOptions<any>>"],"sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;cAkBM,gBAjB4C,CAAA,EAiBd,UAAA,cAAA,GAAiB,cAAjB,CAAiB,CAAA,OAAA,EAAA,CAAA,OAAA,EAChC,CADgC,EAAA,KAAA,EACtB,YADsB,EAAA,GACL,MAAA,CAAO,MADF,CACS,CADT,EACY,CADZ,EAAA,OAAA,CAAA,EAAA,GAAA,CAAA,OAAA,EAG5B,CAH4B,EAAA,KAAA,EAGlB,YAHkB,EAAA,GAGH,OAHG,CAGK,CAHL,CAAA;;;UCd3C;SAEF,KAAA,CAAM,MAAM;ADL8B;eAiBd,SAAA,CAAA;EAAiB,UAAA,eAAA,CAAA;IAChC,aAAA,ECJJ,OAAA,CAAQ,ODIJ,CAAA,OAAA,CAAA;IAAU,WAAA,ECHhB,KAAA,CAAM,cDGU;EAA+B;EAAG,UAAA,cAAA,CAAA;IAAjB,WAAO,CAAA,ECCvC,OAAA,CAAQ,ODD+B,CAAA,OAAA,CAAA;EAE9B;;cCoDnBA,mBDpDoD,ECoD/B,qBDpD+B,CCoDT,oBDpDS,CAAA,GAAA,CAAA,CAAA"}
1
+ {"version":3,"file":"index.d.mts","names":["fastifyEffectRuntime: FastifyPluginCallback<FastifyEffectOptions<any>>"],"sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;cAkBM,gBAjB4C,CAAA,EAiBd,UAAA,cAAA,GAAiB,cAAjB,CAAiB,CAAA,OAAA,EAAA,CAAA,OAAA,EAChC,CADgC,EAAA,KAAA,EACtB,YADsB,EAAA,GACL,MAAA,CAAO,MADF,CACS,CADT,EACY,CADZ,EAAA,OAAA,CAAA,EAAA,GAAA,CAAA,OAAA,EAG5B,CAH4B,EAAA,KAAA,EAGlB,YAHkB,EAAA,GAGH,OAHG,CAGK,CAHL,CAAA;;;UCd3C;UAED,KAAA,CAAM,MAAM;ADL6B;eAiBd,SAAA,CAAA;EAAiB,UAAA,eAAA,CAAA;IAChC,aAAA,ECJJ,OAAA,CAAQ,ODIJ,CAAA,OAAA,CAAA;IAAU,WAAA,ECHhB,KAAA,CAAM,cDGU;EAA+B;EAAG,UAAA,cAAA,CAAA;IAAjB,WAAO,CAAA,ECCvC,OAAA,CAAQ,ODD+B,CAAA,OAAA,CAAA;EAE9B;;cCoDnBA,oBDpDoD,ECoD9B,qBDpD8B,CCoDR,oBDpDQ,CAAA,GAAA,CAAA,CAAA"}
package/dist/index.mjs CHANGED
@@ -17,14 +17,14 @@ const withEffect = (handler) => {
17
17
 
18
18
  //#endregion
19
19
  //#region src/index.ts
20
- const fastifyEffectRuntime = (fastify, opts, next) => {
20
+ const runtime = (fastify, opts, next) => {
21
21
  fastify.decorate("effectRuntime", void 0);
22
22
  fastify.decorate("effectScope", void 0);
23
23
  fastify.decorateRequest("effectScope", void 0);
24
24
  fastify.decorateRequest("runEffect", void 0);
25
25
  fastify.addHook("onReady", async function effectOnReady() {
26
26
  const scope = Effect.runSync(Scope.make());
27
- fastify.effectRuntime = await Effect.runPromise(Layer.toRuntime(opts.layer).pipe(Effect.scoped, Scope.extend(scope)));
27
+ fastify.effectRuntime = await Effect.runPromise(Layer.toRuntime(opts.layers).pipe(Effect.scoped, Scope.extend(scope)));
28
28
  fastify.effectScope = scope;
29
29
  });
30
30
  fastify.addHook("onRequest", async function createEffectScope(request) {
@@ -41,11 +41,11 @@ const fastifyEffectRuntime = (fastify, opts, next) => {
41
41
  });
42
42
  next();
43
43
  };
44
- const fastifyEffectPlugin = fp(fastifyEffectRuntime, {
44
+ const fastifyEffectRuntime = fp(runtime, {
45
45
  name: "fastify-effect",
46
46
  fastify: "5.x"
47
47
  });
48
48
 
49
49
  //#endregion
50
- export { fastifyEffectPlugin as default, withEffect };
50
+ export { fastifyEffectRuntime as default, withEffect };
51
51
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["fastifyEffectRuntime: FastifyPluginCallback<FastifyEffectOptions>","fastifyEffectPlugin: FastifyPluginCallback<FastifyEffectOptions<any>>"],"sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import { Cause, Effect, Exit, Runtime } from \"effect\"\nimport type { FastifyReply, FastifyRequest } from \"fastify\"\n\nconst handleEffectResult = <A, E>(\n exit: Exit.Exit<A, E>,\n reply: FastifyReply,\n): FastifyReply | A => {\n if (Exit.isSuccess(exit)) {\n return exit.value\n }\n\n if (Cause.isDie(exit.cause)) {\n return reply.status(500).send({ error: 'Internal server error' })\n }\n\n return reply.status(500).send({ error: 'An error occurred' })\n}\n\nconst withEffect = <A, E, R extends FastifyRequest = FastifyRequest>(\n handler: (request: R, reply: FastifyReply) => Effect.Effect<A, E, unknown>,\n) => {\n return async (request: R, reply: FastifyReply): Promise<A> => {\n if (!request.effectScope) {\n return reply.status(500).send({ error: 'Effect runtime not available' })\n }\n\n const program = handler(request, reply)\n const exit = await Runtime.runPromiseExit(request.effectScope)(program)\n\n return handleEffectResult(exit, reply)\n }\n}\n\nexport { withEffect }\n","import { Effect, Exit, Layer, Runtime, Scope } from \"effect\"\nimport type { FastifyPluginCallback, FastifyRequest } from \"fastify\"\nimport fp from \"fastify-plugin\"\n\ninterface FastifyEffectOptions<R = any> {\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tlayer: Layer.Layer<R, never, any>\n}\n\ninterface FastifyRequestWithEffect extends FastifyRequest {\n\teffectScope: Runtime.Runtime<unknown>\n}\n\ndeclare module 'fastify' {\n\tinterface FastifyInstance {\n\t\teffectRuntime: Runtime.Runtime<unknown>\n\t\teffectScope: Scope.CloseableScope\n\t}\n\n\tinterface FastifyRequest {\n\t\teffectScope?: Runtime.Runtime<unknown>\n\t}\n}\n\nconst fastifyEffectRuntime: FastifyPluginCallback<FastifyEffectOptions> = (\n fastify,\n\topts,\n next\n) => {\n fastify.decorate(\n\t\t'effectRuntime',\n\t\tundefined as unknown as Runtime.Runtime<unknown>,\n\t)\n\tfastify.decorate('effectScope', undefined as unknown as Scope.CloseableScope)\n\n\tfastify.decorateRequest('effectScope', undefined)\n\tfastify.decorateRequest('runEffect', undefined)\n\n\tfastify.addHook('onReady', async function effectOnReady() {\n\t\tconst scope = Effect.runSync(Scope.make())\n\n\t\tconst runtime = await Effect.runPromise(\n\t\t\t// @ts-expect-error will be fixed when any type will not be used anymore\n\t\t\tLayer.toRuntime(opts.layer).pipe(Effect.scoped, Scope.extend(scope)),\n\t\t)\n\n\t\tfastify.effectRuntime = runtime\n\t\tfastify.effectScope = scope\n\t})\n\n\tfastify.addHook('onRequest', async function createEffectScope(request) {\n\t\tconst requestRuntime = fastify.effectRuntime\n\n\t\tif (!requestRuntime) {\n\t\t\tthrow new Error('Effect runtime is not initialized')\n\t\t}\n\n\t\t;(request as FastifyRequestWithEffect).effectScope = requestRuntime\n\t})\n\n\tfastify.addHook('onClose', async function effectOnClose() {\n\t\tif (fastify.effectScope) {\n\t\t\ttry {\n\t\t\t\tawait Effect.runPromise(Scope.close(fastify.effectScope, Exit.void))\n\t\t\t} catch (error) {\n\t\t\t\tthrow error\n\t\t\t}\n\t\t}\n\t})\n\n\tnext()\n}\n\nconst fastifyEffectPlugin: FastifyPluginCallback<FastifyEffectOptions<any>> = fp(fastifyEffectRuntime, {\n\tname: 'fastify-effect',\n\tfastify: '5.x',\n})\n\nexport { withEffect } from './utils.js'\nexport { fastifyEffectPlugin as default }\n"],"mappings":";;;;AAGA,MAAM,sBACJ,MACA,UACqB;AACrB,KAAI,KAAK,UAAU,KAAK,CACtB,QAAO,KAAK;AAGd,KAAI,MAAM,MAAM,KAAK,MAAM,CACzB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,"file":"index.mjs","names":["runtime: FastifyPluginCallback<FastifyEffectOptions>","fastifyEffectRuntime: FastifyPluginCallback<FastifyEffectOptions<any>>"],"sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import { Cause, Effect, Exit, Runtime } from \"effect\"\nimport type { FastifyReply, FastifyRequest } from \"fastify\"\n\nconst handleEffectResult = <A, E>(\n exit: Exit.Exit<A, E>,\n reply: FastifyReply,\n): FastifyReply | A => {\n if (Exit.isSuccess(exit)) {\n return exit.value\n }\n\n if (Cause.isDie(exit.cause)) {\n return reply.status(500).send({ error: 'Internal server error' })\n }\n\n return reply.status(500).send({ error: 'An error occurred' })\n}\n\nconst withEffect = <A, E, R extends FastifyRequest = FastifyRequest>(\n handler: (request: R, reply: FastifyReply) => Effect.Effect<A, E, unknown>,\n) => {\n return async (request: R, reply: FastifyReply): Promise<A> => {\n if (!request.effectScope) {\n return reply.status(500).send({ error: 'Effect runtime not available' })\n }\n\n const program = handler(request, reply)\n const exit = await Runtime.runPromiseExit(request.effectScope)(program)\n\n return handleEffectResult(exit, reply)\n }\n}\n\nexport { withEffect }\n","import { Effect, Exit, Layer, Runtime, Scope } from \"effect\"\nimport type { FastifyPluginCallback, FastifyRequest } from \"fastify\"\nimport fp from \"fastify-plugin\"\n\ninterface FastifyEffectOptions<R = any> {\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tlayers: Layer.Layer<R, never, any>\n}\n\ninterface FastifyRequestWithEffect extends FastifyRequest {\n\teffectScope: Runtime.Runtime<unknown>\n}\n\ndeclare module 'fastify' {\n\tinterface FastifyInstance {\n\t\teffectRuntime: Runtime.Runtime<unknown>\n\t\teffectScope: Scope.CloseableScope\n\t}\n\n\tinterface FastifyRequest {\n\t\teffectScope?: Runtime.Runtime<unknown>\n\t}\n}\n\nconst runtime: FastifyPluginCallback<FastifyEffectOptions> = (\n fastify,\n\topts,\n next\n) => {\n fastify.decorate(\n\t\t'effectRuntime',\n\t\tundefined as unknown as Runtime.Runtime<unknown>,\n\t)\n\tfastify.decorate('effectScope', undefined as unknown as Scope.CloseableScope)\n\n\tfastify.decorateRequest('effectScope', undefined)\n\tfastify.decorateRequest('runEffect', undefined)\n\n\tfastify.addHook('onReady', async function effectOnReady() {\n\t\tconst scope = Effect.runSync(Scope.make())\n\n\t\tconst runtime = await Effect.runPromise(\n\t\t\t// @ts-expect-error will be fixed when any type will not be used anymore\n\t\t\tLayer.toRuntime(opts.layers).pipe(Effect.scoped, Scope.extend(scope)),\n\t\t)\n\n\t\tfastify.effectRuntime = runtime\n\t\tfastify.effectScope = scope\n\t})\n\n\tfastify.addHook('onRequest', async function createEffectScope(request) {\n\t\tconst requestRuntime = fastify.effectRuntime\n\n\t\tif (!requestRuntime) {\n\t\t\tthrow new Error('Effect runtime is not initialized')\n\t\t}\n\n\t\t;(request as FastifyRequestWithEffect).effectScope = requestRuntime\n\t})\n\n\tfastify.addHook('onClose', async function effectOnClose() {\n\t\tif (fastify.effectScope) {\n\t\t\ttry {\n\t\t\t\tawait Effect.runPromise(Scope.close(fastify.effectScope, Exit.void))\n\t\t\t} catch (error) {\n\t\t\t\tthrow error\n\t\t\t}\n\t\t}\n\t})\n\n\tnext()\n}\n\nconst fastifyEffectRuntime: FastifyPluginCallback<FastifyEffectOptions<any>> = fp(runtime, {\n\tname: 'fastify-effect',\n\tfastify: '5.x',\n})\n\nexport { withEffect } from './utils.js'\nexport { fastifyEffectRuntime as default }\n"],"mappings":";;;;AAGA,MAAM,sBACJ,MACA,UACqB;AACrB,KAAI,KAAK,UAAU,KAAK,CACtB,QAAO,KAAK;AAGd,KAAI,MAAM,MAAM,KAAK,MAAM,CACzB,QAAO,MAAM,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,yBAAyB,CAAC;AAGnE,QAAO,MAAM,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,qBAAqB,CAAC;;AAG/D,MAAM,cACJ,YACG;AACH,QAAO,OAAO,SAAY,UAAoC;AAC5D,MAAI,CAAC,QAAQ,YACX,QAAO,MAAM,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,gCAAgC,CAAC;EAG1E,MAAM,UAAU,QAAQ,SAAS,MAAM;AAGvC,SAAO,mBAFM,MAAM,QAAQ,eAAe,QAAQ,YAAY,CAAC,QAAQ,EAEvC,MAAM;;;;;;ACL1C,MAAMA,WACJ,SACD,MACC,SACG;AACH,SAAQ,SACR,iBACA,OACA;AACD,SAAQ,SAAS,eAAe,OAA6C;AAE7E,SAAQ,gBAAgB,eAAe,OAAU;AACjD,SAAQ,gBAAgB,aAAa,OAAU;AAE/C,SAAQ,QAAQ,WAAW,eAAe,gBAAgB;EACzD,MAAM,QAAQ,OAAO,QAAQ,MAAM,MAAM,CAAC;AAO1C,UAAQ,gBALQ,MAAM,OAAO,WAE5B,MAAM,UAAU,KAAK,OAAO,CAAC,KAAK,OAAO,QAAQ,MAAM,OAAO,MAAM,CAAC,CACrE;AAGD,UAAQ,cAAc;GACrB;AAEF,SAAQ,QAAQ,aAAa,eAAe,kBAAkB,SAAS;EACtE,MAAM,iBAAiB,QAAQ;AAE/B,MAAI,CAAC,eACJ,OAAM,IAAI,MAAM,oCAAoC;AAGpD,EAAC,QAAqC,cAAc;GACpD;AAEF,SAAQ,QAAQ,WAAW,eAAe,gBAAgB;AACzD,MAAI,QAAQ,YACX,KAAI;AACH,SAAM,OAAO,WAAW,MAAM,MAAM,QAAQ,aAAa,KAAK,KAAK,CAAC;WAC5D,OAAO;AACf,SAAM;;GAGP;AAEF,OAAM;;AAGP,MAAMC,uBAAyE,GAAG,SAAS;CAC1F,MAAM;CACN,SAAS;CACT,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "fastify-effect-runtime",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "description": "Library for integrating Effect runtime into fastify",
6
6
  "scripts": {
7
- "build": "tsdown"
7
+ "build": "tsdown",
8
+ "prepare": "husky",
9
+ "lint": "eslint .",
10
+ "format": "prettier --write .",
11
+ "typecheck": "tsc --noEmit"
8
12
  },
9
13
  "keywords": [
10
14
  "fastify",