-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to build custom endpoint url #2
- Loading branch information
Showing
7 changed files
with
108 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fetchMock from "fetch-mock" | ||
import { test } from "uvu" | ||
import { equal } from "uvu/assert" | ||
import { ApiClient } from "../src/_template" | ||
|
||
const t = async <T>(body: T) => { | ||
try { | ||
const baseUrl = "http://localhost" | ||
const client = new ApiClient({ baseUrl }) | ||
fetchMock.mock(`${baseUrl}`, { body: JSON.stringify(body) }) | ||
return client.Fetch<T>("get", "/") | ||
} finally { | ||
fetchMock.restore() | ||
} | ||
} | ||
|
||
test("apiClient dates parsing", async () => { | ||
const date = new Date("2021-01-01T00:00:00Z") | ||
const dateStr = date.toISOString() | ||
|
||
const rs = await t({ date, dates: [date], more: { date, dates: [date] } }) | ||
equal(rs.date.toISOString(), dateStr) | ||
equal(rs.dates[0].toISOString(), dateStr) | ||
equal(rs.more.date.toISOString(), dateStr) | ||
equal(rs.more.dates[0].toISOString(), dateStr) | ||
}) | ||
|
||
test("apiClient base url", async () => { | ||
const c1 = new ApiClient({ baseUrl: "http://localhost" }) | ||
equal(c1.PrepareFetchUrl("/").toString(), "http://localhost/") | ||
equal(c1.PrepareFetchUrl("/api/v1/cats").toString(), "http://localhost/api/v1/cats") | ||
|
||
const c2 = new ApiClient({ baseUrl: "https://example.com" }) | ||
equal(c2.PrepareFetchUrl("/").toString(), "https://example.com/") | ||
equal(c2.PrepareFetchUrl("/api/v1/cats").toString(), "https://example.com/api/v1/cats") | ||
|
||
class MyClient extends ApiClient { | ||
PrepareFetchUrl(path: string) { | ||
return new URL(`${this.Config.baseUrl}/${path}`.replace(/\/{2,}/g, "/")) | ||
} | ||
} | ||
|
||
const c3 = new MyClient({ baseUrl: "https://example.com/api/v1" }) | ||
equal(c3.PrepareFetchUrl("/").toString(), "https://example.com/api/v1/") | ||
equal(c3.PrepareFetchUrl("/cats").toString(), "https://example.com/api/v1/cats") | ||
}) | ||
|
||
test.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters