fastify-effect-runtime 0.0.1
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/README.md +54 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +51 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +74 -0
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# `fastify-effect-runtime`
|
|
2
|
+
|
|
3
|
+
Library for integrating effect runtime into fastify
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm i fastify-effect-runtime
|
|
9
|
+
# yarn add fastify-effect-runtime
|
|
10
|
+
# pnpm add fastify-effect-runtime
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
Register the plugin:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { Layer } from 'effect'
|
|
20
|
+
import fastifyEffectRuntime from 'fastify-effect-runtime'
|
|
21
|
+
|
|
22
|
+
const app = fastify()
|
|
23
|
+
|
|
24
|
+
await app.register(fastifyEffectRuntime, {
|
|
25
|
+
layers: Layer.mergeAll(YourFirstLayer, YourSecondLayer)
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Use effect inside of routes:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import { withEffect } from 'fastify-effect-runtime'
|
|
33
|
+
|
|
34
|
+
app.get('/health',
|
|
35
|
+
withEffect((_, reply) =>
|
|
36
|
+
Effect.succeed(
|
|
37
|
+
reply.status(200).send({ status: 'ok!' })
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Contributing
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
If you want to contribute to improving the project, firstly read [CONTRIBUTING.md](https://github.com/OneLiL05/fastify-effect-runtime/blob/main/CONTRIBUTING.md)
|
|
47
|
+
|
|
48
|
+
## Stay in touch
|
|
49
|
+
|
|
50
|
+
Author - [Kyrylo Savieliev](https://github.com/OneLiL05)
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
`fastify-effect-runtime` is [MIT licensed](https://github.com/OneLiL05/fastify-effect-runtime/blob/main/LICENSE)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Effect, Layer, Runtime, Scope } from "effect";
|
|
2
|
+
import { FastifyPluginCallback, FastifyReply, FastifyRequest } from "fastify";
|
|
3
|
+
|
|
4
|
+
//#region src/utils.d.ts
|
|
5
|
+
declare const withEffect: <A, E, R extends FastifyRequest = FastifyRequest>(handler: (request: R, reply: FastifyReply) => Effect.Effect<A, E, unknown>) => (request: R, reply: FastifyReply) => Promise<A>;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/index.d.ts
|
|
8
|
+
interface FastifyEffectOptions<R = any> {
|
|
9
|
+
layer: Layer.Layer<R, never, any>;
|
|
10
|
+
}
|
|
11
|
+
declare module "fastify" {
|
|
12
|
+
interface FastifyInstance {
|
|
13
|
+
effectRuntime: Runtime.Runtime<unknown>;
|
|
14
|
+
effectScope: Scope.CloseableScope;
|
|
15
|
+
}
|
|
16
|
+
interface FastifyRequest {
|
|
17
|
+
effectScope?: Runtime.Runtime<unknown>;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
declare const fastifyEffectPlugin: FastifyPluginCallback<FastifyEffectOptions<any>>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { fastifyEffectPlugin as default, withEffect };
|
|
23
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +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"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Cause, Effect, Exit, Layer, Runtime, Scope } from "effect";
|
|
2
|
+
import fp from "fastify-plugin";
|
|
3
|
+
|
|
4
|
+
//#region src/utils.ts
|
|
5
|
+
const handleEffectResult = (exit, reply) => {
|
|
6
|
+
if (Exit.isSuccess(exit)) return exit.value;
|
|
7
|
+
if (Cause.isDie(exit.cause)) return reply.status(500).send({ error: "Internal server error" });
|
|
8
|
+
return reply.status(500).send({ error: "An error occurred" });
|
|
9
|
+
};
|
|
10
|
+
const withEffect = (handler) => {
|
|
11
|
+
return async (request, reply) => {
|
|
12
|
+
if (!request.effectScope) return reply.status(500).send({ error: "Effect runtime not available" });
|
|
13
|
+
const program = handler(request, reply);
|
|
14
|
+
return handleEffectResult(await Runtime.runPromiseExit(request.effectScope)(program), reply);
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/index.ts
|
|
20
|
+
const fastifyEffectRuntime = (fastify, opts, next) => {
|
|
21
|
+
fastify.decorate("effectRuntime", void 0);
|
|
22
|
+
fastify.decorate("effectScope", void 0);
|
|
23
|
+
fastify.decorateRequest("effectScope", void 0);
|
|
24
|
+
fastify.decorateRequest("runEffect", void 0);
|
|
25
|
+
fastify.addHook("onReady", async function effectOnReady() {
|
|
26
|
+
const scope = Effect.runSync(Scope.make());
|
|
27
|
+
fastify.effectRuntime = await Effect.runPromise(Layer.toRuntime(opts.layer).pipe(Effect.scoped, Scope.extend(scope)));
|
|
28
|
+
fastify.effectScope = scope;
|
|
29
|
+
});
|
|
30
|
+
fastify.addHook("onRequest", async function createEffectScope(request) {
|
|
31
|
+
const requestRuntime = fastify.effectRuntime;
|
|
32
|
+
if (!requestRuntime) throw new Error("Effect runtime is not initialized");
|
|
33
|
+
request.effectScope = requestRuntime;
|
|
34
|
+
});
|
|
35
|
+
fastify.addHook("onClose", async function effectOnClose() {
|
|
36
|
+
if (fastify.effectScope) try {
|
|
37
|
+
await Effect.runPromise(Scope.close(fastify.effectScope, Exit.void));
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
next();
|
|
43
|
+
};
|
|
44
|
+
const fastifyEffectPlugin = fp(fastifyEffectRuntime, {
|
|
45
|
+
name: "fastify-effect",
|
|
46
|
+
fastify: "5.x"
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
export { fastifyEffectPlugin as default, withEffect };
|
|
51
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +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,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,wBACJ,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,MAAM,CAAC,KAAK,OAAO,QAAQ,MAAM,OAAO,MAAM,CAAC,CACpE;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,sBAAwE,GAAG,sBAAsB;CACtG,MAAM;CACN,SAAS;CACT,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fastify-effect-runtime",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Library for integrating Effect runtime into fastify",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsdown"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"fastify",
|
|
11
|
+
"node.js",
|
|
12
|
+
"effect"
|
|
13
|
+
],
|
|
14
|
+
"author": "Kyrylo Savieliev <k_savieliev@icloud.com>",
|
|
15
|
+
"homepage": "https://github.com/OneLiL05/fastify-effect-runtime",
|
|
16
|
+
"maintainers": [
|
|
17
|
+
{
|
|
18
|
+
"name": "Kyrylo Savieliev",
|
|
19
|
+
"url": "https://github.com/OneLiL05"
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+ssh://git@github.com/OneLiL05/fastify-effect-runtime.git"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/OneLiL05/fastify-effect-runtime/issues"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=18.0.0"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": "./dist/index.mjs",
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"main": "./dist/index.mjs",
|
|
38
|
+
"module": "./dist/index.mjs",
|
|
39
|
+
"types": "./dist/index.d.mts",
|
|
40
|
+
"files": [
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"lint-staged": {
|
|
44
|
+
"**/*.{js,ts,tsx}": [
|
|
45
|
+
"eslint --fix"
|
|
46
|
+
],
|
|
47
|
+
"**/*": "prettier --write --ignore-unknown"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"fastify-plugin": "^5.1.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@commitlint/cli": "^20.2.0",
|
|
54
|
+
"@commitlint/config-conventional": "^20.2.0",
|
|
55
|
+
"@types/node": "^25.0.3",
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "^8.50.1",
|
|
57
|
+
"@typescript-eslint/parser": "^8.50.1",
|
|
58
|
+
"eslint": "^9.39.2",
|
|
59
|
+
"eslint-config-prettier": "^10.1.8",
|
|
60
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
61
|
+
"globals": "^16.5.0",
|
|
62
|
+
"husky": "^9.1.7",
|
|
63
|
+
"lint-staged": "^16.2.7",
|
|
64
|
+
"prettier": "^3.7.4",
|
|
65
|
+
"tsdown": "^0.18.3",
|
|
66
|
+
"typescript": "^5.9.3"
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"effect": ">=3.0.0",
|
|
70
|
+
"fastify": ">=5.0.0"
|
|
71
|
+
},
|
|
72
|
+
"packageManager": "pnpm@10.22.0",
|
|
73
|
+
"private": false
|
|
74
|
+
}
|