Skip to content

Commit

Permalink
pre 0.1.48 , checking test on deno 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Oct 27, 2024
1 parent ed2fe3b commit 02dbd0f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x # Uses latest deno version 1
- run: deno add @cross/test @std/assert # Installs dependencies from jsr.io
deno-version: v2.x
- run: deno add @cross/test @std/assert
- run: deno test -A

- name: Verify formatting
Expand Down
21 changes: 0 additions & 21 deletions .github/workflows/node.yml

This file was deleted.

23 changes: 20 additions & 3 deletions src/composer/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@ const compose =
((isUsing) =>
(
(table) =>
resolveF(o)(table)(p)(isUsing) as (
ctx: Request,
) => Promise<Response> | Response
typeof p.onError === "function"
? onError(table.isAsync)(
resolveF(o)(table)(p)(isUsing) as (
ctx: Request,
) => Response,
)(
getApplyTo(table.isAsync)()(p.onError)(
linker(o)({
...p,
f: p.onError,
onError: undefined,
applyTo: {
type: "onError",
},
}),
)(isUsing),
)
: resolveF(o)(table)(p)(isUsing) as (
ctx: Request,
) => Promise<Response> | Response
)(
//elements int table
{
Expand Down
1 change: 1 addition & 0 deletions src/composer/composerTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const elements = (f: Petition) =>
"io",
"req",
"date",
"error",
f.resolve ? "resolve" : undefined,
"mutable",
f.branch ? "branch" : undefined,
Expand Down
18 changes: 7 additions & 11 deletions src/composer/linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export default (o?: specialOptions) => (p: Petition) => (isUsing: string[]) => {
// Base case: if 'isUsing' is empty and 'branch' is not in options, return the identity function
// TODO: change it and add the branch flag in the petition

if (isUsing.length === 0 && !(o && "branch" in o) && !isApplyTo) {
if (isUsing.length === 0 && !(o && "branch" in o)) {
if (isApplyTo) {
return (r: Request) => (b: unknown) => r;
}
return (r: Request) => r;
}

Expand All @@ -42,8 +45,8 @@ export default (o?: specialOptions) => (p: Petition) => (isUsing: string[]) => {
.map((x) => `${x.name}=>`)
.reduceRight(
(acc, v) => v + acc,
// If applyTo is in
typeof p.applyTo === "object" ? "onError=>" : "",
// Place to add more in the future
"",
);

// Determine the function signature based on options
Expand All @@ -58,12 +61,9 @@ export default (o?: specialOptions) => (p: Petition) => (isUsing: string[]) => {
functionSignature = (o && "branch" in o) || isApplyTo ? "r=>b=>" : "r=>";
}

// Adding applyTo at the table to insert it in the final CTX object
const expandedTable = isApplyTo ? [...table, p.applyTo] : table;

// Build the function body, injecting variables from 'table'
const functionBody = `({${
expandedTable.map((x) => `${x.name}:${x.value}`).join(",")
table.map((x) => `${x.name}:${x.value}`).join(",")
}})`;

// Build the full function string
Expand All @@ -76,9 +76,5 @@ export default (o?: specialOptions) => (p: Petition) => (isUsing: string[]) => {
// Reduce the functions over the generated function
const resultFunction = functions.reduce((s, k) => s(k), generatedFunc);

if (isApplyTo) {
return resultFunction(p.f);
}

return resultFunction;
};
2 changes: 1 addition & 1 deletion src/composer/nativeMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default (o?: FunRouterOptions<any>) =>
(mutable: boolean) =>
([
{ name: "req", value: mutable ? "r[0]" : "r", type: 0 },
{ name: "error", value: "b", type: 0 },
{
name: "param",
value: mutable ? "param(r[0].url)" : "param(r.url)",
Expand Down Expand Up @@ -57,7 +58,6 @@ export default (o?: FunRouterOptions<any>) =>
}`,
type: 1,
},
{ name: "mutable", value: mutable ? "r[1]" : "{}", type: 0 },
{
name: "branch",
value: `${
Expand Down
4 changes: 1 addition & 3 deletions src/morphism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,7 @@ export type BranchMap<T> = {
};

type ApplyToCTX = {
name: string;
value: string;
type: 0 | 1;
type: "onError";
};

type MapOptions = {
Expand Down
28 changes: 28 additions & 0 deletions test/composer/linker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,31 @@ test("Checking branch", async () => {
},
);
});

test("Checking branch", async () => {
// Wraps correctly
assertEquals(
linker({})({
type: "add",
path: "/1/2/:id",
f: mockFunction,
applyTo: {
type: "onError",
},
})([])(requestForTest)(),
requestForTest,
);

// Wraps correctly
assertEquals(
linker({})({
type: "add",
path: "/1/2/:id",
f: mockFunction,
applyTo: {
type: "onError",
},
})(["error"])(requestForTest)("world").error,
"world",
);
});
8 changes: 4 additions & 4 deletions test/exportable/wrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ const opt = plugins.globalOptions({
const wrapped = wrap(
opt,
)()
.stdPetition({
.get({
path: "/stdHello",
f: () => "stdHello",
})
.stdPetition({
.get({
path: "/stdPlugin",
plugins: {
hello: "string",
method: "string",
},
f: (ctx) => ctx.hello() + ctx.method,
})
.customPetition({
.get({
path: "/customHello",
f: () => new Response("customHello"),
})
.route({
.get({
path: "/customsPlugin",
f: ({ hello, method }) => new Response(hello() + method),
onError: ({ error }) =>
Expand Down

0 comments on commit 02dbd0f

Please sign in to comment.