Skip to content

Commit 63f8938

Browse files
authored
Merge pull request #4 from klis87/types
Added Typescript types
2 parents 4608c0f + 0f1223e commit 63f8938

File tree

5 files changed

+233
-4
lines changed

5 files changed

+233
-4
lines changed

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-saga-requests",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Redux Saga addon to simplify AJAX requests",
55
"main": "lib/index.js",
66
"module": "es/index.js",
@@ -9,11 +9,13 @@
99
"repository": "[email protected]:klis87/redux-saga-requests.git",
1010
"author": "Konrad Lisiczynski <[email protected]>",
1111
"license": "MIT",
12+
"typings": "types/index.d.ts",
1213
"files": [
1314
"lib",
1415
"es",
1516
"src",
16-
"dist"
17+
"dist",
18+
"types"
1719
],
1820
"keywords": [
1921
"redux",
@@ -28,7 +30,7 @@
2830
},
2931
"scripts": {
3032
"clean": "rimraf es lib dist",
31-
"test": "cross-env BABEL_ENV=commonjs mocha -r babel-register -r babel-polyfill -r chai/register-assert 'src/**/*.spec.js'",
33+
"test": "cross-env BABEL_ENV=commonjs mocha -r babel-register -r babel-polyfill -r chai/register-assert 'src/**/*.spec.js' 'types/**/*.spec.js'",
3234
"cover": "cross-env BABEL_ENV=coverage nyc --reporter=lcov --reporter=text mocha -r babel-register -r babel-polyfill -r chai/register-assert 'src/**/*.spec.js'",
3335
"report": "nyc report",
3436
"coveralls": "nyc report --reporter=text-lcov | coveralls",
@@ -68,6 +70,8 @@
6870
"redux-saga": "0.15.6",
6971
"rimraf": "2.6.2",
7072
"sinon": "3.3.0",
73+
"typescript": "2.5.3",
74+
"typescript-definition-tester": "0.0.5",
7175
"webpack": "3.6.0"
7276
},
7377
"nyc": {

types/index.d.ts

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
interface actionTypeModifier {
2+
(actionType: string): string;
3+
}
4+
5+
export const success: actionTypeModifier;
6+
7+
export const error: actionTypeModifier;
8+
9+
export const abort: actionTypeModifier;
10+
11+
export function getActionWithSuffix(suffix: string): actionTypeModifier;
12+
13+
interface getSuccessPayload {
14+
(response: any, request: any): any;
15+
}
16+
17+
interface getErrorPayload {
18+
(error: any): any;
19+
}
20+
21+
interface sendRequest {
22+
(config: any): any;
23+
}
24+
25+
interface abortRequest {
26+
(): void;
27+
}
28+
29+
interface requestsHandlers {
30+
sendRequest: sendRequest;
31+
abortRequest?: abortRequest;
32+
}
33+
34+
interface getRequestHandlers {
35+
(requestInstance: any, config?: any): requestsHandlers;
36+
}
37+
38+
interface driver {
39+
getSuccessPayload: getSuccessPayload;
40+
getErrorPayload: getErrorPayload;
41+
getRequestHandlers: getRequestHandlers;
42+
}
43+
44+
export const fetchApiDriver: driver;
45+
46+
interface onRequest {
47+
(request: any): any;
48+
}
49+
50+
interface onSuccess {
51+
(response: any): any;
52+
}
53+
54+
interface onError {
55+
(error: any): any;
56+
}
57+
58+
interface onAbort {
59+
(): any;
60+
}
61+
62+
interface requestInstanceConfig {
63+
success?: actionTypeModifier;
64+
error?: actionTypeModifier;
65+
abort?: actionTypeModifier;
66+
driver?: driver;
67+
onRequest?: onRequest;
68+
onSuccess?: onSuccess;
69+
onError?: onError;
70+
onAbort?: onAbort;
71+
}
72+
73+
export function createRequestInstance(requestInstance: any, config?: requestInstanceConfig): any;
74+
75+
export function getRequestInstance(): any;
76+
77+
type actionWithSingleRequest = {
78+
type: string;
79+
request: any;
80+
}
81+
82+
type actionWithMultipleRequests = {
83+
type: string;
84+
requests: any[];
85+
}
86+
87+
type payloadWithRequest = {
88+
request: any;
89+
}
90+
91+
type payloadWithRequests = {
92+
requests: any[];
93+
}
94+
95+
type actionWithSingleRequestAsPayload = {
96+
type: string;
97+
payload: payloadWithRequest;
98+
}
99+
100+
type actionWithMultipleRequestAsPayload = {
101+
type: string;
102+
payload: payloadWithRequests;
103+
}
104+
105+
type action =
106+
| actionWithSingleRequest
107+
| actionWithMultipleRequests
108+
| actionWithSingleRequestAsPayload
109+
| actionWithMultipleRequestAsPayload
110+
111+
export function sendRequest(action: action, dispatchRequestAction?: boolean): any;
112+
113+
export function watchRequests(): any;

types/typescript.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as tt from 'typescript-definition-tester';
2+
import path from 'path';
3+
4+
describe('TypeScript definitions', () => {
5+
it('should compile against index.d.ts', (done) => {
6+
tt.compile(
7+
[path.join(__dirname, 'typescript.types.ts')],
8+
{},
9+
() => done(),
10+
);
11+
});
12+
});

types/typescript.types.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
success,
3+
error,
4+
abort,
5+
getActionWithSuffix,
6+
fetchApiDriver,
7+
createRequestInstance,
8+
getRequestInstance,
9+
sendRequest,
10+
watchRequests,
11+
} from './index';
12+
13+
success('type');
14+
error('type');
15+
abort('type');
16+
17+
const actionModifier = getActionWithSuffix('suffix');
18+
actionModifier('type');
19+
20+
fetchApiDriver.getSuccessPayload({}, {});
21+
fetchApiDriver.getErrorPayload({});
22+
const requestHandlers = fetchApiDriver.getRequestHandlers({}, {});
23+
requestHandlers.sendRequest({});
24+
25+
createRequestInstance({});
26+
27+
const requestInstanceConfig = {
28+
success: actionModifier,
29+
error: actionModifier,
30+
abort: actionModifier,
31+
driver: fetchApiDriver,
32+
onRequest: request => ({}),
33+
onSuccess: response => ({}),
34+
onError: error => ({}),
35+
onAbort: () => ({}),
36+
}
37+
createRequestInstance({}, requestInstanceConfig);
38+
39+
getRequestInstance();
40+
41+
sendRequest({ type: 'type', request: {} });
42+
sendRequest({ type: 'type', payload: { request: {} } });
43+
sendRequest({ type: 'type', payload: { requests: [{}] } });
44+
sendRequest({ type: 'type', requests: [{}, {}] }, true);
45+
46+
watchRequests();

