4.8.1-beta.4 • Published 6 years ago

@drieam/api v4.8.1-beta.4

Weekly downloads
336
License
MIT
Repository
-
Last release
6 years ago

npm

@drieam/api

Redux api middleware that runs promises to fetch data from an api endpoint.

Table of Contents

Getting Started

Installation

Install the @drieam/api and its dependencies.

yarn add @drieam/api @drieam/common @drieam/models

Usage

import * as api from '@drieam/api';
import * as models from '@drieam/models';
import { setStore } from '@drieam/common';

type API = {
  user: models.canvas.user;
};

type Store = RootReducer<{
  api: api.reducers.Connect<API>;
}>;

const api: ApiRoutes<API> = {
  user: {
    onError,
    path: '/lti/proxy/api/v1/users/:id?',
    mapper: models.canvas.User,
    list: true,
  },
};

const actions = api.actions.connect<API>(api);
const rootReducer = api.reducers.connect<API>(api);
const store = setStore(
  api,
  rootReducer,
  [/** EMPTY MIDDLEWARES **/],
  defaultOptions,
)({
  /* EMPTY INITIAL STATE */
});

ApiRoutes

Routing Structure is base of a key-value object where you can configure your basic reducer structure for use cases where you need to fetch data from Rest APIs. This structure is base on a type and a Hash object from that type.

The API type is a reference between your resource and the class used by the middleware to instantiate it.

path

apiRoute.path: PathtoRegExp | (params) => string

Turn a path string into a regular expression. (More info)

mapper

apiRoute.mapper: class | (data) => any;

The model class to be instantiated by the middleware. It can be a generic class or custom.

list

apiRoute.list: boolean | reducer;

As true sets the base ListState reducer but you can pass a custom reducer.

entity

apiRoute.entity: boolean | reducer;

As true sets the base EntityState reducer but you can pass a custom reducer base on that one.

onSuccess()

apiRoute.onSuccess: (data) => void;

Callback made by the middleware when a api call is successful.

onError()

apiRoute.onError: (error) => void;

Callback made by the middleware when an error occurs.

Note: Whether list or entity should be defined. If both are not defined or false, redux will complaint with an error.

Actions

connect(apiRoutes)

const actions = api.actions.connect<API>(api);

Creates an object structure with all the redux actions needed for each resource definition.

fetchEntity(id, filters, apiOptions) => action

actions.user.fetchEntity(1);

Creates an accion to fetch an entity object.

fetchList(filters, apiOptions) => action

actions.user.fetchList();

Creates an accion to fech a collection of entities. This action refresh the data in the reducer.

fetchPage(filters, apiOptions) => action

actions.user.fetchPage({ page: 3});

Creates an accion to fetch a page of a entity object collection. This page is added in the reducer.

fetchNextPage(filters, apiOptions) => action

actions.user.fetchNextPage();
// actions.user.fetchPage({ page: 'next' });

Creates an accion to fetch the next page of a entity object collection.

saveEntity(attributes, filters, apiOptions) => action

actions.user.saveEntity(user);

Creates an accion which saves or updates an entity object.

deleteEntity(attributes, filters, apiOptions) => action

actions.user.deleteEntity(user);

Creates an accion to delete an entity object.

saveList(ids, attributes, filters, apiOptions) => action

actions.user.saveList([1, 2], user);

Creates an accion which updates a collection of entity objects. The attributes can be one or many and it will be update per id position.

deleteList(ids, , filters, apiOptions) => action

actions.user.deleteList([1, 3]);

Creates an accion which delete a collection of entity objects.

ApiOptions

Settings of an action.

{
  rowKey: 'id',
  csrfToken: null,
  updateMethod: 'PUT',
  credentials: 'same-origin',
  nestedPayloadKeySuffix: 'Attributes',
  headers: {},
  onSuccess: undefined,
  onError: undefined,
}

rowKey

apiOptions.rowKey = string // by default is set 

Row's unique key

csrfToken

apiOptions.csrfToken = string // by default is set 

Cross-site request forgery token.

insert

apiOptions.insert: string = 'prepend'

Order of insertion a listState. default: 'append'

