@laphilosophia/api-tape 1.0.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/LICENSE +21 -0
- package/README.md +94 -0
- package/dist/index.js +118 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Erdem Arslan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# πΌ API Tape
|
|
2
|
+
|
|
3
|
+
**Record and Replay HTTP API responses for offline development.**
|
|
4
|
+
|
|
5
|
+
API Tape is a zero-config CLI tool that acts as a transparent HTTP proxy. It records API responses to local JSON files ("tapes") and replays them instantlyβperfect for offline development, flaky API testing, and reproducible demos.
|
|
6
|
+
|
|
7
|
+
## β¨ Features
|
|
8
|
+
|
|
9
|
+
- π¬ **Record Mode** β Proxies requests to your target API and saves responses
|
|
10
|
+
- π **Replay Mode** β Serves cached responses instantly from disk
|
|
11
|
+
- π¦ **Zero Config** β Works out of the box with sensible defaults
|
|
12
|
+
- π **Binary Safe** β Handles images, compressed responses, and any content type
|
|
13
|
+
- π·οΈ **Replay Header** β Responses include `X-Api-Tape: Replayed` for easy debugging
|
|
14
|
+
|
|
15
|
+
## π¦ Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install -g api-tape
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or use it directly with npx:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx api-tape --target "https://api.example.com" --mode record
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## π Quick Start
|
|
28
|
+
|
|
29
|
+
### Step 1: Record API Responses
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
tape --target "https://jsonplaceholder.typicode.com" --mode record
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
In another terminal:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
curl http://localhost:8080/todos/1
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
You'll see `β RECORD GET /todos/1` in the terminal and a new tape file in `./tapes/`.
|
|
42
|
+
|
|
43
|
+
### Step 2: Replay Offline
|
|
44
|
+
|
|
45
|
+
Stop the server and restart in replay mode:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
tape --target "https://jsonplaceholder.typicode.com" --mode replay
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
curl http://localhost:8080/todos/1
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
You'll see `βΊ REPLAY GET /todos/1` β the response comes from disk, no network needed!
|
|
56
|
+
|
|
57
|
+
## βοΈ CLI Options
|
|
58
|
+
|
|
59
|
+
| Option | Description | Default |
|
|
60
|
+
|--------|-------------|---------|
|
|
61
|
+
| `-t, --target <url>` | Target API URL **(required)** | β |
|
|
62
|
+
| `-m, --mode <mode>` | Operation mode: `record` or `replay` | `replay` |
|
|
63
|
+
| `-p, --port <number>` | Local server port | `8080` |
|
|
64
|
+
| `-d, --dir <path>` | Directory to save tapes | `./tapes` |
|
|
65
|
+
|
|
66
|
+
## π Tape Format
|
|
67
|
+
|
|
68
|
+
Each tape is a JSON file named with an MD5 hash of `METHOD|URL`:
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"meta": {
|
|
73
|
+
"url": "/todos/1",
|
|
74
|
+
"method": "GET",
|
|
75
|
+
"timestamp": "2026-01-14T19:12:39.000Z"
|
|
76
|
+
},
|
|
77
|
+
"statusCode": 200,
|
|
78
|
+
"headers": { ... },
|
|
79
|
+
"body": "eyJ1c2VySWQiOjEsImlkIjoxLC..."
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The body is base64-encoded for binary safety.
|
|
84
|
+
|
|
85
|
+
## π― Use Cases
|
|
86
|
+
|
|
87
|
+
- **Offline Development** β Work without internet or VPN
|
|
88
|
+
- **Flaky API Testing** β Eliminate network inconsistencies in tests
|
|
89
|
+
- **Demo Environments** β Reproducible API responses for presentations
|
|
90
|
+
- **Rate Limit Bypass** β Develop against recorded responses
|
|
91
|
+
|
|
92
|
+
## π License
|
|
93
|
+
|
|
94
|
+
MIT Β© [Erdem Arslan](https://github.com/laphilosophia)
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var import_chalk = __toESM(require("chalk"));
|
|
28
|
+
var import_commander = require("commander");
|
|
29
|
+
var import_crypto = __toESM(require("crypto"));
|
|
30
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
31
|
+
var import_http = __toESM(require("http"));
|
|
32
|
+
var import_http_proxy = __toESM(require("http-proxy"));
|
|
33
|
+
var import_path = __toESM(require("path"));
|
|
34
|
+
import_commander.program.name("api-tape").description("Record and Replay HTTP API responses for offline development.").requiredOption("-t, --target <url>", "Target API URL (e.g., https://api.github.com)").option("-p, --port <number>", "Local server port", "8080").option("-m, --mode <mode>", 'Operation mode: "record" or "replay"', "replay").option("-d, --dir <path>", "Directory to save tapes", "./tapes").version("1.0.0").parse();
|
|
35
|
+
var opts = import_commander.program.opts();
|
|
36
|
+
var TARGET_URL = opts.target;
|
|
37
|
+
var PORT = parseInt(opts.port);
|
|
38
|
+
var MODE = opts.mode;
|
|
39
|
+
var TAPES_DIR = import_path.default.resolve(opts.dir);
|
|
40
|
+
var proxy = import_http_proxy.default.createProxyServer({
|
|
41
|
+
target: TARGET_URL,
|
|
42
|
+
changeOrigin: true,
|
|
43
|
+
selfHandleResponse: true
|
|
44
|
+
});
|
|
45
|
+
import_fs_extra.default.ensureDirSync(TAPES_DIR);
|
|
46
|
+
var getTapeKey = (req) => {
|
|
47
|
+
const key = `${req.method}|${req.url}`;
|
|
48
|
+
return import_crypto.default.createHash("md5").update(key).digest("hex");
|
|
49
|
+
};
|
|
50
|
+
var timestamp = () => import_chalk.default.gray(`[${(/* @__PURE__ */ new Date()).toLocaleTimeString()}]`);
|
|
51
|
+
var server = import_http.default.createServer((req, res) => {
|
|
52
|
+
const tapeKey = getTapeKey(req);
|
|
53
|
+
const tapePath = import_path.default.join(TAPES_DIR, `${tapeKey}.json`);
|
|
54
|
+
if (MODE === "replay") {
|
|
55
|
+
if (import_fs_extra.default.existsSync(tapePath)) {
|
|
56
|
+
try {
|
|
57
|
+
const tape = import_fs_extra.default.readJsonSync(tapePath);
|
|
58
|
+
Object.keys(tape.headers).forEach((key) => {
|
|
59
|
+
res.setHeader(key, tape.headers[key]);
|
|
60
|
+
});
|
|
61
|
+
res.setHeader("X-Api-Tape", "Replayed");
|
|
62
|
+
res.writeHead(tape.statusCode);
|
|
63
|
+
res.end(Buffer.from(tape.body, "base64"));
|
|
64
|
+
console.log(`${timestamp()} ${import_chalk.default.green("\u21BA REPLAY")} ${req.method} ${req.url}`);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
console.error(import_chalk.default.red("Corrupted Tape:"), tapePath);
|
|
67
|
+
res.statusCode = 500;
|
|
68
|
+
res.end("Corrupted Tape");
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
console.log(`${timestamp()} ${import_chalk.default.red("\u2718 MISSING")} ${req.method} ${req.url}`);
|
|
72
|
+
res.statusCode = 404;
|
|
73
|
+
res.end(`Tape not found for: ${req.method} ${req.url}`);
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (MODE === "record") {
|
|
78
|
+
console.log(`${timestamp()} ${import_chalk.default.blue("\u25CF RECORD")} ${req.method} ${req.url}`);
|
|
79
|
+
proxy.web(req, res, {}, (e) => {
|
|
80
|
+
console.error(import_chalk.default.red("Proxy Error:"), e.message);
|
|
81
|
+
res.statusCode = 502;
|
|
82
|
+
res.end("Proxy Error");
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
proxy.on("proxyRes", (proxyRes, req, res) => {
|
|
87
|
+
const bodyChunks = [];
|
|
88
|
+
proxyRes.on("data", (chunk) => bodyChunks.push(chunk));
|
|
89
|
+
proxyRes.on("end", () => {
|
|
90
|
+
const bodyBuffer = Buffer.concat(bodyChunks);
|
|
91
|
+
const tapeData = {
|
|
92
|
+
meta: {
|
|
93
|
+
url: req.url,
|
|
94
|
+
method: req.method,
|
|
95
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
96
|
+
},
|
|
97
|
+
statusCode: proxyRes.statusCode,
|
|
98
|
+
headers: proxyRes.headers,
|
|
99
|
+
body: bodyBuffer.toString("base64")
|
|
100
|
+
};
|
|
101
|
+
const tapeKey = getTapeKey(req);
|
|
102
|
+
const tapePath = import_path.default.join(TAPES_DIR, `${tapeKey}.json`);
|
|
103
|
+
import_fs_extra.default.writeJsonSync(tapePath, tapeData, { spaces: 2 });
|
|
104
|
+
Object.keys(proxyRes.headers).forEach((key) => {
|
|
105
|
+
res.setHeader(key, proxyRes.headers[key]);
|
|
106
|
+
});
|
|
107
|
+
res.writeHead(proxyRes.statusCode || 200);
|
|
108
|
+
res.end(bodyBuffer);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
console.log(import_chalk.default.bold(`
|
|
112
|
+
\u{1F4FC} API Tape Running`));
|
|
113
|
+
console.log(` ${import_chalk.default.dim("Mode:")} ${MODE === "record" ? import_chalk.default.red("\u25CF RECORD") : import_chalk.default.green("\u21BA REPLAY")}`);
|
|
114
|
+
console.log(` ${import_chalk.default.dim("Target:")} ${TARGET_URL}`);
|
|
115
|
+
console.log(` ${import_chalk.default.dim("Port:")} http://localhost:${PORT}`);
|
|
116
|
+
console.log(` ${import_chalk.default.dim("Dir:")} ${TAPES_DIR}
|
|
117
|
+
`);
|
|
118
|
+
server.listen(PORT);
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@laphilosophia/api-tape",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Record and Replay HTTP API responses for offline development",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"tape": "./dist/index.js",
|
|
8
|
+
"api-tape": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsup src/index.ts --format cjs --clean",
|
|
18
|
+
"prepublishOnly": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"api",
|
|
22
|
+
"mock",
|
|
23
|
+
"record",
|
|
24
|
+
"replay",
|
|
25
|
+
"proxy",
|
|
26
|
+
"offline",
|
|
27
|
+
"testing",
|
|
28
|
+
"http",
|
|
29
|
+
"vcr"
|
|
30
|
+
],
|
|
31
|
+
"author": "Erdem Arslan",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/laphilosophia/api-tape.git"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/laphilosophia/api-tape#readme",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/laphilosophia/api-tape/issues"
|
|
40
|
+
},
|
|
41
|
+
"type": "commonjs",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"chalk": "^5.6.2",
|
|
44
|
+
"commander": "^14.0.2",
|
|
45
|
+
"fs-extra": "^11.3.3",
|
|
46
|
+
"http-proxy": "^1.18.1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/fs-extra": "^11.0.4",
|
|
50
|
+
"@types/http-proxy": "^1.17.17",
|
|
51
|
+
"@types/node": "^25.0.8",
|
|
52
|
+
"tsup": "^8.5.1",
|
|
53
|
+
"typescript": "^5.9.3"
|
|
54
|
+
}
|
|
55
|
+
}
|