yarn.lock

+55-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ boxen@^1.0.0:
836836
term-size "^1.2.0"
837837
widest-line "^1.0.0"
838838

839-
brace-expansion@^1.1.7:
839+
brace-expansion@^1.0.0, brace-expansion@^1.1.7:
840840
version "1.1.8"
841841
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.8.tgz#c07b211c7c952ec1f8efd51a77ef0d1d3990a292"
842842
dependencies:
@@ -1394,6 +1394,13 @@ des.js@^1.0.0:
13941394
inherits "^2.0.1"
13951395
minimalistic-assert "^1.0.0"
13961396

1397+
detect-indent@^0.2.0:
1398+
version "0.2.0"
1399+
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-0.2.0.tgz#042914498979ac2d9f3c73e4ff3e6877d3bc92b6"
1400+
dependencies:
1401+
get-stdin "^0.1.0"
1402+
minimist "^0.1.0"
1403+
13971404
detect-indent@^4.0.0:
13981405
version "4.0.0"
13991406
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-4.0.0.tgz#f76d064352cdf43a1cb6ce619c4ee3a9475de208"
@@ -1440,6 +1447,14 @@ dot-prop@^4.1.0:
14401447
dependencies:
14411448
is-obj "^1.0.0"
14421449

1450+
dts-bundle@^0.2.0:
1451+
version "0.2.0"
1452+
resolved "https://registry.yarnpkg.com/dts-bundle/-/dts-bundle-0.2.0.tgz#e165e494b00f81a3b6eb64385cbf6d1b486b7a99"
1453+
dependencies:
1454+
detect-indent "^0.2.0"
1455+
glob "^4.0.2"
1456+
mkdirp "^0.5.0"
1457+
14431458
duplexer3@^0.1.4:
14441459
version "0.1.4"
14451460
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
@@ -2034,6 +2049,10 @@ get-func-name@^2.0.0:
20342049
version "2.0.0"
20352050
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
20362051

