Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uga-rosa/deno-mock-jr
Mock server based on JSON-RPC
https://github.com/uga-rosa/deno-mock-jr
Last synced: 8 days ago
JSON representation
Mock server based on JSON-RPC
- Host: GitHub
- URL: https://github.com/uga-rosa/deno-mock-jr
- Owner: uga-rosa
- Created: 2024-01-12T18:32:03.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-01-14T14:40:47.000Z (11 months ago)
- Last Synced: 2024-10-28T13:08:17.461Z (about 2 months ago)
- Language: TypeScript
- Homepage: https://deno.land/x/mock_jr
- Size: 36.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# deno-mock-jr
**WIP**
Mock server based on JSON-RPC.
# Installation
Use `deno install` to install or update `mock-langserver`.
```bash
deno install -f --allow-read -n mock-langserver https://deno.land/x/mock_jr/main.ts
```# Usage
## As a language server
### Installation
To act as a language server, standard input/output is used to request/response.
```bash
mock-langserver /path/to/def.yaml
```All arguments are considered paths to definition files.
json/yaml/toml are available for definition files.
Check `schemas/definition.json` for format.## Use in Deno
`Server` class is available that returns results in a json string in response to a call in a json string.
The function to be called can be defined with `Server.setProcedureMap`, which sets a map whose keys are the method names and whose values are the functions that receive the params.```typescript
import { ProcedureMap, Server } from "./server.ts";
import { is } from "./deps/unknownutil.ts";const serv = new Server();
// Set procedures
const map: ProcedureMap = new Map();
map.set("add", (params) => {
if (is.ArrayOf(is.Number)(params)) {
return params.reduce((acc, cur) => acc + cur);
}
throw new Error(`Invalid params type: expect number[]`);
});
map.set("subtract", (params) => {
if (is.ArrayOf(is.Number)(params)) {
return params.reduce((acc, cur) => acc - cur);
}
throw new Error(`Invalid params type: expect number[]`);
});
serv.setProcedureMap(map);// Listening on http://localhost:8000/
Deno.serve(async (req) => {
if (req.body) {
const decoded = await req.text();
const resp = serv.call(decoded);
return new Response(resp);
}
return new Response();
});
``````typescript
const resp = await fetch("http://localhost:8000", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ jsonrpc: "2.0", method: "add", params: [1, 2, 3], id: 1 }),
});console.log(await resp.text());
// {"jsonrpc":"2.0","result":6,"id":1}
```