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

ts-resource-tastypie

v1.0.54

Published

REST Typescript client for Django-Tastypie or equivalent schema.

Downloads

68

Readme

Ts Resource Tastypie

RESTful TypeScript client for Django-Tastypie or equivalent schema.

Features:

  • Pagination
  • Complete CRUD
  • Abstract AJAX(J) providing operations which are similar to the Django Model API

Context

RESTful architecture with TypeScript and Django.

IMPORTANT:

  • Backend: Security rules for data persistence and access.
  • Frontend: Usability rules, only!

BENEFITS:

  • Asynchronous development between frontend and backend developers.
  • Reuse of web developers team to create mobile applications.
  • The frontend is isolated, we can distribute it as an application by using Apache Cordova.
  • Independent layers between business rules and usability rules of user interface.
  • Business rules are the same for different types of UI. We can create different UIs with any other programming language, passing through the same business rules on the backend.
  • And more ...

Requirements for the backend:

Install

npm i ts-resource-tastypie

Basic Usage

import * as api from "ts-resource-tastypie";

api.Tastypie.Provider.add(
    new api.Tastypie.Provider({name:'provider1', url:'http://address1/api/v1/', username:'admin', apikey:'123'})
);

let serviceName = new api.Tastypie.Resource('service_name');

serviceName.objects.create(data: any); //return Promise<any> 
serviceName.objects.update(id :number, data: any); //return Promise<any>
serviceName.objects.save(data: any); //return Promise<any>
serviceName.objects.delete(id: number); //return Promise<any>
serviceName.objects.get(id: number); //return Promise<any>
serviceName.objects.find(data: any); //return Promise<page>

serviceName.objects.find(data: any).then(
    function(page){
        //page.objects :Array<any>
        
        //page.meta.total_count :number
        //page.meta.limit : number
        //page.meta.offset : number
        //page.meta.next :string
        //page.meta.previous :string
                
        //page.index :number
        //page.length :number
        //page.range :Array<number>
        
        //page.change(index :number) :Promise<page>
        //page.next() :Promise<page>
        //page.previous() :Promise<page>
        //page.refresh() :Promise<page>
        //page.first() :Promise<page>
        //page.last() :Promise<page>
    }
)

Multiple Provider Usage

import * as api from "ts-resource-tastypie";

api.Tastypie.Provider.add(
    new api.Tastypie.Provider({name:'provider1', url:'http://address1/api/v1/', username:'admin', apikey:'123'}),
    new api.Tastypie.Provider({name:'provider2', url:'http://address2/api/v1/'}),
    new api.Tastypie.Provider({name:'provider3', url:'http://address3/api/v1/'})
);

api.Tastypie.Provider.setDefault('provider3');

let serviceName = new api.Tastypie.Resource('service_name'); //using default provider "provider3" 
let serviceName = new api.Tastypie.Resource('service_name', {provider: 'provider1'}); //using selected provider "provider1" 

Class Model Usage :+1:

import * as api from "ts-resource-tastypie";

api.Tastypie.Provider.add(
    new api.Tastypie.Provider({name:'provider1', url:'http://address1/api/v1/', username:'admin', apikey:'123'})
);

class myClassModel extends api.Tastypie.Model<myClassModel> {
    public static resource = new api.Tastypie.Resource<myClassModel>('serviceName', {model: myClassModel});
    
    public myAttr1: string;
    public myAttr2: number;
    public myAttr3: string;
    
    constructor(obj?:any){
        super(myClassModel.resource, obj);
    }
}

Usage:

let myObj = new myClassModel({myAttr1: 'foo', myAttr2: 200, myAttr3: 'bar'})
myObj.save()

let myObj = new myClassModel()
myObj.myAttr1 = 'foo'
myObj.myAttr2 = 200
myObj.myAttr3 = 'bar'
myObj.save()

//At this moment we no work with generic objects.
//Works with instances of your class that has been defined.
myClassModel.resource.objects.create(data: any); //return Promise<myClassModel> 
myClassModel.resource.objects.update(id :number, data: any); //return Promise<myClassModel>
myClassModel.resource.objects.save(data: any); //return Promise<myClassModel>
myClassModel.resource.objects.delete(id: number); //return Promise<myClassModel>
myClassModel.resource.objects.get(id: number); //return Promise<myClassModel>
myClassModel.resource.objects.find(data: any); //return Promise<page>

myClassModel.resource.objects.find(data: any).then(
    function(page){
        //page.objects :Array<myClassModel>
    }
)

Important

For good practice, attributes that start the name with "_" are considered local. Therefore, these attributes will not be sent to the backend. If you decide to change this logic, you can override the save method whenever you need it.

Override the save method

class myClassModel extends api.Tastypie.Model<myClassModel> {
    public static resource = new api.Tastypie.Resource<myClassModel>('serviceName', {model: myClassModel});
    
    public myAttr1: string;
    public myAttr2: number;
    public _myAttr3: string; //By default, this attribute will not be sent to the backend.
    private _myAttr4: string; //By default, this attribute will not be sent to the backend.
    
    constructor(obj?:any){
        super(myClassModel.resource, obj);
    }
    
    public save(): Promise<myClassModel> {
        return super.save({
            myAttr1: this.myAttr1,
            myAttr2: this.myAttr2,
            _myAttr3: this._myAttr3,
            _myAttr4: this._myAttr4
        });
    }
}

Making queries

Documentation in development ...

Contribute

If you found it useful, please consider paying me a coffee ; paypal

License

ts-resource-tastypie is released under the MIT License.