2052+
get-stdin@^0.1.0:
2053+
version "0.1.0"
2054+
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-0.1.0.tgz#5998af24aafc802d15c82c685657eeb8b10d4a91"
2055+
20372056
get-stream@^3.0.0:
20382057
version "3.0.0"
20392058
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
@@ -2068,6 +2087,15 @@ [email protected]:
20682087
once "^1.3.0"
20692088
path-is-absolute "^1.0.0"
20702089

2090+
glob@^4.0.2:
2091+
version "4.5.3"
2092+
resolved "https://registry.yarnpkg.com/glob/-/glob-4.5.3.tgz#c6cb73d3226c1efef04de3c56d012f03377ee15f"
2093+
dependencies:
2094+
inflight "^1.0.4"
2095+
inherits "2"
2096+
minimatch "^2.0.1"
2097+
once "^1.3.0"
2098+
20712099
glob@^7.0.3, glob@^7.0.5, glob@^7.0.6, glob@^7.1.2, glob@~7.1.2:
20722100
version "7.1.2"
20732101
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
@@ -2848,6 +2876,10 @@ lodash.restparam@^3.0.0:
28482876
version "3.6.1"
28492877
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
28502878

2879+
lodash@^3.6.0:
2880+
version "3.10.1"
2881+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
2882+
28512883
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.3.0:
28522884
version "4.17.4"
28532885
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
@@ -2984,6 +3016,12 @@ minimalistic-crypto-utils@^1.0.0, minimalistic-crypto-utils@^1.0.1:
29843016
version "1.0.1"
29853017
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
29863018

3019+
minimatch@^2.0.1:
3020+
version "2.0.10"
3021+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-2.0.10.tgz#8d087c39c6b38c001b97fca7ce6d0e1e80afbac7"
3022+
dependencies:
3023+
brace-expansion "^1.0.0"
3024+
29873025
minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
29883026
version "3.0.4"
29893027
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
@@ -2998,6 +3036,10 @@ [email protected], minimist@^1.2.0, minimist@~1.2.0:
29983036
version "1.2.0"
29993037
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
30003038

3039+
minimist@^0.1.0:
3040+
version "0.1.0"
3041+
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.1.0.tgz#99df657a52574c21c9057497df742790b2b4c0de"
3042+
30013043
[email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0:
30023044
version "0.5.1"
30033045
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
@@ -4246,6 +4288,18 @@ typedarray@^0.0.6:
42464288
version "0.0.6"
42474289
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
42484290

4291+
4292+
version "0.0.5"
4293+
resolved "https://registry.yarnpkg.com/typescript-definition-tester/-/typescript-definition-tester-0.0.5.tgz#91c574d78ea05b81ed81244d50ec30d8240c356f"
4294+
dependencies:
4295+
assertion-error "^1.0.1"
4296+
dts-bundle "^0.2.0"
4297+
lodash "^3.6.0"
4298+
4299+
4300+
version "2.5.3"
4301+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.5.3.tgz#df3dcdc38f3beb800d4bc322646b04a3f6ca7f0d"
4302+
42494303
ua-parser-js@^0.7.9:
42504304
version "0.7.14"
42514305
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.14.tgz#110d53fa4c3f326c121292bbeac904d2e03387ca"

0 commit comments

Comments
 (0)