@crvouga/mockingbird-adapter-bun 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# @crvouga/mockingbird-adapter-bun
|
|
2
|
+
|
|
3
|
+
Serve any Mockingbird `FetchAPI` (a provider mock such as `StripeAPI`, or your own) as a real HTTP
|
|
4
|
+
server with `Bun.serve`. Use it on Bun when the code under test needs a URL rather than an injected
|
|
5
|
+
`fetch`. On Node, use `@crvouga/mockingbird-adapter-node` instead.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @crvouga/mockingbird-adapter-bun
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Bun >=1.2 only: the runtime code calls the global `Bun.serve`. ESM only.
|
|
14
|
+
|
|
15
|
+
The shipped `.d.ts` references `Bun` types, so TypeScript projects also need `@types/bun` (declared
|
|
16
|
+
as an optional peer dependency) and `"types": ["bun"]` if your tsconfig restricts `types`:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install -D @types/bun
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { serve } from "@crvouga/mockingbird-adapter-bun"
|
|
26
|
+
import type { FetchAPI } from "@crvouga/mockingbird-core"
|
|
27
|
+
|
|
28
|
+
// Any FetchAPI works, e.g. `new StripeAPI()` from @crvouga/mockingbird-service-stripe.
|
|
29
|
+
const api: FetchAPI = {
|
|
30
|
+
fetch: async (request) => Response.json({ method: request.method, url: request.url }),
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Port defaults to 0: the OS picks a free port. `serve` is synchronous.
|
|
34
|
+
const server = serve(api, { hostname: "127.0.0.1" })
|
|
35
|
+
const baseUrl = `http://127.0.0.1:${server.port}`
|
|
36
|
+
|
|
37
|
+
const response = await fetch(`${baseUrl}/v1/customers`)
|
|
38
|
+
console.log(await response.json())
|
|
39
|
+
|
|
40
|
+
server.stop() // e.g. in afterAll
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
| Export | Signature | Description |
|
|
46
|
+
| --- | --- | --- |
|
|
47
|
+
| `serve` | `(api: FetchAPI, options?: BunServeOptions) => BunAdapterServer` | Start `Bun.serve` with `fetch: (request) => api.fetch(request)`. |
|
|
48
|
+
|
|
49
|
+
Types:
|
|
50
|
+
|
|
51
|
+
- `BunServeOptions`: `{ port?: number; hostname?: string }`. `port` defaults to `0` (ephemeral; read
|
|
52
|
+
it from `server.port`); `hostname` is only passed when set.
|
|
53
|
+
- `BunAdapterServer`: `ReturnType<typeof Bun.serve>`, the Bun `Server` (`port`, `url`, `stop()`).
|
|
54
|
+
|
|
55
|
+
Requests and responses pass through unchanged (Bun is Fetch-native), so streaming bodies work.
|
|
56
|
+
|
|
57
|
+
## Related
|
|
58
|
+
|
|
59
|
+
- `@crvouga/mockingbird-core`: the `FetchAPI` contract.
|
|
60
|
+
- `@crvouga/mockingbird-adapter-node`: the same adapter for `node:http`.
|
|
61
|
+
|
|
62
|
+
Part of [mockingbird](https://github.com/crvouga/mockingbird) — agent integration guide: [`@crvouga/mockingbird`](https://github.com/crvouga/mockingbird/tree/main/packages/facade#readme).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FetchAPI } from "@crvouga/mockingbird-core";
|
|
2
|
+
/** Options for {@link serve}. */
|
|
3
|
+
export type BunServeOptions = {
|
|
4
|
+
port?: number;
|
|
5
|
+
hostname?: string;
|
|
6
|
+
};
|
|
7
|
+
export type BunAdapterServer = ReturnType<typeof Bun.serve>;
|
|
8
|
+
/**
|
|
9
|
+
* Serve any Mockingbird {@link FetchAPI} over `Bun.serve`.
|
|
10
|
+
* Port defaults to `0`, so the OS assigns an ephemeral port (read from `server.port`).
|
|
11
|
+
*/
|
|
12
|
+
export declare const serve: (api: FetchAPI, options?: BunServeOptions) => BunAdapterServer;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AAEzD,iCAAiC;AACjC,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,GAAG,CAAC,KAAK,CAAC,CAAA;AAE3D;;;GAGG;AACH,eAAO,MAAM,KAAK,GAAI,KAAK,QAAQ,EAAE,UAAS,eAAoB,KAAG,gBAMpE,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serve any Mockingbird {@link FetchAPI} over `Bun.serve`.
|
|
3
|
+
* Port defaults to `0`, so the OS assigns an ephemeral port (read from `server.port`).
|
|
4
|
+
*/
|
|
5
|
+
export const serve = (api, options = {}) => {
|
|
6
|
+
return Bun.serve({
|
|
7
|
+
port: options.port ?? 0,
|
|
8
|
+
...(options.hostname !== undefined ? { hostname: options.hostname } : {}),
|
|
9
|
+
fetch: (request) => api.fetch(request),
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAUA;;;GAGG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,GAAa,EAAE,UAA2B,EAAE,EAAoB,EAAE;IACtF,OAAO,GAAG,CAAC,KAAK,CAAC;QACf,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,CAAC;QACvB,GAAG,CAAC,OAAO,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACzE,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC;KACvC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@crvouga/mockingbird-adapter-bun",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Serve any Mockingbird FetchAPI with Bun.serve.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public",
|
|
20
|
+
"provenance": true
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/crvouga/mockingbird.git",
|
|
25
|
+
"directory": "packages/adapters/bun"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/crvouga/mockingbird/tree/main/packages/adapters/bun#readme",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"mockingbird",
|
|
30
|
+
"adapter",
|
|
31
|
+
"bun"
|
|
32
|
+
],
|
|
33
|
+
"mockingbird": {
|
|
34
|
+
"runtime": "bun",
|
|
35
|
+
"layer": "adapter"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.build.json",
|
|
39
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
40
|
+
"lint": "biome check .",
|
|
41
|
+
"test": "bun test",
|
|
42
|
+
"portability": "bun ../../../scripts/portability.ts",
|
|
43
|
+
"pack:check": "bun ../../../scripts/pack-check.ts"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@crvouga/mockingbird-core": "0.0.0-development"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"fast-check": "4.9.0",
|
|
50
|
+
"@crvouga/mockingbird-testing": "0.1.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@types/bun": ">=1.2.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependenciesMeta": {
|
|
56
|
+
"@types/bun": {
|
|
57
|
+
"optional": true
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|