Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions integration/client/clientType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export type Routes = {
method: "GET";
path: "/users";
responses: {
code: "200";
information: "users.findMany";
body: {
id: number;
name: string;
age: number;
}[];
};
} | {
method: "GET";
path: "/users/{userId}";
params: {
userId: number;
};
responses: {
code: "422";
information: "extract-error";
body?: undefined;
} | {
code: "200";
information: "users.find";
body: {
id: number;
name: string;
age: number;
};
};
} | {
method: "POST";
path: "/users";
body: {
id: number;
name: string;
age: number;
};
responses: {
code: "422";
information: "extract-error";
body?: undefined;
} | {
code: "200";
information: "users.create";
body: {
id: number;
name: string;
age: number;
};
};
};
201 changes: 201 additions & 0 deletions integration/client/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import { hub } from "@core";
import { createHttpServer } from "@duplojs/http/node";
import { createHttpClient, type NotPredictedClientResponse, type PromiseRequestParams, type RequestErrorContent } from "@duplojs/http/client";
import { type Routes } from "./clientType";
import { E, S, type ExpectType } from "@duplojs/utils";

describe("node server", async() => {
const server = await createHttpServer(hub, {
host: "0.0.0.0",
port: 8946,
});

afterAll(() => {
server.close();
});

const httpClient = createHttpClient<Routes>({
baseUrl: "http://localhost:8946",
});

it("get all users", async() => {
const result = await httpClient.get("/users");

type Check = ExpectType<
typeof result,
| E.EitherLeft<"request-error", RequestErrorContent>
| E.EitherRight<
"response",
| {
code: "200";
information: "users.findMany";
body: {
id: number;
name: string;
age: number;
}[];
ok: boolean | null;
headers: Headers;
type: ResponseType;
url: string;
redirected: boolean;
raw: Response;
requestParams: PromiseRequestParams<Record<string, unknown>>;
predicted: boolean;
}
| NotPredictedClientResponse<
PromiseRequestParams<Record<string, unknown>>
>
>,
"strict"
>;

expect(result).toStrictEqual(
E.right(
"response",
expect.objectContaining({
url: "http://localhost:8946/users",
information: "users.findMany",
body: [
{
age: 28,
id: 23,
name: "",
},
],
predicted: true,
}),
),
);
});

it("get user", async() => {
const result = await httpClient.get("/users/{userId}", { params: { userId: S.to(15) } });

type Check = ExpectType<
typeof result,
| E.EitherLeft<"request-error", RequestErrorContent>
| E.EitherRight<
"response",
| NotPredictedClientResponse<
PromiseRequestParams<Record<string, unknown>>
>
| {
code: "422";
information: "extract-error";
body: undefined;
ok: boolean | null;
headers: Headers;
type: ResponseType;
url: string;
redirected: boolean;
raw: Response;
requestParams: PromiseRequestParams<Record<string, unknown>>;
predicted: boolean;
}
| {
code: "200";
information: "users.find";
body: {
id: number;
name: string;
age: number;
};
ok: boolean | null;
headers: Headers;
type: ResponseType;
url: string;
redirected: boolean;
raw: Response;
requestParams: PromiseRequestParams<Record<string, unknown>>;
predicted: boolean;
}
>,
"strict"
>;

expect(result).toStrictEqual(
E.right(
"response",
expect.objectContaining({
url: "http://localhost:8946/users/15",
information: "users.find",
body: {
age: 28,
id: 15,
name: "",
},
predicted: true,
}),
),
);
});

it("port user", async() => {
const result = await httpClient.post("/users", {
body: {
id: 5,
name: "math",
age: 23,
},
});

type Check = ExpectType<
typeof result,
| E.EitherLeft<"request-error", RequestErrorContent>
| E.EitherRight<
"response",
| {
code: "422";
information: "extract-error";
body: undefined;
ok: boolean | null;
headers: Headers;
type: ResponseType;
url: string;
redirected: boolean;
raw: Response;
requestParams: PromiseRequestParams<Record<string, unknown>>;
predicted: boolean;
}
| {
code: "200";
information: "users.create";
body: {
id: number;
name: string;
age: number;
};
ok: boolean | null;
headers: Headers;
type: ResponseType;
url: string;
redirected: boolean;
raw: Response;
requestParams: PromiseRequestParams<Record<string, unknown>>;
predicted: boolean;
}
| NotPredictedClientResponse<
PromiseRequestParams<Record<string, unknown>>
>
>,
"strict"
>;

expect(result).toStrictEqual(
E.right(
"response",
expect.objectContaining({
url: "http://localhost:8946/users",
information: "users.create",
body: {
id: 5,
name: "math",
age: 23,
},
predicted: true,
}),
),
);
});
});
11 changes: 11 additions & 0 deletions integration/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.test.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@core": ["../core/index.ts"]
},
"types": ["vitest/globals", "node", "web"]
},
"include": ["**/*.ts", "../core/**/*.ts"],
}
33 changes: 1 addition & 32 deletions integration/codeGenerator/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`codeGenerator > correct generate file 1`] = `
"export type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";

export type ResponseCode = \`1\${Digit}\${Digit}\` | \`2\${Digit}\${Digit}\` | \`3\${Digit}\${Digit}\` | \`4\${Digit}\${Digit}\` | \`5\${Digit}\${Digit}\`;

export type Routes = {
"export type Routes = {
method: "GET";
path: "/users";
responses: {
code: ResponseCode;
information: string;
body: unknown;
fromHook: true;
} | {
code: \`5\${Digit}\${Digit}\`;
information: string;
body: unknown;
} | {
code: "200";
information: "users.findMany";
body: {
Expand All @@ -33,15 +20,6 @@ export type Routes = {
userId: number;
};
responses: {
code: ResponseCode;
information: string;
body: unknown;
fromHook: true;
} | {
code: \`5\${Digit}\${Digit}\`;
information: string;
body: unknown;
} | {
code: "422";
information: "extract-error";
body?: undefined;
Expand All @@ -63,15 +41,6 @@ export type Routes = {
age: number;
};
responses: {
code: ResponseCode;
information: string;
body: unknown;
fromHook: true;
} | {
code: \`5\${Digit}\${Digit}\`;
information: string;
body: unknown;
} | {
code: "422";
information: "extract-error";
body?: undefined;
Expand Down
2 changes: 1 addition & 1 deletion integration/codeGenerator/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hub } from "@core";
import { existsSync, readFileSync, rmSync } from "fs";
import { launchHookServer } from "../../dist/core";
import { codeGeneratorPlugin } from "@duplojs/http/codeGenerator";
import { launchHookServer } from "@duplojs/http";

describe("codeGenerator", () => {
const fileName = `${import.meta.dirname}/generateCode.generate.ts`;
Expand Down
1 change: 0 additions & 1 deletion integration/core/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const user = DPE.object({
id: DPE.number(),
name: DPE.string(),
age: DPE.number(),

});

useRouteBuilder("GET", "/users")
Expand Down
Loading
Loading