updateMethod

apiOptions.updateMethod: string = 'PUT'

Set the default udate method to PUT or PATCH

credentials

apiOptions.credentials: omit | same-origin | include = 'same-origin'

Set the default udate method to PUT or PATCH The request credentials you want to use for the request: omit, same-origin, or include. To automatically send cookies for the current domain, this option must be provided.

nestedPayloadKeySuffix

apiOptions.nestedPayloadKeySuffix: string = 'Attributes'

A string to transform the collection name before sending.

headers

apiOptions.headers: object = {}

HTTP headers allow the client and the server to pass additional information with the request or the response. An HTTP header consists of its case-insensitive name.

onSuccess()

apiOptions.onSuccess: (data) => void;

Callback made by the middleware when a api call is successful.

onError()

apiOptions.onError: (error) => void;

Callback made by the middleware when an error occurs.

Reducers

connect(apiRoutes)

const apiReducers = api.reducers.connect<API>(api);

Creates an object structure with all the redux actions needed for each resource definition.

apiReducers..list

Redux Store of a collection of fetched entities.

Example

{
  pending: boolean = false    // True if data is still being loaded for the first time.
  fulfilled: boolean = false  // True if data was loaded successfully.
  rejected: boolean; = false  // True if data was loaded unsuccessfully.
  settled: boolean; = false   // True if the data load completed, if successfully or unsuccessfully.
  value: T[] | null = null    // Value of successfully loaded data; otherwise, null.
  reason: string;             // Error of unsuccessfully loaded data; otherwise, null
  errors: [{                  // Error messages from the middleware or nothing
    code: number              // HTTP [status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes).
    message: string           // Error message.
    errors: {}                // Key-Values error messages.
  }];   
  status: number;             // Last Maximun HTTP [status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) from the ErrorMessages.
  links: { 
    next?: {                  // The link relation for the immediate next page of results.
      page: number,
      url: srting,
    },  
    last?: HeaderLink         // The link relation for the last page of results.
    first?:HeaderLink         // The link relation for the first page of results.
    prev?:HeaderLink          // The link relation for the immediate previous page of results.
  };
  perPage: number;            // The amount of item per page.
  count: number;              // The total number of available elements.
  filters: QueryParams;       // An object of the enabled filters.
}

apiReducers..entity

Redux Store of a fetched entity.

Example

{
  pending: boolean = false    // True if data is still being loaded for the first time.
  fulfilled: boolean = false  // True if data was loaded successfully.
  rejected: boolean; = false  // True if data was loaded unsuccessfully.
  settled: boolean; = false   // True if the data load completed, if successfully or unsuccessfully.
  value: T | null = null      // Value of successfully loaded data; otherwise, null.
  reason: string = null       // Error of unsuccessfully loaded data; otherwise, null
  status: number = null       // Last HTTP [status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes).
}

Contributing

We welcome all contributors who abide by our Code of Conduct. Please see the Contributors Guide for more details on submitting a PR, setting up a local dev environment, running tests, etc...

Versioning

Until this project reaches a 1.0 milestone, minor version numbers will simply be incremented during each release. The Changelog will continue to document the different types of updates, including any "breaking changes".

After the 1.0 milestone, this project will follow SemVer.

9.0.0

4 years ago

8.0.1

4 years ago

8.0.2

4 years ago

8.0.0

4 years ago

7.14.0

4 years ago

7.13.0

4 years ago

7.12.0

5 years ago

7.11.12

5 years ago

7.11.11

5 years ago

7.11.9

5 years ago

7.11.8

5 years ago

7.11.10

5 years ago

7.11.7

5 years ago

7.11.6

5 years ago

7.11.5

5 years ago

7.11.4

5 years ago

7.11.3

5 years ago

7.11.0

5 years ago

7.9.1-next.1

5 years ago

7.9.1-next.2

5 years ago

7.9.1-next.0

5 years ago

7.8.0

5 years ago

7.6.3-next.1

5 years ago

7.6.3-next.0

5 years ago

7.6.0

5 years ago

7.5.5-next.7

5 years ago

7.5.5-next.6

