Skip to content

Commit 7e3e06b

Browse files
committed
chore: relase v1 rc at next dist-tag
Signed-off-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent 19be4cb commit 7e3e06b

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ _I went all-in on TypeScript just this year - the experience is unparalleled._<b
7777
This is ESM-only package with built-in types.
7878

7979
```bash
80-
bun install zagora
80+
bun install zagora@next
8181
```
8282

8383
## Usage

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zagora",
3-
"version": "0.0.0-semantically-released",
3+
"version": "1.0.0-next.2",
44
"license": "Apache-2.0",
55
"description": "A minimalist & robust way to create type-safe and error-safe never throwing functions & libraries in TypeScript - with input/output validation and typed errors. Based on StandardSchema-compliant validation libraries. No batteries, no routers, it's just functions that you can export and use. Simple but robust alternative to oRPC and tRPC, no network layer.",
66
"repository": {
@@ -14,7 +14,7 @@
1414
},
1515
"publishConfig": {
1616
"access": "public",
17-
"provenance": true
17+
"tag": "next"
1818
},
1919
"files": [
2020
"dist"

src/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,15 @@ export class Zagora<
359359
>;
360360

361361
const schemaAny = inputSchema as any;
362-
const isTuple =
363-
schemaAny?._def?.type === "tuple" || schemaAny?.type === "tuple";
362+
const isTupleSchema =
363+
(schemaAny?._def && schemaAny?._def?.type === "tuple") ||
364+
schemaAny?.type === "tuple";
365+
366+
const isArraySchema =
367+
(schemaAny?._def && schemaAny?._def?.type === "array") ||
368+
schemaAny?.type === "array";
369+
370+
const isPrimitiveSchema = !isTupleSchema;
364371

365372
const processor = (mode: "input" | "output", schema: any, data: any) => {
366373
if (schema) {
@@ -372,9 +379,10 @@ export class Zagora<
372379
const processInput = (inputData: any) => {
373380
// NOTE: isTuple is safe/enough here cuz it's based on the inputSchema,
374381
// thus if inputSchema is not defined, then it would be isTuple=false too.
375-
const handlerArgs = isTuple
376-
? handleTupleDefaults(inputSchema as any, inputData as any)
377-
: [inputData];
382+
const handlerArgs =
383+
!isArraySchema && !isPrimitiveSchema
384+
? handleTupleDefaults(inputSchema as any, inputData as any)
385+
: [inputData];
378386

379387
const executionArgs = disableOptions
380388
? handlerArgs
@@ -408,7 +416,7 @@ export class Zagora<
408416
return handleState(state);
409417
};
410418

411-
const inputArgs = isTuple ? args : args[0];
419+
const inputArgs = !isArraySchema && !isPrimitiveSchema ? args : args[0];
412420

413421
const inputResult = inputSchema
414422
? validateInputOutputOrEnv("input", inputSchema, inputArgs)

test/main.test.ts

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ test("typed error returns exact object with isDefined=true", () => {
5656
test("context method works", () => {
5757
const fn = zagora()
5858
.context({ db: "mock" })
59-
.input(z.string())
59+
.input(z.number())
6060
.output(z.string())
6161
.handler(({ context }, input) => {
62+
expectTypeOf(input).toEqualTypeOf<number>();
63+
expectTypeOf(context).toEqualTypeOf<{ db: string }>();
64+
6265
return `${input}-${context.db}`;
6366
})
6467
.callable();
6568

66-
const res = fn("bar");
69+
const res = fn(1001);
6770
if (res.ok) {
68-
expect(res.data).toBe("bar-mock");
71+
expect(res.data).toBe("1001-mock");
6972
} else {
7073
expect(false, "Expected success").toBe(true);
7174
}
@@ -149,7 +152,11 @@ test("async input schema", async () => {
149152
const fn = zagora()
150153
.input(asyncSchema)
151154
.output(z.string())
152-
.handler((_, input) => input.toUpperCase())
155+
.handler((_, input) => {
156+
expectTypeOf(input).toEqualTypeOf<string>();
157+
158+
return input.toUpperCase();
159+
})
153160
.callable();
154161

155162
const res = await fn("ab");
@@ -261,6 +268,7 @@ test("Error.cause is set on wrapped errors", () => {
261268
.input(z.string())
262269
.output(z.string())
263270
.handler((_, input) => {
271+
expectTypeOf(input).toEqualTypeOf<string>();
264272
if (input === "fail") {
265273
throw originalError;
266274
}
@@ -278,21 +286,25 @@ test("Error.cause is set on wrapped errors", () => {
278286
}
279287
});
280288

281-
test("both tuple and object access formats work", () => {
282-
const fn = zagora()
283-
.input(z.string())
284-
.output(z.string())
285-
.handler((_, input) => input)
286-
.callable();
287-
288-
const res = fn("foo");
289-
290-
if (res.ok) {
291-
expect(res.data).toBe("foo");
292-
} else {
293-
expect(false, "Expected success").toBe(true);
294-
}
295-
});
289+
// TODO: fix to support array schemas, and not treat it as tuple schemas!
290+
// test("input schema array of string should work", () => {
291+
// const fn = zagora()
292+
// .input(z.array(z.string()))
293+
// .output(z.string())
294+
// .handler((_, input) => {
295+
// expectTypeOf(input).toEqualTypeOf<string[]>();
296+
// return input.map((x) => x.toUpperCase());
297+
// })
298+
// .callable();
299+
300+
// const res = fn(["foo", "bar"]);
301+
302+
// if (res.ok) {
303+
// expect(res.data).toBe(["foo", "bar"]);
304+
// } else {
305+
// expect(false, "Expected success").toBe(true);
306+
// }
307+
// });
296308

297309
test("input validation failure", () => {
298310
const fn = zagora()
@@ -331,6 +343,8 @@ test("no error schema works", () => {
331343
.input(z.string())
332344
.output(z.string())
333345
.handler((_, input) => {
346+
expectTypeOf(input).toEqualTypeOf<string>();
347+
334348
if (input === "fail") {
335349
throw new Error("Untyped");
336350
}
@@ -350,7 +364,12 @@ test("tuple input arguments", () => {
350364
const fn = zagora()
351365
.input(z.tuple([z.string(), z.number()]))
352366
.output(z.string())
353-
.handler((_, str, num) => `${str}-${num}`)
367+
.handler((_, str, num) => {
368+
expectTypeOf(str).toEqualTypeOf<string>();
369+
expectTypeOf(num).toEqualTypeOf<number>();
370+
371+
return `${str}-${num}`;
372+
})
354373
.callable();
355374

356375
const res = fn("hello", 42);
@@ -368,6 +387,8 @@ test("thrown ZagoraError passed through", () => {
368387
.input(z.string())
369388
.output(z.string())
370389
.handler((_, input) => {
390+
expectTypeOf(input).toEqualTypeOf<string>();
391+
371392
if (input === "throw now") {
372393
throw customErr;
373394
}
@@ -386,9 +407,14 @@ test("thrown ZagoraError passed through", () => {
386407
test("multiple procedures (calculator) from single instance (autoCallable:true)", () => {
387408
const za = zagora({ autoCallable: true, disableOptions: true });
388409

389-
const add = za
390-
.input(z.tuple([z.number(), z.number()]))
391-
.handler((a, b) => a + b);
410+
const add = za.input(z.tuple([z.number(), z.number()])).handler((a, b) => {
411+
expectTypeOf({ a, b }).toEqualTypeOf<{
412+
a: number;
413+
b: number;
414+
}>();
415+
416+
return a + b;
417+
});
392418

393419
const subtract = za
394420
.input(z.tuple([z.number(), z.number()]))
@@ -490,7 +516,14 @@ test("handle optional/default valaues in object schemas", async () => {
490516
.output(SuccessSchema);
491517

492518
const getPrices = getPricesContract.handler(
493-
async ({ errors: err }, { speed, num, includeDetails }) => {
519+
async ({ errors: err }, input) => {
520+
const { speed, num, includeDetails } = input;
521+
expectTypeOf(input).toEqualTypeOf<{
522+
speed: "slow" | "normal" | "fast";
523+
num: number;
524+
includeDetails: boolean;
525+
}>();
526+
494527
// Simulate rate limiting
495528
if (num && num > 1000) {
496529
throw err.RATE_LIMIT({
@@ -595,6 +628,8 @@ test("basic in-memory caching/memoization", async () => {
595628

596629
let called = 0;
597630
const hello = za.input(z.string()).handler(async (_, name) => {
631+
expectTypeOf(name).toEqualTypeOf<string>();
632+
598633
called += 1;
599634
await new Promise((resolve) => setTimeout(resolve, 100));
600635
return `Hello, ${name}!`;
@@ -642,6 +677,10 @@ test("cache adapter passed through `.callable` method", async () => {
642677
.context({ age: 10 })
643678
.input(z.string())
644679
.handler(({ context }) => {
680+
expectTypeOf(context).toEqualTypeOf<{
681+
age: number;
682+
}>;
683+
645684
called += 1;
646685
return context.age + called;
647686
})
@@ -734,7 +773,12 @@ test("basic env schema support through `.env` method", () => {
734773
process.env,
735774
)
736775
.handler(({ env }, input) => {
737-
// env: { DATABASE_URL: string, SOME_SECRET: string, PORT: number }
776+
expectTypeOf(env).toEqualTypeOf<{
777+
DATABASE_URL: string;
778+
SOME_SECRET: string;
779+
PORT: number;
780+
}>;
781+
738782
return `input=${input};url=${env.DATABASE_URL};secret=${env.SOME_SECRET};PORT=${env.PORT}`;
739783
})
740784
.callable({

0 commit comments

Comments
 (0)