Skip to content

Commit

Permalink
adding test for the linker
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Oct 26, 2024
1 parent f3747ea commit ed2fe3b
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 44 deletions.
33 changes: 14 additions & 19 deletions src/composer/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,9 @@ const compose =
((isUsing) =>
(
(table) =>
p.thrush && typeof p.thrush === "object" && p.onError
? onError(table.isAsync)(
resolveF(o)(table)(p)(isUsing) as (
ctx: Request,
) => Response,
)(
resolveF(o)(table)({
// Uses the same petition's body and overdrives `onError` to avoid infinite recursions
...p,
f: p.onError,
onError: undefined,
})(isUsing) as (
ctx: Request,
) => (error: unknown) => Response,
)
: resolveF(o)(table)(p)(isUsing) as (
ctx: Request,
) => Promise<Response> | Response
resolveF(o)(table)(p)(isUsing) as (
ctx: Request,
) => Promise<Response> | Response
)(
//elements int table
{
Expand Down Expand Up @@ -82,7 +67,6 @@ const resolveF =
linker(o)(p)(isUsing),
);

// Passes value, mainly used for `resolve` and `branch`
default:
return getResponse(table.isAsync || table.asyncResolve)()(p.f)(
linker(o)(p)(isUsing),
Expand Down Expand Up @@ -177,6 +161,15 @@ async (request: Request): Promise<Response> => {

// On error wraps

const getApplyTo = (isAsync: boolean) =>
isAsync
//@ts-ignore-start
? () => (f) => (context) => (request) => async (error) =>
await f(await context(request)(error))
//@ts-ignore
: () => (f) => (context) => (request) => (error) =>
f(context(request)(error));

const onError = (isAsync: boolean) => isAsync ? asyncOnError : syncOnError;

const asyncOnError =
Expand All @@ -197,6 +190,8 @@ const syncOnError =
try {
return f(r);
} catch (error) {
console.log(m.toString());

return m(r)(error);
}
};
Expand Down
25 changes: 13 additions & 12 deletions src/composer/linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ export type specialOptions = {
} & FunRouterOptions<any>;

export default (o?: specialOptions) => (p: Petition) => (isUsing: string[]) => {
// Checks if the function has to be applied again for an extra function
const isApplyTo = p.applyTo !== undefined && typeof p.applyTo === "object";

// 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)) {

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

if (o && "branch" in o && p.thrush) {
if (o && "branch" in o && isApplyTo) {
// TODO: implement this, just add another wrap , do it before 0.2.0
throw new Error("PANIC_ONERROR: branch cannot have an on error yet");
}
Expand All @@ -38,26 +42,24 @@ export default (o?: specialOptions) => (p: Petition) => (isUsing: string[]) => {
.map((x) => `${x.name}=>`)
.reduceRight(
(acc, v) => v + acc,
// If thrush is in
typeof p.thrush === "object" ? "onError=>" : "",
// If applyTo is in
typeof p.applyTo === "object" ? "onError=>" : "",
);

// Determine the function signature based on options
let functionSignature = "";
if (needsAsync) {
// We are using the same structure of a branch for OnError

functionSignature = (o && "branch" in o) || typeof p.thrush === "object"
functionSignature = (o && "branch" in o) || isApplyTo
? " r=> async b=> "
: " async r=> ";
} else {
functionSignature = (o && "branch" in o) || p.thrush ? "r=>b=>" : "r=>";
functionSignature = (o && "branch" in o) || isApplyTo ? "r=>b=>" : "r=>";
}

// Adding thrush at the table to insert it in the final CTX object
const expandedTable = typeof p.thrush === "object"
? [...table, p.thrush]
: table;
// 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 = `({${
Expand All @@ -74,8 +76,7 @@ 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 (typeof p.thrush === "object") {
// We apply it so we need a `Request` and the thrown unknown to resolve the ctx
if (isApplyTo) {
return resultFunction(p.f);
}

Expand Down
25 changes: 12 additions & 13 deletions src/morphism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const petitions = {
...I,
o,
// This is done to trigger a flag in compose.ts in the compposer
thrush: {
applyTo: {
name: "onError",
value: "onError(r)(b)",
type: 1,
Expand Down Expand Up @@ -515,7 +515,13 @@ export const petitions = {
] as Petition[],
};

type typeMorphism = "response" | "request" | "morphism" | "base" | "add";
type typeMorphism =
| "response"
| "request"
| "morphism"
| "base"
| "add"
| "applyTo";

export type ResolveMap<T> = {
[K in keyof T]: T[K] extends Morphism<
Expand Down Expand Up @@ -547,7 +553,7 @@ export type BranchMap<T> = {
: never;
};

type ThrushCTX = {
type ApplyToCTX = {
name: string;
value: string;
type: 0 | 1;
Expand Down Expand Up @@ -594,13 +600,6 @@ type PetitionHeader = {
status?: number;
};

type Context = {
resolve?: ResolveMap<any>;
branch?: BranchMap<any>;
query?: QueryOptions;
param?: ParamOptions;
crypto?: CryptoOptions;
};
export type Morphism<
MO extends MapOptions = MapOptions,
RM extends ResolveMap<any> = any,
Expand Down Expand Up @@ -640,7 +639,7 @@ export type Morphism<
readonly query?: QO;
readonly cookie?: CookieOptions;
readonly param?: PO;
readonly thrush?: MO["specialVisible"] extends true ? ThrushCTX : never;
readonly applyTo?: MO["specialVisible"] extends true ? ApplyToCTX : never;
readonly plugins?: ExtractPluginTypes<RO>;
readonly headings?: PetitionHeader;
readonly isAsync?: MO["isAPetition"] extends true | false ? boolean
Expand Down Expand Up @@ -762,8 +761,8 @@ export type WithPlugins<
: {})
& CryptoContext<CR>;

// type Thrush<TH extends ThrushCTX | undefined> =
// TH extends ThrushCTX
// type ApplyTo<TH extends ApplyToCTX | undefined> =
// TH extends ApplyToCTX
// ? TH['name']
// : {}

Expand Down
Loading

0 comments on commit ed2fe3b

Please sign in to comment.