npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

client-rest-framework

v0.2.3

Published

REST API framework to construct domain API repositories

Downloads

13

Readme

client-rest-framework

Build and Test npm version

WORK IN PROGRESS.

Introduction

The client-rest-framework is a Typescript library that allows developers to create repositories to manage entities in a few lines of code, in a style similar to Django REST framework. The library provides APIs, serializers, and repositories that make it easy to interact with RESTful APIs and manage entities in a structured and efficient manner.

Features

  • Powerful and flexible two-directional serializers for creating your own serialization and deserialization logic.
  • Support for custom serializers to tailor serialization to your specific needs.
  • Strongly-typed entities based on the defined serializers, providing type safety and avoiding common errors.
  • Built-in support for Axios API, with the ability to easily customize and use custom API classes.
  • Object-oriented design inspired by Django REST framework, making it easy to manage entities in a structured and efficient manner.

Installation

To install the client-rest-framework, run the following command:

npm install client-rest-framework

Usage

To use the library, you need to import the repositories and serializers modules from the client-rest-framework package, and define your API endpoints, serializers, and repositories.

API

To define an API, create a class that extends the RESTAPI class, provide an HTTPClient instance, and set the url property to the API endpoint URL. You can then pass this API to an ApiRepository as a realization of CRUD operations for the corresponding entity.

Example API class:

import { api, pagination } from "client-rest-framework";
import { PublicUserDTO } from "./types"; 

// Configure HTTPClient
class HTTPClient extends api.AxiosHTTPClient {
  getExtraHeaders() {
    return { Authorization: `Bearer ${access}` };
  }

  onUnauthenticate = () => {
    return;
  }
}

class API<T> extends api.RESTAPI<T> {
  client = new HTTPClient({
    baseURL: BASE_URL
  });

}

class PublicUserAPI extends API<PublicUserDTO> {
  pagination = pagination.PageNumberPagination<PublicUserDTO>()
	url = "/api/users";
}

Serializers

Serializers are two-directional data-mappers that help to explicitly describe the domain model and to enforce its types in a simple case.

To define a serializer, create a class that extends the ModelSerializer class, and define fields for each attribute of the corresponding entity. You can use the StringField, NumberField, BooleanField, DateField, and EnumField classes to define different types of fields.

Example serializer class:

import { serializers } from "client-rest-framework"; 
import { PublicUserDTO, RoleKey, UserStatusKey, CategoryKey } from "./types";  

export class PublicUserSerializer extends ModelSerializer<PublicUserDTO> {
	id = new serializers.NumberField({ readonly: true });
	username = new serializers.StringField({ readonly: true });
	email = new serializers.StringField({ readonly: true });
	display_name = new serializers.StringField({ readonly: true });
	date_joined = new serializers.DateField({ readonly: true });
	notes = new serializers.StringField({});
	phone = new serializers.StringField({});
	// TS limutation, in this case you need to pass T, Readonly and Many generics explicitly:
	roles = new serializers.EnumField<RoleKey, false, true>({ many: true });
	status = new serializers.EnumField<UserStatusKey, false, false>({});
	categories = new serializers.EnumField<CategoryKey, false, true>({ many: true });
}

The library supports inferring the resulting domain type from the serializer class using the ReturnType type operator. You can use this to get the type of the entity returned by the repository's methods.

Example:

export type PublicUser = ReturnType<PublicUserSerializer["fromDTO"]>

Repositories

To define a repository, create a class that extends the APIRepository class, and set the api property to an instance of the corresponding API class, and the serializer property to an instance of the corresponding serializer class.

Example repository class:

import { repositories } from "client-rest-framework"; 
import { PublicUserDTO } from "./types";
import { PublicUserAPI } from "./api";
import { PublicUserSerializer } from "./serializers";

export class PublicUserApiRepository extends repositories.APIRepository {
	api = new PublicUserAPI();
	serializer = new PublicUserSerializer();
}

Using the repository

You can now use the repository to interact with the API and manage entities.

Example usage:

import { PublicUserApiRepository } from "./repositories";
import { PublicUser } from "./types";

const publicUsersRepository = new PublicUserApiRepository();  


// Get a user by ID 
const user = await publicUsersRepository.get(1);

// List all users
const users = await publicUsersRepository.list();

// Create a new user
const user = await publicUsersRepository.create(data);

// update a user
const user = await publicUsersRepository.update(1, diff);

// delete a user
await repo.delete(1);

Important Notes and Limitations

  • So far, list method works only with rest_framework.pagination.PageNumberPagination and expects this setting to be configured in DRF.
  • Package is in early alpha, interfaces may change