@newhomestar/sdk 0.4.2 → 0.4.4

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { z, ZodTypeAny } from "zod";
1
+ import { z, type ZodTypeAny } from "zod";
2
+ import type { IncomingMessage, ServerResponse } from "node:http";
2
3
  export interface ActionDef<I extends ZodTypeAny, O extends ZodTypeAny> {
3
4
  name: string;
4
5
  input: I;
@@ -31,12 +32,12 @@ export declare function action<I extends ZodTypeAny, O extends ZodTypeAny>(cfg:
31
32
  path?: string;
32
33
  handler: (input: z.infer<I>, ctx: ActionCtx) => Promise<z.infer<O>>;
33
34
  }): ActionDef<I, O>;
34
- import type { NovaSpec } from './parseSpec';
35
+ import type { NovaSpec } from './parseSpec.js';
35
36
  /**
36
37
  * FGA policy hints embedded in nova.yaml
37
38
  */
38
39
  export type FgaSpec = NovaSpec['fga'];
39
- import { WorkerDef } from './workerSchema';
40
+ import { type WorkerDef } from './workerSchema.js';
40
41
  /**
41
42
  * Register a worker definition - SAME API as before
42
43
  */
@@ -45,10 +46,18 @@ export declare function defineWorker<T extends WorkerDef>(def: T): T;
45
46
  export declare function enqueue<P extends object>(actionPath: `${string}.${string}`, payload: P): Promise<{
46
47
  job_id: string;
47
48
  }>;
49
+ /**
50
+ * Create an oRPC router from a WorkerDef, mapping each action to an oRPC procedure
51
+ */
52
+ export declare function createORPCRouter<T extends WorkerDef>(def: T): Record<string, any>;
48
53
  export interface ORPCServerOptions {
49
54
  port?: number;
50
55
  plugins?: any[];
51
56
  }
57
+ /**
58
+ * Run an official oRPC server - fully compliant with oRPC standards!
59
+ */
60
+ export declare function runORPCServer<T extends WorkerDef>(def: T, options?: ORPCServerOptions): import("http").Server<typeof IncomingMessage, typeof ServerResponse>;
52
61
  export declare function runWorker(def: WorkerDef): Promise<void>;
