Socket
Socket
Sign inDemoInstall

@datx/network

Package Overview
Dependencies
2
Maintainers
7
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @datx/network

DatX network layer


Version published
Maintainers
7
Install size
695 kB
Created

Readme

Source

@datx/network

DatX is an opinionated data store. It features support for references to other models and first-class TypeScript support.

@datx/network is a datx mixin that adds a networking layer support. It can be used with any REST-like API and probably also other types of an API.


Basic usage

import { Collection, Model, Attribute } from '@datx/core';
import { BaseRequest, collection, setUrl } from '@datx/network';

class Person extends Model {
  public static type = 'person'; // Unique name of the model class

  @Attribute()
  public name: string; // A normal attribute without a default value

  @Attribute()
  public surname: string;

  @Attribute({ toOne: Person })
  public spouse?: Person; // A reference to a Person model

  public get fullName() {
    return `${this.name} ${this.surname}`;
  }
}

class AppData extends Collection {
  public static types = [Person]; // A list of models available in the collection
}

const store = new AppData();

// Create a base request with a basic configuration (baseUrl and linked collection)
const baseRequest = new BaseRequest('https://example.com').pipe(collection(store));

// Create separate request points
const getPerson = baseRequest.pipe<Person>(setUrl('/people/{id}', Person));
const getPeople = baseRequest.pipe<Array<Person>>(setUrl('/people', Person));

// Pure JS loading
const peopleResponse = await getPeople.fetch();

// Loading in a React component
const PersonInfo = ({ userId }) => {
  const [response, loading, error] = getPerson.useHook({ id: userId });

  if (loading || error) {
    return null;
  }

  const user = response.data;

  return <div>{user.fullName}</div>;
};

Getting started

npm install --save @datx/network

Polyfilling

The lib makes use of the following features that are not yet available everywhere. Based on your browser support, you might want to polyfill them:

  • Symbol.for
  • Object.assign
  • Array.prototype.find
  • Promise
  • Fetch API

How to add the polyfills. Note: Fetch API is not included in the polyfills mentioned in the Troubleshooting page. Instead, you need to add it as a separate library. If you don't have any special requirements (like server-side rendering), you can use the window.fetch polyfill.

API reference

Troubleshooting

Having issues with the library? Check out the troubleshooting page or open an issue.


Build Status npm version Dependency Status devDependency Status

License

The MIT License

Credits

@datx/network is maintained and sponsored by Infinum.

Keywords

FAQs

Last updated on 29 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc