@rest-api/redux
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

@rest-api/redux

npm version CircleCI codecov

Core library which handles all ajax calls on redux.

Caution: if you have some random typescript error (pex: ts2589) on creating or editing models, kill the process and restart npm start (on Visual Studio Code restart program).

Major changes

A model on a schema type is the type of model. If you want to has the id in order to populate, you can write with following type:

	{ type: model, idOnly: true }

Declaring models

Declare the models of your application, given a Schema, an id (need to be required field) and a base url (final argument is reserved for opts):

import { Model, Schema, required } from  '@rest-api/redux'

const librarySchema = Schema({
    id: {
	    type: Number,
	    required
    },
    name: String
})
export default new Model(librarySchema, 
    'id', 
    '/api/library')

Or define your model with metadata, passing some arguments in order to build the metadata:

    import { Model, Schema, required } from  '@rest-api/redux'
    
    const librarySchema = Schema({
        id: {
    	    type: Number,
    	    required
        },
        name: String
    })
    export default new Model(librarySchema, 
	    'id', 
	    '/api/library',
		Schema({ count: { type: Number, required: true }, items: [{ type: librarySchema, required: true }] }), //define the data exactly returns your endpoint GET
		data => data.items, //function that returns an array of items as librarySchema
		d => d.items.length //function that returns the metadata from the request
	)

The opts for a model are an object with all properties as optional, which can be:

	{
		headers: A json with a key value string, this headers will be passed to all requests
		trailingSlash: Feature that pass a trailingSlash to each endpoint (solving django trailing slash endpoints)
	}

This schema can be updated or delete some fields, but returns a new Schema:

	const librarySchemaWithFailName = librarySchema.updateSchema({ name: Number })
	const librarySchemaWithoutFields = librarySchemaWithFailName.deleteFields('name', 'id)

You can use complex objects on a Schema simplier creating subschemas:

import { Schema } from  '@rest-api/redux'

const testSchema = Schema({
    subSchema: Schema({
    	id: { type: String },
    	name: String
	})
})

And foreign keys of your model can be representated:

import { ModelType, ModelPopulatedType, Schema, Model } from  '@rest-api/redux'
import libraryModel from './libraryModel'

const bookSchema = Schema({
    id: { type: Number, required: true },
    name: { type: String, required: true },
    description: String,
    library: {
	    type: libraryModel,
	    required: true,
		idOnly: true
    }
})
export type BookType = ModelType<typeof bookSchema>
export type BookPopulatedType = ModelPopulatedType<typeof bookSchema>
export default new Model(bookSchema, 'id', '/api/book')

An option can be passed to model declaration in order to works with django "trailing slash" or pass custom headers:

new Model(bookSchema, 'id', '/api/book', { trailingSlash: true, headers: { Authorization: "Basic xxxx" } })

Dependents (0)

Package Sidebar

Install

npm i @rest-api/redux

Weekly Downloads

1

Version

2.1.0

License

ISC

Unpacked Size

152 kB

Total Files

52

Last publish

Collaborators

  • hector7