@gravito/impulse-bridge 1.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/dist/index.cjs ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ImpulseBridge: () => ImpulseBridge,
24
+ impulseBridgeMiddleware: () => impulseBridgeMiddleware
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var ImpulseBridge = {
28
+ /**
29
+ * Share a FormRequest blueprint with the frontend (via Inertia).
30
+ *
31
+ * @example
32
+ * async show(ctx: Context) {
33
+ * ImpulseBridge.share(ctx, 'login', LoginFormRequest)
34
+ * return ctx.inertia('Login')
35
+ * }
36
+ */
37
+ share(ctx, key, RequestClass) {
38
+ const request = new RequestClass();
39
+ const blueprint = request.getBlueprint();
40
+ const inertia = ctx.get("inertia");
41
+ if (inertia && typeof inertia.share === "function") {
42
+ const currentBlueprints = inertia.getSharedProps?.().blueprints || {};
43
+ currentBlueprints[key] = blueprint;
44
+ inertia.share("blueprints", currentBlueprints);
45
+ return;
46
+ }
47
+ const shared = ctx.get("inertiaShared") || {};
48
+ shared.blueprints = shared.blueprints || {};
49
+ shared.blueprints[key] = blueprint;
50
+ ctx.set("inertiaShared", shared);
51
+ }
52
+ };
53
+ function impulseBridgeMiddleware(blueprints) {
54
+ return async (ctx, next) => {
55
+ for (const [key, RequestClass] of Object.entries(blueprints)) {
56
+ ImpulseBridge.share(ctx, key, RequestClass);
57
+ }
58
+ return await next();
59
+ };
60
+ }
61
+ // Annotate the CommonJS export names for ESM import in node:
62
+ 0 && (module.exports = {
63
+ ImpulseBridge,
64
+ impulseBridgeMiddleware
65
+ });
@@ -0,0 +1,26 @@
1
+ import { GravitoContext } from '@gravito/core';
2
+ import { FormRequest } from '@gravito/impulse';
3
+
4
+ /**
5
+ * Impulse Bridge
6
+ *
7
+ * Provides utilities to synchronize backend validation rules with the frontend.
8
+ */
9
+ declare const ImpulseBridge: {
10
+ /**
11
+ * Share a FormRequest blueprint with the frontend (via Inertia).
12
+ *
13
+ * @example
14
+ * async show(ctx: Context) {
15
+ * ImpulseBridge.share(ctx, 'login', LoginFormRequest)
16
+ * return ctx.inertia('Login')
17
+ * }
18
+ */
19
+ share(ctx: GravitoContext, key: string, RequestClass: new () => FormRequest): void;
20
+ };
21
+ /**
22
+ * Global middleware to automatically inject blueprints if requested.
23
+ */
24
+ declare function impulseBridgeMiddleware(blueprints: Record<string, new () => FormRequest>): (ctx: GravitoContext, next: () => Promise<Response | undefined>) => Promise<Response | undefined>;
25
+
26
+ export { ImpulseBridge, impulseBridgeMiddleware };
@@ -0,0 +1,26 @@
1
+ import { GravitoContext } from '@gravito/core';
2
+ import { FormRequest } from '@gravito/impulse';
3
+
4
+ /**
5
+ * Impulse Bridge
6
+ *
7
+ * Provides utilities to synchronize backend validation rules with the frontend.
8
+ */
9
+ declare const ImpulseBridge: {
10
+ /**
11
+ * Share a FormRequest blueprint with the frontend (via Inertia).
12
+ *
13
+ * @example
14
+ * async show(ctx: Context) {
15
+ * ImpulseBridge.share(ctx, 'login', LoginFormRequest)
16
+ * return ctx.inertia('Login')
17
+ * }
18
+ */
19
+ share(ctx: GravitoContext, key: string, RequestClass: new () => FormRequest): void;
20
+ };
21
+ /**
22
+ * Global middleware to automatically inject blueprints if requested.
23
+ */
24
+ declare function impulseBridgeMiddleware(blueprints: Record<string, new () => FormRequest>): (ctx: GravitoContext, next: () => Promise<Response | undefined>) => Promise<Response | undefined>;
25
+
26
+ export { ImpulseBridge, impulseBridgeMiddleware };
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ // src/index.ts
2
+ var ImpulseBridge = {
3
+ /**
4
+ * Share a FormRequest blueprint with the frontend (via Inertia).
5
+ *
6
+ * @example
7
+ * async show(ctx: Context) {
8
+ * ImpulseBridge.share(ctx, 'login', LoginFormRequest)
9
+ * return ctx.inertia('Login')
10
+ * }
11
+ */
12
+ share(ctx, key, RequestClass) {
13
+ const request = new RequestClass();
14
+ const blueprint = request.getBlueprint();
15
+ const inertia = ctx.get("inertia");
16
+ if (inertia && typeof inertia.share === "function") {
17
+ const currentBlueprints = inertia.getSharedProps?.().blueprints || {};
18
+ currentBlueprints[key] = blueprint;
19
+ inertia.share("blueprints", currentBlueprints);
20
+ return;
21
+ }
22
+ const shared = ctx.get("inertiaShared") || {};
23
+ shared.blueprints = shared.blueprints || {};
24
+ shared.blueprints[key] = blueprint;
25
+ ctx.set("inertiaShared", shared);
26
+ }
27
+ };
28
+ function impulseBridgeMiddleware(blueprints) {
29
+ return async (ctx, next) => {
30
+ for (const [key, RequestClass] of Object.entries(blueprints)) {
31
+ ImpulseBridge.share(ctx, key, RequestClass);
32
+ }
33
+ return await next();
34
+ };
35
+ }
36
+ export {
37
+ ImpulseBridge,
38
+ impulseBridgeMiddleware
39
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@gravito/impulse-bridge",
3
+ "version": "1.0.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Validation bridge between Impulse and Frontend (Inertia/Prism)",
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "type": "module",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
24
+ "typecheck": "bun tsc -p tsconfig.json --noEmit --skipLibCheck"
25
+ },
26
+ "peerDependencies": {
27
+ "@gravito/core": "workspace:*",
28
+ "@gravito/impulse": "workspace:*"
29
+ },
30
+ "devDependencies": {
31
+ "@gravito/core": "workspace:*",
32
+ "@gravito/impulse": "workspace:*",
33
+ "tsup": "^8.3.5",
34
+ "typescript": "^5.9.3"
35
+ }
36
+ }