2.1.2 • Published 5 months ago

@superbia/client v2.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@superbia/client

Connect to a Superbia server.

Installation

npm i @superbia/client

Available on browser and Node.

Get started

Browser

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
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.

const response = await client.request({
  user: { userId: "1" },
  userPosts: { userId: "1", first: 20 },
});

const result = response.result(); // { user: {...}, userPosts: {...} }

Uploads

Browser

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

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.

client.init(); // create WebSocket connection

const subscription = client.subscribe({ counter: null });

subscription.on("result", (result) => {
  // { counter: Int }
});

Headers

Headers can be passed to the server for both requests and subscriptions.

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" } });
2.1.2

5 months ago

2.1.1

5 months ago

2.1.0

9 months ago

2.0.9

9 months ago

2.0.10

9 months ago

2.0.8

9 months ago

2.0.7

9 months ago

2.0.6

9 months ago

2.0.3

9 months ago

2.0.2

9 months ago

2.0.5

9 months ago

2.0.4

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago