|
1 | 1 | --- |
2 | 2 | title: JavaScript API Documentation |
3 | | -description: Explore Mokapi’s JavaScript API to extend its functionality. Learn how to use modules for HTTP, Kafka, YAML, Mustache, and more. |
| 3 | +description: Extend and customize Mokapi using JavaScript. Learn how to structure scripts, use built-in modules, and handle runtime limitations. |
4 | 4 | --- |
| 5 | + |
5 | 6 | # Mokapi JavaScript API |
6 | 7 |
|
7 | | -Mokapi provides a powerful JavaScript API that allows you to extend its functionality. |
8 | | -This documentation outlines the available modules and their capabilities. |
9 | | -To learn how Mokapi handles module imports, see [Modules](/docs/javascript-api/modules.md). |
| 8 | +Mokapi allows you to extend and customize its behavior using JavaScript. |
| 9 | +JavaScript is used to implement dynamic logic such as request handling, data generation, |
| 10 | +scheduled jobs, and event-driven workflows. |
| 11 | + |
| 12 | +Mokapi executes JavaScript in a **simple and explicit way**: |
| 13 | +only files that export a **default function** are executed. |
| 14 | +All other JavaScript files are treated as **modules**. |
| 15 | + |
| 16 | +This documentation explains: |
| 17 | +- How JavaScript is executed in Mokapi |
| 18 | +- Script vs module structure |
| 19 | +- Runtime environment limitations |
| 20 | +- Built-in modules |
| 21 | + |
| 22 | +## How JavaScript Is Used in Mokapi |
| 23 | + |
| 24 | +JavaScript can be used for tasks such as: |
| 25 | + |
| 26 | +- Handling HTTP, Kafka, and other protocol events |
| 27 | +- Generating dynamic mock data |
| 28 | +- Scheduling background jobs |
| 29 | +- Transforming or enriching request and response data |
| 30 | + |
| 31 | +Each JavaScript file is either: |
| 32 | +- an **executable script**, or |
| 33 | +- a **module** imported by other scripts |
| 34 | + |
| 35 | +## Executable Scripts (Default Function) |
| 36 | + |
| 37 | +A JavaScript file is executed **only if it exports a default function**. |
| 38 | + |
| 39 | +```js |
| 40 | +export default function (ctx) { |
| 41 | + // executed by Mokapi |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +- The default function is the entry point of the script |
| 46 | +- Mokapi invokes this function directly |
| 47 | +- All execution logic must be placed inside this function |
| 48 | + |
| 49 | +If a file does not export a default function, Mokapi **will not execute it**. |
| 50 | + |
| 51 | +## Runtime Environment |
| 52 | + |
| 53 | +Mokapi executes JavaScript in its own runtime. |
| 54 | +It is **not** a Node.js environment and **not** a browser environment. |
| 55 | + |
| 56 | +As a result, many APIs that are commonly available in Node.js or browsers are **not available** in Mokapi. |
| 57 | + |
| 58 | +### ⚠️ Important Limitations |
| 59 | + |
| 60 | +- Node.js built-ins such as `fs`, `path`, `os`, or `net` are not available |
| 61 | +- Browser APIs such as `window`, `document`, or `fetch` are not available |
| 62 | +- Third-party packages that rely on Node.js or browser APIs may fail |
| 63 | + |
| 64 | +### Provided Alternatives |
| 65 | + |
| 66 | +Mokapi provides its own APIs for common tasks: |
| 67 | + |
| 68 | +- **HTTP requests** |
| 69 | + Use the `fetch` function from `mokapi/http`: |
| 70 | + ```js |
| 71 | + import { fetch } from "mokapi/http" |
| 72 | + ``` |
| 73 | +- **File access** |
| 74 | + Use the global open() function to read files: |
| 75 | + ```js |
| 76 | + const text = open("data/example.json") |
| 77 | + ``` |
| 78 | +- **Environment variables** |
| 79 | + Use env() from the core API: |
| 80 | + ```js |
| 81 | + import { env } from "mokapi" |
| 82 | + ``` |
| 83 | + |
| 84 | +## Modules |
| 85 | + |
| 86 | +JavaScript files without a **default export** are treated as **modules**. |
| 87 | +Modules allow you to organize and reuse code. |
| 88 | + |
| 89 | +```js |
| 90 | +export function normalizeUser(user) { |
| 91 | + return { ...user, active: true } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +- Modules are not executed by Mokapi |
| 96 | +- Can be imported by executable scripts or other modules |
| 97 | +- Used to organize shared logic |
| 98 | + |
| 99 | +For more details on the different types of modules (built-in, local filesystem, JSON/YAML), see the dedicated [JavaScript Modules guide](/docs/javascript-api/modules.md). |
| 100 | + |
| 101 | +## Module Resolution |
| 102 | + |
| 103 | +Mokapi resolves imports using the same algorithm as Node.js: |
| 104 | + |
| 105 | +1. The directory of the importing file |
| 106 | +2. Any node_modules directory in the same folder |
| 107 | +3. Parent directories up to the nearest package.json |
| 108 | + |
| 109 | +> Module resolution only determines where Mokapi looks for the file. |
| 110 | + Runtime limitations still apply: modules that rely on Node.js or browser APIs may fail. |
| 111 | + |
| 112 | +## TypeScript Support |
10 | 113 |
|
11 | 114 | ``` box=tip url=[@types/mokapi on npm](https://www.npmjs.com/package/@types/mokapi) |
12 | | -Mokapi offers a TypeScript definition package. Install it using: |
| 115 | +Mokapi provides TypeScript type definitions for its JavaScript API. |
| 116 | +Install them using: |
13 | 117 | `npm install @types/mokapi --save-dev` |
14 | 118 | ``` |
15 | 119 |
|
16 | | -## Available Modules |
| 120 | +## Built-in Modules |
| 121 | + |
| 122 | +Mokapi provides a set of built-in JavaScript modules that can be used from executable scripts |
| 123 | +and custom modules. |
17 | 124 |
|
18 | 125 | ### mokapi (Core API) |
19 | 126 |
|
20 | 127 | Provides core functions for scheduling jobs, handling events, and accessing environment variables. |
21 | 128 |
|
22 | | -| Functions | Description | |
23 | | -|------------------------------------------------------------------------------|-------------------------------------------------------| |
24 | | -| [cron( expression, handler, \[args\] )](/docs/javascript-api/mokapi/cron.md) | Schedules a new periodic job using a cron expression. | |
25 | | -| [date( \[args\] )](/docs/javascript-api/mokapi/date.md) | Returns a formatted date string. | |
26 | | -| [env( name )](/docs/javascript-api/mokapi/env.md) | Gets the value of an environment variable. | |
27 | | -| [every( interval, handler, \[args\] )](/docs/javascript-api/mokapi/every.md) | Runs a periodic job at a fixed interval. | |
28 | | -| [on( event, handler, \[args\]](/docs/javascript-api/mokapi/on.md) ) | Registers an event handler. | |
29 | | -| [sleep( time )](/docs/javascript-api/mokapi/sleep.md) | Pauses execution for a specified duration. | |
30 | | -| [marshal( value, \[encoding\] )](/docs/javascript-api/mokapi/marshal.md) | Converts a value to a marshaled string. | |
| 129 | +| Functions | Description | |
| 130 | +|------------------------------------------------------------------------------|---------------------------------------------------| |
| 131 | +| [cron( expression, handler, \[args\] )](/docs/javascript-api/mokapi/cron.md) | Schedules a periodic job using a cron expression. | |
| 132 | +| [date( \[args\] )](/docs/javascript-api/mokapi/date.md) | Returns a formatted date string. | |
| 133 | +| [env( name )](/docs/javascript-api/mokapi/env.md) | Gets the value of an environment variable. | |
| 134 | +| [every( interval, handler, \[args\] )](/docs/javascript-api/mokapi/every.md) | Runs a periodic job at a fixed interval. | |
| 135 | +| [on( event, handler, \[args\]](/docs/javascript-api/mokapi/on.md) ) | Registers an event handler. | |
| 136 | +| [sleep( time )](/docs/javascript-api/mokapi/sleep.md) | Pauses execution | |
| 137 | +| [marshal( value, \[encoding\] )](/docs/javascript-api/mokapi/marshal.md) | Converts a value to a marshaled string. | |
31 | 138 |
|
32 | 139 | ### mokapi/http (HTTP Requests) |
33 | 140 |
|
34 | 141 | Functions to send HTTP requests within Mokapi scripts. |
35 | 142 |
|
36 | | -| Functions | Description | |
37 | | -|-----------------------------------------------------------------------------------|----------------------------------------| |
38 | | -| [get( url, \[args\] )](/docs/javascript-api/mokapi-http/get.md) | Sends an HTTP GET request. | |
39 | | -| [post( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/post.md) | Sends an HTTP POST request | |
40 | | -| [put( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/put.md) | Sends an HTTP PUT request | |
41 | | -| [head( url, \[args\] )](/docs/javascript-api/mokapi-http/head.md) | Sends an HTTP HEAD request | |
42 | | -| [patch( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/patch.md) | Sends an HTTP PATCH request | |
43 | | -| [delete( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/delete.md) | Sends an HTTP DELETE request | |
44 | | -| [options( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/options.md) | Sends an HTTP OPTIONS request | |
45 | | -| [fetch( url, \[opts\] )](/docs/javascript-api/mokapi-http/fetch.md) | Create an HTTP request using Fetch API | |
| 143 | +| Functions | Description | |
| 144 | +|-----------------------------------------------------------------------------------|-------------------------------| |
| 145 | +| [get( url, \[args\] )](/docs/javascript-api/mokapi-http/get.md) | Sends an HTTP GET request. | |
| 146 | +| [post( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/post.md) | Sends an HTTP POST request | |
| 147 | +| [put( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/put.md) | Sends an HTTP PUT request | |
| 148 | +| [head( url, \[args\] )](/docs/javascript-api/mokapi-http/head.md) | Sends an HTTP HEAD request | |
| 149 | +| [patch( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/patch.md) | Sends an HTTP PATCH request | |
| 150 | +| [delete( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/delete.md) | Sends an HTTP DELETE request | |
| 151 | +| [options( url, \[body\], \[args\] )](/docs/javascript-api/mokapi-http/options.md) | Sends an HTTP OPTIONS request | |
| 152 | +| [fetch( url, \[opts\] )](/docs/javascript-api/mokapi-http/fetch.md) | Fetch using Mokapi's API | |
46 | 153 |
|
47 | 154 | ### mokapi/faker (Mock Data Generator) |
48 | 155 |
|
@@ -72,19 +179,19 @@ Processes Mustache templates with dynamic data. |
72 | 179 |
|
73 | 180 | Handles YAML data parsing and conversion. |
74 | 181 |
|
75 | | -| Functions | Description | |
76 | | -|---------------------------------------------------------------------|-------------------------------------------------| |
77 | | -| [parse( text )](/docs/javascript-api/mokapi-yaml/parse.md) | Parses a YAML string into a JavaScript object. | |
78 | | -| [stringify( value )](/docs/javascript-api/mokapi-yaml/stringify.md) | Converts a JavaScript object into YAML format. | |
| 182 | +| Functions | Description | |
| 183 | +|---------------------------------------------------------------------|--------------------------------------| |
| 184 | +| [parse( text )](/docs/javascript-api/mokapi-yaml/parse.md) | Parses a YAML string into an object. | |
| 185 | +| [stringify( value )](/docs/javascript-api/mokapi-yaml/stringify.md) | Converts an object to YAML. | |
79 | 186 |
|
80 | 187 | ### mokapi/encoding (Encoding Utilities) |
81 | 188 |
|
82 | 189 | Functions for encoding and decoding data. |
83 | 190 |
|
84 | | -| Functions | Description | |
85 | | -|---------------------------------------------------------------------------------|-----------------------------------| |
86 | | -| [base64.encode( input )](/docs/javascript-api/mokapi-encoding/base64-encode.md) | Encodes a string using Base64. | |
87 | | -| [base64.decode( input )](/docs/javascript-api/mokapi-encoding/base64-decode.md) | Decodes a Base64-encoded string. | |
| 191 | +| Functions | Description | |
| 192 | +|---------------------------------------------------------------------------------|-----------------------------| |
| 193 | +| [base64.encode( input )](/docs/javascript-api/mokapi-encoding/base64-encode.md) | Encodes a string to Base64. | |
| 194 | +| [base64.decode( input )](/docs/javascript-api/mokapi-encoding/base64-decode.md) | Decodes a Base64 string. | |
88 | 195 |
|
89 | 196 |
|
90 | 197 |
|
|
0 commit comments