├── .npmignore ├── .gitignore ├── .babelrc ├── .travis.yml ├── src ├── __tests__ │ ├── init.js │ ├── isError-test.js │ └── isFSA-test.js └── index.js ├── .eslintrc ├── Makefile ├── package.json └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | *.log 4 | lib 5 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "stage": 0, 3 | "loose": "all" 4 | } 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "iojs" 4 | -------------------------------------------------------------------------------- /src/__tests__/init.js: -------------------------------------------------------------------------------- 1 | import chai from 'chai'; 2 | global.expect = chai.expect; 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-airbnb", 3 | "env": { 4 | "mocha": true, 5 | "node": true 6 | }, 7 | "globals": { 8 | "expect": true 9 | }, 10 | "rules": { 11 | "padded-blocks": 0, 12 | "no-use-before-define": [2, "nofunc"], 13 | "no-unused-expressions": 0 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/__tests__/isError-test.js: -------------------------------------------------------------------------------- 1 | import { isError } from '../'; 2 | 3 | const type = 'ACTION_TYPE'; 4 | 5 | describe('isError()', () => { 6 | it('returns true if action.error is strictly true', () => { 7 | expect(isError({ type, error: true })).to.be.true; 8 | expect(isError({ type, error: 'true' })).to.be.false; 9 | expect(isError({ type })).to.be.false; 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import isPlainObject from 'lodash.isplainobject'; 2 | 3 | const validKeys = [ 4 | 'type', 5 | 'payload', 6 | 'error', 7 | 'meta' 8 | ]; 9 | 10 | function isValidKey(key) { 11 | return validKeys.indexOf(key) > -1; 12 | } 13 | 14 | export function isFSA(action) { 15 | return ( 16 | isPlainObject(action) && 17 | typeof action.type !== 'undefined' && 18 | Object.keys(action).every(isValidKey) 19 | ); 20 | } 21 | 22 | export function isError(action) { 23 | return action.error === true; 24 | } 25 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BIN=node_modules/.bin 2 | 3 | MOCHA_ARGS= --compilers js:babel/register \ 4 | --recursive \ 5 | --require src/__tests__/init.js \ 6 | src/**/*-test.js 7 | MOCHA_TARGET=src/**/*-test.js 8 | 9 | build: 10 | $(BIN)/babel src --out-dir lib 11 | 12 | clean: 13 | rm -rf lib 14 | 15 | test: lint 16 | NODE_ENV=test $(BIN)/mocha $(MOCHA_ARGS) $(MOCHA_TARGET) 17 | 18 | test-watch: lint 19 | NODE_ENV=test $(BIN)/mocha $(MOCHA_ARGS) -w $(MOCHA_TARGET) 20 | 21 | lint: 22 | $(BIN)/eslint src 23 | 24 | PHONY: build clean test test-watch lint 25 | -------------------------------------------------------------------------------- /src/__tests__/isFSA-test.js: -------------------------------------------------------------------------------- 1 | import { isFSA } from '../'; 2 | 3 | const type = 'ACTION_TYPE'; 4 | 5 | describe('isFSA()', () => { 6 | it('requires a type', () => { 7 | expect(isFSA({ type })).to.be.true; 8 | }); 9 | 10 | it('only accepts plain objects', () => { 11 | const action = () => {}; 12 | action.type = type; 13 | expect(isFSA(action)).to.be.false; 14 | }); 15 | 16 | it('returns false if there are invalid keys', () => { 17 | expect(isFSA({ type, payload: 'foobar' })).to.be.true; 18 | expect(isFSA({ type, meta: 'foobar' })).to.be.true; 19 | expect(isFSA({ type, error: new Error() })).to.be.true; 20 | expect(isFSA({ type, extra: 'foobar' })).to.be.false; 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "flux-standard-action", 3 | "version": "0.6.0", 4 | "description": "A human-friendly standard for Flux action objects", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "make test", 8 | "prepublish": "make clean build" 9 | }, 10 | "keywords": [ 11 | "flux", 12 | "redux", 13 | "actions", 14 | "fsa" 15 | ], 16 | "author": "Andrew Clark ", 17 | "license": "MIT", 18 | "devDependencies": { 19 | "babel": "^5.6.14", 20 | "babel-core": "^5.6.15", 21 | "babel-eslint": "^3.1.20", 22 | "chai": "^3.0.0", 23 | "eslint": "^0.24.0", 24 | "eslint-config-airbnb": "0.0.6", 25 | "mocha": "^2.2.5" 26 | }, 27 | "dependencies": { 28 | "lodash.isplainobject": "^3.2.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Flux Standard Action 2 | ==================== 3 | 4 | [![build status](https://img.shields.io/travis/acdlite/flux-standard-action/master.svg?style=flat-square)](https://travis-ci.org/acdlite/flux-standard-action) 5 | [![npm version](https://img.shields.io/npm/v/flux-standard-action.svg?style=flat-square)](https://www.npmjs.com/package/flux-standard-action) 6 | 7 | ## Introduction 8 | 9 | A human-friendly standard for Flux action objects. Feedback welcome. 10 | 11 | ### Motivation 12 | 13 | It's much easier to work with Flux actions if we can make certain assumptions about their shape. For example, essentially all Flux actions have an identifier field, such as `type`, `actionType`, or `actionId`. Many Flux implementations also include a way for actions to indicate success or failure, especially as the result of a data-fetching operation. Defining a minimal, common standard for these patterns enables the creation of useful tools and abstractions. 14 | 15 | ### Errors as a first class concept 16 | 17 | Flux actions can be thought of as an asychronous sequence of values. It is important for asynchronous sequences to deal with errors. Currently, many Flux implementations don't do this, and instead define separate action types like `LOAD_SUCCESS` and `LOAD_FAILURE`. This is less than ideal, because it overloads two separate concerns: disambiguating actions of a certain type from the "global" action sequence, and indicating whether or not an action represents an error. FSA treats errors as a first class concept. 18 | 19 | ### Design goals 20 | 21 | - **Human-friendly.** FSA actions should be easy to read and write by humans. 22 | - **Useful**. FSA actions should enable the creation of useful tools and abstractions. 23 | - **Simple.** FSA should be simple, straightforward, and flexible in its design. 24 | 25 | ### Example 26 | 27 | A basic Flux Standard Action: 28 | 29 | ```js 30 | { 31 | type: 'ADD_TODO', 32 | payload: { 33 | text: 'Do something.' 34 | } 35 | } 36 | ``` 37 | 38 | An FSA that represents an error, analogous to a rejected Promise: 39 | 40 | ```js 41 | { 42 | type: 'ADD_TODO', 43 | payload: new Error(), 44 | error: true 45 | } 46 | ``` 47 | 48 | ## Actions 49 | 50 | An action MUST 51 | 52 | - be a plain JavaScript object. 53 | - have a `type` property. 54 | 55 | An action MAY 56 | 57 | - have a `error` property. 58 | - have a `payload` property. 59 | - have a `meta` property. 60 | 61 | An action MUST NOT include properties other than `type`, `payload`, and `error`, and `meta`. 62 | 63 | ### `type` 64 | 65 | The `type` of an action identifies to the consumer the nature of the action that has occurred. Two actions with the same `type` MUST be strictly equivalent (using `===`). By convention, `type` is usually string constant or a Symbol. 66 | 67 | ### `payload` 68 | 69 | The optional `payload` property MAY be any type of value. It represents the payload of the action. Any information about the action that is not the `type` or status of the action should be part of the `payload` field. 70 | 71 | By convention, if `error` is `true`, the `payload` SHOULD be an error object. This is akin to rejecting a promise with an error object. 72 | 73 | ### `error` 74 | 75 | The optional `error` property MAY be set to `true` if the action represents an error. 76 | 77 | An action whose `error` is true is analogous to a rejected Promise. By convention, the `payload` SHOULD be an error object. 78 | 79 | If `error` has any other value besides `true`, including `undefined` and `null`, the action MUST NOT be interpreted as an error. 80 | 81 | ### `meta` 82 | 83 | The optional `meta` property MAY be any type of value. It is intended for any extra information that is not part of the payload. 84 | 85 | ## Utility functions 86 | 87 | The module `flux-standard-action` is available on npm. It exports a few utlity functions. 88 | 89 | ```js 90 | import { isFSA } from 'flux-standard-action'; 91 | ``` 92 | ### `isFSA(action)` 93 | 94 | Returns true if `action` is FSA compliant. 95 | 96 | ## Libraries 97 | 98 | - [redux-actions](https://github.com/acdlite/redux-actions) - a set of helpers for creating and handling FSA actions in Redux. 99 | - [redux-promise](https://github.com/acdlite/redux-promise) - Redux promise middleware that supports FSA actions. 100 | - [redux-rx](https://github.com/acdlite/redux-rx) - RxJS utilities for Redux, including a middleware that supports FSA actions. 101 | --------------------------------------------------------------------------------