generated from DCKT/rescript-bindings-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
200 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ test/bundle/*.js | |
test/bundle/*.js.gz | ||
promise-* | ||
_release | ||
|
||
*.mjs |
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 |
---|---|---|
@@ -1 +1,33 @@ | ||
# rescript-bindings-template | ||
# rescript-ky | ||
|
||
ReScript bindings for ky HTTP client | ||
|
||
## Setup | ||
|
||
1. Install the module | ||
|
||
```bash | ||
bun install @dck/rescript-ky | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
yarn install @dck/rescript-ky | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
npm install @dck/rescript-ky | ||
``` | ||
|
||
2. Add it to your `rescript.json` config | ||
|
||
```json | ||
{ | ||
"bsc-flags": ["@dck/rescript-ky"] | ||
} | ||
``` | ||
|
||
## API |
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 was deleted.
Oops, something went wrong.
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,61 @@ | ||
type request | ||
type response<'data> = {json: unit => promise<'data>, status: int, url: string, ok: bool} | ||
type error<'data> = {response: option<response<'data>>, request: option<request>, name: string} | ||
|
||
type httpMethod = | ||
| GET | ||
| POST | ||
| PUT | ||
| HEAD | ||
| DELETE | ||
| PATCH | ||
|
||
type retryMethod = | ||
| GET | ||
| PUT | ||
| HEAD | ||
| DELETE | ||
| OPTIONS | ||
| TRACE | ||
|
||
type retryOptions = { | ||
limit?: int, | ||
methods?: array<retryMethod>, | ||
statusCodes?: array<int>, | ||
backoffLimit?: int, | ||
delay?: int => float, | ||
} | ||
|
||
type retryCallbackParams = { | ||
request: request, | ||
retryCount: int, | ||
} | ||
|
||
type beforeRequestCallback = request => unit | ||
type beforeRetryCallback = retryCallbackParams => unit | ||
type beforeErrorCallback<'data> = error<'data> => unit | ||
|
||
type hooks<'errorData> = { | ||
beforeRequest?: array<beforeRequestCallback>, | ||
beforeRetry?: array<beforeRetryCallback>, | ||
beforeError?: array<beforeErrorCallback<'errorData>>, | ||
} | ||
|
||
@unboxed | ||
type retry = | ||
| Int(int) | ||
| Options(retryOptions) | ||
|
||
type requestOptions<'json, 'searchParams, 'errorData> = { | ||
prefixUrl?: string, | ||
method?: httpMethod, | ||
json?: 'json, | ||
searchParams?: 'searchParams, | ||
retry?: retry, | ||
timeout?: int, | ||
hooks?: hooks<'errorData>, | ||
} | ||
|
||
@module("ky") | ||
external fetch: (string, requestOptions<'json, 'searchParams, 'errorData>) => response<'data> = | ||
"default" |
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,46 @@ | ||
type expect<'a> = {toBe: 'a} | ||
@module("bun:test") | ||
external expect: 'a => expect<'b> = "expect" | ||
@module("bun:test") | ||
external test: (string, unit => unit) => unit = "test" | ||
@module("bun:test") | ||
external testAsync: (string, unit => promise<unit>) => unit = "test" | ||
|
||
type mock = { | ||
mockInstance: unit => unit, | ||
path: string, | ||
} | ||
|
||
@module("./mock.ts") | ||
external mockBasePath: string = "mockBasePath" | ||
|
||
@module("./mock.ts") | ||
external initMockServer: unit => unit = "initMockServer" | ||
|
||
initMockServer() | ||
|
||
testAsync("Simple fetch", async () => { | ||
let response = await Ky.fetch("", {prefixUrl: mockBasePath, method: GET}).json() | ||
|
||
expect(response["test"]).toBe(1) | ||
}) | ||
|
||
testAsync("Custom retry", async () => { | ||
let response = await Ky.fetch( | ||
`retry`, | ||
{prefixUrl: mockBasePath, method: GET, retry: Int(1)}, | ||
).json() | ||
|
||
expect(response["retryCount"]).toBe(1) | ||
}) | ||
|
||
testAsync("Custom timeout", async () => { | ||
try { | ||
await Ky.fetch(`timeout`, {prefixUrl: mockBasePath, method: GET, timeout: 100}).json() | ||
} catch { | ||
| JsError(err) => { | ||
let err: Ky.error<unit> = err->Obj.magic | ||
expect(err.name).toBe("TimeoutError") | ||
} | ||
} | ||
}) |
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,28 @@ | ||
export const mockBasePath = "http://localhost:3000"; | ||
|
||
let retry = 0; | ||
|
||
function wait(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
export const initMockServer = () => | ||
Bun.serve({ | ||
async fetch(req) { | ||
const url = new URL(req.url); | ||
if (url.pathname === "/") return Response.json({ test: 1 }); | ||
if (url.pathname === "/timeout") { | ||
await wait(2000); | ||
return Response.json({ test: 1 }); | ||
} | ||
if (url.pathname === "/retry") { | ||
if (retry === 0) { | ||
retry = retry + 1; | ||
return new Response("busy !", { status: 429 }); | ||
} else { | ||
return Response.json({ retryCount: retry }); | ||
} | ||
} | ||
return new Response("404!", { status: 404 }); | ||
}, | ||
}); |