Skip to content

Commit

Permalink
new type system
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Aug 16, 2024
1 parent c60717c commit 8ef4e55
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 42 deletions.
115 changes: 115 additions & 0 deletions experimental/typeSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Basic types

// Options
type Options = {
queryOptions: QueryOptions;
resolveMap: ResolveMap<any>;
branchMap: BranchMap<any>;
};
type QueryOptions = {
unique?: true;
name: string;
} | {
only?: string[];
} | {};

type ResolveMap<T> = {
[K in keyof T]: Morphism<
{
type: "morphism";
branch: undefined;

}
>;
};

type BranchMap<T> = {
[K in keyof T]: Morphism<
{
type: "morphism";
branch: boolean | undefined;
}
>;
};


// Map

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

type HasPath<P extends Map> = P extends { hasPath: true }
? { readonly path: string }
: {};

type HasType<P extends Map> = P extends { type: typeMorphism }
? P extends { typeNotNeeded: true } ? {}
: P extends { type: "morphism" } ? {}
: { readonly type: P["type"] }
: {};

type ExtraKeys<P extends Map> = HasPath<P> & HasType<P>;

type Map = {
hasPath?: boolean;
typeNotNeeded?: boolean;
type?: typeMorphism;
branch?: boolean;
isAPetition?: boolean;
mutable?: true;
specificReturnType?: boolean;
returnType?: unknown;
};

type Morphism<
MO extends Map = Map,
OP extends Options = Options,
R = any,
> =
{
readonly active?: MO["isAPetition"] extends true ? boolean : never;
readonly isUsing?: MO["isAPetition"] extends true ? string[] : never;
readonly query?: OP['queryOptions'];
readonly branch?: OP["branchMap"];
readonly resolve?: OP["resolveMap"];
readonly r?: MO["type"] extends "response"
? (r: Request) => Promise<Response> | Response
: never;
readonly f: {
(
ctx: Ctx<
OP['queryOptions']
>,
): MO["specificReturnType"] extends true ? MO["returnType"]
: MO["type"] extends "response" ? Response | Promise<Response>
: MO["type"] extends "request" ? Response | Promise<Response>
: MO["type"] extends "base" ? BodyInit | Promise<BodyInit> | null
: R;
};
}
& ExtraKeys<MO>;

interface Ctx<
QueryType = any
> {
//resolve: { [V in keyof R]: Awaited<ReturnType<R[V]["f"]>> };
query: QueryType
}





const ff = <
a extends { type: "morphism"},
B extends > ( d : Morphism<a,B>) => ''



ff({
query: {
unique: true,
name: 'ji'
},
// This type should be string
f: s => s.query
})
53 changes: 24 additions & 29 deletions src/composer/throws.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
const genericError = new Response(null, {
status: 500
})

const throwable = (f: (r:Request) => Response ) => (r:Request) => {

try {
return f(r)
}catch(e){

if( e instanceof Response ){
return e
}

return genericError.clone()
const genericError = new Response(null, {
status: 500,
});

const throwable = (f: (r: Request) => Response) => (r: Request) => {
try {
return f(r);
} catch (e) {
if (e instanceof Response) {
return e;
}

return genericError.clone();
}

const asyncThrowable = (f: (r:Request) => Promise<Response> ) => async (r:Request) => {

};

const asyncThrowable =
(f: (r: Request) => Promise<Response>) => async (r: Request) => {
try {
return await f(r)
}catch(e){

if( e instanceof Response ){
return e
return await f(r);
} catch (e) {
if (e instanceof Response) {
return e;
}
return genericError.clone()

return genericError.clone();
}
}
};

export {
throwable , asyncThrowable
}
export { asyncThrowable, throwable };
17 changes: 6 additions & 11 deletions src/staticFiles/staticFileTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ export default {
join: (base: string) => (target: string): string =>
base.endsWith("/") ? base + target : base + "/" + target,

// Checks
// Checks
mimeForm: (f: fileServerPetition<any>) =>
"mime" in f && f.mime === false
? []
: f.extra
// Creates a map of mime and overwrites the extra keys if already exist
? [... f.extra.reduce( ( map , v) => (
map.set(v[0], v[1]),
map
) , new Map<string,string>(mime))]

"mime" in f && f.mime === false ? [] : f.extra
// Creates a map of mime and overwrites the extra keys if already exist
? [...f.extra.reduce((map, v) => (
map.set(v[0], v[1]), map
), new Map<string, string>(mime))]
: mime,

getMime: (mimes: [string, string][]) => (ext: string): string =>
Expand All @@ -48,7 +44,6 @@ export default {
x.path.includes(y)
),
),

) as Petition[]
: petitions,

Expand Down
4 changes: 2 additions & 2 deletions test/staticFile/mime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test(
type: "fileServer",
path: "./",
name: "/hello/",
}).find( val => val[0] === '.ts'),
}).find((val) => val[0] === ".ts"),
[".ts", "video/mp2t"],
);
assertEquals(
Expand All @@ -38,7 +38,7 @@ test(
path: "./",
name: "/hello/",
extra: [[".ts", "hello/hello"]],
}).find( val => val[0] === '.ts'),
}).find((val) => val[0] === ".ts"),
[".ts", "hello/hello"],
);
},
Expand Down

0 comments on commit 8ef4e55

Please sign in to comment.