5 years ago

7.5.5-next.5

5 years ago

7.5.5-next.4

5 years ago

7.5.5-next.3

5 years ago

7.5.5-next.0

5 years ago

7.5.5-next.2

5 years ago

7.5.5-next.1

5 years ago

7.5.4

5 years ago

7.5.3

5 years ago

7.4.0

5 years ago

7.2.0

5 years ago

7.1.3-next.0

5 years ago

7.1.3-next.2

5 years ago

7.1.3-next.1

5 years ago

7.1.3

5 years ago

7.1.1

5 years ago

7.1.0

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.14.1-next.6

5 years ago

6.14.1-next.5

5 years ago

6.14.1-next.4

5 years ago

6.14.1-next.3

5 years ago

6.14.1-next.1

5 years ago

6.14.1-next.0

5 years ago

6.13.4-next.1

6 years ago

6.13.4-next.0

6 years ago

6.13.3-next.1

6 years ago

6.13.3-next.0

6 years ago

6.13.2-next.8

6 years ago

6.13.2-next.7

6 years ago

6.13.2-next.6

6 years ago

6.13.2-next.5

6 years ago

6.13.2-next.4

6 years ago

6.13.2-next.1

6 years ago

6.13.2-next.3

6 years ago

6.13.2-next.2

6 years ago

6.13.2-next.0

6 years ago

6.12.3

6 years ago

6.10.1

6 years ago

6.10.0

6 years ago

6.9.0

6 years ago

6.8.5-next.0

6 years ago

6.7.1-next.0

6 years ago

6.5.1

6 years ago

6.5.0

6 years ago

6.3.1-next.0

6 years ago

6.2.4

6 years ago

6.2.2

6 years ago

6.2.0

6 years ago

6.2.0-next.3

6 years ago

6.2.0-next.2

6 years ago

6.2.0-next.1

6 years ago

6.2.0-next.0

6 years ago

6.1.1-next.22

6 years ago

6.1.1-next.21

6 years ago

6.1.1-next.20

6 years ago

6.1.1-next.19

6 years ago

6.1.1-next.18

6 years ago

6.1.1-next.17

6 years ago

6.1.1-next.16

6 years ago

6.1.1-next.15

6 years ago

6.1.1-next.14

6 years ago

6.1.1-next.12

6 years ago

6.1.1-next.13

6 years ago

6.1.1-next.11

6 years ago

6.1.1-next.10

6 years ago

6.1.1-next.9

6 years ago

6.1.1-next.8

6 years ago

6.1.1-next.7

6 years ago

6.1.1-next.6

6 years ago

6.1.1-next.5

6 years ago

6.1.1-next.4

6 years ago

6.1.1-next.3

6 years ago

6.1.1-next.1

6 years ago

6.1.1-next.0

6 years ago

6.1.1

6 years ago

6.0.1-next.2

6 years ago

6.0.1-next.1

6 years ago

6.0.1-next.0

6 years ago

6.0.0

6 years ago

5.0.0

6 years ago

4.20.0

6 years ago

4.17.0

6 years ago

4.16.0

6 years ago

4.15.6-beta.1

6 years ago

4.15.3

6 years ago

4.15.1-beta.3

6 years ago

4.15.1-beta.2

6 years ago

4.15.1-beta.1

6 years ago

4.15.0

6 years ago

4.13.2-beta.4

6 years ago

4.13.2-beta.3

6 years ago

4.14.6-beta.19

6 years ago

4.13.2-beta.1

6 years ago

4.14.10

6 years ago

4.14.6-beta.18

6 years ago

4.14.6-beta.23

6 years ago

4.14.10-beta.0

6 years ago

4.14.9-beta.0

6 years ago

4.14.9-beta.7

6 years ago

4.14.9-beta.8

6 years ago

4.14.9-beta.5

6 years ago

4.14.8

6 years ago

4.14.8-beta.0

6 years ago

4.14.7

6 years ago

4.14.7-beta.0

6 years ago

4.14.6

6 years ago

4.14.5

6 years ago

4.14.4

6 years ago

4.14.6-beta.0

6 years ago

