Skip to content

Commit

Permalink
ready fro pr
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragkumar19 committed Mar 5, 2024
1 parent c4e8516 commit c0d5034
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 71 deletions.
68 changes: 0 additions & 68 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,11 @@
import { $fetch } from "../src/node";

interface ApiDefinition {
"/api/v1": {
default: {
response: { message: string };
};
};
"/api/v1/auth/register": {
post: {
response: { message: string };
request: {
body: {
name: string;
email: string;
username: string;
password: string;
};
};
};
};
"/api/v1/users/search": {
get: {
response: { users: { username: string }[] };
request: {
query: {
username: string;
};
};
};
};
"/api/users/:username": {
get: {
response: { user: { username: string; name: string; isFriend: boolean } };
request: {
params: {
username: string;
};
};
};
post: {
response: { message: string };
request: {
params: {
username: string;
};
};
};
};
}

async function main() {
// const r = await $fetch<string>('http://google.com/404')
const r = await $fetch<string>("http://httpstat.us/500");
// const r = await $fetch<string>('http://httpstat/500')
// eslint-disable-next-line no-console
console.log(r);

const fetch$ = $fetch.create<unknown, ApiDefinition>({});

const { message } = await fetch$("/api/v1");
console.log(message);

const r1 = await fetch$("/api/users/:username", {
method: "POST",
params: { username: "anurag" },
});

console.log(r1.message);

const r2 = await fetch$("/api/users/:username", {
method: "get",
params: { username: "anurag" },
});

console.log(r2.user);
}

// eslint-disable-next-line unicorn/prefer-top-level-await
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// $fetch API
// --------------------------

// TODO: set default to any for backward compatibility
export interface $Fetch<DefaultT = unknown, A extends object = InternalApi> {
<
T = DefaultT,
Expand Down Expand Up @@ -241,7 +242,7 @@ export type TypedInternalQuery<
A extends object,
Default,
Method extends RouterMethod = RouterMethod,
> = Route extends string
> = Route extends string // TODO: Allow user overrides
? Method extends keyof A[MatchedRoutes<Route, A>]
? A[MatchedRoutes<Route, A>][Method] extends {
request: { query: infer T };
Expand All @@ -260,7 +261,7 @@ export type TypedInternalParams<
A extends object,
Default,
Method extends RouterMethod = RouterMethod,
> = Route extends string
> = Route extends string // TODO: Allow user overrides
? Method extends keyof A[MatchedRoutes<Route, A>]
? A[MatchedRoutes<Route, A>][Method] extends {
request: { params: infer T };
Expand All @@ -279,7 +280,7 @@ export type TypedInternalBody<
A extends object,
Default,
Method extends RouterMethod = RouterMethod,
> = Route extends string
> = Route extends string // TODO: Allow user overrides
? Method extends keyof A[MatchedRoutes<Route, A>]
? A[MatchedRoutes<Route, A>][Method] extends {
request: { body: infer T };
Expand All @@ -295,6 +296,7 @@ export type TypedInternalBody<

// Extract the route method from options which might be undefined or without a method parameter.
export type ExtractedRouteMethod<
// TODO: improvement needed
A extends object,
R extends ExtendedFetchRequest<A>,
> = R extends string
Expand Down

0 comments on commit c0d5034

Please sign in to comment.