go-mokapi 0.30.1 → 0.37.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 +117 -73
- package/bin/mokapi.js +54 -27
- package/package.json +15 -13
- package/types/file.d.ts +50 -0
- package/types/index.d.ts +172 -18
- package/dist/mokapi-darwin-arm64/mokapi +0 -0
- package/dist/mokapi-linux-amd64/mokapi +0 -0
- package/dist/mokapi-windows-amd64/mokapi.exe +0 -0
package/README.md
CHANGED
|
@@ -3,122 +3,166 @@
|
|
|
3
3
|
<img src="https://raw.githubusercontent.com/marle3003/mokapi/refs/heads/main/logo.svg" alt="Mokapi" title="Mokapi" width="300" />
|
|
4
4
|
</a>
|
|
5
5
|
</p>
|
|
6
|
+
<h3 align="center">Mock APIs Across Protocols. Test Faster. Ship Better.</h3>
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/go-mokapi"><img src="https://img.shields.io/npm/v/go-mokapi.svg" alt="npm version"></a>
|
|
9
|
+
<a href="https://github.com/marle3003/mokapi/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License"></a>
|
|
10
|
+
<a href="https://github.com/marle3003/mokapi"><img src="https://img.shields.io/github/stars/marle3003/mokapi?style=social" alt="GitHub stars"></a>
|
|
11
|
+
</p>
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
## What is Mokapi?
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
Mokapi is an open-source API mocking tool that lets you develop and test without waiting
|
|
16
|
+
for backends. Mock REST APIs, Kafka topics, LDAP directories, and SMTP servers using
|
|
17
|
+
OpenAPI and AsyncAPI specifications.
|
|
10
18
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
the risk of bugs in production.
|
|
19
|
+
Perfect for:
|
|
20
|
+
- Frontend developers building UIs before backends exist
|
|
21
|
+
- QA teams testing edge cases, errors, and timeouts
|
|
22
|
+
- DevOps engineers running reliable CI/CD tests without external dependencies
|
|
23
|
+
- API designers prototyping and validating contracts early
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
## Quick Start
|
|
19
26
|
|
|
20
|
-
|
|
21
|
-
- **Everything as Code**: Reusing, version control, consistency and integrate mocks with your CI/CD.
|
|
22
|
-
- **An embedded JavaScript engine** to control everything - status, headers, delays, errors or other edge cases.
|
|
23
|
-
- **Patch Configuration** changes for mocking needs, rather than changing the original contract
|
|
24
|
-
- **Multiple Provider support**: File, HTTP, GIT, NPM to gather configurations and scripts.
|
|
25
|
-
- **Dashboard** to see what's going on.
|
|
27
|
+
Try Instantly
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
```
|
|
30
|
+
npx go-mokapi https://petstore3.swagger.io/api/v3/openapi.json
|
|
31
|
+
```
|
|
28
32
|
|
|
29
|
-
|
|
30
|
-
Replace the URL with your own OpenAPI or AsyncAPI specification.
|
|
33
|
+
Then test your mock:
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
choco install mokapi
|
|
35
|
-
mokapi https://petstore31.swagger.io/api/v31/openapi.json
|
|
35
|
+
```
|
|
36
|
+
curl http://localhost/api/v3/pet/1 -H 'Accept: application/json'
|
|
36
37
|
```
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
Install Globally
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
npm install -g go-mokapi
|
|
43
|
+
mokapi https://petstore3.swagger.io/api/v3/openapi.json
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Other Installation Methods
|
|
47
|
+
|
|
48
|
+
Homebrew (macOS/Linux):
|
|
49
|
+
```
|
|
50
|
+
brew tap marle3003/tap
|
|
41
51
|
brew install mokapi
|
|
42
|
-
mokapi https://petstore31.swagger.io/api/v31/openapi.json
|
|
43
52
|
```
|
|
44
53
|
|
|
45
|
-
|
|
46
|
-
```
|
|
47
|
-
|
|
54
|
+
Chocolatey (Windows):
|
|
55
|
+
```yaml
|
|
56
|
+
choco install mokapi
|
|
48
57
|
```
|
|
49
58
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
Docker:
|
|
60
|
+
```
|
|
61
|
+
docker run -p 80:80 -p 8080:8080 mokapi/mokapi:latest \
|
|
62
|
+
https://petstore3.swagger.io/api/v3/openapi.json
|
|
54
63
|
```
|
|
55
64
|
|
|
56
|
-
|
|
65
|
+
## Key Features
|
|
66
|
+
|
|
67
|
+
### Multi-Protocol Support
|
|
68
|
+
Mock HTTP/HTTPS, Apache Kafka, LDAP, and SMTP — all from a single tool.
|
|
57
69
|
|
|
58
|
-
|
|
70
|
+
### Specification-Driven
|
|
71
|
+
Uses OpenAPI and AsyncAPI specs as the source of truth. Your mocks stay aligned with your API contracts.
|
|
59
72
|
|
|
60
|
-
|
|
73
|
+
### Dynamic Behavior with JavaScript
|
|
61
74
|
|
|
62
|
-
|
|
63
|
-
|
|
75
|
+
Control responses, simulate errors, add delays, or create complex workflows using embedded JavaScript:
|
|
76
|
+
```javascript
|
|
77
|
+
import { on } from 'mokapi'
|
|
64
78
|
|
|
65
79
|
export default function() {
|
|
66
|
-
on('http', (request, response)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
case 9:
|
|
72
|
-
response.statusCode = 404;
|
|
80
|
+
on('http', (request, response) => {
|
|
81
|
+
// Return 404 for specific IDs
|
|
82
|
+
if (request.path.petId === '999') {
|
|
83
|
+
response.statusCode = 404
|
|
84
|
+
return
|
|
73
85
|
}
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
|
|
87
|
+
// Customize response data
|
|
88
|
+
response.data.name = 'Custom Pet Name'
|
|
89
|
+
})
|
|
76
90
|
}
|
|
77
91
|
```
|
|
78
92
|
|
|
79
|
-
|
|
93
|
+
### Everything as Code
|
|
94
|
+
Version control your mocks alongside your code. Run them in CI/CD pipelines. No UI configuration required.
|
|
80
95
|
|
|
81
|
-
|
|
96
|
+
### Configuration Patching
|
|
97
|
+
Override parts of your OpenAPI spec without modifying the original file. Perfect for testing different scenarios.
|
|
82
98
|
|
|
99
|
+
### Built-in Dashboard
|
|
100
|
+
Visualize requests, responses, and logs in real-time at http://localhost:8080
|
|
83
101
|
<img src="https://raw.githubusercontent.com/marle3003/mokapi/refs/heads/main/webui.png" alt="Mokapi Web UI" title="Mokapi Web UI" />
|
|
84
102
|
|
|
85
|
-
|
|
103
|
+
### Multiple Providers
|
|
104
|
+
Load specs from local files, HTTP URLs, Git repositories, or NPM packages.
|
|
105
|
+
|
|
106
|
+
## Common Use Cases
|
|
107
|
+
|
|
108
|
+
### Frontend Development
|
|
109
|
+
Mock backend APIs while building UIs. Test loading states, errors, and edge cases without waiting for real endpoints.
|
|
110
|
+
|
|
111
|
+
### API Testing
|
|
112
|
+
Simulate timeouts, 500 errors, rate limits, and malformed responses. Test how your application handles failures.
|
|
113
|
+
|
|
114
|
+
### CI/CD Integration
|
|
115
|
+
Run fast, reliable tests without external dependencies. No flaky tests due to network issues or unavailable services.
|
|
116
|
+
|
|
117
|
+
### Contract Validation
|
|
118
|
+
Validate that your requests and responses match your OpenAPI specification. Catch breaking changes early.
|
|
119
|
+
|
|
120
|
+
# Example Tutorials
|
|
86
121
|
|
|
87
122
|
Explore tutorials that walk you through mocking different protocols and scenarios:
|
|
88
123
|
|
|
89
|
-
-
|
|
90
|
-
|
|
124
|
+
- [Get started with REST API](https://mokapi.io/resources/tutorials/get-started-with-rest-api)
|
|
125
|
+
Mock a REST API using OpenAPI specification
|
|
91
126
|
|
|
92
|
-
-
|
|
93
|
-
|
|
127
|
+
- [Mock Kafka with AsyncAPI](https://mokapi.io/resources/tutorials/get-started-with-kafka)
|
|
128
|
+
Simulate Kafka topics and validate message producers
|
|
94
129
|
|
|
95
|
-
-
|
|
96
|
-
|
|
130
|
+
- [Mock LDAP Authentication](https://mokapi.io/resources/tutorials/mock-ldap-authentication-in-node)\
|
|
131
|
+
Test authentication flows without a real LDAP server
|
|
97
132
|
|
|
98
|
-
-
|
|
99
|
-
|
|
133
|
+
- [Mock SMTP Mail Servers](https://mokapi.io/resources/tutorials/mock-smtp-server-send-mail-using-node)\
|
|
134
|
+
Test email workflows without sending real messages
|
|
100
135
|
|
|
101
|
-
-
|
|
102
|
-
|
|
136
|
+
- [CI/CD Integration with GitHub Actions](https://mokapi.io/resources/tutorials/running-mokapi-in-a-ci-cd-pipeline)\
|
|
137
|
+
Run Mokapi in automated test pipelines
|
|
103
138
|
|
|
104
|
-
> More examples
|
|
139
|
+
> More examples [mokapi.io/resources](https://mokapi.io/resources)
|
|
105
140
|
|
|
106
|
-
|
|
141
|
+
## Documentation
|
|
107
142
|
|
|
108
|
-
- [
|
|
109
|
-
- [HTTP](https://mokapi.io/docs/
|
|
110
|
-
- [Kafka](https://mokapi.io/docs/
|
|
111
|
-
- [LDAP](https://mokapi.io/docs/
|
|
112
|
-
- [SMTP](https://mokapi.io/docs/
|
|
113
|
-
- [
|
|
114
|
-
- [
|
|
143
|
+
- [Getting Started Guide](https://mokapi.io/docs/welcome)
|
|
144
|
+
- [HTTP/REST API Documentation](https://mokapi.io/docs/http/overview)
|
|
145
|
+
- [Kafka Documentation](https://mokapi.io/docs/kafka/overview)
|
|
146
|
+
- [LDAP Documentation](https://mokapi.io/docs/ldap/overview)
|
|
147
|
+
- [SMTP/Mail Documentation](https://mokapi.io/docs/mail/overview)
|
|
148
|
+
- [JavaScript API Reference](https://mokapi.io/docs/javascript-api/overview)
|
|
149
|
+
- [Configuration Guide](https://mokapi.io/docs/configuration/overview)
|
|
115
150
|
|
|
116
|
-
|
|
151
|
+
## Support the Project
|
|
117
152
|
|
|
118
|
-
If
|
|
153
|
+
If Mokapi helps your team ship faster, consider supporting development:
|
|
119
154
|
|
|
120
155
|
<a href="https://www.buymeacoffee.com/mokapi" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
|
121
156
|
|
|
122
|
-
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
MIT License - see [LICENSE](https://github.com/marle3003/mokapi/blob/main/LICENSE) for details.
|
|
160
|
+
|
|
161
|
+
## Links
|
|
123
162
|
|
|
124
|
-
|
|
163
|
+
- Website: [mokapi.io](https://mokapi.io)
|
|
164
|
+
- GitHub: [github.com/marle3003/mokapi](https://github.com/marle3003/mokapi)
|
|
165
|
+
- NPM Package: [npmjs.com/package/go-mokapi](https://npmjs.com/package/go-mokapi)
|
|
166
|
+
- Documentation: [mokapi.io/docs](https://mokapi.io/docs)
|
|
167
|
+
- Tutorials: [mokapi.io/resources/tutorials](https://mokapi.io/resources/tutorials)
|
|
168
|
+
- Blog: [mokapi.io/resources/blogs](https://mokapi.io/resources/blogs)
|
package/bin/mokapi.js
CHANGED
|
@@ -1,32 +1,59 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
const { spawn } = require('child_process');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
// Mapping Node platform + arch to scoped platform packages
|
|
6
|
+
function resolveBinary() {
|
|
7
|
+
const platform = process.platform;
|
|
8
|
+
const arch = process.arch;
|
|
9
|
+
|
|
10
|
+
let pkgName;
|
|
11
|
+
|
|
12
|
+
switch (platform) {
|
|
13
|
+
case 'linux':
|
|
14
|
+
pkgName = arch === 'arm64'
|
|
15
|
+
? '@go-mokapi/linux-arm64'
|
|
16
|
+
: '@go-mokapi/linux-x64';
|
|
17
|
+
break;
|
|
18
|
+
case 'darwin':
|
|
19
|
+
pkgName = arch === 'arm64'
|
|
20
|
+
? '@go-mokapi/darwin-arm64'
|
|
21
|
+
: '@go-mokapi/darwin-x64';
|
|
22
|
+
break;
|
|
23
|
+
case 'win32':
|
|
24
|
+
pkgName = arch === 'arm64'
|
|
25
|
+
? '@go-mokapi/win32-arm64'
|
|
26
|
+
: '@go-mokapi/win32-x64';
|
|
27
|
+
break;
|
|
28
|
+
default:
|
|
29
|
+
console.error(`Unsupported platform/arch: ${platform}/${arch}`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
// Each platform package exports the full path to the binary
|
|
35
|
+
const binaryPath = require(pkgName);
|
|
36
|
+
if (!fs.existsSync(binaryPath)) {
|
|
37
|
+
console.error(`Binary not found at ${binaryPath}`);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
return binaryPath;
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error(`Platform package ${pkgName} is not installed.`);
|
|
43
|
+
console.error(`Run 'npm install' to install the correct platform binary.`);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
21
46
|
}
|
|
22
|
-
const exePath = path.resolve(__dirname, exe)
|
|
23
|
-
const mokapi = spawn(exePath, process.argv.slice(2), {cwd: process.cwd()})
|
|
24
47
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
48
|
+
// Get the binary path for current platform
|
|
49
|
+
const exe = resolveBinary();
|
|
50
|
+
|
|
51
|
+
// Spawn Mokapi process with arguments from CLI
|
|
52
|
+
const mokapi = spawn(exe, process.argv.slice(2), { cwd: process.cwd() });
|
|
28
53
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
54
|
+
// Forward stdout/stderr
|
|
55
|
+
mokapi.stdout.on('data', data => process.stdout.write(data));
|
|
56
|
+
mokapi.stderr.on('data', data => process.stderr.write(data));
|
|
32
57
|
|
|
58
|
+
// Exit with the same code as Mokapi
|
|
59
|
+
mokapi.on('close', code => process.exit(code));
|
package/package.json
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "go-mokapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.37.0",
|
|
4
4
|
"description": "Easy and flexible API mocking",
|
|
5
5
|
"homepage": "https://mokapi.io",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/marle3003/mokapi.git"
|
|
8
|
+
"url": "git+https://github.com/marle3003/mokapi.git"
|
|
9
9
|
},
|
|
10
10
|
"bin": {
|
|
11
11
|
"mokapi": "bin/mokapi.js"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
14
|
-
|
|
13
|
+
"preferUnplugged": true,
|
|
14
|
+
"optionalDependencies": {
|
|
15
|
+
"@go-mokapi/darwin-arm64": "*",
|
|
16
|
+
"@go-mokapi/darwin-x64": "*",
|
|
17
|
+
"@go-mokapi/linux-arm64": "*",
|
|
18
|
+
"@go-mokapi/linux-x64": "*",
|
|
19
|
+
"@go-mokapi/win32-arm64": "*",
|
|
20
|
+
"@go-mokapi/win32-x64": "*"
|
|
15
21
|
},
|
|
16
22
|
"keywords": [
|
|
17
23
|
"mock",
|
|
18
24
|
"api",
|
|
25
|
+
"http",
|
|
19
26
|
"kafka",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
27
|
+
"ldap",
|
|
28
|
+
"smtp",
|
|
29
|
+
"imap"
|
|
22
30
|
],
|
|
23
31
|
"author": "Marcel Lehmann",
|
|
24
32
|
"license": "MIT",
|
|
25
|
-
"types": "index.d.ts"
|
|
26
|
-
"files": [
|
|
27
|
-
"bin",
|
|
28
|
-
"dist",
|
|
29
|
-
"types",
|
|
30
|
-
"index.d.ts"
|
|
31
|
-
]
|
|
33
|
+
"types": "index.d.ts"
|
|
32
34
|
}
|
package/types/file.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the contents of a file and returns it as a string.
|
|
3
|
+
*
|
|
4
|
+
* If the path is relative, it is resolved relative to the entry script file.
|
|
5
|
+
*
|
|
6
|
+
* @param path - Path to the file to read.
|
|
7
|
+
* @returns The contents of the file.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* export default function() {
|
|
11
|
+
* const data = read('data.json')
|
|
12
|
+
* console.log(data)
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
export function read(path: string): string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Writes a string to a file at the given path.
|
|
19
|
+
*
|
|
20
|
+
* If the path is relative, it is resolved relative to the entry script file.
|
|
21
|
+
* If the file does not exist, it will be created.
|
|
22
|
+
* If the file exists, it will be overwritten.
|
|
23
|
+
*
|
|
24
|
+
* @param path - Path to the file to write.
|
|
25
|
+
* @param s - The string content to write.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* export default function() {
|
|
29
|
+
* writeString('data.json', 'Hello World')
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
export function writeString(path: string, s: string): void;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Appends a string to a file at the given path.
|
|
36
|
+
*
|
|
37
|
+
* If the path is relative, it is resolved relative to the entry script file.
|
|
38
|
+
* If the file does not exist, it will be created.
|
|
39
|
+
* If the file exists, the string will be appended.
|
|
40
|
+
*
|
|
41
|
+
* @param path - Path to the file to append to.
|
|
42
|
+
* @param s - The string content to append.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* export default function() {
|
|
46
|
+
* writeString('data.json', 'Hello')
|
|
47
|
+
* appendString('data.json', ' World')
|
|
48
|
+
* }
|
|
49
|
+
*/
|
|
50
|
+
export function appendString(path: string, s: string): void;
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Mokapi JavaScript API
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* This module exposes the core scripting API for Mokapi.
|
|
5
|
+
* It allows you to intercept and manipulate protocol events (HTTP, Kafka, LDAP, SMTP),
|
|
6
|
+
* schedule jobs, generate mock data, and share state between scripts.
|
|
7
|
+
*
|
|
8
|
+
* Documentation:
|
|
9
|
+
* https://mokapi.io/docs/javascript-api/overview
|
|
4
10
|
*/
|
|
5
11
|
|
|
6
12
|
import "./faker";
|
|
@@ -10,13 +16,18 @@ import "./mustache";
|
|
|
10
16
|
import "./yaml";
|
|
11
17
|
import "./encoding";
|
|
12
18
|
import "./mail";
|
|
19
|
+
import "./file";
|
|
13
20
|
|
|
14
21
|
/**
|
|
15
22
|
* Attaches an event handler for the given event.
|
|
23
|
+
*
|
|
24
|
+
* Event handlers are executed in priority order whenever the event occurs.
|
|
25
|
+
* Multiple handlers can be registered for the same event.
|
|
26
|
+
*
|
|
16
27
|
* https://mokapi.io/docs/javascript-api/mokapi/on
|
|
17
|
-
* @param event Event type such as http
|
|
18
|
-
* @param handler
|
|
19
|
-
* @param args
|
|
28
|
+
* @param event Event type such as `http`, `kafka`, `ldap`, or `smtp`
|
|
29
|
+
* @param handler Function executed when the event is triggered
|
|
30
|
+
* @param args Optional event configuration such as priority, tracking, or tags
|
|
20
31
|
* @example
|
|
21
32
|
* export default function() {
|
|
22
33
|
* on('http', function(request, response) {
|
|
@@ -29,7 +40,7 @@ import "./mail";
|
|
|
29
40
|
* })
|
|
30
41
|
* }
|
|
31
42
|
*/
|
|
32
|
-
export function on<T extends keyof EventHandler>(event: T, handler: EventHandler[T], args?:
|
|
43
|
+
export function on<T extends keyof EventHandler>(event: T, handler: EventHandler[T], args?: TypedEventArgs[T]): void;
|
|
33
44
|
|
|
34
45
|
/**
|
|
35
46
|
* Schedules a new periodic job with interval.
|
|
@@ -109,16 +120,18 @@ export interface EventHandler {
|
|
|
109
120
|
}
|
|
110
121
|
|
|
111
122
|
/**
|
|
112
|
-
* HttpEventHandler is
|
|
123
|
+
* HttpEventHandler is invoked for every incoming HTTP request.
|
|
124
|
+
*
|
|
125
|
+
* Handlers may modify the response object to influence the outgoing response.
|
|
126
|
+
* The return value is ignored.
|
|
127
|
+
*
|
|
113
128
|
* https://mokapi.io/docs/javascript-api/mokapi/eventhandler/httpeventhandler
|
|
114
129
|
* @example
|
|
115
130
|
* export default function() {
|
|
116
131
|
* on('http', function(request, response) {
|
|
117
132
|
* if (request.operationId === 'time') {
|
|
118
133
|
* response.body = date()
|
|
119
|
-
* return true
|
|
120
134
|
* }
|
|
121
|
-
* return false
|
|
122
135
|
* })
|
|
123
136
|
* }
|
|
124
137
|
*/
|
|
@@ -157,6 +170,9 @@ export interface HttpRequest {
|
|
|
157
170
|
/** Object contains querystring parameters specified by OpenAPI querystring parameters. */
|
|
158
171
|
readonly querystring: any;
|
|
159
172
|
|
|
173
|
+
/** Name of the API, as defined in the OpenAPI `info.title` field */
|
|
174
|
+
readonly api: string;
|
|
175
|
+
|
|
160
176
|
/** Path value specified by the OpenAPI path */
|
|
161
177
|
readonly key: string;
|
|
162
178
|
|
|
@@ -164,7 +180,7 @@ export interface HttpRequest {
|
|
|
164
180
|
readonly operationId: string;
|
|
165
181
|
|
|
166
182
|
/** Returns a string representing this HttpRequest object. */
|
|
167
|
-
toString(): string
|
|
183
|
+
toString(): string;
|
|
168
184
|
}
|
|
169
185
|
|
|
170
186
|
/**
|
|
@@ -183,6 +199,31 @@ export interface HttpResponse {
|
|
|
183
199
|
|
|
184
200
|
/** Data will be encoded with the OpenAPI response definition. */
|
|
185
201
|
data: any;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Rebuilds the entire HTTP response using the OpenAPI response definition.
|
|
205
|
+
*
|
|
206
|
+
* This resets the status code, headers, and response body/data
|
|
207
|
+
* based on the OpenAPI specification.
|
|
208
|
+
*
|
|
209
|
+
* - If `statusCode` is omitted, the OpenAPI `default` response is used.
|
|
210
|
+
* - If `contentType` is omitted, the first defined content type for the
|
|
211
|
+
* selected status code is used.
|
|
212
|
+
*
|
|
213
|
+
* Use this when switching to a different response (e.g. error handling)
|
|
214
|
+
* while keeping the response schema valid.
|
|
215
|
+
*
|
|
216
|
+
* @throws Error if the status code or content type is not defined in the OpenAPI spec
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* on('http', (request, response) => {
|
|
220
|
+
* if (request.path.petId === 10) {
|
|
221
|
+
* response.rebuild(404)
|
|
222
|
+
* response.data.message = 'Pet not found'
|
|
223
|
+
* }
|
|
224
|
+
* })
|
|
225
|
+
*/
|
|
226
|
+
rebuild: (statusCode?: number, contentType?: string) => void;
|
|
186
227
|
}
|
|
187
228
|
|
|
188
229
|
/**
|
|
@@ -421,10 +462,11 @@ export type DateLayout =
|
|
|
421
462
|
| "RFC3339Nano";
|
|
422
463
|
|
|
423
464
|
/**
|
|
424
|
-
* EventArgs
|
|
465
|
+
* EventArgs provides optional configuration for an event handler.
|
|
425
466
|
* https://mokapi.io/docs/javascript-api/mokapi/on
|
|
426
467
|
*
|
|
427
|
-
* Use this to
|
|
468
|
+
* Use this object to control how the event is tracked, labeled,
|
|
469
|
+
* and ordered in the execution pipeline.
|
|
428
470
|
*
|
|
429
471
|
* @example
|
|
430
472
|
* export default function() {
|
|
@@ -438,16 +480,123 @@ export type DateLayout =
|
|
|
438
480
|
*/
|
|
439
481
|
export interface EventArgs {
|
|
440
482
|
/**
|
|
441
|
-
* Adds or overrides
|
|
483
|
+
* Adds or overrides tags used to label this event in the dashboard.
|
|
484
|
+
* Tags can be used for filtering, grouping, or ownership attribution.
|
|
442
485
|
*/
|
|
443
486
|
tags?: { [key: string]: string };
|
|
444
487
|
|
|
445
488
|
/**
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
489
|
+
* Defines the execution order of the event handler.
|
|
490
|
+
*
|
|
491
|
+
* Event handlers are executed in descending priority order.
|
|
492
|
+
* Handlers with the same priority are executed in registration order.
|
|
493
|
+
*
|
|
494
|
+
* Handlers with higher priority values run first.
|
|
495
|
+
* Handlers with lower priority values run later.
|
|
496
|
+
*
|
|
497
|
+
* Use negative priorities (e.g. -1) to run a handler after
|
|
498
|
+
* the response has been fully populated by other handlers,
|
|
499
|
+
* such as for logging or recording purposes.
|
|
500
|
+
*/
|
|
501
|
+
priority?: number;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* TypedEventArgs provides strongly typed argument objects
|
|
506
|
+
* for each supported event type.
|
|
507
|
+
*
|
|
508
|
+
* It is mainly used internally to map event names
|
|
509
|
+
* (e.g. `http`, `kafka`) to their corresponding argument types.
|
|
510
|
+
*/
|
|
511
|
+
export interface TypedEventArgs {
|
|
512
|
+
/**
|
|
513
|
+
* Arguments for HTTP event handlers.
|
|
514
|
+
*/
|
|
515
|
+
http: HttpEventArgs;
|
|
516
|
+
/**
|
|
517
|
+
* Arguments for Kafka event handlers.
|
|
518
|
+
*/
|
|
519
|
+
kafka: KafkaEventArgs;
|
|
520
|
+
/**
|
|
521
|
+
* Arguments for LDAP event handlers.
|
|
522
|
+
*/
|
|
523
|
+
ldap: LdapEventArgs;
|
|
524
|
+
/**
|
|
525
|
+
* Arguments for SMTP event handlers.
|
|
449
526
|
*/
|
|
450
|
-
|
|
527
|
+
smtp: SmtpEventArgs;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Configuration options for HTTP event handlers.
|
|
532
|
+
*
|
|
533
|
+
* These arguments control execution behavior such as
|
|
534
|
+
* priority, tagging, and dashboard tracking.
|
|
535
|
+
*/
|
|
536
|
+
export interface HttpEventArgs extends EventArgs {
|
|
537
|
+
/**
|
|
538
|
+
* Controls whether this event handler is tracked in the dashboard.
|
|
539
|
+
*
|
|
540
|
+
* - true: always track this handler
|
|
541
|
+
* - false: never track this handler
|
|
542
|
+
* - undefined: Mokapi determines tracking automatically based on
|
|
543
|
+
* whether the response object was modified by the handler
|
|
544
|
+
*/
|
|
545
|
+
track?: boolean | ((request: HttpRequest, response: HttpResponse) => boolean);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Configuration options for Kafka event handlers.
|
|
550
|
+
*
|
|
551
|
+
* These arguments control execution behavior such as
|
|
552
|
+
* priority, tagging, and dashboard tracking.
|
|
553
|
+
*/
|
|
554
|
+
export interface KafkaEventArgs extends EventArgs {
|
|
555
|
+
/**
|
|
556
|
+
* Controls whether this event handler is tracked in the dashboard.
|
|
557
|
+
*
|
|
558
|
+
* - true: always track this handler
|
|
559
|
+
* - false: never track this handler
|
|
560
|
+
* - undefined: Mokapi determines tracking automatically based on
|
|
561
|
+
* whether the message was modified or acknowledged by the handler
|
|
562
|
+
*/
|
|
563
|
+
track?: boolean | ((message: KafkaEventMessage) => boolean);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Configuration options for LDAP event handlers.
|
|
568
|
+
*
|
|
569
|
+
* These arguments control execution behavior such as
|
|
570
|
+
* priority, tagging, and dashboard tracking.
|
|
571
|
+
*/
|
|
572
|
+
export interface LdapEventArgs extends EventArgs {
|
|
573
|
+
/**
|
|
574
|
+
* Controls whether this event handler is tracked in the dashboard.
|
|
575
|
+
*
|
|
576
|
+
* - true: always track this handler
|
|
577
|
+
* - false: never track this handler
|
|
578
|
+
* - undefined: Mokapi determines tracking automatically based on
|
|
579
|
+
* whether the response object was modified by the handler
|
|
580
|
+
*/
|
|
581
|
+
track?: boolean | ((request: LdapSearchRequest, response: LdapSearchResponse) => boolean);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Configuration options for SMTP event handlers.
|
|
586
|
+
*
|
|
587
|
+
* These arguments control execution behavior such as
|
|
588
|
+
* priority, tagging, and dashboard tracking.
|
|
589
|
+
*/
|
|
590
|
+
export interface SmtpEventArgs extends EventArgs {
|
|
591
|
+
/**
|
|
592
|
+
* Controls whether this event handler is tracked in the dashboard.
|
|
593
|
+
*
|
|
594
|
+
* - true: always track this handler
|
|
595
|
+
* - false: never track this handler
|
|
596
|
+
* - undefined: Mokapi determines tracking automatically based on
|
|
597
|
+
* whether the message was processed or modified by the handler
|
|
598
|
+
*/
|
|
599
|
+
track?: boolean | ((record: SmtpEventMessage) => boolean);
|
|
451
600
|
}
|
|
452
601
|
|
|
453
602
|
/**
|
|
@@ -462,6 +611,10 @@ export interface EventArgs {
|
|
|
462
611
|
*/
|
|
463
612
|
export type ScheduledEventHandler = () => void | Promise<void>;
|
|
464
613
|
|
|
614
|
+
/**
|
|
615
|
+
* Configuration options for scheduled event handlers
|
|
616
|
+
* created via `every` or `cron`.
|
|
617
|
+
*/
|
|
465
618
|
export interface ScheduledEventArgs {
|
|
466
619
|
/**
|
|
467
620
|
* Adds or overrides existing tags used in dashboard
|
|
@@ -625,7 +778,8 @@ export interface SharedMemory {
|
|
|
625
778
|
* The `mokapi.shared` object provides a way to persist and share
|
|
626
779
|
* data between multiple scripts running in the same Mokapi instance.
|
|
627
780
|
*
|
|
628
|
-
* Values are stored in memory and shared across all scripts
|
|
781
|
+
* Values are stored in memory and shared across all scripts
|
|
782
|
+
* within the same Mokapi process.
|
|
629
783
|
* This allows you to coordinate state, cache data, or simulate
|
|
630
784
|
* application-level variables without using global variables.
|
|
631
785
|
* All values are persisted for the lifetime of the Mokapi process.
|
|
@@ -640,4 +794,4 @@ export interface SharedMemory {
|
|
|
640
794
|
* mokapi.log(`Current counter: ${count}`)
|
|
641
795
|
* ```
|
|
642
796
|
*/
|
|
643
|
-
export const shared: SharedMemory;
|
|
797
|
+
export const shared: SharedMemory;
|
|
Binary file
|
|
Binary file
|
|
Binary file
|