|
1 |
| -import Hapi, { type Request } from "@hapi/hapi"; |
2 |
| -import Joi from "joi"; |
3 | 1 | import { test, type TestContext } from "node:test";
|
4 |
| -import { Client } from "../lib/index.ts"; |
| 2 | +import { server, Client } from "./fixture.ts"; |
| 3 | +import type { RouteMap } from "./generated.ts"; |
5 | 4 |
|
6 |
| -const expectType = <T>(_: T): void => void 0; |
7 |
| -type TypeEqual<Target, Value> = (<T>() => T extends Target ? 1 : 2) extends <T>() => T extends Value ? 1 : 2 ? true : false; |
| 5 | +await test("GET - server.inject", async (t) => { |
| 6 | + const client = new Client({ server }); |
8 | 7 |
|
9 |
| -await test("server.inject", async (t) => { |
10 |
| - await t.test("can send strongly typed requests", async (t: TestContext) => { |
11 |
| - const server = Hapi.server(); |
12 |
| - server.route({ |
13 |
| - method: "GET", |
14 |
| - path: "/test", |
15 |
| - options: { |
16 |
| - validate: { |
17 |
| - query: Joi.object({ |
18 |
| - success: Joi.boolean().default(true), |
19 |
| - }), |
20 |
| - }, |
21 |
| - response: { |
22 |
| - status: { |
23 |
| - 200: Joi.object({ success: Joi.boolean() }), |
24 |
| - }, |
25 |
| - }, |
26 |
| - handler(req: Request<{ Query: { success: boolean } }>) { |
27 |
| - return { success: req.query.success }; |
28 |
| - }, |
29 |
| - }, |
30 |
| - }); |
31 |
| - |
32 |
| - type TestRoutes = { |
33 |
| - GET: { |
34 |
| - "/test": { |
35 |
| - query?: { |
36 |
| - success?: boolean; |
37 |
| - }; |
38 |
| - response: { |
39 |
| - success: boolean; |
40 |
| - }; |
41 |
| - }; |
42 |
| - }; |
43 |
| - }; |
44 |
| - |
45 |
| - const client = new Client<TestRoutes>({ server }); |
46 |
| - |
47 |
| - type availablePaths = Parameters<typeof client.get>[0]; |
48 |
| - type expectedPaths = "/test"; |
49 |
| - expectType<TypeEqual<availablePaths, expectedPaths>>(true); |
50 |
| - |
51 |
| - type availableOptions = Parameters<typeof client.get>[1]; |
52 |
| - type expectedOptions = { |
53 |
| - headers?: HeadersInit; |
54 |
| - params?: never; |
55 |
| - query?: { |
56 |
| - success?: boolean; |
57 |
| - }; |
58 |
| - payload?: never; |
59 |
| - }; |
60 |
| - expectType<TypeEqual<availableOptions, expectedOptions>>(true); |
61 |
| - |
62 |
| - const res = await client.get("/test"); |
| 8 | + await t.test("/simple", async (t: TestContext) => { |
| 9 | + const res = await client.get("/simple"); |
63 | 10 | t.assert.equal(res.status, 200);
|
64 |
| - t.assert.equal(res.url, "/test"); |
| 11 | + t.assert.equal(res.url, "/simple"); |
| 12 | + t.assert.deepStrictEqual<RouteMap["GET"]["/simple"]["response"]>(res.data, { success: true }); |
| 13 | + }); |
65 | 14 |
|
66 |
| - expectType<{ success: boolean }>(res.data); |
67 |
| - t.assert.deepStrictEqual(res.data, { success: true }); |
| 15 | + await t.test("/query", async (t: TestContext) => { |
| 16 | + const withoutQueryRes = await client.get("/query"); |
| 17 | + t.assert.equal(withoutQueryRes.status, 200); |
| 18 | + t.assert.equal(withoutQueryRes.url, "/query"); |
| 19 | + t.assert.deepStrictEqual<RouteMap["GET"]["/query"]["response"]>(withoutQueryRes.data, { flag: false }); |
| 20 | + |
| 21 | + const query: RouteMap["GET"]["/query"]["query"] = { flag: true }; |
| 22 | + const withQueryRes = await client.get("/query", { query }); |
| 23 | + t.assert.equal(withQueryRes.status, 200); |
| 24 | + t.assert.equal(withQueryRes.url, "/query?flag=true"); |
| 25 | + t.assert.deepStrictEqual<RouteMap["GET"]["/query"]["response"]>(withQueryRes.data, { flag: true }); |
68 | 26 | });
|
69 | 27 | });
|
0 commit comments