logic-sdk 0.2.0 → 0.4.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/README.md +9 -9
- package/dist/ai.d.ts +2 -5
- package/dist/ai.js +7 -5
- package/dist/fetch.d.ts +2 -4
- package/dist/fetch.js +1 -2
- package/dist/index.d.ts +140 -159
- package/dist/index.js +96 -77
- package/dist/orpc.d.ts +2 -5
- package/dist/orpc.js +5 -5
- package/dist/testing.d.ts +10 -8
- package/dist/testing.js +2 -1
- package/package.json +5 -5
- package/src/ai.ts +12 -11
- package/src/drizzle.ts +1 -1
- package/src/fetch.ts +3 -6
- package/src/index.ts +249 -153
- package/src/orpc.ts +6 -7
- package/src/testing.ts +18 -15
package/src/orpc.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { ORPCError, os, type Procedure } from "@orpc/server";
|
|
2
2
|
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
-
import { isDomainError, type Caller, type ErrorKind, type Fn, type Registry } from "./index.ts";
|
|
3
|
+
import { isDomainError, type Caller, type ErrorKind, type ExposedOf, type Fn, type Kind, type Registry } from "./index.ts";
|
|
4
4
|
|
|
5
5
|
/** oRPC context the router expects: bind who is calling per request, in your handler or middleware. */
|
|
6
6
|
export type ORPCContext<S extends object> = { caller: Caller<S> };
|
|
7
7
|
|
|
8
8
|
type ProcedureOf<S extends object, F> =
|
|
9
|
-
F extends Fn<
|
|
9
|
+
F extends Fn<Kind, infer I, infer O, true>
|
|
10
10
|
? Procedure<ORPCContext<S>, ORPCContext<S>, StandardSchemaV1<I, I>, StandardSchemaV1<O, O>, {}>
|
|
11
11
|
: never;
|
|
12
12
|
|
|
@@ -16,7 +16,6 @@ export type ORPCRouterOf<S extends object, Mods> = {
|
|
|
16
16
|
[N in keyof ExposedOf<Mods[M]>]: ProcedureOf<S, Mods[M][N & keyof Mods[M]]>;
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
|
-
type ExposedOf<T> = { [N in keyof T as T[N] extends Fn<any, any, any, true> ? N : never]: T[N] };
|
|
20
19
|
|
|
21
20
|
/** Domain error kind → oRPC error code (and so HTTP status through oRPC's handlers). */
|
|
22
21
|
export const orpcCodeOf: Record<ErrorKind, string> = {
|
|
@@ -46,11 +45,11 @@ export function toORPCRouter<S extends object, Mods extends Record<string, objec
|
|
|
46
45
|
|
|
47
46
|
for (const f of registry.functions) {
|
|
48
47
|
if (!f.exposed) continue;
|
|
49
|
-
let b = base.input(f.fn.
|
|
50
|
-
if (f.fn.
|
|
51
|
-
(router[f.module] ??= {})[f.name] = b.handler(async ({ input, context }) => {
|
|
48
|
+
let b = f.fn.input ? base.input(f.fn.input) : base;
|
|
49
|
+
if (f.fn.output) b = b.output(f.fn.output) as any;
|
|
50
|
+
(router[f.module] ??= {})[f.name] = b.handler(async ({ input, context, signal }) => {
|
|
52
51
|
try {
|
|
53
|
-
return await context.caller.run(f.fn, input);
|
|
52
|
+
return await context.caller.run(f.fn, input, { signal });
|
|
54
53
|
} catch (e) {
|
|
55
54
|
if (isDomainError(e))
|
|
56
55
|
throw new ORPCError(orpcCodeOf[e.kind], {
|
package/src/testing.ts
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
import { internal, type Caller, type
|
|
1
|
+
import { internal, type Caller, type DeepPartial, type Envelope, type Fn, type FnIn, type FnOut, type Registry } from "./index.ts";
|
|
2
2
|
|
|
3
|
-
export interface TestOptions {
|
|
3
|
+
export interface TestOptions<Cf = unknown> {
|
|
4
4
|
/** Replace services (e.g. `{ mailer: memoryMailer() }`). */
|
|
5
5
|
services?: object;
|
|
6
|
+
/** Override parts of `config` (e.g. `{ orders: { maxItems: 1 } }`). Merged over the registry's, section by section. */
|
|
7
|
+
config?: DeepPartial<Cf>;
|
|
6
8
|
/** Freeze time. */
|
|
7
9
|
now?: Date | string | (() => Date);
|
|
8
10
|
/** Run listeners inline after each mutation (default), or not at all to test one module in isolation. */
|
|
9
11
|
listeners?: boolean;
|
|
10
12
|
}
|
|
11
13
|
|
|
12
|
-
export interface TestTransaction<S extends object, Mods extends Record<string, object> = Record<string, object
|
|
14
|
+
export interface TestTransaction<S extends object, Mods extends Record<string, object> = Record<string, object>, Cf = unknown> {
|
|
13
15
|
/** Bind who is calling, exactly like `registry.as(...)`. */
|
|
14
|
-
as(scope: S
|
|
16
|
+
as(scope: S): Caller<S>;
|
|
15
17
|
/** 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
|
|
18
|
+
stub<F extends Fn>(fn: F, impl: (ctx: unknown, input: FnIn<F>) => Promise<FnOut<F>>): void;
|
|
17
19
|
/** Every event emitted (and committed) so far, in order. */
|
|
18
20
|
readonly events: Envelope[];
|
|
19
21
|
/** The test transaction itself: read rows to assert on them. */
|
|
20
22
|
readonly db: any;
|
|
21
23
|
/** The registry bound to this test transaction, for adapters: `toFetchHandler(tx.registry, …)`. */
|
|
22
|
-
readonly registry: Registry<S, Mods>;
|
|
24
|
+
readonly registry: Registry<S, Mods, Cf>;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
class Rollback extends Error {}
|
|
@@ -29,9 +31,9 @@ class Rollback extends Error {}
|
|
|
29
31
|
* Every top-level mutation inside becomes a savepoint, so a failing mutation rolls back just its own writes,
|
|
30
32
|
* like it would in production. Run top-level mutations one at a time inside a test (one connection, nested savepoints).
|
|
31
33
|
*/
|
|
32
|
-
export function createTestRegistry<S extends object, Mods extends Record<string, object
|
|
33
|
-
registry: Registry<S, Mods>,
|
|
34
|
-
options: TestOptions = {},
|
|
34
|
+
export function createTestRegistry<S extends object, Mods extends Record<string, object>, Cf>(
|
|
35
|
+
registry: Registry<S, Mods, Cf>,
|
|
36
|
+
options: TestOptions<Cf> = {},
|
|
35
37
|
) {
|
|
36
38
|
const { options: registryOptions } = internal(registry);
|
|
37
39
|
const now =
|
|
@@ -40,29 +42,30 @@ export function createTestRegistry<S extends object, Mods extends Record<string,
|
|
|
40
42
|
: typeof options.now === "function"
|
|
41
43
|
? options.now
|
|
42
44
|
: (() => {
|
|
43
|
-
const fixed = new Date(options.now
|
|
45
|
+
const fixed = new Date(options.now);
|
|
44
46
|
return () => new Date(fixed);
|
|
45
47
|
})();
|
|
46
48
|
|
|
47
49
|
return {
|
|
48
|
-
async transaction<T>(fn: (t: TestTransaction<S, Mods>) => Promise<T>): Promise<T> {
|
|
50
|
+
async transaction<T>(fn: (t: TestTransaction<S, Mods, Cf>) => Promise<T>): Promise<T> {
|
|
49
51
|
const database = registryOptions.database;
|
|
50
52
|
let result!: T;
|
|
51
53
|
|
|
52
54
|
const body = async (outer: unknown) => {
|
|
53
|
-
const stubs = new Map<Fn, (
|
|
55
|
+
const stubs = new Map<Fn, (ctx: never, input: never) => Promise<unknown>>();
|
|
54
56
|
const events: Envelope[] = [];
|
|
55
57
|
const derived = internal(registry).derive({
|
|
56
58
|
database: outer !== undefined && database?.nest ? database.nest(outer) : database,
|
|
57
59
|
services: options.services,
|
|
60
|
+
config: options.config as object | undefined,
|
|
58
61
|
now,
|
|
59
62
|
stubs,
|
|
60
63
|
record: (e) => events.push(e),
|
|
61
64
|
events: options.listeners === false ? { deliver: async () => {} } : undefined,
|
|
62
|
-
})
|
|
65
|
+
});
|
|
63
66
|
result = await fn({
|
|
64
|
-
as: (scope
|
|
65
|
-
stub: (f, impl) => void stubs.set(f, impl
|
|
67
|
+
as: (scope) => derived.as(scope),
|
|
68
|
+
stub: (f, impl) => void stubs.set(f, impl),
|
|
66
69
|
events,
|
|
67
70
|
db: outer,
|
|
68
71
|
registry: derived,
|