53
62
  export declare function generateOpenAPISpec<T extends WorkerDef>(def: T): Promise<{
54
63
  openapi: string;
@@ -66,5 +75,5 @@ export declare function runHttpServer<T extends WorkerDef>(def: T, opts?: {
66
75
  port?: number;
67
76
  }): void;
68
77
  export type { ZodTypeAny as SchemaAny, ZodTypeAny };
69
- export { parseNovaSpec } from "./parseSpec";
70
- export type { NovaSpec } from "./parseSpec";
78
+ export { parseNovaSpec } from "./parseSpec.js";
79
+ export type { NovaSpec } from "./parseSpec.js";
package/dist/index.js CHANGED
@@ -1,23 +1,26 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.parseNovaSpec = void 0;
7
- exports.action = action;
8
- exports.defineWorker = defineWorker;
9
- exports.enqueue = enqueue;
10
- exports.runWorker = runWorker;
11
- exports.generateOpenAPISpec = generateOpenAPISpec;
12
- exports.runHttpServer = runHttpServer;
13
- const dotenv_1 = __importDefault(require("dotenv"));
14
- const supabase_js_1 = require("@supabase/supabase-js");
15
- const sdk_1 = require("@openfga/sdk");
1
+ // nova-sdk-esm – Modern ESM Nova SDK with full oRPC integration (v0.4.2)
2
+ // =====================================================
3
+ // 1. Public API – action(), defineWorker(), enqueue() - SAME AS BEFORE
4
+ // 2. Enhanced HTTP server with REST endpoints and custom routing
5
+ // 3. Full oRPC integration with OpenAPI spec generation
6
+ // 4. Runtime harness for *worker* pipelines using Supabase RPC
7
+ // 5. Modern ESM architecture for future compatibility
8
+ // -----------------------------------------------------------
9
+ import { z } from "zod";
10
+ import dotenv from "dotenv";
11
+ import { createClient } from "@supabase/supabase-js";
12
+ import { OpenFgaClient } from "@openfga/sdk";
13
+ import { createServer } from "node:http";
14
+ // Full oRPC imports now working with ESM
15
+ import { os } from "@orpc/server";
16
+ import { RPCHandler } from "@orpc/server/node";
17
+ import { CORSPlugin } from "@orpc/server/plugins";
18
+ import { OpenAPIHandler } from "@orpc/openapi/fetch";
16
19
  if (!process.env.RUNTIME_SUPABASE_URL) {
17
20
  // local dev – read .env.local
18
- dotenv_1.default.config({ path: ".env.local", override: true });
21
+ dotenv.config({ path: ".env.local", override: true });
19
22
  }
20
- function action(cfg) {
23
+ export function action(cfg) {
21
24
  return {
22
25
  name: cfg.name ?? "unnamed",
23
26
  method: cfg.method ?? 'POST',
@@ -26,13 +29,13 @@ function action(cfg) {
26
29
  };
27
30
  }
28
31
  // WorkerDef represents the code-level definition passed into defineWorker()
29
- const workerSchema_1 = require("./workerSchema");
32
+ import { WorkerDefSchema } from './workerSchema.js';
30
33
  /**
31
34
  * Register a worker definition - SAME API as before
32
35
  */
33
- function defineWorker(def) {
36
+ export function defineWorker(def) {
34
37
  // Runtime validation of the worker definition
35
- workerSchema_1.WorkerDefSchema.parse(def);
38
+ WorkerDefSchema.parse(def);
36
39
  return def;
37
40
  }
38
41
  /*──────────────────────* Client‑side enqueue() (UNCHANGED) *──────────────────*/
@@ -42,10 +45,10 @@ let clientSupabase;
42
45
  function getClient() {
43
46
  if (!CLIENT_SUPABASE_URL || !CLIENT_SUPABASE_KEY)
44
47
  throw new Error("CLIENT_SUPABASE_* env vars not set");
45
- return (clientSupabase ?? (clientSupabase = (0, supabase_js_1.createClient)(CLIENT_SUPABASE_URL, CLIENT_SUPABASE_KEY)));
48
+ return (clientSupabase ??= createClient(CLIENT_SUPABASE_URL, CLIENT_SUPABASE_KEY));
46
49
  }
47
50
  /** Enqueue an async action and receive `{ job_id }` */
48
- async function enqueue(actionPath, payload) {
51
+ export async function enqueue(actionPath, payload) {
49
52
  const [pipeline, action] = actionPath.split(".");
50
53
  const { data, error } = await getClient().rpc("nova_enqueue", {
51
54
  pipeline_name: pipeline,
@@ -56,13 +59,69 @@ async function enqueue(actionPath, payload) {
56
59
  throw error;
57
60
  return data;
58
61
  }
62
+ /*──────────────── Full oRPC Integration (NOW WORKING!) ───────────────*/
63
+ /**
64
+ * Create an oRPC router from a WorkerDef, mapping each action to an oRPC procedure
65
+ */
66
+ export function createORPCRouter(def) {
67
+ const procedures = {};
68
+ for (const [actionName, actionDef] of Object.entries(def.actions)) {
69
+ // Create oRPC procedure - convert Nova action to oRPC procedure
70
+ const procedure = os
71
+ .input(actionDef.input)
72
+ .output(actionDef.output)
73
+ .handler(async ({ input, context }) => {
74
+ const ctx = {
75
+ jobId: context?.jobId || `orpc-${Date.now()}`,
76
+ progress: (percent, meta) => {
77
+ console.log(`[${actionName}] Progress: ${percent}%`, meta);
78
+ }
79
+ };
80
+ return await actionDef.handler(input, ctx);
81
+ });
82
+ procedures[actionName] = procedure;
83
+ }
84
+ return procedures;
85
+ }
86
+ /**
87
+ * Run an official oRPC server - fully compliant with oRPC standards!
88
+ */
89
+ export function runORPCServer(def, options = {}) {
90
+ const router = createORPCRouter(def);
91
+ console.log(`[nova] Starting official oRPC server "${def.name}"`);
92
+ // Use official oRPC serving pattern
93
+ const handler = new RPCHandler(router, {
94
+ plugins: [new CORSPlugin(), ...(options.plugins || [])]
95
+ });
96
+ const server = createServer(async (req, res) => {
97
+ const result = await handler.handle(req, res, {
98
+ context: {
99
+ headers: req.headers,
100
+ jobId: `orpc-${Date.now()}`,
101
+ // Add any other Nova-specific context
102
+ }
103
+ });
104
+ if (!result.matched) {
105
+ res.statusCode = 404;
106
+ res.end('No procedure matched');
107
+ }
108
+ });
109
+ const port = options.port ?? (process.env.PORT ? parseInt(process.env.PORT) : 8000);
110
+ server.listen(port, () => {
111
+ console.log(`[nova] Official oRPC server listening on http://localhost:${port}`);
112
+ console.log(`[nova] Available procedures: ${Object.keys(def.actions).join(', ')}`);
113
+ console.log(`[nova] 🔥 RPC protocol: ACTIVE (not HTTP fallback)`);
114
+ console.log(`[nova] 🌐 CORS: Enabled via CORSPlugin`);
115
+ });
116
+ return server;
117
+ }
59
118
  /*──────────────── Runtime harness (Supabase RPC) - UNCHANGED ───────────────*/
60
119
  const RUNTIME_SUPABASE_URL = process.env.RUNTIME_SUPABASE_URL;
61
120
  const RUNTIME_SUPABASE_KEY = process.env.RUNTIME_SUPABASE_SERVICE_ROLE_KEY;
62
121
  const runtime = RUNTIME_SUPABASE_URL && RUNTIME_SUPABASE_KEY
63
- ? (0, supabase_js_1.createClient)(RUNTIME_SUPABASE_URL, RUNTIME_SUPABASE_KEY)
122
+ ? createClient(RUNTIME_SUPABASE_URL, RUNTIME_SUPABASE_KEY)
64
123
  : undefined;
65
- async function runWorker(def) {
124
+ export async function runWorker(def) {
66
125
  if (!runtime)
67
126
  throw new Error("RUNTIME_SUPABASE_* env vars not configured");
68
127
  console.log(`[nova] worker '${def.name}' polling ${def.queue}`);
@@ -100,7 +159,7 @@ async function runWorker(def) {
100
159
  if (!apiEndpoint || !storeId || !authToken) {
101
160
  throw new Error('Missing OPENFGA_API_ENDPOINT, OPENFGA_STORE_ID, or OPENFGA_AUTH_TOKEN for FGA enforcement');
102
161
  }
103
- const fgaClient = new sdk_1.OpenFgaClient({ apiUrl: apiEndpoint, storeId });
162
+ const fgaClient = new OpenFgaClient({ apiUrl: apiEndpoint, storeId });
104
163
  let authorized = true;
105
164
  for (const hint of hints) {
106
165
  const key = hint.resourceIdKey;
@@ -159,7 +218,7 @@ async function nack(id, q) {
159
218
  }
160
219
  function delay(ms) { return new Promise(r => setTimeout(r, ms)); }
161
220
  /*──────────────── NEW: OpenAPI Spec Generation ───────────────*/
162
- async function generateOpenAPISpec(def) {
221
+ export async function generateOpenAPISpec(def) {
163
222
  // This would use oRPC's built-in OpenAPI generation
164
223
  // For now, return a basic spec structure
165
224
  return {
@@ -200,14 +259,14 @@ async function generateOpenAPISpec(def) {
200
259
  };
201
260
  }
202
261
  /*──────────────── HTTP Server Harness (Enhanced) ───────────────*/
203
- const express_1 = __importDefault(require("express"));
204
- const body_parser_1 = __importDefault(require("body-parser"));
262
+ import express from "express";
263
+ import bodyParser from "body-parser";
205
264
  /**
206
265
  * Enhanced HTTP server exposing each action under configurable routes
207
266
  */
208
- function runHttpServer(def, opts = {}) {
209
- const app = (0, express_1.default)();
210
- app.use(body_parser_1.default.json());
267
+ export function runHttpServer(def, opts = {}) {
268
+ const app = express();
269
+ app.use(bodyParser.json());
211
270
  for (const [actionName, act] of Object.entries(def.actions)) {
212
271
  const method = (act.method || 'POST').toLowerCase();
213
272
  const route = act.path || `/${def.name}/${actionName}`;
@@ -243,5 +302,4 @@ function runHttpServer(def, opts = {}) {
243
302
  });
244
303
  }
245
304
  // YAML spec parsing utility
246
- var parseSpec_1 = require("./parseSpec");
247
- Object.defineProperty(exports, "parseNovaSpec", { enumerable: true, get: function () { return parseSpec_1.parseNovaSpec; } });
305
+ export { parseNovaSpec } from "./parseSpec.js";
package/dist/parseSpec.js CHANGED
@@ -1,74 +1,70 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NovaSpecSchema = void 0;
4
- exports.parseNovaSpec = parseNovaSpec;
5
- const yaml_1 = require("yaml");
6
- const zod_1 = require("zod");
1
+ import { parse as parseYAML } from "yaml";
2
+ import { z } from "zod";
7
3
  // Zod schema for nova.yaml
8
- exports.NovaSpecSchema = zod_1.z.object({
9
- apiVersion: zod_1.z.string(),
10
- kind: zod_1.z.string(),
11
- metadata: zod_1.z.object({
12
- name: zod_1.z.string(),
13
- displayName: zod_1.z.string().optional(),
14
- description: zod_1.z.string().optional(),
15
- icon: zod_1.z.string().optional(),
16
- tags: zod_1.z.array(zod_1.z.string()).optional(),
4
+ export const NovaSpecSchema = z.object({
5
+ apiVersion: z.string(),
6
+ kind: z.string(),
7
+ metadata: z.object({
8
+ name: z.string(),
9
+ displayName: z.string().optional(),
10
+ description: z.string().optional(),
11
+ icon: z.string().optional(),
12
+ tags: z.array(z.string()).optional(),
17
13
  }),
18
- spec: zod_1.z.object({
19
- runtime: zod_1.z.object({
20
- type: zod_1.z.string(),
21
- image: zod_1.z.string(),
22
- resources: zod_1.z.object({
23
- cpu: zod_1.z.string(),
24
- memory: zod_1.z.string(),
14
+ spec: z.object({
15
+ runtime: z.object({
16
+ type: z.string(),
17
+ image: z.string(),
18
+ resources: z.object({
19
+ cpu: z.string(),
20
+ memory: z.string(),
25
21
  }),
26
- command: zod_1.z.array(zod_1.z.string()),
27
- queue: zod_1.z.string(),
28
- port: zod_1.z.number(),
29
- envSpec: zod_1.z.array(zod_1.z.object({
30
- name: zod_1.z.string(),
31
- value: zod_1.z.string().optional(),
32
- secret: zod_1.z.boolean().optional(),
33
- default: zod_1.z.string().optional(),
22
+ command: z.array(z.string()),
23
+ queue: z.string(),
24
+ port: z.number(),
25
+ envSpec: z.array(z.object({
26
+ name: z.string(),
27
+ value: z.string().optional(),
28
+ secret: z.boolean().optional(),
29
+ default: z.string().optional(),
34
30
  })).optional(),
35
31
  }),
36
- actions: zod_1.z.array(zod_1.z.object({
37
- name: zod_1.z.string(),
38
- displayName: zod_1.z.string().optional(),
39
- description: zod_1.z.string().optional(),
40
- icon: zod_1.z.string().optional(),
41
- async: zod_1.z.boolean().optional(),
42
- input: zod_1.z.unknown().optional(), // JSON schema object
43
- output: zod_1.z.unknown().optional(), // JSON schema object
44
- schema: zod_1.z.object({
45
- input: zod_1.z.string(),
46
- output: zod_1.z.string(),
32
+ actions: z.array(z.object({
33
+ name: z.string(),
34
+ displayName: z.string().optional(),
35
+ description: z.string().optional(),
36
+ icon: z.string().optional(),
37
+ async: z.boolean().optional(),
38
+ input: z.unknown().optional(), // JSON schema object
39
+ output: z.unknown().optional(), // JSON schema object
40
+ schema: z.object({
41
+ input: z.string(),
42
+ output: z.string(),
47
43
  }).optional(),
48
- fga: zod_1.z.object({
49
- resourceType: zod_1.z.string(),
50
- relation: zod_1.z.string(),
44
+ fga: z.object({
45
+ resourceType: z.string(),
46
+ relation: z.string(),
51
47
  }).optional(),
52
48
  })),
53
49
  }),
54
- build: zod_1.z.object({
55
- dockerfile: zod_1.z.string(),
56
- context: zod_1.z.string(),
50
+ build: z.object({
51
+ dockerfile: z.string(),
52
+ context: z.string(),
57
53
  }).optional(),
58
- ui: zod_1.z.object({
59
- category: zod_1.z.string().optional(),
60
- color: zod_1.z.string().optional(),
54
+ ui: z.object({
55
+ category: z.string().optional(),
56
+ color: z.string().optional(),
61
57
  }).optional(),
62
58
  // OpenFGA policy hints embedded in nova.yaml
63
- fga: zod_1.z.object({
64
- types: zod_1.z.array(zod_1.z.object({
65
- name: zod_1.z.string(),
66
- relations: zod_1.z.record(zod_1.z.string(), zod_1.z.union([
67
- zod_1.z.array(zod_1.z.string()),
68
- zod_1.z.object({
69
- computedUserset: zod_1.z.object({
70
- object: zod_1.z.string(),
71
- relation: zod_1.z.string(),
59
+ fga: z.object({
60
+ types: z.array(z.object({
61
+ name: z.string(),
62
+ relations: z.record(z.string(), z.union([
63
+ z.array(z.string()),
64
+ z.object({
65
+ computedUserset: z.object({
66
+ object: z.string(),
67
+ relation: z.string(),
72
68
  }),
73
69
  }),
74
70
  ])),
@@ -82,19 +78,19 @@ exports.NovaSpecSchema = zod_1.z.object({
82
78
  * @returns Parsed NovaSpec object
83
79
  * @throws Error with validation details if parsing or validation fail
84
80
  */
85
- function parseNovaSpec(yamlContent) {
81
+ export function parseNovaSpec(yamlContent) {
86
82
  let parsed;
87
83
  try {
88
- parsed = (0, yaml_1.parse)(yamlContent);
84
+ parsed = parseYAML(yamlContent);
89
85
  }
90
86
  catch (e) {
91
87
  throw new Error(`Failed to parse YAML content: ${e.message}`);
92
88
  }
93
89
  try {
94
- return exports.NovaSpecSchema.parse(parsed);
90
+ return NovaSpecSchema.parse(parsed);
95
91
  }
96
92
  catch (e) {
97
- if (e instanceof zod_1.z.ZodError) {
93
+ if (e instanceof z.ZodError) {
98
94
  const details = e.issues
99
95
  .map((issue) => `Path '${issue.path.join(".")}': ${issue.message}`)
100
96
  .join("; ");
@@ -1,41 +1,38 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WorkerDefSchema = void 0;
4
- const zod_1 = require("zod");
1
+ import { z } from 'zod';
5
2
  /**
6
3
  * Schema for code-level defineWorker() argument.
7
4
  * This enforces worker metadata, actions, envSpec, and optional FGA hints.
8
5
  */
9
- exports.WorkerDefSchema = zod_1.z.object({
10
- name: zod_1.z.string(),
11
- queue: zod_1.z.string(),
6
+ export const WorkerDefSchema = z.object({
7
+ name: z.string(),
8
+ queue: z.string(),
12
9
  // Optional environment variable spec for nova.yaml generation
13
- envSpec: zod_1.z.array(zod_1.z.object({ name: zod_1.z.string(), secret: zod_1.z.boolean(), default: zod_1.z.string().optional() })).optional(),
10
+ envSpec: z.array(z.object({ name: z.string(), secret: z.boolean(), default: z.string().optional() })).optional(),
14
11
  // Optional top-level FGA policy types
15
- fga: zod_1.z.object({
16
- types: zod_1.z.array(zod_1.z.object({
17
- name: zod_1.z.string(),
18
- relations: zod_1.z.record(zod_1.z.string(), zod_1.z.union([
19
- zod_1.z.array(zod_1.z.string()),
20
- zod_1.z.object({ computedUserset: zod_1.z.object({ object: zod_1.z.string(), relation: zod_1.z.string() }) })
12
+ fga: z.object({
13
+ types: z.array(z.object({
14
+ name: z.string(),
15
+ relations: z.record(z.string(), z.union([
16
+ z.array(z.string()),
17
+ z.object({ computedUserset: z.object({ object: z.string(), relation: z.string() }) })
21
18
  ])),
22
19
  }))
23
20
  }).optional(),
24
21
  // Map of action definitions
25
- actions: zod_1.z.record(zod_1.z.string(), zod_1.z.object({
26
- name: zod_1.z.string().optional(),
27
- input: zod_1.z.any(), // Zod schema instance expected
28
- output: zod_1.z.any(), // Zod schema instance expected
22
+ actions: z.record(z.string(), z.object({
23
+ name: z.string().optional(),
24
+ input: z.any(), // Zod schema instance expected
25
+ output: z.any(), // Zod schema instance expected
29
26
  // NEW: HTTP routing support for oRPC
30
- method: zod_1.z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).optional(),
31
- path: zod_1.z.string().optional(),
27
+ method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).optional(),
28
+ path: z.string().optional(),
32
29
  // Optional per-action OpenFGA hints: resource type, relation, ID key, and optional policy caveat
33
- fga: zod_1.z.object({
34
- resourceType: zod_1.z.string(),
35
- relation: zod_1.z.string(),
36
- resourceIdKey: zod_1.z.string().optional(),
37
- policy: zod_1.z.string().optional(),
30
+ fga: z.object({
31
+ resourceType: z.string(),
32
+ relation: z.string(),
33
+ resourceIdKey: z.string().optional(),
34
+ policy: z.string().optional(),
38
35
  }).optional(),
39
- handler: zod_1.z.any(), // function with signature (input, ctx) => Promise<…>
36
+ handler: z.any(), // function with signature (input, ctx) => Promise<…>
40
37
  })),
41
38
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newhomestar/sdk",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Type-safe SDK for building Nova pipelines (workers & functions)",
5
5
  "homepage": "https://github.com/newhomestar/nova-node-sdk#readme",
6
6
  "bugs": {
@@ -12,9 +12,15 @@
12
12
  },
13
13
  "license": "ISC",
14
14
  "author": "Christian Gomez",
15
- "type": "commonjs",
15
+ "type": "module",
16
16
  "main": "dist/index.js",
17
17
  "types": "dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ }
23
+ },
18
24
  "files": [
19
25
  "dist"
20
26
  ],