logic-sdk 0.0.0-stage → 0.2.0
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 +21 -0
- package/README.md +129 -2
- package/bin/logic-sdk.js +14 -0
- package/dist/ai.d.ts +45 -0
- package/dist/ai.js +51 -0
- package/dist/drizzle.d.ts +18 -0
- package/dist/drizzle.js +23 -0
- package/dist/fetch.d.ts +24 -0
- package/dist/fetch.js +68 -0
- package/dist/index.d.ts +444 -0
- package/dist/index.js +388 -0
- package/dist/orpc.d.ts +31 -0
- package/dist/orpc.js +46 -0
- package/dist/testing.d.ts +29 -0
- package/dist/testing.js +61 -0
- package/package.json +106 -4
- package/src/ai.ts +84 -0
- package/src/drizzle.ts +39 -0
- package/src/fetch.ts +82 -0
- package/src/index.ts +736 -0
- package/src/orpc.ts +65 -0
- package/src/testing.ts +88 -0
package/src/orpc.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ORPCError, os, type Procedure } from "@orpc/server";
|
|
2
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
import { isDomainError, type Caller, type ErrorKind, type Fn, type Registry } from "./index.ts";
|
|
4
|
+
|
|
5
|
+
/** oRPC context the router expects: bind who is calling per request, in your handler or middleware. */
|
|
6
|
+
export type ORPCContext<S extends object> = { caller: Caller<S> };
|
|
7
|
+
|
|
8
|
+
type ProcedureOf<S extends object, F> =
|
|
9
|
+
F extends Fn<any, infer I, infer O, true>
|
|
10
|
+
? Procedure<ORPCContext<S>, ORPCContext<S>, StandardSchemaV1<I, I>, StandardSchemaV1<O, O>, {}>
|
|
11
|
+
: never;
|
|
12
|
+
|
|
13
|
+
/** `{ bookings: { confirm, cancel, … } }`: only functions with `expose: true`. */
|
|
14
|
+
export type ORPCRouterOf<S extends object, Mods> = {
|
|
15
|
+
[M in keyof Mods as {} extends ExposedOf<Mods[M]> ? never : M]: {
|
|
16
|
+
[N in keyof ExposedOf<Mods[M]>]: ProcedureOf<S, Mods[M][N & keyof Mods[M]]>;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
type ExposedOf<T> = { [N in keyof T as T[N] extends Fn<any, any, any, true> ? N : never]: T[N] };
|
|
20
|
+
|
|
21
|
+
/** Domain error kind → oRPC error code (and so HTTP status through oRPC's handlers). */
|
|
22
|
+
export const orpcCodeOf: Record<ErrorKind, string> = {
|
|
23
|
+
invalid: "BAD_REQUEST",
|
|
24
|
+
unauthenticated: "UNAUTHORIZED",
|
|
25
|
+
forbidden: "FORBIDDEN",
|
|
26
|
+
not_found: "NOT_FOUND",
|
|
27
|
+
conflict: "CONFLICT",
|
|
28
|
+
rejected: "UNPROCESSABLE_CONTENT",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Turn the registry into an oRPC router. Your modules stay the core; oRPC is the edge that gives you
|
|
33
|
+
* a typed client, OpenAPI and MCP. Validation runs once, in the registry.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const router = toORPCRouter(registry);
|
|
37
|
+
* const handler = new RPCHandler(router);
|
|
38
|
+
* handler.handle(request, { prefix: "/rpc", context: { caller: registry.as(await scopeFrom(request)) } });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function toORPCRouter<S extends object, Mods extends Record<string, object>>(
|
|
42
|
+
registry: Registry<S, Mods>,
|
|
43
|
+
): ORPCRouterOf<S, Mods> {
|
|
44
|
+
const base = os.$context<ORPCContext<S>>().$config({ disableInputValidation: true, disableOutputValidation: true });
|
|
45
|
+
const router: Record<string, Record<string, unknown>> = {};
|
|
46
|
+
|
|
47
|
+
for (const f of registry.functions) {
|
|
48
|
+
if (!f.exposed) continue;
|
|
49
|
+
let b = base.input(f.fn.inputSchema as StandardSchemaV1);
|
|
50
|
+
if (f.fn.outputSchema) b = b.output(f.fn.outputSchema as StandardSchemaV1) as any;
|
|
51
|
+
(router[f.module] ??= {})[f.name] = b.handler(async ({ input, context }) => {
|
|
52
|
+
try {
|
|
53
|
+
return await context.caller.run(f.fn, input);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
if (isDomainError(e))
|
|
56
|
+
throw new ORPCError(orpcCodeOf[e.kind], {
|
|
57
|
+
message: e.code,
|
|
58
|
+
data: { kind: e.kind, code: e.code, details: e.details },
|
|
59
|
+
});
|
|
60
|
+
throw e;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return router as ORPCRouterOf<S, Mods>;
|
|
65
|
+
}
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { internal, type Caller, type CallerOptions, type Envelope, type Fn, type FnIn, type FnOut, type Registry } from "./index.ts";
|
|
2
|
+
|
|
3
|
+
export interface TestOptions {
|
|
4
|
+
/** Replace services (e.g. `{ mailer: memoryMailer() }`). */
|
|
5
|
+
services?: object;
|
|
6
|
+
/** Freeze time. */
|
|
7
|
+
now?: Date | string | (() => Date);
|
|
8
|
+
/** Run listeners inline after each mutation (default), or not at all to test one module in isolation. */
|
|
9
|
+
listeners?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface TestTransaction<S extends object, Mods extends Record<string, object> = Record<string, object>> {
|
|
13
|
+
/** Bind who is calling, exactly like `registry.as(...)`. */
|
|
14
|
+
as(scope: S, opts?: CallerOptions): Caller<S>;
|
|
15
|
+
/** Replace a function's implementation for this test. Matched by identity, so `run(availability.hold, …)` hits the stub. */
|
|
16
|
+
stub<F extends Fn>(fn: F, impl: (input: FnIn<F>, ctx: any) => Promise<FnOut<F>>): void;
|
|
17
|
+
/** Every event emitted (and committed) so far, in order. */
|
|
18
|
+
readonly events: Envelope[];
|
|
19
|
+
/** The test transaction itself: read rows to assert on them. */
|
|
20
|
+
readonly db: any;
|
|
21
|
+
/** The registry bound to this test transaction, for adapters: `toFetchHandler(tx.registry, …)`. */
|
|
22
|
+
readonly registry: Registry<S, Mods>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class Rollback extends Error {}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Run each test inside a transaction that is always rolled back.
|
|
29
|
+
* Every top-level mutation inside becomes a savepoint, so a failing mutation rolls back just its own writes,
|
|
30
|
+
* like it would in production. Run top-level mutations one at a time inside a test (one connection, nested savepoints).
|
|
31
|
+
*/
|
|
32
|
+
export function createTestRegistry<S extends object, Mods extends Record<string, object>>(
|
|
33
|
+
registry: Registry<S, Mods>,
|
|
34
|
+
options: TestOptions = {},
|
|
35
|
+
) {
|
|
36
|
+
const { options: registryOptions } = internal(registry);
|
|
37
|
+
const now =
|
|
38
|
+
options.now === undefined
|
|
39
|
+
? undefined
|
|
40
|
+
: typeof options.now === "function"
|
|
41
|
+
? options.now
|
|
42
|
+
: (() => {
|
|
43
|
+
const fixed = new Date(options.now as Date | string);
|
|
44
|
+
return () => new Date(fixed);
|
|
45
|
+
})();
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
async transaction<T>(fn: (t: TestTransaction<S, Mods>) => Promise<T>): Promise<T> {
|
|
49
|
+
const database = registryOptions.database;
|
|
50
|
+
let result!: T;
|
|
51
|
+
|
|
52
|
+
const body = async (outer: unknown) => {
|
|
53
|
+
const stubs = new Map<Fn, (input: any, ctx: any) => Promise<any>>();
|
|
54
|
+
const events: Envelope[] = [];
|
|
55
|
+
const derived = internal(registry).derive({
|
|
56
|
+
database: outer !== undefined && database?.nest ? database.nest(outer) : database,
|
|
57
|
+
services: options.services,
|
|
58
|
+
now,
|
|
59
|
+
stubs,
|
|
60
|
+
record: (e) => events.push(e),
|
|
61
|
+
events: options.listeners === false ? { deliver: async () => {} } : undefined,
|
|
62
|
+
}) as Registry<S, Mods>;
|
|
63
|
+
result = await fn({
|
|
64
|
+
as: (scope, opts) => derived.as(scope, opts),
|
|
65
|
+
stub: (f, impl) => void stubs.set(f, impl as (input: any, ctx: any) => Promise<any>),
|
|
66
|
+
events,
|
|
67
|
+
db: outer,
|
|
68
|
+
registry: derived,
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
if (!database) {
|
|
73
|
+
await body(undefined);
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
if (!database.nest) throw new Error("[logic-sdk] This database adapter can't nest transactions, so tests can't roll back.");
|
|
77
|
+
try {
|
|
78
|
+
await database.transaction(async (outer) => {
|
|
79
|
+
await body(outer);
|
|
80
|
+
throw new Rollback();
|
|
81
|
+
});
|
|
82
|
+
} catch (e) {
|
|
83
|
+
if (!(e instanceof Rollback)) throw e;
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|