4.14.5-beta.0

6 years ago

4.14.4-beta.0

6 years ago

4.14.3

6 years ago

4.14.3-beta.0

6 years ago

4.14.2

6 years ago

4.14.1

6 years ago

4.14.1-beta.3

6 years ago

4.14.1-beta.1

6 years ago

4.14.1-beta.0

6 years ago

4.14.0

6 years ago

4.14.0-beta.7

6 years ago

4.14.0-beta.6

6 years ago

4.14.0-beta.4

6 years ago

4.14.0-beta.3

6 years ago

4.14.0-beta.2

6 years ago

4.14.0-beta.1

6 years ago

4.14.0-beta.0

6 years ago

4.13.2-beta.0

6 years ago

4.13.2-beta.2

6 years ago

4.13.1

6 years ago

4.13.1-beta.1

6 years ago

4.13.0

6 years ago

4.11.2-beta.12

6 years ago

4.11.2-beta.11

6 years ago

4.11.2-beta.7

6 years ago

4.11.2-beta.6

6 years ago

4.11.2-beta.5

6 years ago

4.11.1

6 years ago

4.11.1-beta.1

6 years ago

4.11.0

6 years ago

4.10.1-beta.3

6 years ago

4.10.0

6 years ago

4.9.4-beta.5

6 years ago

4.9.4-beta.7

6 years ago

4.9.4-beta.1

6 years ago

4.9.3

6 years ago

4.9.3-beta.1

6 years ago

4.9.1

6 years ago

4.9.1-beta.3

6 years ago

4.9.1-beta.2

6 years ago

4.8.1-beta.4

6 years ago

4.9.0

6 years ago

4.8.1-beta.2

6 years ago

4.8.0

6 years ago

4.7.2-beta.3

6 years ago

4.7.0

6 years ago

4.6.1-beta.2

6 years ago

4.6.0

6 years ago

4.5.1-beta.2

6 years ago

4.5.0

6 years ago

4.4.3

6 years ago

4.4.2

6 years ago

4.4.1

6 years ago

4.3.1-beta.11

6 years ago

4.4.0

6 years ago

4.3.1-beta.8

6 years ago

4.3.1-beta.7

6 years ago

4.3.1-beta.5

6 years ago

4.3.1-beta.4

6 years ago

4.3.1-beta.3

6 years ago

4.3.1-beta.2

6 years ago

4.3.0

6 years ago

4.2.1-beta.4

6 years ago

4.2.0

6 years ago

4.1.3-beta.2

6 years ago

4.1.2

6 years ago

4.1.2-beta.1

6 years ago

4.1.1

6 years ago

4.1.1-beta.1

6 years ago

4.1.0

6 years ago

4.0.1-beta.18

6 years ago

4.0.1-beta.17

6 years ago

4.0.1-beta.16

6 years ago

4.0.1-beta.14

6 years ago

4.0.1-beta.10

6 years ago

4.0.1-beta.9

6 years ago

4.0.1-beta.8

6 years ago

4.0.1-beta.7

6 years ago

4.0.1-beta.6

6 years ago

4.0.1-beta.5

6 years ago

4.0.1-beta.2

6 years ago

4.0.1-beta.1

6 years ago

4.0.0

6 years ago

3.2.3

6 years ago

3.2.2-beta.25

6 years ago

3.2.3-beta.1

6 years ago

3.2.2-beta.24

6 years ago

3.2.2

6 years ago

3.2.2-beta.22

6 years ago

3.2.2-beta.20

6 years ago

3.2.2-beta.19

6 years ago

3.2.2-beta.18

6 years ago

3.2.2-beta.16

6 years ago

3.2.2-beta.15

6 years ago

3.2.2-beta.14

6 years ago

3.2.2-beta.10

6 years ago

3.2.2-beta.6

6 years ago

3.2.2-beta.9

6 years ago

3.2.2-beta.5

6 years ago

3.2.2-beta.1

7 years ago

3.2.1

7 years ago

3.2.1-beta.1

7 years ago

3.2.0

7 years ago

3.1.2-beta.10

7 years ago

3.1.2-beta.8

7 years ago

