Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iconshot/superbia-client
Connect to a Superbia server.
https://github.com/iconshot/superbia-client
Last synced: 2 months ago
JSON representation
Connect to a Superbia server.
- Host: GitHub
- URL: https://github.com/iconshot/superbia-client
- Owner: iconshot
- License: mit
- Created: 2023-06-29T19:10:47.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-22T06:01:59.000Z (4 months ago)
- Last Synced: 2024-11-17T10:36:06.464Z (3 months ago)
- Language: TypeScript
- Size: 42 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [@superbia/client](https://superbia.dev/client)
Connect to a Superbia server.
## Installation
```
npm i @superbia/client
```Available on browser and Node.
## Get started
#### Browser
```js
import { Client } from "@superbia/client";const client = new Client({
url: "http://localhost:8080", // server url
wsUrl: "ws://localhost:8080", // websocket url
fetch, // window.fetch
WebSocket, // window.WebSocket
FormData, // window.FormData
});
```#### Node
```
npm i node-fetch ws form-data
``````js
const { Client } = require("@superbia/client");const fetch = require("node-fetch");
const WebSocket = require("ws");
const FormData = require("form-data");const client = new Client({
url: "http://localhost:8080", // server url
wsUrl: "ws://localhost:8080", // websocket url
fetch, // node-fetch package
WebSocket, // ws package
FormData, // form-data package
});
```## Requests
You can access multiple endpoints in a single request.
```js
const response = await client.request({
user: { userId: "1" },
userPosts: { userId: "1", first: 20 },
});const result = response.result(); // { user: {...}, userPosts: {...} }
```## Uploads
#### Browser
```js
import { Upload } from "@superbia/client";const input = document.getElementById("input");
const file = input.files[0];
const response = await client.request({
uploadPhoto: { upload: new Upload(file) },
});const result = response.result(); // { uploadPhoto: null }
```#### Node
```js
const fs = require("fs");const { Upload } = require("@superbia/client");
const stream = fs.createReadStream("./photo.jpg");
const response = await client.request({
uploadPhoto: { upload: new Upload(stream) },
});const result = response.result(); // { uploadPhoto: null }
```## Subscriptions
Subscriptions use `WebSocket` under the hood.
```js
client.init(); // create WebSocket connectionconst subscription = client.subscribe({ counter: null });
subscription.on("result", (result) => {
// { counter: Int }
});
```## Headers
Headers can be passed to the server for both requests and subscriptions.
```js
const headers = {
Authorization: "Bearer _TOKEN_",
"Accept-Language": "en",
};client.update(headers);
// this request will pass the new headers
const response = await client.request({ userPosts: { userId: "1" } });
// create the WebSocket with the new headers
client.init();
const subscription = client.subscribe({ newPost: { userId: "1" } });
```