Skip to content

Commit

Permalink
adding route , get, post, put, delete.
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Oct 26, 2024
1 parent 14f1c02 commit 7f06319
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 57 deletions.
141 changes: 94 additions & 47 deletions src/exportable/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,70 @@ import type {
} from "../morphism.ts";
import type { ParamsMethod } from "../router/types.ts";


/**
* `MethodMorphism` is a utility type that defines the function signatures for HTTP method-specific
* route definitions such as `get`, `post`, `delete`, and `put` within the `Wrap` interface.
*
* This type extends the `Morphism` type but omits the `method` field from its parameters. The HTTP
* method is enforced by the method name itself (e.g., `get`, `post`), ensuring that users cannot
* override the method accidentally by specifying it in the parameters.
*
*
* @example
* ```typescript
* const api = wrap()()
* .get({
* path: "/users",
* f: () => "Get Users",
* })
* .post({
* path: "/users",
* f: () => "Create User",
* })
* .delete({
* path: "/users/:id",
* f: (c) => `Delete User ${c.param.id}`,
* })
* .put({
* path: "/users/:id",
* f: (c) => `Update User ${c.param.id}`,
* });
* ```
*
* @typeparam O - The type of the router options (`FunRouterOptions`).
*/
type MethodMorphism<O extends FunRouterOptions<any>> = <
RM extends ResolveMap<any>,
BM extends BranchMap<any>,
QO extends QueryOptions,
PO extends ParamOptions,
CO extends CryptoOptions,
AR = any,
R = any,
>(
ob: Omit<
Morphism<
{
type: "add";
hasPath: true;
isAPetition: true;
typeNotNeeded: true;
hasMaybe: true;
},
RM,
BM,
QO,
PO,
O,
CO,
AR,
R
>,
"method"
>,
) => Wrap<O>;

/**
* The second `()` can either be left empty or used to add another `wrap`.
* This allows for flexible composition of your application's routing and request handling.
Expand All @@ -33,6 +97,11 @@ import type { ParamsMethod } from "../router/types.ts";
* For more details, see the [first-and-second-curried](https://vixeny.dev/library/wrap#first-and-second-curried).
*/
type Wrap<O extends FunRouterOptions<any>> = {
get: MethodMorphism<O>;
post: MethodMorphism<O>;
delete: MethodMorphism<O>;
put: MethodMorphism<O>;
route: MethodMorphism<O>;
/**
* * @deprecated use `add` instead
*
Expand All @@ -51,51 +120,6 @@ type Wrap<O extends FunRouterOptions<any>> = {
method?: ParamsMethod;
r: (ctx: Request) => Response | Promise<Response>;
}) => Wrap<O>;
/**
* Defines a standard Petition where `f` returns either a `BodyInit` or a `Promise<BodyInit>`.
*
* @example
* Usage example:
* ```javascript
* export const root = wrap()()
* .add({
* path: "/",
* f: () => "helloWorld",
* })
* .add({
* path: "/withRequest",
* f: () => new Response("helloWorld"),
* })
* ```
* For more details, see the [customPetition](https://vixeny.dev/library/wrap#custompetition.
*/
add: <
RM extends ResolveMap<any>,
BM extends BranchMap<any>,
QO extends QueryOptions,
PO extends ParamOptions,
CO extends CryptoOptions,
AR = any,
R = any,
>(
ob: Morphism<
{
type: "add";
hasPath: true;
isAPetition: true;
typeNotNeeded: true;
hasMaybe: true;
},
RM,
BM,
QO,
PO,
O,
CO,
AR,
R
>,
) => Wrap<O>;
/**
* @deprecated use `add` instead
*
Expand Down Expand Up @@ -529,7 +553,7 @@ export const wrap = ((o?) => (a = []) => ({
//@ts-ignore
{ ...ob, type: "request" } as Petition,
)),
add: (
route: (
ob,
) =>
wrap(o)(a.concat(
Expand Down Expand Up @@ -585,7 +609,30 @@ export const wrap = ((o?) => (a = []) => ({
[...a],
),
),

get: (ob) =>
wrap(o)(
a.concat(
{ ...ob, method: "GET", type: "add" } as Petition,
),
),
post: (ob) =>
wrap(o)(
a.concat(
{ ...ob, method: "POST", type: "add" } as Petition,
),
),
delete: (ob) =>
wrap(o)(
a.concat(
{ ...ob, method: "DELETE", type: "add" } as Petition,
),
),
put: (ob) =>
wrap(o)(
a.concat(
{ ...ob, method: "PUT", type: "add" } as Petition,
),
),
changeOptions: (o) =>
wrap({ ...o })(
[...a],
Expand Down
12 changes: 6 additions & 6 deletions src/morphism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const petitions = {
* });
* ```
*/
maybe: <
onError: <
FC extends CyclePluginMap,
O extends FunRouterOptions<FC>,
>(o?: O) =>
Expand Down Expand Up @@ -92,8 +92,8 @@ export const petitions = {
...I,
o,
thrush: {
name: "maybe",
value: "maybe(r)(b)",
name: "onError",
value: "onError(r)(b)",
type: 0,
isAsync: false,
},
Expand Down Expand Up @@ -607,8 +607,8 @@ export type Morphism<
> = {
readonly active?: MO["isAPetition"] extends true ? boolean : never;
readonly isUsing?: MO["isAPetition"] extends true ? string[] : never;
// TODO: Adding support for maybe
readonly maybe?: MO["hasMaybe"] extends true ? (
// TODO: Adding support for error
readonly onError?: MO["hasMaybe"] extends true ? (
a: WithPlugins<
RM,
BM,
Expand Down Expand Up @@ -803,7 +803,7 @@ interface Ctx<
TH extends boolean | undefined,
AR = any,
> {
maybe: TH extends true ? unknown : never;
error: TH extends true ? unknown : never;
args: AR extends undefined ? never : AR;
/**
* The `resolve` property is integral to ensuring that all necessary data is fetched or calculations are performed before the main function (`f`) of a morphism is executed. It consists of a map where each key corresponds to a resolve function that is executed prior to `f`. The results of these resolves are then made available in the `CTX` for use in the main function.
Expand Down
8 changes: 4 additions & 4 deletions test/exportable/wrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ const wrapped = wrap(
path: "/customHello",
f: () => new Response("customHello"),
})
.add({
.route({
path: "/customsPlugin",
f: ({ hello, method }) => new Response(hello() + method),
maybe: ({ maybe }) =>
maybe instanceof Response
? maybe
onError: ({ error }) =>
error instanceof Response
? error
: new Response("Critical error", { status: 501 }),
})
.petitionWithoutCTX({
Expand Down

0 comments on commit 7f06319

Please sign in to comment.