3.1.2-beta.7

7 years ago

3.1.2-beta.4

7 years ago

3.1.2-beta.3

7 years ago

3.1.2-beta.12

7 years ago

3.1.2-beta.2

7 years ago

3.1.2

7 years ago

3.1.2-beta.0

7 years ago

3.1.0

7 years ago

3.0.3-beta.7

7 years ago

3.0.3-beta.6

7 years ago

3.0.3-beta.5

7 years ago

3.0.3-beta.4

7 years ago

3.0.3-beta.1

7 years ago

3.0.1

7 years ago

3.0.1-beta.0

7 years ago

3.0.0

7 years ago

2.4.3-beta.9

7 years ago

2.4.3-beta.8

7 years ago

2.4.3-beta.6

7 years ago

2.4.3-beta.3

7 years ago

2.4.3-beta.2

7 years ago

2.4.2

7 years ago

2.4.2-beta.0

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.1-beta.0

7 years ago

2.3.0

7 years ago

2.2.1-beta.0

7 years ago

2.2.0

7 years ago

2.1.1-beta.2

7 years ago

2.1.1-beta.1

7 years ago

2.1.1-beta.0

7 years ago

2.1.0

7 years ago

2.0.2-beta.9

7 years ago

2.0.2-beta.8

7 years ago

2.0.2-beta.7

7 years ago

2.0.2-beta.6

7 years ago

2.0.2-beta.5

7 years ago

2.0.2-beta.4

7 years ago

2.0.2-beta.3

7 years ago

2.0.2-beta.2

7 years ago

2.0.2-beta.0

7 years ago

2.0.1

7 years ago

2.0.1-beta.0

7 years ago

2.0.0

7 years ago

1.9.2-beta.14

7 years ago

1.9.2-beta.13

7 years ago

1.9.2-beta.12

7 years ago

1.9.2-beta.8

7 years ago

1.9.2-beta.7

7 years ago

1.9.2-beta.6

7 years ago

1.9.2-beta.5

7 years ago

1.9.2-beta.4

7 years ago

1.9.2-beta.1

7 years ago

1.9.2-beta.0

7 years ago

1.9.1

7 years ago

1.9.1-beta.5

7 years ago

1.9.1-beta.1

7 years ago

1.9.0

7 years ago

1.8.1-beta.1

7 years ago

1.8.0

7 years ago

1.7.2-beta.3

7 years ago

1.7.2

7 years ago

1.7.2-beta.0

7 years ago

1.7.1

7 years ago

1.6.4-beta.3

7 years ago

1.7.0

7 years ago

1.6.4-beta.2

7 years ago

1.6.4-beta.0

7 years ago

1.6.2

7 years ago

1.5.2-beta.2

7 years ago

1.6.0

7 years ago

1.5.2-beta.0

7 years ago

1.5.1-beta.71

7 years ago

1.5.0

7 years ago

1.4.4-beta.2

7 years ago

1.4.4-beta.1

7 years ago

1.4.3

7 years ago

1.4.3-beta.1

7 years ago

1.4.3-beta.0

7 years ago

1.4.2

7 years ago

1.4.2-beta.0

7 years ago

1.4.1

7 years ago

1.3.9-beta.1

7 years ago

1.3.8

7 years ago

1.3.8-beta.0

7 years ago

1.3.7

7 years ago

0.37.2-beta.1

7 years ago

0.37.1

7 years ago

0.26.2-beta.34

7 years ago

0.26.2-beta.33

7 years ago

0.37.0

7 years ago

0.26.2-beta.32

7 years ago

0.26.2-beta.31

7 years ago

0.26.2-beta.30

7 years ago

0.26.2-beta.29

7 years ago

0.34.2

7 years ago

0.26.2-beta.21

7 years ago

0.26.2-beta.28

7 years ago

0.34.0

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.32.0

7 years ago

0.29.0

7 years ago

0.26.1

7 years ago

0.25.1

7 years ago

0.24.0

7 years ago

0.23.1

7 years ago

0.17.0

8 years ago

0.12.0

8 years ago

0.11.0

8 years ago

0.10.0

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago